Skip to content

Commit 111023b

Browse files
arcseldonhzalaz
authored andcommitted
RSA Support
1 parent 4086365 commit 111023b

25 files changed

+1256
-366
lines changed

README.md

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,73 @@
22

33
An implementation of [JSON Web Tokens](http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html) developed against `draft-ietf-oauth-json-web-token-08`.
44

5-
### Usage
6-
Note for Auth0 users:
7-
By default, Auth0's CLIENT_SECRET is base64-encoded.
8-
To work with JWTVerifier, it must be decoded first.
5+
## Installation
96

10-
```java
11-
public class Application {
12-
public static void main (String [] args) {
13-
try {
14-
Base64 decoder = new Base64(true);
15-
byte[] secret = decoder.decodeBase64(CLIENT_SECRET);
16-
Map<String,Object> decodedPayload =
17-
new JWTVerifier(secret, "audience").verify("my-token");
18-
19-
// Get custom fields from decoded Payload
20-
System.out.println(decodedPayload.get("name"));
21-
} catch (SignatureException signatureException) {
22-
System.err.println("Invalid signature!");
23-
} catch (IllegalStateException illegalStateException) {
24-
System.err.println("Invalid Token! " + illegalStateException);
25-
}
26-
}
27-
}
28-
```
29-
30-
#### Maven coordinates?
31-
32-
Yes, here you are:
7+
### Maven
338

349
```xml
3510
<dependency>
3611
<groupId>com.auth0</groupId>
3712
<artifactId>java-jwt</artifactId>
38-
<version>2.1.0</version>
13+
<version>2.2.1</version>
3914
</dependency>
4015
```
4116

42-
### Credits
17+
### Gradle
18+
19+
```gradle
20+
compile 'com.auth0.java-jwt:2.2.1'
21+
```
22+
23+
## Usage
24+
25+
### Sign JWT (HS256)
26+
27+
```java
28+
final String issuer = "https://mydomain.com/";
29+
final String secret = "{{a secret used for signing}}";
30+
31+
final long iat = System.currentTimeMillis() / 1000l; // issued at claim
32+
final long exp = iat + 60L; // expires claim. In this case the token expires in 60 seconds
33+
34+
final JWTSigner signer = new JWTSigner(secret);
35+
final HashMap<String, Object> claims = new HashMap<String, Object>();
36+
claims.put("iss", issuer);
37+
claims.put("exp", exp);
38+
claims.put("iat", iat);
39+
40+
final String jwt = signer.sign(claims);
41+
```
42+
43+
### Verify JWT (HS256)
44+
45+
```java
46+
final String secret = "{{secret used for signing}}";
47+
try {
48+
final JWTVerifier verifier = new JWTVerifier(secret);
49+
final Map<String,Object> claims= jwtVerifier.verify(jwt);
50+
} catch (JWTVerifyException e) {
51+
// Invalid Token
52+
}
53+
```
54+
55+
### Validate aud & iss claims
56+
57+
```java
58+
final String secret = "{{secret used for signing}}";
59+
try {
60+
final JWTVerifier verifier = new JWTVerifier(secret, "{{my-audience}}", "{{my-issuer}}");
61+
final Map<String,Object> claims= jwtVerifier.verify(jwt);
62+
} catch (JWTVerifyException e) {
63+
// Invalid Token
64+
}
65+
```
4366

44-
Most of the code have been written by Luis Faja <https://bitbucket.org/lluisfaja/javajwt>. We just wrapped it in a nicer interface and published it to Maven Central. We'll be adding support for signing and other algorithms in the future.
4567

4668
### Why another JSON Web Token implementation for Java?
47-
We think that current JWT implementations are either too complex or not tested enough. We want something simple with the right number of abstractions.
69+
70+
We believe existing JWT implementations in Java are either too complex or not tested enough.
71+
This library aims to be simple and achieve the right level of abstraction.
4872

4973
## Issue Reporting
5074

pom.xml

Lines changed: 78 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
23
<modelVersion>4.0.0</modelVersion>
34

45
<parent>
@@ -9,14 +10,15 @@
910

1011
<groupId>com.auth0</groupId>
1112
<artifactId>java-jwt</artifactId>
12-
<version>2.1.1-SNAPSHOT</version>
13+
<version>2.2.1-SNAPSHOT</version>
1314

1415
<name>Java JWT</name>
15-
<description>Java implementation of JSON Web Token developed against draft-ietf-oauth-json-web-token-08.</description>
16+
<description>Java implementation of JSON Web Token developed against draft-ietf-oauth-json-web-token-08.
17+
</description>
1618
<url>http://www.jwt.io</url>
1719

1820
<properties>
19-
<java.version>1.5</java.version>
21+
<java.version>1.7</java.version>
2022
<repackage.base>com.auth0.jwt.internal</repackage.base>
2123
</properties>
2224

@@ -28,41 +30,53 @@
2830
</license>
2931
</licenses>
3032

31-
<developers>
32-
<developer>
33-
<name>Alberto Pose</name>
34-
<id>pose</id>
35-
<roles>
36-
<role>Developer</role>
37-
</roles>
38-
</developer>
39-
</developers>
40-
4133
<scm>
4234
<url>https://github.com/auth0/java-jwt</url>
4335
<developerConnection>scm:git:[email protected]:auth0/java-jwt.git</developerConnection>
4436
<connection>scm:git:[email protected]:auth0/java-jwt.git</connection>
4537
</scm>
4638

4739
<dependencies>
48-
<!-- For JWT parsing-->
40+
4941
<dependency>
5042
<groupId>com.fasterxml.jackson.core</groupId>
5143
<artifactId>jackson-databind</artifactId>
5244
<version>2.0.1</version>
5345
</dependency>
46+
47+
<dependency>
48+
<groupId>org.bouncycastle</groupId>
49+
<artifactId>bcprov-jdk15on</artifactId>
50+
<version>1.52</version>
51+
</dependency>
52+
5453
<dependency>
5554
<groupId>commons-codec</groupId>
5655
<artifactId>commons-codec</artifactId>
5756
<version>1.4</version>
5857
</dependency>
5958

59+
<dependency>
60+
<groupId>org.apache.commons</groupId>
61+
<artifactId>commons-lang3</artifactId>
62+
<version>3.4</version>
63+
</dependency>
64+
65+
<dependency>
66+
<groupId>org.apache.commons</groupId>
67+
<artifactId>commons-io</artifactId>
68+
<version>1.3.2</version>
69+
</dependency>
70+
71+
<!-- test -->
72+
6073
<dependency>
6174
<groupId>junit</groupId>
6275
<artifactId>junit</artifactId>
6376
<version>4.11</version>
6477
<scope>test</scope>
6578
</dependency>
79+
6680
</dependencies>
6781

6882
<build>
@@ -77,33 +91,55 @@
7791
<encoding>UTF-8</encoding>
7892
</configuration>
7993
</plugin>
80-
<plugin>
81-
<groupId>org.apache.maven.plugins</groupId>
82-
<artifactId>maven-shade-plugin</artifactId>
83-
<version>2.2</version>
84-
<executions>
85-
<execution>
86-
<phase>package</phase>
87-
<goals>
88-
<goal>shade</goal>
89-
</goals>
90-
<configuration>
91-
<shadedArtifactAttached>false</shadedArtifactAttached>
92-
<createDependencyReducedPom>true</createDependencyReducedPom>
93-
<relocations>
94-
<relocation>
95-
<pattern>com.fasterxml.jackson</pattern>
96-
<shadedPattern>${repackage.base}.com.fasterxml.jackson</shadedPattern>
97-
</relocation>
98-
<relocation>
99-
<pattern>org.apache.commons.codec</pattern>
100-
<shadedPattern>${repackage.base}.org.apache.commons.codec</shadedPattern>
101-
</relocation>
102-
</relocations>
103-
</configuration>
104-
</execution>
105-
</executions>
106-
</plugin>
94+
<plugin>
95+
<groupId>org.apache.maven.plugins</groupId>
96+
<artifactId>maven-shade-plugin</artifactId>
97+
<version>2.2</version>
98+
<executions>
99+
<execution>
100+
<phase>package</phase>
101+
<goals>
102+
<goal>shade</goal>
103+
</goals>
104+
<configuration>
105+
<filters>
106+
<filter>
107+
<artifact>*:*</artifact>
108+
<excludes>
109+
<exclude>META-INF/*.SF</exclude>
110+
<exclude>META-INF/*.DSA</exclude>
111+
<exclude>META-INF/*.RSA</exclude>
112+
</excludes>
113+
</filter>
114+
</filters>
115+
<shadedArtifactAttached>false</shadedArtifactAttached>
116+
<createDependencyReducedPom>true</createDependencyReducedPom>
117+
<relocations>
118+
<relocation>
119+
<pattern>com.fasterxml.jackson</pattern>
120+
<shadedPattern>${repackage.base}.com.fasterxml.jackson</shadedPattern>
121+
</relocation>
122+
<relocation>
123+
<pattern>org.apache.commons.codec</pattern>
124+
<shadedPattern>${repackage.base}.org.apache.commons.codec</shadedPattern>
125+
</relocation>
126+
<relocation>
127+
<pattern>org.apache.commons.io</pattern>
128+
<shadedPattern>${repackage.base}.org.apache.commons.io</shadedPattern>
129+
</relocation>
130+
<relocation>
131+
<pattern>org.apache.commons.lang3</pattern>
132+
<shadedPattern>${repackage.base}.org.apache.commons.lang3</shadedPattern>
133+
</relocation>
134+
<relocation>
135+
<pattern>org.bouncycastle</pattern>
136+
<shadedPattern>${repackage.base}.org.bouncycastle</shadedPattern>
137+
</relocation>
138+
</relocations>
139+
</configuration>
140+
</execution>
141+
</executions>
142+
</plugin>
107143
</plugins>
108144
</build>
109145
</project>
Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
11
package com.auth0.jwt;
22

3+
import org.apache.commons.lang3.Validate;
4+
5+
/**
6+
* Supported Library Algorithms
7+
*
8+
* https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator
9+
*/
310
public enum Algorithm {
4-
HS256("HmacSHA256"), HS384("HmacSHA384"), HS512("HmacSHA512"), RS256("RS256"), RS384("RS384"), RS512("RS512");
511

6-
private Algorithm(String value) {
12+
HS256("HmacSHA256"), HS384("HmacSHA384"), HS512("HmacSHA512"), RS256("SHA256withRSA"), RS384("SHA384withRSA"), RS512("SHA512withRSA");
13+
14+
private Algorithm(final String value) {
715
this.value = value;
816
}
9-
17+
1018
private String value;
1119

1220
public String getValue() {
1321
return value;
1422
}
23+
24+
public static Algorithm findByName(final String name) throws JWTAlgorithmException {
25+
Validate.notNull(name);
26+
try {
27+
return Algorithm.valueOf(name);
28+
} catch (IllegalArgumentException e) {
29+
throw new JWTAlgorithmException("Unsupported algorithm: " + name);
30+
}
31+
}
32+
1533
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.auth0.jwt;
2+
3+
/**
4+
* Represents Exception related to Algorithm - for example JWT header algorithm is unsupported / missing
5+
*/
6+
public class JWTAlgorithmException extends JWTVerifyException {
7+
8+
9+
public JWTAlgorithmException() {}
10+
11+
public JWTAlgorithmException(final String message, final Throwable cause) {
12+
super(message, cause);
13+
}
14+
15+
public JWTAlgorithmException(final String message) {
16+
super(message);
17+
}
18+
19+
}
20+

src/main/java/com/auth0/jwt/JWTAudienceException.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
package com.auth0.jwt;
22

33
import com.fasterxml.jackson.databind.JsonNode;
4+
import org.apache.commons.lang3.Validate;
45

56
import java.util.ArrayList;
67
import java.util.List;
78

9+
/**
10+
* Represents Exception related to Audience - for example illegal audience on JWT Verification
11+
*/
812
public class JWTAudienceException extends JWTVerifyException {
13+
914
private JsonNode audienceNode;
1015

11-
public JWTAudienceException(JsonNode audienceNode) {
16+
public JWTAudienceException(final JsonNode audienceNode) {
17+
Validate.notNull(audienceNode);
1218
this.audienceNode = audienceNode;
1319
}
1420

15-
public JWTAudienceException(String message, JsonNode audienceNode) {
21+
public JWTAudienceException(final String message, final JsonNode audienceNode) {
1622
super(message);
23+
Validate.notNull(audienceNode);
1724
this.audienceNode = audienceNode;
1825
}
1926

2027
public List<String> getAudience() {
21-
ArrayList<String> audience = new ArrayList<String>();
28+
final ArrayList<String> audience = new ArrayList<>();
2229
if (audienceNode.isArray()) {
23-
for (JsonNode jsonNode : audienceNode) {
30+
for (final JsonNode jsonNode : audienceNode) {
2431
audience.add(jsonNode.textValue());
2532
}
2633
} else if (audienceNode.isTextual()) {

src/main/java/com/auth0/jwt/JWTExpiredException.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package com.auth0.jwt;
22

3+
4+
/**
5+
* Represents Exception related to Expiration - for example JWT token has expired
6+
*/
37
public class JWTExpiredException extends JWTVerifyException {
8+
49
private long expiration;
510

6-
public JWTExpiredException(long expiration) {
11+
public JWTExpiredException(final long expiration) {
712
this.expiration = expiration;
813
}
914

10-
public JWTExpiredException(String message, long expiration) {
15+
public JWTExpiredException(final String message, final long expiration) {
1116
super(message);
1217
this.expiration = expiration;
1318
}

0 commit comments

Comments
 (0)