public static String createJwt(String secret, String issuer, String subject) { final long iat = System.currentTimeMillis() / 1000l; // issued at claim final long exp = iat + 60L; // expires claim. In this case the token expires in 60 seconds final JWTSigner signer = new JWTSigner(secret); final HashMap<String, Object> claims = new HashMap<String, Object>(); claims.put("iss", issuer); claims.put("exp", exp); claims.put("iat", iat); claims.put("sub", subject); return signer.sign(claims); } }
public String constructToken(long iat, String jti) { Map<String, Object> claims = new HashMap<>(); claims.put("iat", iat); claims.put("application_id", this.applicationId); claims.put("jti", jti); JWTSigner.Options options = new JWTSigner.Options() .setAlgorithm(Algorithm.RS256); return this.signer.sign(claims, options); }
public JWTAuthMethod(final String applicationId, final byte[] privateKey) throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException { this.applicationId = applicationId; byte[] decodedPrivateKey = privateKey; if (privateKey[0] == '-') { decodedPrivateKey = decodePrivateKey(privateKey); } PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decodedPrivateKey); KeyFactory kf = KeyFactory.getInstance("RSA"); PrivateKey key = kf.generatePrivate(spec); this.signer = new JWTSigner(key); }
final String jwt = signer.sign(claims, this.options); return DcosLoginPayload.create(loginEndpoint, uid, jwt); } catch (JWTAlgorithmException e) {
final PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(content); final KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC"); return new JWTSigner(keyFactory.generatePrivate(keySpec)); } catch (IOException e) { final String errorMessage = "Error encountered closing PEM reader:\n" + e.getMessage();
/** * get jwt String of object * * @param object the POJO object * @param maxAge the milliseconds of life time * @return the jwt token */ public static <T> String sign(T object, long maxAge) { try { final JWTSigner signer = new JWTSigner(SIGN); final Map<String, Object> claims = new HashMap<String, Object>(); ObjectMapper mapper = new ObjectMapper(); String jsonString = mapper.writeValueAsString(object); claims.put(PAYLOAD, jsonString); claims.put(EXP, System.currentTimeMillis() + maxAge); return signer.sign(claims); } catch (Exception e) { return null; } }
public static String sign(LoginUserInfo user) { try { final JWTSigner signer = new JWTSigner(SECRET); final Map<String, Object> claims = new HashMap<String, Object>(); ObjectMapper mapper = new ObjectMapper(); String jsonString = mapper.writeValueAsString(user); claims.put(PAYLOAD, jsonString); claims.put(USERID,user.getId()); return signer.sign(claims); } catch(Exception e) { return null; } }
long epoch_expire = epoch_now + jwtExpireLength; JWTSigner jwts = new JWTSigner(secret); Map<String, Object> payload = new HashMap<>(); payload.put("iss", jwtIssuer); payload.put("exp", epoch_expire); payload.put("apiKey", apiKey); jwt = jwts.sign(payload); } catch (Exception e) { LOG.error("createJwt() failed: " + ExceptionUtils.getRootCauseMessage(e));
private Map<String, Object> getTokenRegistrationParams(final UserEntity userEntity, final String portalUri) { // generate a JWT to store user's information and for security purpose final Map<String, Object> claims = new HashMap<>(); claims.put(Claims.ISSUER, environment.getProperty("jwt.issuer", DEFAULT_JWT_ISSUER)); claims.put(Claims.SUBJECT, userEntity.getId()); claims.put(Claims.EMAIL, userEntity.getEmail()); claims.put(Claims.FIRSTNAME, userEntity.getFirstname()); claims.put(Claims.LASTNAME, userEntity.getLastname()); final JWTSigner.Options options = new JWTSigner.Options(); options.setExpirySeconds(environment.getProperty("user.creation.token.expire-after", Integer.class, DEFAULT_JWT_EMAIL_REGISTRATION_EXPIRE_AFTER)); options.setIssuedAt(true); options.setJwtId(true); // send a confirm email with the token final String jwtSecret = environment.getProperty("jwt.secret"); if (jwtSecret == null || jwtSecret.isEmpty()) { throw new IllegalStateException("JWT secret is mandatory"); } final String token = new JWTSigner(jwtSecret).sign(claims, options); String portalUrl = environment.getProperty("portalURL"); if (portalUrl!= null && portalUrl.endsWith("/")) { portalUrl = portalUrl.substring(0, portalUrl.length() - 1); } String registrationUrl = portalUrl + portalUri + token; return new NotificationParamsBuilder() .user(userEntity) .token(token) .registrationUrl(registrationUrl) .build(); }
private Map<String, Object> getTokenRegistrationParams(final UserEntity userEntity, final String portalUri) { // generate a JWT to store user's information and for security purpose final Map<String, Object> claims = new HashMap<>(); claims.put(Claims.ISSUER, environment.getProperty("jwt.issuer", DEFAULT_JWT_ISSUER)); claims.put(Claims.SUBJECT, userEntity.getId()); claims.put(Claims.EMAIL, userEntity.getEmail()); claims.put(Claims.FIRSTNAME, userEntity.getFirstname()); claims.put(Claims.LASTNAME, userEntity.getLastname()); final JWTSigner.Options options = new JWTSigner.Options(); options.setExpirySeconds(environment.getProperty("user.creation.token.expire-after", Integer.class, DEFAULT_JWT_EMAIL_REGISTRATION_EXPIRE_AFTER)); options.setIssuedAt(true); options.setJwtId(true); // send a confirm email with the token final String jwtSecret = environment.getProperty("jwt.secret"); if (jwtSecret == null || jwtSecret.isEmpty()) { throw new IllegalStateException("JWT secret is mandatory"); } final String token = new JWTSigner(jwtSecret).sign(claims, options); String portalUrl = environment.getProperty("portalURL"); if (portalUrl!= null && portalUrl.endsWith("/")) { portalUrl = portalUrl.substring(0, portalUrl.length() - 1); } String registrationUrl = portalUrl + portalUri + token; return new NotificationParamsBuilder() .user(userEntity) .token(token) .registrationUrl(registrationUrl) .build(); }