Skip to content

Commit

Permalink
Pass dispatcher as optional constructor arg into auth's TokenRepository
Browse files Browse the repository at this point in the history
Running suspend funs in here using withContextbdDispatchers.IO) now forces us to be able to inject a CoroutineDispatcher, as otherwise tests won't work.
  • Loading branch information
michpohl committed Sep 16, 2024
1 parent ab807b5 commit 08ea6b9
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 41 deletions.
51 changes: 26 additions & 25 deletions auth/src/main/kotlin/com/tidal/sdk/auth/TokenRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.tidal.sdk.common.UnexpectedError
import com.tidal.sdk.common.d
import com.tidal.sdk.common.logger
import java.net.HttpURLConnection
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.sync.Mutex
Expand All @@ -36,8 +37,11 @@ internal class TokenRepository(
private val upgradeBackoffPolicy: RetryPolicy,
private val tokenMutex: Mutex,
private val bus: MutableSharedFlow<TidalMessage>,
coroutineDispatcher: CoroutineDispatcher? = null,
) {

private val dispatcher = coroutineDispatcher ?: Dispatchers.IO

private fun needsCredentialsUpgrade(): Boolean {
val storedCredentials = getLatestTokens()?.credentials
return with(authConfig) {
Expand All @@ -57,14 +61,12 @@ internal class TokenRepository(
}
}

internal fun getLatestTokens(): Tokens? {
return tokensStore.getLatestTokens(authConfig.credentialsKey)
}
internal fun getLatestTokens(): Tokens? = tokensStore.getLatestTokens(authConfig.credentialsKey)

@Suppress("UnusedPrivateMember")
suspend fun getCredentials(apiErrorSubStatus: String?): AuthResult<Credentials> {
logger.d { "Received subStatus: $apiErrorSubStatus" }
return withContext(Dispatchers.IO) {
return withContext(dispatcher) {
tokenMutex.withLock {
var upgradedRefreshToken: String? = null
val credentials = getLatestTokens()
Expand All @@ -91,26 +93,24 @@ internal class TokenRepository(
private suspend fun updateCredentials(
storedTokens: Tokens?,
apiErrorSubStatus: String?,
): AuthResult<Credentials> {
return when {
storedTokens?.credentials?.isExpired(timeProvider) == false &&
apiErrorSubStatus.shouldRefreshToken().not() -> {
success(storedTokens.credentials)
}
// if a refreshToken is available, we'll use it
storedTokens?.refreshToken != null -> {
val refreshToken = storedTokens.refreshToken
refreshCredentials { refreshUserCredentials(refreshToken) }
}

// if nothing is stored, we will try and refresh using a client secret
authConfig.clientSecret != null -> {
refreshCredentials { getClientCredentials(authConfig.clientSecret) }
}
): AuthResult<Credentials> = when {
storedTokens?.credentials?.isExpired(timeProvider) == false &&
apiErrorSubStatus.shouldRefreshToken().not() -> {
success(storedTokens.credentials)
}
// if a refreshToken is available, we'll use it
storedTokens?.refreshToken != null -> {
val refreshToken = storedTokens.refreshToken
refreshCredentials { refreshUserCredentials(refreshToken) }
}

// as a last resort we return a token-less Credentials, we're logged out
else -> logout()
// if nothing is stored, we will try and refresh using a client secret
authConfig.clientSecret != null -> {
refreshCredentials { getClientCredentials(authConfig.clientSecret) }
}

// as a last resort we return a token-less Credentials, we're logged out
else -> logout()
}

private suspend fun upgradeTokens(storedTokens: Tokens): AuthResult<Tokens> {
Expand Down Expand Up @@ -212,16 +212,17 @@ internal class TokenRepository(
}
}

private suspend fun getClientCredentials(clientSecret: String): AuthResult<RefreshResponse> {
return retryWithPolicy(defaultBackoffPolicy) {
private suspend fun getClientCredentials(clientSecret: String): AuthResult<RefreshResponse> =
retryWithPolicy(
defaultBackoffPolicy,
) {
tokenService.getTokenFromClientSecret(
authConfig.clientId,
clientSecret,
GRANT_TYPE_CLIENT_CREDENTIALS,
authConfig.scopes.toScopesString(),
)
}
}

companion object {

Expand Down
Loading

0 comments on commit 08ea6b9

Please sign in to comment.