Refresh the token when expired #699

This commit is contained in:
stom79 2019-01-05 13:00:15 +01:00
parent eefe1edc3b
commit 4d8547fe52
5 changed files with 70 additions and 13 deletions

View File

@ -165,12 +165,15 @@ public class WebviewConnectActivity extends BaseActivity {
try {
resobj = new JSONObject(response);
String token = resobj.get("access_token").toString();
String refresh_token = null;
if( resobj.has("refresh_token"))
refresh_token = resobj.get("access_token").toString();
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token);
editor.apply();
//Update the account with the token;
new UpdateAccountInfoAsyncTask(WebviewConnectActivity.this, token, instance, social).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new UpdateAccountInfoAsyncTask(WebviewConnectActivity.this, token, clientId, clientSecret, refresh_token, instance, social).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} catch (JSONException ignored) {}
} catch (Exception ignored) {}
}}).start();

View File

@ -24,10 +24,12 @@ import android.os.AsyncTask;
import java.io.UnsupportedEncodingException;
import java.lang.ref.WeakReference;
import java.net.URLDecoder;
import java.util.HashMap;
import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.HttpsConnection;
import fr.gouv.etalab.mastodon.client.PeertubeAPI;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
@ -67,8 +69,16 @@ public class UpdateAccountInfoAsyncTask extends AsyncTask<Void, Void, Void> {
account = new API(this.contextReference.get(), instance, null).verifyCredentials();
account.setSocial("MASTODON");
}else if( social == SOCIAL.PEERTUBE) {
account = new PeertubeAPI(this.contextReference.get(), instance, null).verifyCredentials();
account.setSocial("PEERTUBE");
try {
account = new PeertubeAPI(this.contextReference.get(), instance, null).verifyCredentials();
account.setSocial("PEERTUBE");
}catch (HttpsConnection.HttpsConnectionException exception){
if(exception.getStatusCode() == 401){
HashMap<String, String> values = new PeertubeAPI(this.contextReference.get(), instance, null).refreshToken(client_id, client_secret, refresh_token);
this.token = values.get("access_token");
this.refresh_token = values.get("refresh_token");
}
}
}
if( account == null)

View File

@ -25,6 +25,7 @@ import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Emojis;
import fr.gouv.etalab.mastodon.client.HttpsConnection;
import fr.gouv.etalab.mastodon.client.PeertubeAPI;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnUpdateAccountInfoInterface;
@ -58,8 +59,13 @@ public class UpdateAccountInfoByIDAsyncTask extends AsyncTask<Void, Void, Void>
Account account = null;
if( social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON)
account = new API(this.contextReference.get()).getAccount(userId);
else if( social == UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE)
account = new PeertubeAPI(this.contextReference.get()).verifyCredentials();
else if( social == UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE) {
try {
account = new PeertubeAPI(this.contextReference.get()).verifyCredentials();
} catch (HttpsConnection.HttpsConnectionException e) {
e.printStackTrace();
}
}
if( account == null)
return null;
account.setInstance(Helper.getLiveInstance(contextReference.get()));

View File

@ -206,15 +206,12 @@ public class PeertubeAPI {
* Verifiy credential of the authenticated user *synchronously*
* @return Account
*/
public Account verifyCredentials() {
public Account verifyCredentials() throws HttpsConnection.HttpsConnectionException {
account = new Account();
try {
String response = new HttpsConnection(context).get(getAbsoluteUrl("/users/me"), 60, null, prefKeyOauthTokenT);
JSONObject accountObject = new JSONObject(response).getJSONObject("account");
account = parseAccountResponsePeertube(context, accountObject);
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
@ -227,6 +224,46 @@ public class PeertubeAPI {
return account;
}
/***
* Verifiy credential of the authenticated user *synchronously*
* @return Account
*/
public HashMap<String, String> refreshToken(String client_id, String client_secret, String refresh_token) {
account = new Account();
HashMap<String, String> params = new HashMap<>();
HashMap<String, String> newValues = new HashMap<>();
params.put("grant_type", "refresh_token");
params.put("client_id", client_id);
params.put("client_secret", client_secret);
params.put("refresh_token", refresh_token);
try {
String response = new HttpsConnection(context).post(getAbsoluteUrl("/users/token"), 60, params, null);
JSONObject resobj = new JSONObject(response);
String token = resobj.get("access_token").toString();
if( resobj.has("refresh_token"))
refresh_token = resobj.get("access_token").toString();
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token);
editor.apply();
newValues.put("access_token",token);
newValues.put("refresh_token",refresh_token);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (HttpsConnection.HttpsConnectionException e) {
e.printStackTrace();
}
return newValues;
}
/**
* Returns an account
* @param accountId String account fetched

View File

@ -83,6 +83,9 @@ public class Sqlite extends SQLiteOpenHelper {
static final String COL_OAUTHTOKEN = "OAUTH_TOKEN";
static final String COL_EMOJIS = "EMOJIS";
static final String COL_SOCIAL = "SOCIAL";
static final String COL_CLIENT_ID = "CLIENT_ID";
static final String COL_CLIENT_SECRET = "CLIENT_SECRET";
static final String COL_REFRESH_TOKEN = "REFRESH_TOKEN";
private static final String CREATE_TABLE_USER_ACCOUNT = "CREATE TABLE " + TABLE_USER_ACCOUNT + " ("
+ COL_USER_ID + " TEXT PRIMARY KEY, " + COL_USERNAME + " TEXT NOT NULL, " + COL_ACCT + " TEXT NOT NULL, "
@ -93,6 +96,7 @@ public class Sqlite extends SQLiteOpenHelper {
+ COL_HEADER + " TEXT NOT NULL, "+ COL_HEADER_STATIC + " TEXT NOT NULL, "
+ COL_EMOJIS + " TEXT, "
+ COL_SOCIAL + " TEXT, "
+ COL_CLIENT_ID + " TEXT, " + COL_CLIENT_SECRET + " TEXT, " + COL_REFRESH_TOKEN + " TEXT,"
+ COL_INSTANCE + " TEXT NOT NULL, " + COL_OAUTHTOKEN + " TEXT NOT NULL, " + COL_CREATED_AT + " TEXT NOT NULL)";
@ -166,9 +170,7 @@ public class Sqlite extends SQLiteOpenHelper {
static final String COL_PINNED = "PINNED";
static final String COL_DATE_BACKUP = "DATE_BACKUP";
static final String COL_CARD = "CARD";
static final String COL_CLIENT_ID = "CLIENT_ID";
static final String COL_CLIENT_SECRET = "CLIENT_SECRET";
static final String COL_REFRESH_TOKEN = "REFRESH_TOKEN";
private final String CREATE_TABLE_STATUSES_CACHE = "CREATE TABLE " + TABLE_STATUSES_CACHE + " ("
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
@ -180,7 +182,6 @@ public class Sqlite extends SQLiteOpenHelper {
+ COL_REBLOGGED + " INTEGER, " + COL_FAVOURITED + " INTEGER, " + COL_MUTED + " INTEGER, " + COL_SENSITIVE + " INTEGER, "
+ COL_SPOILER_TEXT + " TEXT, " + COL_VISIBILITY + " TEXT NOT NULL, " + COL_MEDIA_ATTACHMENTS + " TEXT," + COL_CARD + " TEXT,"
+ COL_MENTIONS + " TEXT, " + COL_TAGS + " TEXT, " + COL_APPLICATION + " TEXT,"
+ COL_CLIENT_ID + " TEXT, " + COL_CLIENT_SECRET + " TEXT, " + COL_REFRESH_TOKEN + " TEXT,"
+ COL_LANGUAGE + " TEXT," + COL_PINNED + " INTEGER)";
private final String CREATE_UNIQUE_CACHE_INDEX = "CREATE UNIQUE INDEX instance_statusid on "