Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AuthorizationModule(private val context: Context){
- val tag = "AuthorizationModule"
- fun startAuthorization(activityForResult: Activity, code: Int) {
- val authService = createAuthorizationService()
- val authRequest: AuthorizationRequest? = createAuthRequest()
- if (authRequest!=null)
- activityForResult.startActivityForResult( authService.getAuthorizationRequestIntent(authRequest),code)
- }
- fun getToken(){
- createInitAuthState()
- .performActionWithFreshTokens(createAuthorizationService()) { accessToken, idToken, ex ->
- Log.d(tag,"accessToken = $accessToken, idToken = $idToken, ex = $ex")
- }
- val authState = createInitAuthState()
- Log.d(tag, "is auth = ${authState.isAuthorized}")
- Log.d(tag, "is accessToken = ${authState.accessToken}")
- }
- fun getTokenFromServer(intent: Intent) {
- val response = AuthorizationResponse.fromIntent(intent)
- val ex = AuthorizationException.fromIntent(intent)
- Log.d(tag, "getTokenFromServer response = $response, ex = $ex")
- }
- private fun createAuthRequest(): AuthorizationRequest? {
- val configuration = createInitAuthState().authorizationServiceConfiguration
- return if (configuration!=null)
- AuthorizationRequest.Builder(
- configuration,
- CLIENT_ID,
- CODE,
- Uri.parse(REDIRECT_URI))
- .setScope(AUTHORIZATION_SCOPE)
- .build()
- else null
- }
- private fun createAuthorizationService(): AuthorizationService {
- val builder = Builder()
- .setBrowserMatcher(AnyBrowserMatcher.INSTANCE)
- .setConnectionBuilder(DefaultConnectionBuilder.INSTANCE)
- return AuthorizationService(context, builder.build())
- }
- private fun createInitAuthState() = AuthState(
- AuthorizationServiceConfiguration(
- Uri.parse(AUTH_ENDPOINT_URI),
- Uri.parse(TOKEN_ENDPOINT_URI)))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement