@Override public String convertToDatabaseColumn(JWT attribute) { if (attribute != null) { return attribute.serialize(); } else { return null; } }
private String getIssuer(String accessToken) { try { JWT jwt = JWTParser.parse(accessToken); String issuer = jwt.getJWTClaimsSet().getIssuer(); return issuer; } catch (ParseException e) { throw new IllegalArgumentException("Unable to parse JWT", e); } }
@Override public JWT convertToEntityAttribute(String dbData) { if (dbData != null) { try { JWT jwt = JWTParser.parse(dbData); return jwt; } catch (ParseException e) { logger.error("Unable to parse JWT", e); return null; } } else { return null; } }
boolean valid = false; final JWTClaimsSet claimsSet = jwtToken.getJWTClaimsSet(); if (claimsSet == null) { logger.error("Claims set is missing from Knox JWT."); final Date expiration = claimsSet.getExpirationTime();
/** * Pull the assertion out of the request and send it up to the auth manager for processing. */ @Override public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException { // check for appropriate parameters String assertionType = request.getParameter("client_assertion_type"); String assertion = request.getParameter("client_assertion"); try { JWT jwt = JWTParser.parse(assertion); String clientId = jwt.getJWTClaimsSet().getSubject(); Authentication authRequest = new JWTBearerAssertionAuthenticationToken(jwt); return this.getAuthenticationManager().authenticate(authRequest); } catch (ParseException e) { throw new BadCredentialsException("Invalid JWT credential: " + assertion); } }
/** * Create an unauthenticated token with the given subject and jwt * @param subject * @param jwt */ public JWTBearerAssertionAuthenticationToken(JWT jwt) { super(null); try { // save the subject of the JWT in case the credentials get erased later this.subject = jwt.getJWTClaimsSet().getSubject(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.jwt = jwt; setAuthenticated(false); }
@Override public OAuth2Request createOAuth2Request(ClientDetails client, TokenRequest tokenRequest, JWT assertion) { try { JWTClaimsSet claims = assertion.getJWTClaimsSet(); Set<String> scope = OAuth2Utils.parseParameterList(claims.getStringClaim("scope")); Set<String> resources = Sets.newHashSet(claims.getAudience()); return new OAuth2Request(tokenRequest.getRequestParameters(), client.getClientId(), client.getAuthorities(), true, scope, resources, null, null, null); } catch (ParseException e) { return null; } }
@Override public JWTClaimsSet process(SignedJWT signedJWT, SecurityContext context) throws BadJOSEException, JOSEException { try { return signedJWT.getJWTClaimsSet(); } catch (ParseException e) { // Payload not a JSON object throw new BadJWTException(e.getMessage(), e); } } }
private Jwt createJwt(JWT parsedJwt, JWTClaimsSet jwtClaimsSet) { Map<String, Object> headers = new LinkedHashMap<>(parsedJwt.getHeader().toJSONObject()); Map<String, Object> claims = this.claimSetConverter.convert(jwtClaimsSet.getClaims()); Instant expiresAt = (Instant) claims.get(JwtClaimNames.EXP); Instant issuedAt = (Instant) claims.get(JwtClaimNames.IAT); return new Jwt(parsedJwt.getParsedString(), issuedAt, expiresAt, headers, claims); }
@Override public Mono<Jwt> decode(String token) throws JwtException { JWT jwt = parse(token); if (jwt instanceof SignedJWT) { return this.decode((SignedJWT) jwt); } throw new JwtException("Unsupported algorithm of " + jwt.getHeader().getAlgorithm()); }
/** * Create an authenticated token with the given clientID, jwt, and authorities set * @param subject * @param jwt * @param authorities */ public JWTBearerAssertionAuthenticationToken(JWT jwt, Collection<? extends GrantedAuthority> authorities) { super(authorities); try { // save the subject of the JWT in case the credentials get erased later this.subject = jwt.getJWTClaimsSet().getSubject(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.jwt = jwt; setAuthenticated(true); }
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException, ParseException { in.defaultReadObject(); Object o = in.readObject(); if (o != null) { idToken = JWTParser.parse((String)o); } }
/** * Get the JWT-encoded value of this token */ @Override @Transient public String getValue() { return jwt.serialize(); }
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException, ParseException { in.defaultReadObject(); Object o = in.readObject(); if (o != null) { idToken = JWTParser.parse((String)o); } }
/** * Get the string-encoded value of this access token. */ @Override @Transient public String getValue() { return jwtValue.serialize(); }
private JWT parse(String token) { try { return JWTParser.parse(token); } catch (Exception ex) { throw new JwtException("An error occurred while attempting to decode the Jwt: " + ex.getMessage(), ex); } }
private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject(); if (idToken == null) { out.writeObject(null); } else { out.writeObject(idToken.serialize()); } } private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException, ParseException {
private JWT parse(String token) { try { return JWTParser.parse(token); } catch (Exception ex) { throw new JwtException(String.format(DECODING_ERROR_MESSAGE_TEMPLATE, ex.getMessage()), ex); } }
private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject(); if (idToken == null) { out.writeObject(null); } else { out.writeObject(idToken.serialize()); } } private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException, ParseException {
/** * Add the ID Token to the additionalInformation map for a token response. * @param idToken */ @Transient public void setIdToken(JWT idToken) { if (idToken != null) { additionalInformation.put(ID_TOKEN_FIELD_NAME, idToken.serialize()); } } }