-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement single sign-on token generation - resolves #12
- Loading branch information
Jared King
committed
Dec 5, 2017
1 parent
c066d85
commit f78ef5c
Showing
4 changed files
with
97 additions
and
0 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
9 changes: 9 additions & 0 deletions
9
src/main/java/com/invoiced/exception/SingleSignOnException.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,9 @@ | ||
package com.invoiced.exception; | ||
|
||
public class SingleSignOnException extends InvoicedException { | ||
private static final long serialVersionUID = 1L; | ||
|
||
public SingleSignOnException(Throwable cause) { | ||
super(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
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); | ||
} | ||
} | ||
} |
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,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()); | ||
} | ||
|
||
} |