Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. class AuthorizationModule(private val context: Context){
  2.  
  3. val tag = "AuthorizationModule"
  4. fun startAuthorization(activityForResult: Activity, code: Int) {
  5.  
  6. val authService = createAuthorizationService()
  7. val authRequest: AuthorizationRequest? = createAuthRequest()
  8.  
  9. if (authRequest!=null)
  10. activityForResult.startActivityForResult( authService.getAuthorizationRequestIntent(authRequest),code)
  11. }
  12.  
  13. fun getToken(){
  14. createInitAuthState()
  15. .performActionWithFreshTokens(createAuthorizationService()) { accessToken, idToken, ex ->
  16. Log.d(tag,"accessToken = $accessToken, idToken = $idToken, ex = $ex")
  17. }
  18. val authState = createInitAuthState()
  19. Log.d(tag, "is auth = ${authState.isAuthorized}")
  20. Log.d(tag, "is accessToken = ${authState.accessToken}")
  21. }
  22.  
  23. fun getTokenFromServer(intent: Intent) {
  24.  
  25. val response = AuthorizationResponse.fromIntent(intent)
  26. val ex = AuthorizationException.fromIntent(intent)
  27. Log.d(tag, "getTokenFromServer response = $response, ex = $ex")
  28. }
  29. private fun createAuthRequest(): AuthorizationRequest? {
  30. val configuration = createInitAuthState().authorizationServiceConfiguration
  31.  
  32. return if (configuration!=null)
  33. AuthorizationRequest.Builder(
  34. configuration,
  35. CLIENT_ID,
  36. CODE,
  37. Uri.parse(REDIRECT_URI))
  38. .setScope(AUTHORIZATION_SCOPE)
  39. .build()
  40. else null
  41. }
  42. private fun createAuthorizationService(): AuthorizationService {
  43. val builder = Builder()
  44. .setBrowserMatcher(AnyBrowserMatcher.INSTANCE)
  45. .setConnectionBuilder(DefaultConnectionBuilder.INSTANCE)
  46.  
  47. return AuthorizationService(context, builder.build())
  48. }
  49.  
  50.  
  51. private fun createInitAuthState() = AuthState(
  52. AuthorizationServiceConfiguration(
  53. Uri.parse(AUTH_ENDPOINT_URI),
  54. Uri.parse(TOKEN_ENDPOINT_URI)))
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement