Skip to content

Commit

Permalink
Changing Old account to new account
Browse files Browse the repository at this point in the history
  • Loading branch information
ForceTower committed Aug 13, 2024
1 parent d694ee4 commit 0071f25
Show file tree
Hide file tree
Showing 40 changed files with 781 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ class FirebaseActionsService : FirebaseMessagingService() {

override fun onNewToken(token: String) {
Timber.d("On Token received: $token")
repository.onNewToken(token)
coroutineScope.launch {
repository.onNewToken(token)
}
}

override fun onDestroy() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.forcetower.uefs.core.model.edge

import com.google.gson.annotations.SerializedName

data class ChangePictureDTO(
@SerializedName("base64")
val base64: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.forcetower.uefs.core.model.edge

import com.google.gson.annotations.SerializedName

data class SendMessagingTokenDTO(
@SerializedName("token")
val token: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ import com.forcetower.uefs.core.model.unes.EdgeAccessToken
abstract class EdgeAccessTokenDao : BaseDao<EdgeAccessToken>() {
@Query("SELECT * FROM EdgeAccessToken LIMIT 1")
abstract suspend fun require(): EdgeAccessToken?

@Query("DELETE FROM EdgeAccessToken")
abstract suspend fun deleteAll()
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ import kotlinx.coroutines.flow.Flow
abstract class EdgeServiceAccountDao : BaseDao<EdgeServiceAccount>() {
@Query("SELECT * FROM EdgeServiceAccount WHERE me = 1 LIMIT 1")
abstract fun me(): Flow<EdgeServiceAccount>

@Query("SELECT * FROM EdgeServiceAccount WHERE me = 1 LIMIT 1")
abstract suspend fun requireMe(): EdgeServiceAccount?

@Query("DELETE FROM EdgeServiceAccount")
abstract suspend fun deleteAll()
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.forcetower.uefs.core.storage.network

import com.forcetower.uefs.core.model.edge.AssertionData
import com.forcetower.uefs.core.model.edge.ChangePictureDTO
import com.forcetower.uefs.core.model.edge.CompleteAssertionData
import com.forcetower.uefs.core.model.edge.EdgeAccessTokenDTO
import com.forcetower.uefs.core.model.edge.EdgeLoginBody
Expand All @@ -9,6 +10,7 @@ import com.forcetower.uefs.core.model.edge.EmailLinkConfirmDTO
import com.forcetower.uefs.core.model.edge.LinkEmailResponseDTO
import com.forcetower.uefs.core.model.edge.RegisterPasskeyCredential
import com.forcetower.uefs.core.model.edge.RegisterPasskeyStart
import com.forcetower.uefs.core.model.edge.SendMessagingTokenDTO
import com.forcetower.uefs.core.model.edge.ServiceAccountDTO
import com.forcetower.uefs.core.model.edge.ServiceResponseWrapper
import com.forcetower.uefs.core.model.unes.AccessToken
Expand All @@ -25,7 +27,7 @@ interface EdgeService {
suspend fun startAssertion(): AssertionData

@POST("auth/login/passkey/assertion/finish")
suspend fun completeAssertion(@Body data: CompleteAssertionData): AccessToken
suspend fun completeAssertion(@Body data: CompleteAssertionData): EdgeAccessTokenDTO

@GET("passkeys/register/start")
suspend fun registerPasskeyStart(): RegisterPasskeyStart
Expand All @@ -40,5 +42,11 @@ interface EdgeService {
suspend fun linkEmailStart(@Body data: EmailLinkBodyDTO): Response<ServiceResponseWrapper<LinkEmailResponseDTO>>

@POST("account/register/complete")
suspend fun linkEmailComplete(@Body data: EmailLinkConfirmDTO): Response<ServiceResponseWrapper<Unit>>
suspend fun linkEmailComplete(@Body data: EmailLinkConfirmDTO): Response<ServiceResponseWrapper<String>>

@POST("account/fcm")
suspend fun fcm(@Body data: SendMessagingTokenDTO): ServiceResponseWrapper<String>

@POST("account/picture")
suspend fun uploadPicture(@Body data: ChangePictureDTO): ServiceResponseWrapper<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ interface UService {
@POST("account/update_fcm")
fun sendToken(@Body data: Map<String, String>): Call<UResponse<Void>>

@POST("account/update_fcm")
suspend fun sendTokenSuspend(@Body data: Map<String, String>): UResponse<Void>

@GET("account")
fun getAccount(): Call<Account>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ import androidx.work.WorkManager
import com.forcetower.sagres.SagresNavigator
import com.forcetower.uefs.AppExecutors
import com.forcetower.uefs.BuildConfig
import com.forcetower.uefs.core.model.edge.SendMessagingTokenDTO
import com.forcetower.uefs.core.model.unes.Message
import com.forcetower.uefs.core.notification.StatementNotificationProcessor
import com.forcetower.uefs.core.storage.database.UDatabase
import com.forcetower.uefs.core.storage.network.EdgeService
import com.forcetower.uefs.core.storage.network.UService
import com.forcetower.uefs.core.work.hourglass.HourglassContributeWorker
import com.forcetower.uefs.core.work.sync.SyncLinkedWorker
Expand All @@ -41,13 +43,15 @@ import com.forcetower.uefs.service.NotificationCreator
import com.google.android.gms.tasks.Tasks
import com.google.firebase.messaging.FirebaseMessaging
import com.google.firebase.messaging.RemoteMessage
import kotlinx.coroutines.tasks.await
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class FirebaseMessageRepository @Inject constructor(
private val service: UService,
private val edgeService: EdgeService,
private val database: UDatabase,
private val preferences: SharedPreferences,
private val context: Context,
Expand Down Expand Up @@ -277,15 +281,19 @@ class FirebaseMessageRepository @Inject constructor(
NotificationCreator.showSimpleNotification(context, title, content)
}

fun onNewToken(token: String) {
val auth = database.accessTokenDao().getAccessTokenDirect()
suspend fun onNewToken(token: String) {
val auth = database.accessTokenDao().getAccessTokenDirectSuspend()
if (auth != null) {
try {
service.sendToken(mapOf("token" to token)).execute()
} catch (t: Throwable) { }
runCatching { service.sendTokenSuspend(mapOf("token" to token)) }
} else {
Timber.d("Disconnected")
Timber.d("Disconnected Base UNES")
}

val auth2 = database.edgeAccessToken.require()
if (auth2 != null) {
runCatching { edgeService.fcm(SendMessagingTokenDTO(token)) }
}

preferences.edit().putString("current_firebase_token", token).apply()
}

Expand All @@ -301,23 +309,13 @@ class FirebaseMessageRepository @Inject constructor(
}
}

@MainThread
fun sendNewTokenOrNot(): LiveData<Boolean> {
val result = MutableLiveData<Boolean>()
executors.diskIO().execute {
try {
sendNewToken()
result.postValue(true)
} catch (t: Throwable) {
result.postValue(false)
}
suspend fun sendNewTokenOrNot() {
try {
val task = FirebaseMessaging.getInstance().token
val value = task.await()
onNewToken(value)
} catch (e: Throwable) {
Timber.e(e, "Failed to update fcm token")
}
return result
}

private fun sendNewToken() {
val task = FirebaseMessaging.getInstance().token
val value = Tasks.await(task)
onNewToken(value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class SagresDataRepository @Inject constructor(
}
}

suspend fun logoutSuspend() {
database.edgeAccessToken.deleteAll()
database.edgeServiceAccount.deleteAll()
}

fun getFlags() = database.flagsDao().getFlags()
fun getSemesters() = database.semesterDao().getParticipatingSemesters()
fun getCourse() = database.profileDao().getProfileCourse()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.forcetower.uefs.core.storage.repository.cloud

import com.forcetower.uefs.core.model.edge.ChangePictureDTO
import com.forcetower.uefs.core.model.unes.EdgeServiceAccount
import com.forcetower.uefs.core.storage.database.UDatabase
import com.forcetower.uefs.core.storage.network.EdgeService
Expand Down Expand Up @@ -28,4 +29,9 @@ class EdgeAccountRepository @Inject constructor(
)
database.edgeServiceAccount.insertOrUpdate(value)
}

suspend fun uploadPicture(base64: String) {
service.uploadPicture(ChangePictureDTO(base64))
runCatching { fetchAccountIfNeeded() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.forcetower.uefs.core.model.edge.RegisterPasskeyStart
import com.forcetower.uefs.core.model.ui.edge.EmailLinkComplete
import com.forcetower.uefs.core.model.ui.edge.EmailLinkStart
import com.forcetower.uefs.core.model.unes.EdgeAccessToken
import com.forcetower.uefs.core.model.unes.EdgeServiceAccount
import com.forcetower.uefs.core.storage.database.UDatabase
import com.forcetower.uefs.core.storage.network.EdgeService
import timber.log.Timber
Expand All @@ -30,9 +31,23 @@ class EdgeAuthRepository @Inject constructor(
return service.startAssertion()
}

suspend fun completeAssertion(flowId: String, response: String) {
suspend fun completeAssertion(flowId: String, response: String): EdgeServiceAccount? {
val token = service.completeAssertion(CompleteAssertionData(flowId, response))
Timber.d("Token $token")
database.edgeAccessToken.insert(EdgeAccessToken(token.accessToken))

runCatching {
val me = service.me().data
val value = EdgeServiceAccount(
id = me.id,
name = me.name,
email = me.email,
imageUrl = me.imageUrl,
me = true
)
database.edgeServiceAccount.insertOrUpdate(value)
}

return database.edgeServiceAccount.requireMe()
}

suspend fun passkeyRegisterStart(): RegisterPasskeyStart {
Expand Down Expand Up @@ -66,6 +81,7 @@ class EdgeAuthRepository @Inject constructor(

return EmailLinkComplete.ConnectionError
} catch (error: Throwable) {
Timber.e(error, "Failed to finish registration!")
return EmailLinkComplete.ConnectionError
}
}
Expand Down Expand Up @@ -93,7 +109,7 @@ class EdgeAuthRepository @Inject constructor(
database.edgeAccessToken.insert(EdgeAccessToken(result.accessToken))
}

suspend fun doAnonymousLogin() {
suspend fun doAnonymousLogin(): EdgeServiceAccount? {
val access = database.accessDao().getAccessDirectSuspend() ?: throw IllegalStateException("Access is null!")

if (!access.valid) {
Expand All @@ -103,5 +119,19 @@ class EdgeAuthRepository @Inject constructor(
val result = service.loginAnonymous(EdgeLoginBody(access.username, access.password))
Timber.i("Login completed with result $result")
database.edgeAccessToken.insert(EdgeAccessToken(result.accessToken))

runCatching {
val me = service.me().data
val value = EdgeServiceAccount(
id = me.id,
name = me.name,
email = me.email,
imageUrl = me.imageUrl,
me = true
)
database.edgeServiceAccount.insertOrUpdate(value)
}

return database.edgeServiceAccount.requireMe()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.forcetower.uefs.domain.usecase.account

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.media.ThumbnailUtils
import android.net.Uri
import android.util.Base64
import com.forcetower.uefs.core.storage.repository.cloud.EdgeAccountRepository
import dagger.Reusable
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.ByteArrayOutputStream
import javax.inject.Inject

@Reusable
class ChangeProfilePictureUseCase @Inject constructor(
private val repository: EdgeAccountRepository,
@ApplicationContext private val context: Context
) {
suspend operator fun invoke(uri: Uri) = withContext(Dispatchers.IO) {
val resolver = context.contentResolver
val stream = resolver.openInputStream(uri) ?: return@withContext

val image = BitmapFactory.decodeStream(stream)
image ?: return@withContext

val bitmap = ThumbnailUtils.extractThumbnail(image, 1080, 1080)

val baos = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos)
val data = baos.toByteArray()

val encoded = Base64.encodeToString(data, Base64.DEFAULT)

repository.uploadPicture(encoded)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.forcetower.uefs.domain.usecase.auth

import com.forcetower.uefs.core.model.unes.EdgeServiceAccount
import com.forcetower.uefs.core.storage.repository.cloud.EdgeAuthRepository
import dagger.Reusable
import timber.log.Timber
Expand All @@ -9,8 +10,8 @@ import javax.inject.Inject
class CompleteAssertionUseCase @Inject constructor(
private val auth: EdgeAuthRepository
) {
suspend operator fun invoke(flowId: String, response: String) {
suspend operator fun invoke(flowId: String, response: String): EdgeServiceAccount? {
Timber.d("Credential: $response")
auth.completeAssertion(flowId, response)
return auth.completeAssertion(flowId, response)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.forcetower.uefs.domain.usecase.auth

import com.forcetower.uefs.core.model.unes.EdgeServiceAccount
import com.forcetower.uefs.core.storage.repository.cloud.EdgeAuthRepository
import dagger.Reusable
import javax.inject.Inject
Expand All @@ -9,11 +10,11 @@ class EdgeAnonymousLoginUseCase @Inject constructor(
private val repository: EdgeAuthRepository
) {
suspend fun prepareAndLogin() {
repository.prepareAndLogin()
return repository.prepareAndLogin()
}

suspend fun loginOrThrow() {
repository.doAnonymousLogin()
suspend fun loginOrThrow(): EdgeServiceAccount? {
return repository.doAnonymousLogin()
}

suspend fun invoke(username: String, password: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class RegisterPasskeyUseCase @Inject constructor(
) {
suspend fun start(): RegisterPasskeyStart {
val data = edge.passkeyRegisterStart()
Timber.d("Original data: ${data.create}")
val register = gson.fromJson(data.create, PasskeyRegister::class.java)
return data.copy(create = gson.toJson(register.publicKey))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,7 @@ class HomeActivity : UGameActivity() {
viewModel.accessToken.observe(this) { onAccessTokenUpdate(it) }
viewModel.snackbarMessage.observe(this, EventObserver { showSnack(it) })
dynamicDFMViewModel.snackbarMessage.observe(this, EventObserver { showSnack(it) })
viewModel.sendToken().observe(this) {
Timber.d("Sent token... I guess ($it)")
}
viewModel.sendToken()
if (preferences.isStudentFromUEFS()) {
// Update and unlock achievements for participating in a class with the creator
viewModel.connectToServiceIfNeeded()
Expand Down
Loading

0 comments on commit 0071f25

Please sign in to comment.