Created
April 7, 2018 00:11
-
-
Save adrianhall/b90bc379175e10519e588664fcb441f5 to your computer and use it in GitHub Desktop.
Simple implementation of the AWSIndentityManager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AWSIdentityManager(context: Context) : IdentityManager { | |
/** | |
* The stored "current user" object | |
*/ | |
private val mutableCurrentUser: MutableLiveData<User> = MutableLiveData() | |
/** | |
* Reference to cognito user pools | |
*/ | |
private val userPool: CognitoUserPool | |
init { | |
val awsConfig = AWSConfiguration(context) | |
userPool = CognitoUserPool(context, awsConfig) | |
mutableCurrentUser.value = null | |
} | |
/** | |
* Property for the current user record - null if the user is not signed in | |
*/ | |
override val currentUser: LiveData<User?> = mutableCurrentUser | |
/** | |
* Sign in with a username / password | |
*/ | |
override fun signin(username: String, password: String) { | |
val cognitoUser = userPool.currentUser | |
val authHandler = object : AuthenticationHandler { | |
override fun onSuccess(userSession: CognitoUserSession?, newDevice: CognitoDevice?) { | |
val internalUser = User(cognitoUser.userId, userSession.username) | |
mutableCurrentUser.value = internalUser | |
} | |
override fun onFailure(exception: Exception?) { | |
// Do something with the failure here - this probably means setting an | |
// error property and then setting state to FAILED, which is picked up | |
// via LiveData<> observers | |
} | |
override fun getAuthenticationDetails(continuation: AuthenticationContinuation?, userId: String?) { | |
val authDetails = AuthenticationDetails(username, password, null) | |
continuation.setAuthenticationDetails(authDetails) | |
continuation.continueTask() | |
} | |
override fun authenticationChallenge(continuation: ChallengeContinuation?) { | |
// Custom challenge (e.g. TOTP) - handle the same way as MFA codes | |
} | |
override fun getMFACode(continuation: MultiFactorAuthenticationContinuation?) { | |
// If you need to deal with MFA, do it here - generally speaking, add a state | |
// to the repository that is mutated according to the requirements. The activity | |
// (via the view model and observers) puts up an MFA request and submits | |
continuation?.continueTask() | |
} | |
} | |
cognitoUser.getSession(authHandler) | |
} | |
/** | |
* Sign out of the system | |
*/ | |
override fun signout() { | |
val cognitoUser = userPool.currentUser | |
cognitoUser.signOut() | |
mutableCurrentUser.value = null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment