-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Conflicts: build.gradle
- Loading branch information
Showing
19 changed files
with
578 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,91 @@ | ||
[JSON Web Tokens](https://tools.ietf.org/html/rfc7519) | ||
--------------------------------------------------------------------------------------------------------------------- | ||
|
||
[![Build Status](https://travis-ci.org/RootServices/jwt.svg?branch=development)](https://travis-ci.org/RootServices/jwt) | ||
|
||
|
||
Documentation | ||
------------ | ||
Documentation is written in [github pages](http://rootservices.github.io/jwt/). Which is located in the branch, [gh-pages](https://github.com/RootServices/jwt/tree/gh-pages) | ||
More documentation is available [here](http://rootservices.github.io/jwt/). | ||
|
||
Quick Start | ||
----------- | ||
This is a Java implementation of JWT, JWS, and JWE. | ||
|
||
## Unsecured JWT | ||
```java | ||
UnsecureCompactBuilder compactBuilder = new UnsecureCompactBuilder(); | ||
|
||
Claim claim = new Claim(); | ||
claim.setUriIsRoot(true); | ||
|
||
ByteArrayOutputStream encodedJwt = compactBuilder.claims(claim).build(); | ||
``` | ||
|
||
## JWS Compact Serialization | ||
|
||
### Asymmetric key | ||
```java | ||
SecureCompactBuilder compactBuilder = new SecureCompactBuilder(); | ||
|
||
RSAKeyPair key = Factory.makeRSAKeyPair(); | ||
key.setKeyId(Optional.of("test-key-id")); | ||
|
||
Claim claim = new Claim(); | ||
claim.setUriIsRoot(true); | ||
|
||
ByteArrayOutputStream actual = subject.alg(Algorithm.RS256) | ||
.key(key) | ||
.claims(claim) | ||
.build(); | ||
``` | ||
|
||
### Symmetric key | ||
```java | ||
SecureCompactBuilder compactBuilder = new SecureCompactBuilder(); | ||
|
||
SymmetricKey key = Factory.makeSymmetricKey(); | ||
key.setKeyId(Optional.of("test-key-id")); | ||
|
||
Claim claim = new Claim(); | ||
claim.setUriIsRoot(true); | ||
|
||
ByteArrayOutputStream actual = compactBuilder.alg(Algorithm.HS256) | ||
.key(key) | ||
.claims(claim) | ||
.build(); | ||
``` | ||
|
||
## JWE Compact Serialization | ||
|
||
### Asymmetric key | ||
```java | ||
EncryptedCompactBuilder compactBuilder = new EncryptedCompactBuilder(); | ||
|
||
byte[] payload = "Help me, Obi-Wan Kenobi. You're my only hope.".getBytes(); | ||
|
||
RSAPublicKey publicKey = Factory.makeRSAPublicKeyForJWE(); | ||
publicKey.setKeyId(Optional.of(UUID.randomUUID().toString())); | ||
|
||
ByteArrayOutputStream actual = compactBuilder.encAlg(EncryptionAlgorithm.AES_GCM_256) | ||
.alg(Algorithm.RSAES_OAEP) | ||
.payload(payload) | ||
.rsa(publicKey) | ||
.build(); | ||
``` | ||
|
||
### Symmetric key | ||
```java | ||
EncryptedCompactBuilder compactBuilder = new EncryptedCompactBuilder(); | ||
|
||
SymmetricKey key = Factory.makeSymmetricKeyForJWE(); | ||
|
||
byte[] payload = "Help me, Obi-Wan Kenobi. You're my only hope.".getBytes(); | ||
|
||
Travis Results | ||
------------------------------------- | ||
- development branch [![Build Status](https://travis-ci.org/RootServices/jwt.svg?branch=development)](https://travis-ci.org/RootServices/jwt) | ||
ByteArrayOutputStream actual = compactBuilder.encAlg(EncryptionAlgorithm.AES_GCM_256) | ||
.alg(Algorithm.DIRECT) | ||
.encAlg(EncryptionAlgorithm.AES_GCM_256) | ||
.payload(payload) | ||
.cek(key) | ||
.build(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
src/main/java/org/rootservices/jwt/builder/compact/EncryptedCompactBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package org.rootservices.jwt.builder.compact; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.rootservices.jwt.builder.exception.CompactException; | ||
import org.rootservices.jwt.config.JwtAppFactory; | ||
import org.rootservices.jwt.entity.jwe.EncryptionAlgorithm; | ||
import org.rootservices.jwt.entity.jwk.RSAPublicKey; | ||
import org.rootservices.jwt.entity.jwk.SymmetricKey; | ||
import org.rootservices.jwt.entity.jwt.header.Algorithm; | ||
import org.rootservices.jwt.entity.jwt.header.Header; | ||
import org.rootservices.jwt.jwe.entity.JWE; | ||
import org.rootservices.jwt.jwe.factory.exception.CipherException; | ||
import org.rootservices.jwt.jwe.serialization.JweSerializer; | ||
import org.rootservices.jwt.jws.signer.factory.rsa.exception.PublicKeyException; | ||
import org.rootservices.jwt.serialization.exception.EncryptException; | ||
import org.rootservices.jwt.serialization.exception.JsonToJwtException; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.util.Base64; | ||
import java.util.Optional; | ||
|
||
public class EncryptedCompactBuilder { | ||
private static final Logger LOGGER = LogManager.getLogger(EncryptedCompactBuilder.class); | ||
public static final String UNABLE_TO_BUILD_COMPACT_JWE = "Unable to build compact jwe"; | ||
private static JwtAppFactory jwtAppFactory = new JwtAppFactory(); | ||
|
||
private byte[] payload; | ||
private SymmetricKey cek; | ||
private Algorithm alg; | ||
private EncryptionAlgorithm encAlg; | ||
|
||
private RSAPublicKey publicKey; | ||
|
||
public EncryptedCompactBuilder payload(byte[] payload) { | ||
this.payload = payload; | ||
return this; | ||
} | ||
|
||
public EncryptedCompactBuilder rsa(RSAPublicKey publicKey) { | ||
this.publicKey = publicKey; | ||
return this; | ||
} | ||
|
||
public EncryptedCompactBuilder cek(SymmetricKey cek) { | ||
this.cek = cek; | ||
return this; | ||
} | ||
|
||
public EncryptedCompactBuilder alg(Algorithm alg) { | ||
this.alg = alg; | ||
return this; | ||
} | ||
|
||
public EncryptedCompactBuilder encAlg(EncryptionAlgorithm encAlg) { | ||
this.encAlg = encAlg; | ||
return this; | ||
} | ||
|
||
public ByteArrayOutputStream build() throws CompactException { | ||
JWE jwe = jwe(); | ||
JweSerializer jweSerializer = jweSerializer(); | ||
|
||
try { | ||
return jweSerializer.JWEToCompact(jwe); | ||
} catch (JsonToJwtException | CipherException | EncryptException e) { | ||
LOGGER.error(e.getMessage(), e); | ||
throw new CompactException(UNABLE_TO_BUILD_COMPACT_JWE, e); | ||
} | ||
} | ||
|
||
// a few factory methods to help the build method. | ||
|
||
protected JweSerializer jweSerializer() throws CompactException { | ||
JweSerializer jweSerializer; | ||
if (publicKey != null) { | ||
jweSerializer = jweRsaSerializer(); | ||
} else { | ||
jweSerializer = jwtAppFactory.jweDirectSerializer(); | ||
} | ||
return jweSerializer; | ||
} | ||
|
||
protected JweSerializer jweRsaSerializer() throws CompactException { | ||
try { | ||
return jwtAppFactory.jweRsaSerializer(publicKey); | ||
} catch (PublicKeyException | CipherException e) { | ||
LOGGER.error(e.getMessage(), e); | ||
throw new CompactException(UNABLE_TO_BUILD_COMPACT_JWE, e); | ||
} | ||
} | ||
|
||
protected JWE jwe() { | ||
if (publicKey != null) { | ||
return jweForRsa(); | ||
} else { | ||
return jweForDirect(); | ||
} | ||
} | ||
|
||
protected JWE jweForDirect() { | ||
Base64.Decoder decoder = jwtAppFactory.urlDecoder(); | ||
|
||
JWE jwe = new JWE(); | ||
Header header = new Header(); | ||
|
||
header.setEncryptionAlgorithm(Optional.of(this.encAlg)); | ||
header.setAlgorithm(this.alg); | ||
header.setKeyId(cek.getKeyId()); | ||
|
||
jwe.setHeader(header); | ||
jwe.setPayload(payload); | ||
jwe.setCek(decoder.decode(cek.getKey())); | ||
|
||
return jwe; | ||
} | ||
|
||
protected JWE jweForRsa() { | ||
JWE jwe = new JWE(); | ||
Header header = new Header(); | ||
|
||
header.setKeyId(publicKey.getKeyId()); | ||
header.setEncryptionAlgorithm(Optional.of(this.encAlg)); | ||
header.setAlgorithm(this.alg); | ||
|
||
jwe.setHeader(header); | ||
jwe.setPayload(payload); | ||
|
||
return jwe; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/org/rootservices/jwt/builder/compact/SecureCompactBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.rootservices.jwt.builder.compact; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.rootservices.jwt.builder.exception.CompactException; | ||
import org.rootservices.jwt.config.JwtAppFactory; | ||
import org.rootservices.jwt.entity.jwk.Key; | ||
import org.rootservices.jwt.entity.jwt.Claims; | ||
import org.rootservices.jwt.entity.jwt.header.Algorithm; | ||
import org.rootservices.jwt.exception.SignatureException; | ||
import org.rootservices.jwt.jws.serialization.SecureJwtSerializer; | ||
import org.rootservices.jwt.serialization.exception.JwtToJsonException; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
|
||
public class SecureCompactBuilder { | ||
private static final Logger LOGGER = LogManager.getLogger(SecureCompactBuilder.class); | ||
public static final String UNABLE_TO_BUILD_COMPACT_JWT = "Unable to build compact jwt"; | ||
private static JwtAppFactory jwtAppFactory = new JwtAppFactory(); | ||
|
||
private Claims claims; | ||
private Key key; | ||
private Algorithm alg; | ||
|
||
public SecureCompactBuilder claims(Claims claims) { | ||
this.claims = claims; | ||
return this; | ||
} | ||
|
||
public SecureCompactBuilder key(Key key) { | ||
this.key = key; | ||
return this; | ||
} | ||
|
||
public SecureCompactBuilder alg(Algorithm alg) { | ||
this.alg = alg; | ||
return this; | ||
} | ||
|
||
|
||
public ByteArrayOutputStream build() throws CompactException { | ||
SecureJwtSerializer secureJwtSerializer; | ||
try { | ||
secureJwtSerializer = jwtAppFactory.secureJwtSerializer(alg, key); | ||
} catch (SignatureException e) { | ||
LOGGER.error(e.getMessage(), e); | ||
throw new CompactException(UNABLE_TO_BUILD_COMPACT_JWT, e); | ||
} | ||
|
||
try { | ||
return secureJwtSerializer.compactJwt(claims); | ||
} catch (JwtToJsonException e) { | ||
LOGGER.error(e.getMessage(), e); | ||
throw new CompactException(UNABLE_TO_BUILD_COMPACT_JWT, e); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/org/rootservices/jwt/builder/compact/UnsecureCompactBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.rootservices.jwt.builder.compact; | ||
|
||
import org.rootservices.jwt.config.JwtAppFactory; | ||
import org.rootservices.jwt.entity.jwt.Claims; | ||
import org.rootservices.jwt.serialization.UnSecureJwtSerializer; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
|
||
public class UnsecureCompactBuilder { | ||
private static JwtAppFactory jwtAppFactory = new JwtAppFactory(); | ||
private Claims claims; | ||
|
||
public UnsecureCompactBuilder claims(Claims claims) { | ||
this.claims = claims; | ||
return this; | ||
} | ||
|
||
public ByteArrayOutputStream build() { | ||
UnSecureJwtSerializer unSecureJwtSerializer = jwtAppFactory.unSecureJwtSerializer(); | ||
return unSecureJwtSerializer.compactJwt(claims); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/org/rootservices/jwt/builder/exception/CompactException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.rootservices.jwt.builder.exception; | ||
|
||
public class CompactException extends Exception { | ||
public CompactException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.