|
|
@ -56,25 +56,21 @@ class OAuth(private val authorizationServiceFactory: AuthorizationServiceFactory |
|
|
|
|
|
|
|
|
|
|
|
fun isAuthorized(context: Context): Boolean { |
|
|
|
fun isAuthorized(context: Context): Boolean { |
|
|
|
val state = tryState() |
|
|
|
val state = tryState() |
|
|
|
return if (state != null) { |
|
|
|
return (if (state != null) { |
|
|
|
state.isAuthorized || refreshAccessToken(context) |
|
|
|
state.validAuthorization() || refreshAccessToken(state, context) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
false |
|
|
|
false |
|
|
|
}.also { |
|
|
|
}).also { |
|
|
|
it.logInfo("isAuthorized()") |
|
|
|
it.logInfo("isAuthorized()") |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun AuthState.validAuthorization() = this.isAuthorized && !this.needsTokenRefresh |
|
|
|
|
|
|
|
|
|
|
|
fun tryRefreshAccessToken(context: Context): Boolean { |
|
|
|
fun tryRefreshAccessToken(context: Context): Boolean { |
|
|
|
tryState()?.let { state -> |
|
|
|
tryState()?.let { state -> |
|
|
|
return if (state.needsTokenRefresh && state.refreshToken != null) { |
|
|
|
return if (state.needsTokenRefresh && state.refreshToken != null) { |
|
|
|
Log.i( |
|
|
|
refreshAccessToken(state, context) |
|
|
|
"OAuth", |
|
|
|
|
|
|
|
"needsTokenRefresh()=${state.needsTokenRefresh}, refreshToken=${ |
|
|
|
|
|
|
|
state.refreshToken!!.subSequence(0, 5) |
|
|
|
|
|
|
|
}..." |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
refreshAccessToken(context) |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
state.isAuthorized |
|
|
|
state.isAuthorized |
|
|
|
}.also { it.logInfo("tryRefreshAccessToken()") } |
|
|
|
}.also { it.logInfo("tryRefreshAccessToken()") } |
|
|
@ -83,9 +79,12 @@ class OAuth(private val authorizationServiceFactory: AuthorizationServiceFactory |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun refreshAccessToken(context: Context): Boolean { |
|
|
|
fun refreshAccessToken(context: Context): Boolean { |
|
|
|
|
|
|
|
return tryState()?.let { refreshAccessToken(it, context) } ?: false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun refreshAccessToken(state: AuthState, context: Context): Boolean { |
|
|
|
Log.i("OAuth", "refreshAccessToken()") |
|
|
|
Log.i("OAuth", "refreshAccessToken()") |
|
|
|
val state = tryState() |
|
|
|
return if (state.refreshToken != null) { |
|
|
|
return if (state != null && state.refreshToken != null) { |
|
|
|
|
|
|
|
val refreshRequest = state.createTokenRefreshRequest() |
|
|
|
val refreshRequest = state.createTokenRefreshRequest() |
|
|
|
val auth = ClientSecretPost(state.clientSecret) |
|
|
|
val auth = ClientSecretPost(state.clientSecret) |
|
|
|
runBlocking { |
|
|
|
runBlocking { |
|
|
|