From 3737d28ba9f1a4f40f5e11aeacb9c8bacda8591e Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Sat, 24 Feb 2018 02:10:15 +0100 Subject: [PATCH] Get the endpoints from the website instead of knowing them --- .idea/vcs.xml | 6 +++ .../eu/stuifzand/micropub/MainActivity.java | 28 +++++++++++--- .../micropub/auth/AuthenticatedActivity.java | 2 +- .../micropub/auth/AuthenticationActivity.java | 15 +++----- .../micropub/auth/Authenticator.java | 6 ++- .../micropub/auth/WebsigninTask.java | 38 +++++++++++-------- 6 files changed, 63 insertions(+), 32 deletions(-) create mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/java/eu/stuifzand/micropub/MainActivity.java b/app/src/main/java/eu/stuifzand/micropub/MainActivity.java index 585496c..0b7b5b1 100644 --- a/app/src/main/java/eu/stuifzand/micropub/MainActivity.java +++ b/app/src/main/java/eu/stuifzand/micropub/MainActivity.java @@ -75,12 +75,18 @@ public class MainActivity extends AppCompatActivity { public void sendPost(View view) { AccountManager am = AccountManager.get(this); Bundle options = new Bundle(); - am.getAuthTokenByFeatures("Indieauth", "token", null, this, options, null, new OnTokenAcquired(), null); + am.getAuthTokenByFeatures("Indieauth", "token", null, this, options, null, new OnTokenAcquired(true), null); } private class OnTokenAcquired implements AccountManagerCallback { + private boolean sendMessage; + + public OnTokenAcquired(boolean sendMessage) { + this.sendMessage = sendMessage; + } + @Override public void run(AccountManagerFuture result) { // Get the result of the operation from the AccountManagerFuture. @@ -96,9 +102,21 @@ public class MainActivity extends AppCompatActivity { // is stored in the constant AccountManager.KEY_AUTHTOKEN. String token = bundle.getString(AccountManager.KEY_AUTHTOKEN); - EditText mEdit = (EditText) findViewById(R.id.content); - //new PostMessageTask(MainActivity.this, token).execute("http://192.168.178.21:5000/micropub", mEdit.getText().toString()); - new PostMessageTask(MainActivity.this, token, mEdit).execute("https://publog.stuifzandapp.com/micropub", mEdit.getText().toString()); + if (sendMessage) { + EditText mEdit = (EditText) findViewById(R.id.content); + AccountManager am = AccountManager.get(MainActivity.this); + Account[] accounts = am.getAccountsByType(bundle.getString("accountType")); + String accountName = bundle.getString("authAccount"); + String micropub = null; + for (Account account : accounts) { + if (account.name.equals(accountName)) { + micropub = am.getUserData(account, "micropub"); + } + } + String micropubBackend = micropub; + Log.i("micropub", "Sending message to " + micropubBackend); + new PostMessageTask(MainActivity.this, token, mEdit).execute(micropubBackend, mEdit.getText().toString()); + } Log.d("micropub", "GetTokenForAccount Bundle is " + token); } catch (OperationCanceledException e) { @@ -114,7 +132,7 @@ public class MainActivity extends AppCompatActivity { public void startSignin(View view) { AccountManager am = AccountManager.get(this); Bundle options = new Bundle(); - am.getAuthTokenByFeatures("Indieauth", "token", null, this, options, null, new OnTokenAcquired(), null); + am.getAuthTokenByFeatures("Indieauth", "token", null, this, options, null, new OnTokenAcquired(false), null); } private void showMessage(final String msg) { diff --git a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/AuthenticatedActivity.java b/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/AuthenticatedActivity.java index f97477b..3b26247 100644 --- a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/AuthenticatedActivity.java +++ b/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/AuthenticatedActivity.java @@ -20,7 +20,7 @@ public class AuthenticatedActivity extends AppCompatActivity { Intent intent = getIntent(); String urlString = intent.getStringExtra("url"); - String endpoint = intent.getStringExtra(WebsigninTask.ENDPOINT); + String endpoint = intent.getStringExtra("authorization_endpoint"); String me = intent.getStringExtra(WebsigninTask.ME); TextView textResult = findViewById(R.id.textResult); diff --git a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/AuthenticationActivity.java b/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/AuthenticationActivity.java index be2e648..ebd1205 100644 --- a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/AuthenticationActivity.java +++ b/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/AuthenticationActivity.java @@ -26,7 +26,6 @@ public class AuthenticationActivity extends AccountAuthenticatorActivity { private static final String TAG = "AuthenticationActivity"; public static final String PARAM_USER_PASS = "eu.stuifzand.micropub.UserPass"; - private static final int REQ_SIGNUP = 1; @Override protected void onCreate(Bundle savedInstanceState) { @@ -34,7 +33,7 @@ public class AuthenticationActivity extends AccountAuthenticatorActivity { setContentView(R.layout.activity_authentication); Intent intent = getIntent(); - final String endpoint = intent.getStringExtra(WebsigninTask.ENDPOINT); + final String endpoint = intent.getStringExtra("authorization_endpoint"); final String me = intent.getStringExtra(WebsigninTask.ME); final AccountAuthenticatorResponse response = intent.getParcelableExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE); @@ -59,13 +58,6 @@ public class AuthenticationActivity extends AccountAuthenticatorActivity { Log.i("micropub", "New API: " + request.getUrl().toString()); String url = request.getUrl().toString(); if (url.startsWith("https://stuifzand.eu/micropub-auth")) { - -// Intent intent = new Intent(AuthenticationActivity.this, AuthenticatedActivity.class); -// intent.putExtra("url", url); -// intent.putExtra(ENDPOINT, endpoint); -// intent.putExtra(ME, me); -// startActivity(intent); - HttpUrl httpUrl = HttpUrl.parse(url); String code = httpUrl.queryParameter("code"); String state = httpUrl.queryParameter("state"); @@ -104,7 +96,10 @@ public class AuthenticationActivity extends AccountAuthenticatorActivity { // Creating the account on the device and setting the auth token we got // (Not setting the auth token will cause another call to the server to authenticate the user) - accountManager.addAccountExplicitly(account, accountPassword, null); + Bundle bundle = new Bundle(); + bundle.putAll(getIntent().getExtras()); + Log.i("micropub", bundle.toString()); + accountManager.addAccountExplicitly(account, accountPassword, bundle); accountManager.setAuthToken(account, authtokenType, authtoken); } else { Log.d("micropub", TAG + "> finishLogin > setPassword"); diff --git a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/Authenticator.java b/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/Authenticator.java index 52e3544..70a63ec 100644 --- a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/Authenticator.java +++ b/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/Authenticator.java @@ -13,6 +13,8 @@ import android.text.TextUtils; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import org.w3c.dom.Text; + import java.io.IOException; import okhttp3.Call; @@ -79,9 +81,11 @@ public class Authenticator extends AbstractAccountAuthenticator { .add("me", account.name) .add("grant_type", "authorization_code") .build(); + + String tokenEndpoint = am.getUserData(account, "token_endpoint"); Request request = new Request.Builder() .addHeader("Accept", "application/json") - .url("https://tokens.indieauth.com/token") + .url(tokenEndpoint) .method("POST", formBody) .build(); diff --git a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/WebsigninTask.java b/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/WebsigninTask.java index 19055a4..c5e68b4 100644 --- a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/WebsigninTask.java +++ b/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/auth/WebsigninTask.java @@ -5,6 +5,8 @@ import android.accounts.AccountManager; import android.app.Activity; import android.content.Intent; import android.os.AsyncTask; +import android.os.Bundle; +import android.util.Log; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; @@ -14,8 +16,7 @@ import org.jsoup.select.Elements; import java.io.IOException; -public class WebsigninTask extends AsyncTask { - public static final String ENDPOINT = "eu.stuifzand.microsub.ENDPOINT"; +public class WebsigninTask extends AsyncTask { public static final String ME = "eu.stuifzand.micropub.ME"; protected AccountAuthenticatorResponse response; protected Activity activity; @@ -31,30 +32,37 @@ public class WebsigninTask extends AsyncTask