Fix refresh token

This commit is contained in:
Thomas 2020-07-25 19:05:16 +02:00
parent abe8812e6e
commit 42b17d8d71
5 changed files with 27 additions and 9 deletions

View File

@ -586,7 +586,8 @@ public class LoginActivity extends BaseActivity {
}
runOnUiThread(() -> {
JSONObject resobj;
if (socialNetwork != UpdateAccountInfoAsyncTask.SOCIAL.GNU && socialNetwork != UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA) {
if (socialNetwork != UpdateAccountInfoAsyncTask.SOCIAL.GNU && socialNetwork != UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA)
{
try {
resobj = new JSONObject(response);
String token = resobj.getString("access_token");
@ -607,7 +608,8 @@ public class LoginActivity extends BaseActivity {
} catch (JSONException e) {
e.printStackTrace();
}
} else {
} else
{
try {
resobj = new JSONObject(response);
Account account = GNUAPI.parseAccountResponse(resobj);
@ -623,9 +625,10 @@ public class LoginActivity extends BaseActivity {
editor.putBoolean(Helper.PREF_IS_ADMINISTRATOR, account.isAdmin());
editor.putString(Helper.PREF_INSTANCE, instance);
editor.apply();
if (userExists)
if (userExists) {
new AccountDAO(LoginActivity.this, db).updateAccount(account);
else {
}else {
if (account.getUsername() != null && account.getCreated_at() != null)
new AccountDAO(LoginActivity.this, db).insertAccount(account);
}

View File

@ -88,6 +88,7 @@ public class UpdateAccountInfoAsyncTask extends AsyncTask<Void, Void, Void> {
account.setClient_secret(client_secret);
account.setRefresh_token(refresh_token);
account.setInstance(instance);
SQLiteDatabase db = Sqlite.getInstance(this.contextReference.get().getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
boolean userExists = new AccountDAO(this.contextReference.get(), db).userExist(account);
SharedPreferences.Editor editor = sharedpreferences.edit();

View File

@ -80,7 +80,6 @@ public class UpdateAccountInfoByIDAsyncTask extends AsyncTask<Void, Void, Void>
account.setClient_secret(accountOld.getClient_secret());
new AccountDAO(this.contextReference.get(), db).updateAccountCredential(account);
}
if (social == UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE || social == UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED) {
new API(contextReference.get()).refreshToken(account);
}

View File

@ -2552,6 +2552,7 @@ public class API {
try {
String response;
if (account.getSocial().compareTo("PEERTUBE") == 0) {
response = new HttpsConnection(context, instance).post(getAbsoluteUrl("/users/token"), 60, params, null);
} else {
response = new HttpsConnection(context, instance).post("https://" + instance + "/oauth/token", 60, params, null);
@ -2573,6 +2574,11 @@ public class API {
}
account.setRefresh_token(newRefreshToken);
account.setToken(newToken);
if( account.getHeader() == null){
account.setHeader("");
account.setHeader_static("");
}
account.setInstance(instance);
new AccountDAO(context, db).updateAccount(account);
} catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) {
e.printStackTrace();

View File

@ -123,7 +123,9 @@ public class AccountDAO {
values.put(Sqlite.COL_HEADER_STATIC, account.getHeader_static());
values.put(Sqlite.COL_CREATED_AT, Helper.dateToString(account.getCreated_at()));
values.put(Sqlite.COL_EMOJIS, Helper.emojisToStringStorage(account.getEmojis()));
values.put(Sqlite.COL_SOCIAL, account.getSocial());
if( account.getSocial() != null) {
values.put(Sqlite.COL_SOCIAL, account.getSocial());
}
if (account.getClient_id() != null && account.getClient_secret() != null) {
values.put(Sqlite.COL_CLIENT_ID, account.getClient_id());
values.put(Sqlite.COL_CLIENT_SECRET, account.getClient_secret());
@ -131,11 +133,18 @@ public class AccountDAO {
if (account.getRefresh_token() != null) {
values.put(Sqlite.COL_REFRESH_TOKEN, account.getRefresh_token());
}
if (account.getToken() != null)
values.put(Sqlite.COL_OAUTHTOKEN, account.getToken());
return db.update(Sqlite.TABLE_USER_ACCOUNT,
values, Sqlite.COL_USER_ID + " = ? AND " + Sqlite.COL_INSTANCE + " =?",
new String[]{account.getId(), account.getInstance()});
try {
return db.update(Sqlite.TABLE_USER_ACCOUNT,
values, Sqlite.COL_USER_ID + " = ? AND " + Sqlite.COL_INSTANCE + " =?",
new String[]{account.getId(), account.getInstance()});
}catch (Exception e){
e.printStackTrace();
return -1;
}
}