Try to validate the client credentials by parsing and validating the JWT.
85 JWTBearerAssertionAuthenticationToken jwtAuth = (JWTBearerAssertionAuthenticationToken)authentication;
91 JWT jwt = jwtAuth.getJwt();
92 JWTClaimsSet jwtClaims = jwt.getJWTClaimsSet();
95 if (jwt instanceof SignedJWT) {
96 SignedJWT jws = (SignedJWT)jwt;
98 JWSAlgorithm alg = jws.getHeader().getAlgorithm();
100 if (client.getTokenEndpointAuthSigningAlg() != null &&
101 !client.getTokenEndpointAuthSigningAlg().equals(alg)) {
102 throw new InvalidClientException(
"Client's registered request object signing algorithm (" + client.getRequestObjectSigningAlg() +
") does not match request object's actual algorithm (" + alg.getName() +
")");
105 if (client.getTokenEndpointAuthMethod() == null ||
106 client.getTokenEndpointAuthMethod().equals(AuthMethod.NONE) ||
107 client.getTokenEndpointAuthMethod().equals(AuthMethod.SECRET_BASIC) ||
108 client.getTokenEndpointAuthMethod().equals(AuthMethod.SECRET_POST)) {
111 throw new AuthenticationServiceException(
"Client does not support this authentication method.");
113 }
else if ((client.getTokenEndpointAuthMethod().equals(AuthMethod.PRIVATE_KEY) &&
114 (alg.equals(JWSAlgorithm.RS256)
115 || alg.equals(JWSAlgorithm.RS384)
116 || alg.equals(JWSAlgorithm.RS512)
117 || alg.equals(JWSAlgorithm.ES256)
118 || alg.equals(JWSAlgorithm.ES384)
119 || alg.equals(JWSAlgorithm.ES512)
120 || alg.equals(JWSAlgorithm.PS256)
121 || alg.equals(JWSAlgorithm.PS384)
122 || alg.equals(JWSAlgorithm.PS512)))
123 || (client.getTokenEndpointAuthMethod().equals(AuthMethod.SECRET_JWT) &&
124 (alg.equals(JWSAlgorithm.HS256)
125 || alg.equals(JWSAlgorithm.HS384)
126 || alg.equals(JWSAlgorithm.HS512)))) {
129 if (
config.
isHeartMode() && !client.getTokenEndpointAuthMethod().equals(AuthMethod.PRIVATE_KEY)) {
130 throw new AuthenticationServiceException(
"[HEART mode] Invalid authentication method");
135 if (validator == null) {
136 throw new AuthenticationServiceException(
"Unable to create signature validator for client " + client +
" and algorithm " + alg);
139 if (!validator.validateSignature(jws)) {
140 throw new AuthenticationServiceException(
"Signature did not validate for presented JWT authentication.");
143 throw new AuthenticationServiceException(
"Unable to create signature validator for method " + client.getTokenEndpointAuthMethod() +
" and algorithm " + alg);
148 if (jwtClaims.getIssuer() == null) {
149 throw new AuthenticationServiceException(
"Assertion Token Issuer is null");
150 }
else if (!jwtClaims.getIssuer().equals(client.getClientId())){
151 throw new AuthenticationServiceException(
"Issuers do not match, expected " + client.getClientId() +
" got " + jwtClaims.getIssuer());
155 if (jwtClaims.getExpirationTime() == null) {
156 throw new AuthenticationServiceException(
"Assertion Token does not have required expiration claim");
160 if (now.after(jwtClaims.getExpirationTime())) {
161 throw new AuthenticationServiceException(
"Assertion Token is expired: " + jwtClaims.getExpirationTime());
166 if (jwtClaims.getNotBeforeTime() != null) {
168 if (now.before(jwtClaims.getNotBeforeTime())){
169 throw new AuthenticationServiceException(
"Assertion Token not valid untill: " + jwtClaims.getNotBeforeTime());
174 if (jwtClaims.getIssueTime() != null) {
177 if (now.before(jwtClaims.getIssueTime())) {
178 throw new AuthenticationServiceException(
"Assertion Token was issued in the future: " + jwtClaims.getIssueTime());
183 if (jwtClaims.getAudience() == null) {
184 throw new AuthenticationServiceException(
"Assertion token audience is null");
186 throw new AuthenticationServiceException(
"Audience does not match, expected " +
config.
getIssuer() +
" or " + (
config.
getIssuer() +
"token") +
" got " + jwtClaims.getAudience());
192 Set<GrantedAuthority> authorities =
new HashSet<>(client.getAuthorities());
195 return new JWTBearerAssertionAuthenticationToken(jwt, authorities);
197 }
catch (InvalidClientException e) {
198 throw new UsernameNotFoundException(
"Could not find client: " + jwtAuth.getName());
199 }
catch (ParseException e) {
201 logger.error(
"Failure during authentication, error was: ", e);
203 throw new AuthenticationServiceException(
"Invalid JWT format");
ConfigurationPropertiesBean config
Definition: JWTBearerAuthenticationProvider.java:77
static final Logger logger
Definition: JWTBearerAuthenticationProvider.java:60
JWTSigningAndValidationService getValidator(ClientDetailsEntity client, JWSAlgorithm alg)
Definition: ClientKeyCacheService.java:77
ClientKeyCacheService validators
Definition: JWTBearerAuthenticationProvider.java:66
static final GrantedAuthority ROLE_CLIENT
Definition: JWTBearerAuthenticationProvider.java:62
ClientDetailsEntityService clientService
Definition: JWTBearerAuthenticationProvider.java:73
ClientDetailsEntity loadClientByClientId(String clientId)
String getIssuer()
Definition: ConfigurationPropertiesBean.java:100
boolean isHeartMode()
Definition: ConfigurationPropertiesBean.java:250
int timeSkewAllowance
Definition: JWTBearerAuthenticationProvider.java:69