Improve logging in with the app

This commit is contained in:
Peter Stuifzand 2018-05-15 21:36:34 +02:00
parent aca666cdf1
commit 5cccd03e2f
6 changed files with 165 additions and 10 deletions

View File

@ -7,8 +7,8 @@ android {
applicationId "eu.stuifzand.micropub"
minSdkVersion 15
targetSdkVersion 26
versionCode 14
versionName '0.0.14-alpha'
versionCode 15
versionName '0.1.0-alpha'
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
@ -39,7 +39,7 @@ android {
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.android.support:design:26.1.0'
implementation 'com.squareup.okhttp3:okhttp:3.9.1'
testImplementation 'com.squareup.okhttp3:mockwebserver:3.9.1';
@ -48,21 +48,32 @@ dependencies {
// Logging
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'
// ViewModel and LiveData
implementation 'android.arch.lifecycle:extensions:1.1.0'
annotationProcessor "android.arch.lifecycle:compiler:1.1.0"
implementation 'android.arch.lifecycle:extensions:1.1.1'
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
// Java8 support for Lifecycles
implementation 'android.arch.lifecycle:common-java8:1.1.0'
implementation 'android.arch.lifecycle:common-java8:1.1.1'
// jsoup
implementation 'org.jsoup:jsoup:1.11.2'
implementation 'com.google.code.gson:gson:2.8.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:support-annotations:27.0.0'
implementation 'com.parse.bolts:bolts-tasks:1.4.0'
implementation 'com.android.support:support-annotations:27.1.1'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation "io.reactivex.rxjava2:rxjava:2.1.12"
def room_version = "1.1.0"
implementation "android.arch.persistence.room:runtime:$room_version"
annotationProcessor "android.arch.persistence.room:compiler:$room_version"
// Test helpers
testImplementation "android.arch.persistence.room:testing:$room_version"
}
repositories {
mavenCentral()

View File

@ -0,0 +1,9 @@
package eu.stuifzand.micropub.auth;
import android.arch.persistence.room.Database;
import android.arch.persistence.room.RoomDatabase;
@Database(entities = {Auth.class}, version = 2)
public abstract class AppDatabase extends RoomDatabase {
public abstract AuthDao authDao();
}

View File

@ -0,0 +1,47 @@
package eu.stuifzand.micropub.auth;
import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;
@Entity
public class Auth {
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name = "state")
private String state;
@ColumnInfo(name = "me")
private String me;
//private String authorization_endpoint;
public Auth(String state, String me) {
this.state = state;
this.me = me;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getMe() {
return me;
}
public void setMe(String me) {
this.me = me;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}

View File

@ -0,0 +1,18 @@
package eu.stuifzand.micropub.auth;
import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Delete;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.Query;
@Dao
public interface AuthDao {
@Query("SELECT * FROM auth WHERE state = :state")
Auth load(String state);
@Insert
void save(Auth auth);
@Delete
void delete(Auth auth);
}

View File

@ -4,8 +4,10 @@ import android.accounts.Account;
import android.accounts.AccountAuthenticatorActivity;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.arch.persistence.room.Room;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
@ -19,6 +21,7 @@ import android.webkit.WebViewClient;
import java.net.URI;
import eu.stuifzand.micropub.R;
import eu.stuifzand.micropub.utils.RandomStringUtils;
import okhttp3.HttpUrl;
public class AuthenticationActivity extends AccountAuthenticatorActivity {
@ -46,18 +49,70 @@ public class AuthenticationActivity extends AccountAuthenticatorActivity {
Intent intent = getIntent();
bundle = intent.getExtras();
if ("android.intent.action.VIEW".equals(intent.getAction())) {
Log.i("micropub", intent.toString());
Uri uri = intent.getData();
String code = uri.getQueryParameter("code");
String state = uri.getQueryParameter("state"); // @TODO: check/use state
AppDatabase db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "auth").build();
new AsyncTask<String, Void, Auth>() {
@Override
protected Auth doInBackground(String... strings) {
String state = strings[0];
Auth auth = db.authDao().load(state);
db.close();
return auth;
}
@Override
protected void onPostExecute(Auth auth) {
if (auth != null) {
finish();
return;
}
Bundle bundle = new Bundle();
bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, "Indieauth");
bundle.putString(AccountManager.KEY_ACCOUNT_NAME, auth.getMe());
bundle.putString(AuthenticationActivity.PARAM_USER_PASS, code);
Intent loginIntent = new Intent();
loginIntent.putExtras(bundle);
finishLogin(loginIntent);
}
}.execute(state);
return;
}
String endpoint = bundle.getString("authorization_endpoint");
String me = bundle.getString(WebsigninTask.ME);
AccountAuthenticatorResponse response = bundle.getParcelable(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
String state = RandomStringUtils.randomString(16);
HttpUrl.Builder builder = HttpUrl.parse(endpoint).newBuilder();
builder.setQueryParameter("me", me)
.setQueryParameter("client_id", "https://wrimini.net")
.setQueryParameter("redirect_uri", "https://wrimini.net/oauth/callback")
.setQueryParameter("response_type", "code")
.setQueryParameter("state", "1234") // @TODO use random states, check the state later
.setQueryParameter("state", state)
.setQueryParameter("scope", "create"); // @TODO use different scope
AppDatabase db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "auth")
.fallbackToDestructiveMigration()
.build();
Auth auth = new Auth(state, me);
new AsyncTask<Auth, Void, Void>() {
@Override
protected Void doInBackground(Auth... auths) {
db.authDao().save(auths[0]);
db.close();
return null;
}
}.execute(auth);
String url = builder.toString();
Log.i("micropub", "LoadUrl: " + url);
Intent webIntent = new Intent(Intent.ACTION_VIEW);

View File

@ -0,0 +1,15 @@
package eu.stuifzand.micropub.utils;
import java.util.Random;
import java.util.stream.IntStream;
public class RandomStringUtils {
public static String randomString(int n) {
Random r = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append((char)('a'+r.nextInt(('z'-'a')+1)));
}
return sb.toString();
}
}