public Authentication getAuthentication(String token) { Claims claims = Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token).getBody(); Collection<? extends GrantedAuthority> authorities = Arrays.stream(claims.get(AUTHORITIES_KEY).toString().split(",")) .map(SimpleGrantedAuthority::new) .collect(Collectors.toList()); User principal = new User(claims.getSubject(), "", authorities); return new UsernamePasswordAuthenticationToken(principal, "", authorities); }
public String getAudienceFromToken(String token) { String audience; try { final Claims claims = this.getClaimsFromToken(token); audience = (String) claims.get("audience"); } catch (Exception e) { audience = null; } return audience; }
public Date getExpirationDateFromToken(String token) { Date expiration; try { final Claims claims = this.getClaimsFromToken(token); expiration = claims.getExpiration(); } catch (Exception e) { expiration = null; } return expiration; }
public Date getCreatedDateFromToken(String token) { Date created; try { final Claims claims = this.getClaimsFromToken(token); created = new Date((Long) claims.get("created")); } catch (Exception e) { created = null; } return created; }
public String getOriginFromToken(String token) { String origin; try { final Claims claims = this.getClaimsFromToken(token); origin = (String) claims.get("origin"); } catch (Exception e) { origin = null; } return origin; }
public String getAuthoritiesFromToken(String token) { String authorities; try { final Claims claims = this.getClaimsFromToken(token); authorities = (String) claims.get("authorities"); } catch (Exception e) { authorities = null; } return authorities; }
public String getUsernameFromToken(String token) { String username; try { final Claims claims = this.getClaimsFromToken(token); username = claims.getSubject(); } catch (Exception e) { username = null; } return username; }
public String refreshToken(String token) { String refreshedToken; try { final Claims claims = this.getClaimsFromToken(token); claims.put("created", this.generateCurrentDate()); refreshedToken = this.generateToken(claims); } catch (Exception e) { refreshedToken = null; } return refreshedToken; }
public com.dsmc.data.tables.pojos.AdminUser getAdminUserFromToken(String token) { try { Jwt<Header, Claims> jwt = Jwts.parser().setSigningKeyResolver(signingKeyResolver).parseClaimsJwt(token); Claims claims = jwt.getBody(); Integer claimUserId = claims.get("userId", Integer.TYPE); Integer claimCompanyId = claims.get("companyId", Integer.TYPE); String claimUsername = claims.get("username", String.class); return new com.dsmc.data.tables.pojos.AdminUser( claimUserId, claimCompanyId, claimUsername, null, null, null, null); } catch (Exception e) { LOGGER.error("Token verification failed.", e); } return null; }
public void parseJWT(String jwt) { try { Claims claims = Jwts.parser() .setSigningKey( DatatypeConverter.parseBase64Binary(APPKEY_SECRET)) // apiKey.getSecret() .parseClaimsJws(jwt) .getBody(); System.out.println("ID: " + claims.getId()); System.out.println("Subject: " + claims.getSubject()); System.out.println("Issuer: " + claims.getIssuer()); System.out.println("Expiration: " + claims.getExpiration()); } catch (SignatureException e) { // throw new ServiceException("Token has been tampered", ErrorCode.TOKEN_TAMPERED); throw buildException(Response.Status.BAD_REQUEST, "403", "Token has been tampered"); } catch (ExpiredJwtException e) { // throw new ServiceException("Token expired", ErrorCode.TOKEN_EXPIRED); throw buildException(Response.Status.REQUEST_TIMEOUT, "500", "Token expired"); } }
public ServiceResult execute(final JsonNode jsonRequest) { final Principal principal = (Principal) Http.Context.current().args.get(Principal.class.getName()); final Project project = Json.fromJson(jsonRequest, Project.class); final Claims claims = principal.getClaims(); final ProjectMember ownerMember = new ProjectMember(); ownerMember.setUserId(new Integer((String) claims.get("id"))); ownerMember.setForename((String) claims.get("forename")); ownerMember.setSurname((String) claims.get("surname")); ownerMember.setEmail((String) claims.get("email")); ownerMember.setRole(ProjectMember.Role.OWNER); project.setOwner(ownerMember); final Project savedProject = projectRepository.set(project); // TODO email project members return new ServiceResult(Json.toJson(savedProject)); }