/** * Generates the JWT for the request. * * JWT is signed with the api secret, and contains the qid, apiKey and expiry time in the claims section. * * @return String JWT */ private String createJWT () { // The JWT signature algorithm we will be using to sign the token (HMAC using SHA-256) SignatureAlgorithm sa = SignatureAlgorithm.HS256; // Add our claims Map <String, Object> cm = new HashMap<String, Object>(); cm.put("key", this.apiKey); cm.put("exp", System.currentTimeMillis() + 60000); // expiry date 1 minute from now // set the claims String jwt = Jwts.builder().setHeaderParam("typ", "JWT") .setClaims(cm).signWith(sa, TextCodec.BASE64.encode(this.apiSecret)).compact(); //System.out.println("JWT TOKEN: " + jwt); return jwt; }
@SuppressWarnings("serial") protected String getNewCertificateRequest(final KeyPair userKey, final String nonce, final PKCS10CertificationRequest csr) throws IOException { return Jwts.builder() .setHeaderParam(NONCE_KEY, nonce) .setHeaderParam(JwsHeader.JSON_WEB_KEY, JWKUtils.getWebKey(userKey.getPublic())) .setClaims(new TreeMap<String, Object>(){{ put(RESOURCE_KEY, RESOURCE_NEW_CERT); put(CSR_KEY, TextCodec.BASE64URL.encode(csr.getEncoded())); }}) .signWith(getJWSSignatureAlgorithm(), userKey.getPrivate()) .compact(); }
public static TreeMap<String, Object> getWebKey(PublicKey publicKey) { TreeMap<String, Object> key = new TreeMap<>(); if (publicKey instanceof RSAPublicKey){ key.put("kty","RSA"); key.put("e", TextCodec.BASE64URL.encode(toIntegerBytes(((RSAPublicKey) publicKey).getPublicExponent()))); key.put("n", TextCodec.BASE64URL.encode(toIntegerBytes(((RSAPublicKey) publicKey).getModulus()))); return key; }else{ throw new IllegalArgumentException(); } }
public static String getWebKeyThumbprintSHA256(PublicKey publicKey){ try { TreeMap<String, Object> webKey = JWKUtils.getWebKey(publicKey); String webKeyJson = new ObjectMapper().writeValueAsString(webKey); return TextCodec.BASE64URL.encode(SHA256(webKeyJson)); } catch (JsonProcessingException e) { throw new RuntimeException(e); } }
private PublicCredentials createCredentialEntity(String kuid, PublicKey key) { return new PublicCredentials(kuid, TextCodec.BASE64.encode(key.getEncoded())); }
@Override public boolean isValid(String jwtWithoutSignature, String base64UrlEncodedSignature) { byte[] data = jwtWithoutSignature.getBytes(US_ASCII); byte[] signature = TextCodec.BASE64URL.decode(base64UrlEncodedSignature); return this.signatureValidator.isValid(data, signature); }
@Override public String sign(String jwtWithoutSignature) { byte[] bytesToSign = jwtWithoutSignature.getBytes(US_ASCII); byte[] signature = signer.sign(bytesToSign); return TextCodec.BASE64URL.encode(signature); }