Skip to content

Commit

Permalink
Refactored code.| #716
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Aug 12, 2020
1 parent 9df7464 commit eaddd70
Showing 1 changed file with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ class OAuth2AuthCredentialsViewModel(application: Application) : BaseAndroidView
return@launch
}

val claims = validateTokenAndGetClaims(response.data?.idToken ?: "")
val claims = validateTokenAndGetClaims(response.data?.idToken ?: "", authRequest
.clientId, jwks = JWKS_MICROSOFT)
val email: String? = claims.getClaimValueAsString(CLAIM_EMAIL)?.toLowerCase(Locale.US)
val displayName: String? = claims.getClaimValueAsString(CLAIM_NAME)

Expand All @@ -85,20 +86,23 @@ class OAuth2AuthCredentialsViewModel(application: Application) : BaseAndroidView
}
}

private suspend fun validateTokenAndGetClaims(idToken: String): JwtClaims =
private suspend fun validateTokenAndGetClaims(idToken: String, clientId: String, jwks: String):
JwtClaims =
withContext(Dispatchers.IO) {
//https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration
val httpsJkws = HttpsJwks("https://login.microsoftonline.com/common/discovery/v2.0/keys")
val httpsJkws = HttpsJwks(jwks)
val httpsJwksKeyResolver = HttpsJwksVerificationKeyResolver(httpsJkws)
val jwtConsumer = JwtConsumerBuilder()
.setVerificationKeyResolver(httpsJwksKeyResolver)
.setExpectedAudience("3be51534-5f76-4970-9a34-40ef197aa018")
.setExpectedAudience(clientId)
.build()
return@withContext jwtConsumer.processToClaims(idToken)
}

companion object {
private const val CLAIM_EMAIL = "email"
private const val CLAIM_NAME = "name"

//https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration
private const val JWKS_MICROSOFT = "https://login.microsoftonline.com/common/discovery/v2.0/keys"
}
}

0 comments on commit eaddd70

Please sign in to comment.