Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
Change “reauth” on iOS to a server ping
Browse files Browse the repository at this point in the history
  • Loading branch information
syoung-smallwisdom committed Mar 20, 2023
1 parent fac142b commit c937725
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,7 @@ class AuthenticationRepository(
return false
}

override suspend fun reAuth() : Boolean = reAuthWithError().first

suspend fun reAuthWithError() : Pair<Boolean, Error?> {
override suspend fun reAuth() : Boolean {
val sessionInfo = session()
return sessionInfo?.reauthToken?.let { reauthToken ->
val signIn = SignIn(
Expand All @@ -274,14 +272,12 @@ class AuthenticationRepository(
reauthToken = reauthToken
)
var success = false
var responseError: Error? = null
try {
val userSession = authenticationApi.reauthenticate(signIn)
updateCachedSession(sessionInfo, userSession)
Logger.i("Session token updated.")
success = true
} catch (err: Throwable) {
responseError = Error(err.message ?: "Error requesting reAuth: $err")
if (err is ResponseException && (err.response.status == HttpStatusCode.Unauthorized ||
err.response.status == HttpStatusCode.Forbidden ||
err.response.status == HttpStatusCode.NotFound ||
Expand All @@ -299,8 +295,8 @@ class AuthenticationRepository(
Logger.i("User reauth failed. Ignoring. $err")
}
}
Pair(success, responseError)
} ?: Pair(false, Error("reAuth token is null"))
success
} ?: false
}

override fun notifyUIOfBridgeError(statusCode: HttpStatusCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import io.ktor.http.*
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.datetime.Clock
import kotlinx.datetime.DateTimeUnit
import kotlinx.datetime.Instant
import kotlinx.datetime.minus
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import org.sagebionetworks.bridge.kmm.shared.cache.ResourceResult
Expand All @@ -14,13 +18,16 @@ import org.sagebionetworks.bridge.kmm.shared.models.UserSessionInfo
import org.sagebionetworks.bridge.kmm.shared.repo.AppStatus
import org.sagebionetworks.bridge.kmm.shared.repo.AuthenticationRepository
import org.sagebionetworks.bridge.kmm.shared.repo.ParticipantRepo
import org.sagebionetworks.bridge.kmm.shared.repo.ParticipantReportRepo
import platform.Foundation.NSUUID

open class NativeAuthenticationManager(
private val viewUpdate: (UserSessionInfo?) -> Unit
) : KoinComponent {

private val authManager : AuthenticationRepository by inject(mode = LazyThreadSafetyMode.NONE)
private val participantManager : ParticipantRepo by inject(mode = LazyThreadSafetyMode.NONE)
private val reportRepo : ParticipantReportRepo by inject(mode = LazyThreadSafetyMode.NONE)

private val scope = MainScope()

Expand Down Expand Up @@ -55,11 +62,20 @@ open class NativeAuthenticationManager(

open fun reauth(completion: (Error?) -> Unit) {
scope.launch {
val response = authManager.reAuthWithError()
if (response.first) {
completion(null)
val studyId = authManager.session()?.studyIds?.firstOrNull()
if (studyId == null) {
completion(Error("reAuth failed. Null session."))
} else {
completion(response.second ?: Error("reAuth failed. Unknown cause."))
// Ping the server to reauthenticate using a participant report
val end = Clock.System.now()
val start = end.minus(1, DateTimeUnit.MINUTE)
val uuid = NSUUID().UUIDString
val ping = reportRepo.loadRemoteReports(studyId, "Ping$uuid", start, end)
if (ping) {
completion(null)
} else {
completion(Error("reAuth failed. Unknown cause."))
}
}
}
}
Expand Down

0 comments on commit c937725

Please sign in to comment.