Skip to content

Commit

Permalink
Improved JWT validation.| #716
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Aug 25, 2020
1 parent 23c909b commit 83c1126
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import net.openid.appauth.AuthorizationServiceConfiguration
import net.openid.appauth.AuthorizationServiceDiscovery
import org.jose4j.jwk.HttpsJwks
import org.jose4j.jwt.JwtClaims
import org.jose4j.jwt.consumer.InvalidJwtException
import org.jose4j.jwt.consumer.JwtConsumerBuilder
import org.jose4j.keys.resolvers.HttpsJwksVerificationKeyResolver
import org.json.JSONException
Expand Down Expand Up @@ -131,7 +132,11 @@ class OAuth2AuthCredentialsViewModel(application: Application) : BaseAndroidView

microsoftOAuth2TokenLiveData.postValue(Result.success(recommendAuthCredentials))
} catch (e: Exception) {
microsoftOAuth2TokenLiveData.postValue(Result.exception(e))
if (e is InvalidJwtException) {
microsoftOAuth2TokenLiveData.postValue(Result.exception(InvalidJwtException("JWT validation was failed!\n\n", e.errorDetails, e.jwtContext)))
} else {
microsoftOAuth2TokenLiveData.postValue(Result.exception(e))
}
}
}
}
Expand All @@ -144,6 +149,9 @@ class OAuth2AuthCredentialsViewModel(application: Application) : BaseAndroidView
val jwtConsumer = JwtConsumerBuilder()
.setVerificationKeyResolver(verificationKeyResolver)
.setExpectedAudience(clientId)
.setRequireIssuedAt()
.setRequireNotBefore()
.setRequireExpirationTime()
.build()
return@withContext jwtConsumer.processToClaims(idToken)
}
Expand Down

0 comments on commit 83c1126

Please sign in to comment.