Guest User

Untitled

a guest
Apr 14th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. class AWSIdentityManager(context: Context) : IdentityManager {
  2. /**
  3. * The stored "current user" object
  4. */
  5. private val mutableCurrentUser: MutableLiveData<User> = MutableLiveData()
  6.  
  7. /**
  8. * Reference to cognito user pools
  9. */
  10. private val userPool: CognitoUserPool
  11.  
  12. init {
  13. val awsConfig = AWSConfiguration(context)
  14. userPool = CognitoUserPool(context, awsConfig)
  15. mutableCurrentUser.value = null
  16. }
  17.  
  18. /**
  19. * Property for the current user record - null if the user is not signed in
  20. */
  21. override val currentUser: LiveData<User?> = mutableCurrentUser
  22.  
  23. /**
  24. * Sign in with a username / password
  25. */
  26. override fun signin(username: String, password: String) {
  27. val cognitoUser = userPool.currentUser
  28.  
  29. val authHandler = object : AuthenticationHandler {
  30. override fun onSuccess(userSession: CognitoUserSession?, newDevice: CognitoDevice?) {
  31. val internalUser = User(cognitoUser.userId, userSession.username)
  32. mutableCurrentUser.value = internalUser
  33. }
  34.  
  35. override fun onFailure(exception: Exception?) {
  36. // Do something with the failure here - this probably means setting an
  37. // error property and then setting state to FAILED, which is picked up
  38. // via LiveData<> observers
  39. }
  40.  
  41. override fun getAuthenticationDetails(continuation: AuthenticationContinuation?, userId: String?) {
  42. val authDetails = AuthenticationDetails(username, password, null)
  43. continuation.setAuthenticationDetails(authDetails)
  44. continuation.continueTask()
  45. }
  46.  
  47. override fun authenticationChallenge(continuation: ChallengeContinuation?) {
  48. // Custom challenge (e.g. TOTP) - handle the same way as MFA codes
  49. }
  50.  
  51. override fun getMFACode(continuation: MultiFactorAuthenticationContinuation?) {
  52. // If you need to deal with MFA, do it here - generally speaking, add a state
  53. // to the repository that is mutated according to the requirements. The activity
  54. // (via the view model and observers) puts up an MFA request and submits
  55. continuation?.continueTask()
  56. }
  57.  
  58. }
  59. cognitoUser.getSession(authHandler)
  60. }
  61.  
  62. /**
  63. * Sign out of the system
  64. */
  65. override fun signout() {
  66. val cognitoUser = userPool.currentUser
  67. cognitoUser.signOut()
  68. mutableCurrentUser.value = null
  69. }
  70. }
Add Comment
Please, Sign In to add comment