Skip to content

Commit

Permalink
implement single sign-on token generation - resolves #12
Browse files Browse the repository at this point in the history
  • Loading branch information
Jared King committed Dec 5, 2017
1 parent c066d85 commit f78ef5c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.2'
compile group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.5.2'
compile group: 'com.mashape.unirest', name: 'unirest-java', version: '1.4.9'
compile 'com.auth0:java-jwt:3.3.0'

compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.12'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.invoiced.exception;

public class SingleSignOnException extends InvoicedException {
private static final long serialVersionUID = 1L;

public SingleSignOnException(Throwable cause) {
super(cause);
}
}
33 changes: 33 additions & 0 deletions src/main/java/com/invoiced/util/SingleSignOn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.invoiced.util;

import java.io.UnsupportedEncodingException;
import java.util.Date;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.invoiced.exception.SingleSignOnException;

public class SingleSignOn {

private final String ssoKey;

public SingleSignOn(String ssoKey) {
this.ssoKey = ssoKey;
}

public String generateToken(int customerId, int ttlSeconds) throws SingleSignOnException {
try {
Algorithm algorithm = Algorithm.HMAC256(this.ssoKey);

long expiresAtMs = (long) (System.currentTimeMillis() + ttlSeconds * 1000.0);
Date expiresAt = new Date(expiresAtMs);

return JWT.create().withIssuer("Invoiced Java").withIssuedAt(new Date())
.withSubject(Integer.toString(customerId)).withExpiresAt(expiresAt).sign(algorithm);
} catch (IllegalArgumentException e) {
throw new SingleSignOnException(e);
} catch (UnsupportedEncodingException e) {
throw new SingleSignOnException(e);
}
}
}
54 changes: 54 additions & 0 deletions src/test/java/com/invoiced/util/SingleSignOnTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.invoiced.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.UnsupportedEncodingException;

import org.junit.Test;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.invoiced.exception.SingleSignOnException;

public class SingleSignOnTest {

@Test
public void testGenerateToken() {
String secret = "8baa4dbc338a54bbf7696eef3ee4aa2daadd61bba85fcfe8df96c7cfa227c43";
SingleSignOn sso = new SingleSignOn(secret);
String token = null;
try {
token = sso.generateToken(1234, 3600);
} catch (SingleSignOnException e) {
e.printStackTrace();
fail();
}

Algorithm algorithm = null;
try {
algorithm = Algorithm.HMAC256(secret);
} catch (IllegalArgumentException e) {
e.printStackTrace();
fail();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
fail();
}

JWTVerifier verifier = JWT.require(algorithm).withIssuer("Invoiced Java").build();
DecodedJWT jwt = null;
try {
jwt = verifier.verify(token);
} catch (JWTVerificationException e) {
e.printStackTrace();
fail();
}

assertEquals("1234", jwt.getSubject());
}

}

0 comments on commit f78ef5c

Please sign in to comment.