Get the endpoints from the website instead of knowing them

This commit is contained in:
Peter Stuifzand 2018-02-24 02:10:15 +01:00
parent 777af7f40d
commit 3737d28ba9
6 changed files with 63 additions and 32 deletions

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -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<Bundle> {
private boolean sendMessage;
public OnTokenAcquired(boolean sendMessage) {
this.sendMessage = sendMessage;
}
@Override
public void run(AccountManagerFuture<Bundle> 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) {

View File

@ -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);

View File

@ -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");

View File

@ -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();

View File

@ -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<String, Void, WebsigninTask.AuthResponse> {
public static final String ENDPOINT = "eu.stuifzand.microsub.ENDPOINT";
public class WebsigninTask extends AsyncTask<String, Void, Bundle> {
public static final String ME = "eu.stuifzand.micropub.ME";
protected AccountAuthenticatorResponse response;
protected Activity activity;
@ -31,30 +32,37 @@ public class WebsigninTask extends AsyncTask<String, Void, WebsigninTask.AuthRes
}
@Override
protected AuthResponse doInBackground(String... strings) {
protected Bundle doInBackground(String... strings) {
Bundle bundle = new Bundle();
try {
bundle.putString(ME, strings[0]);
Document doc = Jsoup.connect(strings[0]).get();
Elements links = doc.select("link[rel=\"authorization_endpoint\"]");
Elements links = doc.select("link");
for (Element link : links) {
String href = link.attr("href");
AuthResponse auth = new AuthResponse();
auth.me = strings[0];
auth.endpoint = href;
return auth;
String rel = link.attr("rel");
if (rel.equals("authorization_endpoint")) {
bundle.putString(rel, link.attr("href"));
}
else if (rel .equals("token_endpoint")) {
bundle.putString(rel, link.attr("href"));
}
else if (rel.equals("micropub")) {
bundle.putString(rel, link.attr("href"));
}
}
} catch (IOException e) {
return null;
return bundle;
}
return null;
Log.i("micropub", bundle.toString());
return bundle;
}
@Override
protected void onPostExecute(AuthResponse auth) {
protected void onPostExecute(Bundle bundle) {
Intent intent = new Intent(this.activity, AuthenticationActivity.class);
intent.putExtras(activity.getIntent());
intent.putExtra(ENDPOINT, auth.endpoint);
intent.putExtra(ME, auth.me);
intent.putExtras(bundle);
intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
this.activity.startActivity(intent);