Get the endpoints from the website instead of knowing them
This commit is contained in:
parent
777af7f40d
commit
3737d28ba9
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal 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>
|
|
@ -75,12 +75,18 @@ public class MainActivity extends AppCompatActivity {
|
||||||
public void sendPost(View view) {
|
public void sendPost(View view) {
|
||||||
AccountManager am = AccountManager.get(this);
|
AccountManager am = AccountManager.get(this);
|
||||||
Bundle options = new Bundle();
|
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 class OnTokenAcquired implements AccountManagerCallback<Bundle> {
|
||||||
|
private boolean sendMessage;
|
||||||
|
|
||||||
|
public OnTokenAcquired(boolean sendMessage) {
|
||||||
|
this.sendMessage = sendMessage;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(AccountManagerFuture<Bundle> result) {
|
public void run(AccountManagerFuture<Bundle> result) {
|
||||||
// Get the result of the operation from the AccountManagerFuture.
|
// 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.
|
// is stored in the constant AccountManager.KEY_AUTHTOKEN.
|
||||||
String token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
|
String token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
|
||||||
|
|
||||||
EditText mEdit = (EditText) findViewById(R.id.content);
|
if (sendMessage) {
|
||||||
//new PostMessageTask(MainActivity.this, token).execute("http://192.168.178.21:5000/micropub", mEdit.getText().toString());
|
EditText mEdit = (EditText) findViewById(R.id.content);
|
||||||
new PostMessageTask(MainActivity.this, token, mEdit).execute("https://publog.stuifzandapp.com/micropub", mEdit.getText().toString());
|
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);
|
Log.d("micropub", "GetTokenForAccount Bundle is " + token);
|
||||||
} catch (OperationCanceledException e) {
|
} catch (OperationCanceledException e) {
|
||||||
|
@ -114,7 +132,7 @@ public class MainActivity extends AppCompatActivity {
|
||||||
public void startSignin(View view) {
|
public void startSignin(View view) {
|
||||||
AccountManager am = AccountManager.get(this);
|
AccountManager am = AccountManager.get(this);
|
||||||
Bundle options = new Bundle();
|
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) {
|
private void showMessage(final String msg) {
|
||||||
|
|
|
@ -20,7 +20,7 @@ public class AuthenticatedActivity extends AppCompatActivity {
|
||||||
|
|
||||||
Intent intent = getIntent();
|
Intent intent = getIntent();
|
||||||
String urlString = intent.getStringExtra("url");
|
String urlString = intent.getStringExtra("url");
|
||||||
String endpoint = intent.getStringExtra(WebsigninTask.ENDPOINT);
|
String endpoint = intent.getStringExtra("authorization_endpoint");
|
||||||
String me = intent.getStringExtra(WebsigninTask.ME);
|
String me = intent.getStringExtra(WebsigninTask.ME);
|
||||||
|
|
||||||
TextView textResult = findViewById(R.id.textResult);
|
TextView textResult = findViewById(R.id.textResult);
|
||||||
|
|
|
@ -26,7 +26,6 @@ public class AuthenticationActivity extends AccountAuthenticatorActivity {
|
||||||
|
|
||||||
private static final String TAG = "AuthenticationActivity";
|
private static final String TAG = "AuthenticationActivity";
|
||||||
public static final String PARAM_USER_PASS = "eu.stuifzand.micropub.UserPass";
|
public static final String PARAM_USER_PASS = "eu.stuifzand.micropub.UserPass";
|
||||||
private static final int REQ_SIGNUP = 1;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
@ -34,7 +33,7 @@ public class AuthenticationActivity extends AccountAuthenticatorActivity {
|
||||||
setContentView(R.layout.activity_authentication);
|
setContentView(R.layout.activity_authentication);
|
||||||
|
|
||||||
Intent intent = getIntent();
|
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 String me = intent.getStringExtra(WebsigninTask.ME);
|
||||||
final AccountAuthenticatorResponse response = intent.getParcelableExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
|
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());
|
Log.i("micropub", "New API: " + request.getUrl().toString());
|
||||||
String url = request.getUrl().toString();
|
String url = request.getUrl().toString();
|
||||||
if (url.startsWith("https://stuifzand.eu/micropub-auth")) {
|
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);
|
HttpUrl httpUrl = HttpUrl.parse(url);
|
||||||
String code = httpUrl.queryParameter("code");
|
String code = httpUrl.queryParameter("code");
|
||||||
String state = httpUrl.queryParameter("state");
|
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
|
// 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)
|
// (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);
|
accountManager.setAuthToken(account, authtokenType, authtoken);
|
||||||
} else {
|
} else {
|
||||||
Log.d("micropub", TAG + "> finishLogin > setPassword");
|
Log.d("micropub", TAG + "> finishLogin > setPassword");
|
||||||
|
|
|
@ -13,6 +13,8 @@ import android.text.TextUtils;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
|
|
||||||
|
import org.w3c.dom.Text;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import okhttp3.Call;
|
import okhttp3.Call;
|
||||||
|
@ -79,9 +81,11 @@ public class Authenticator extends AbstractAccountAuthenticator {
|
||||||
.add("me", account.name)
|
.add("me", account.name)
|
||||||
.add("grant_type", "authorization_code")
|
.add("grant_type", "authorization_code")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
String tokenEndpoint = am.getUserData(account, "token_endpoint");
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
.addHeader("Accept", "application/json")
|
.addHeader("Accept", "application/json")
|
||||||
.url("https://tokens.indieauth.com/token")
|
.url(tokenEndpoint)
|
||||||
.method("POST", formBody)
|
.method("POST", formBody)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,8 @@ import android.accounts.AccountManager;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import org.jsoup.Jsoup;
|
import org.jsoup.Jsoup;
|
||||||
import org.jsoup.nodes.Document;
|
import org.jsoup.nodes.Document;
|
||||||
|
@ -14,8 +16,7 @@ import org.jsoup.select.Elements;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
|
||||||
public class WebsigninTask extends AsyncTask<String, Void, WebsigninTask.AuthResponse> {
|
public class WebsigninTask extends AsyncTask<String, Void, Bundle> {
|
||||||
public static final String ENDPOINT = "eu.stuifzand.microsub.ENDPOINT";
|
|
||||||
public static final String ME = "eu.stuifzand.micropub.ME";
|
public static final String ME = "eu.stuifzand.micropub.ME";
|
||||||
protected AccountAuthenticatorResponse response;
|
protected AccountAuthenticatorResponse response;
|
||||||
protected Activity activity;
|
protected Activity activity;
|
||||||
|
@ -31,30 +32,37 @@ public class WebsigninTask extends AsyncTask<String, Void, WebsigninTask.AuthRes
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected AuthResponse doInBackground(String... strings) {
|
protected Bundle doInBackground(String... strings) {
|
||||||
|
Bundle bundle = new Bundle();
|
||||||
try {
|
try {
|
||||||
|
bundle.putString(ME, strings[0]);
|
||||||
Document doc = Jsoup.connect(strings[0]).get();
|
Document doc = Jsoup.connect(strings[0]).get();
|
||||||
Elements links = doc.select("link[rel=\"authorization_endpoint\"]");
|
Elements links = doc.select("link");
|
||||||
for (Element link : links) {
|
for (Element link : links) {
|
||||||
String href = link.attr("href");
|
String rel = link.attr("rel");
|
||||||
|
if (rel.equals("authorization_endpoint")) {
|
||||||
AuthResponse auth = new AuthResponse();
|
bundle.putString(rel, link.attr("href"));
|
||||||
auth.me = strings[0];
|
}
|
||||||
auth.endpoint = href;
|
else if (rel .equals("token_endpoint")) {
|
||||||
return auth;
|
bundle.putString(rel, link.attr("href"));
|
||||||
|
}
|
||||||
|
else if (rel.equals("micropub")) {
|
||||||
|
bundle.putString(rel, link.attr("href"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
return null;
|
return bundle;
|
||||||
}
|
}
|
||||||
return null;
|
Log.i("micropub", bundle.toString());
|
||||||
|
return bundle;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(AuthResponse auth) {
|
protected void onPostExecute(Bundle bundle) {
|
||||||
Intent intent = new Intent(this.activity, AuthenticationActivity.class);
|
Intent intent = new Intent(this.activity, AuthenticationActivity.class);
|
||||||
intent.putExtras(activity.getIntent());
|
intent.putExtras(activity.getIntent());
|
||||||
intent.putExtra(ENDPOINT, auth.endpoint);
|
intent.putExtras(bundle);
|
||||||
intent.putExtra(ME, auth.me);
|
|
||||||
intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
|
intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
|
||||||
|
|
||||||
this.activity.startActivity(intent);
|
this.activity.startActivity(intent);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user