Store refresh tokens

This commit is contained in:
Matthieu 2020-11-29 14:58:54 +01:00
parent 18df0d030b
commit bf9d5287ba
12 changed files with 76 additions and 67 deletions

View File

@ -43,13 +43,14 @@ class CameraTest {
db.userDao().insertUser(
UserDatabaseEntity(
user_id = "123",
instance_uri = "http://localhost",
username = "Testi",
display_name = "Testi Testo",
avatar_static = "some_avatar_url",
isActive = true,
accessToken = "token"
user_id = "123",
instance_uri = "http://localhost",
username = "Testi",
display_name = "Testi Testo",
avatar_static = "some_avatar_url",
isActive = true,
accessToken = "token",
refreshToken = refreshToken
)
)
db.close()

View File

@ -54,13 +54,14 @@ class DrawerMenuTest {
db.userDao().insertUser(
UserDatabaseEntity(
user_id = "123",
instance_uri = baseUrl.toString(),
username = "Testi",
display_name = "Testi Testo",
avatar_static = "some_avatar_url",
isActive = true,
accessToken = "token"
user_id = "123",
instance_uri = baseUrl.toString(),
username = "Testi",
display_name = "Testi Testo",
avatar_static = "some_avatar_url",
isActive = true,
accessToken = "token",
refreshToken = refreshToken
)
)
db.close()

View File

@ -61,13 +61,14 @@ class HomeFeedTest {
)
db.userDao().insertUser(
UserDatabaseEntity(
user_id = "123",
instance_uri = baseUrl.toString(),
username = "Testi",
display_name = "Testi Testo",
avatar_static = "some_avatar_url",
isActive = true,
accessToken = "token"
user_id = "123",
instance_uri = baseUrl.toString(),
username = "Testi",
display_name = "Testi Testo",
avatar_static = "some_avatar_url",
isActive = true,
accessToken = "token",
refreshToken = refreshToken
)
)
db.close()

View File

@ -77,13 +77,14 @@ class IntentTest {
db.userDao().insertUser(
UserDatabaseEntity(
user_id = "123",
instance_uri = baseUrl.toString(),
username = "Testi",
display_name = "Testi Testo",
avatar_static = "some_avatar_url",
isActive = true,
accessToken = "token"
user_id = "123",
instance_uri = baseUrl.toString(),
username = "Testi",
display_name = "Testi Testo",
avatar_static = "some_avatar_url",
isActive = true,
accessToken = "token",
refreshToken = refreshToken
)
)
db.close()

View File

@ -117,13 +117,14 @@ class LoginActivityOnlineTest {
db.userDao().insertUser(
UserDatabaseEntity(
user_id = "123",
instance_uri = server.getUrl().toString(),
username = "Testi",
display_name = "Testi Testo",
avatar_static = "some_avatar_url",
isActive = true,
accessToken = "token"
user_id = "123",
instance_uri = server.getUrl().toString(),
username = "Testi",
display_name = "Testi Testo",
avatar_static = "some_avatar_url",
isActive = true,
accessToken = "token",
refreshToken = refreshToken
)
)
db.close()

View File

@ -89,13 +89,14 @@ class PostFragmentUITests {
db.userDao().insertUser(
UserDatabaseEntity(
user_id = "123",
instance_uri = baseUrl.toString(),
username = "Testi",
display_name = "Testi Testo",
avatar_static = "some_avatar_url",
isActive = true,
accessToken = "token"
user_id = "123",
instance_uri = baseUrl.toString(),
username = "Testi",
display_name = "Testi Testo",
avatar_static = "some_avatar_url",
isActive = true,
accessToken = "token",
refreshToken = refreshToken
)
)
db.close()

View File

@ -55,13 +55,14 @@ class PostTest {
db.userDao().insertUser(
UserDatabaseEntity(
user_id = "123",
instance_uri = baseUrl.toString(),
username = "Testi",
display_name = "Testi Testo",
avatar_static = "some_avatar_url",
isActive = true,
accessToken = "token"
user_id = "123",
instance_uri = baseUrl.toString(),
username = "Testi",
display_name = "Testi Testo",
avatar_static = "some_avatar_url",
isActive = true,
accessToken = "token",
refreshToken = refreshToken
)
)
db.close()

View File

@ -24,7 +24,6 @@ import io.reactivex.SingleObserver
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.functions.BiFunction
import io.reactivex.functions.Function3
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_login.*
import okhttp3.HttpUrl
@ -256,7 +255,7 @@ class LoginActivity : AppCompatActivity() {
//TODO check why we can't do onErrorReturn { null } which would make more sense ¯\_(ツ)_/¯
//Also, maybe find a nicer way to do this, this feels hacky (although it can work fine)
val nullInstance = Instance(null, null, null, null, null, null, null, null)
val nullToken = Token(null, null, null, null)
val nullToken = Token(null, null, null, null, null)
Single.zip(
pixelfedAPI.instance().onErrorReturn { nullInstance },
@ -280,7 +279,7 @@ class LoginActivity : AppCompatActivity() {
}
DBUtils.storeInstance(db, instance)
storeUser(token.access_token, instance.uri)
storeUser(token.access_token, token.refresh_token, instance.uri)
wipeSharedSettings()
}
@ -314,7 +313,7 @@ class LoginActivity : AppCompatActivity() {
}
}
private fun storeUser(accessToken: String, instance: String) {
private fun storeUser(accessToken: String, refreshToken: String?, instance: String) {
pixelfedAPI.verifyCredentials("Bearer $accessToken")
.enqueue(object : Callback<Account> {
override fun onResponse(call: Call<Account>, response: Response<Account>) {
@ -326,7 +325,8 @@ class LoginActivity : AppCompatActivity() {
user,
instance,
activeUser = true,
accessToken = accessToken
accessToken = accessToken,
refreshToken = refreshToken
)
apiHolder.setDomainToCurrentUser(db)
val intent = Intent(this@LoginActivity, MainActivity::class.java)

View File

@ -8,7 +8,6 @@ import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ImageView
import androidx.annotation.DrawableRes
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.GravityCompat
@ -190,6 +189,7 @@ class MainActivity : AppCompatActivity() {
if (hasInternet(applicationContext)) {
val domain = user?.instance_uri.orEmpty()
val accessToken = user?.accessToken.orEmpty()
val refreshToken = user?.refreshToken
val api = apiHolder.api ?: apiHolder.setDomainToCurrentUser(db)
api.verifyCredentials("Bearer $accessToken")
.enqueue(object : Callback<Account> {
@ -199,7 +199,7 @@ class MainActivity : AppCompatActivity() {
) {
if (response.body() != null && response.isSuccessful) {
val account = response.body() as Account
DBUtils.addUser(db, account, domain, accessToken = accessToken)
DBUtils.addUser(db, account, domain, accessToken = accessToken, refreshToken = refreshToken)
fillDrawerAccountInfo(account.id!!)
}
}

View File

@ -3,7 +3,6 @@ package com.h.pixeldroid.db.entities
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Index
import com.h.pixeldroid.db.entities.InstanceDatabaseEntity
@Entity(
tableName = "users",
@ -17,12 +16,13 @@ import com.h.pixeldroid.db.entities.InstanceDatabaseEntity
)],
indices = [Index(value = ["instance_uri"])]
)
data class UserDatabaseEntity (
var user_id: String,
var instance_uri: String,
var username: String,
var display_name: String,
var avatar_static: String,
var isActive: Boolean,
var accessToken: String
data class UserDatabaseEntity(
var user_id: String,
var instance_uri: String,
var username: String,
var display_name: String,
var avatar_static: String,
var isActive: Boolean,
var accessToken: String,
val refreshToken: String?
)

View File

@ -2,6 +2,7 @@ package com.h.pixeldroid.objects
data class Token(
val access_token: String?,
val refresh_token: String?,
val token_type: String?,
val scope: String?,
val created_at: Int?

View File

@ -19,7 +19,7 @@ class DBUtils {
}
}
fun addUser(db: AppDatabase, account: Account, instance_uri: String, activeUser: Boolean = true, accessToken: String) {
fun addUser(db: AppDatabase, account: Account, instance_uri: String, activeUser: Boolean = true, accessToken: String, refreshToken: String?) {
db.userDao().insertUser(
UserDatabaseEntity(
user_id = account.id!!,
@ -29,7 +29,8 @@ class DBUtils {
display_name = account.getDisplayName(),
avatar_static = account.avatar_static.orEmpty(),
isActive = activeUser,
accessToken = accessToken
accessToken = accessToken,
refreshToken = refreshToken
)
)
}