Created
September 19, 2017 20:19
-
-
Save daveRanjan/3f02281c95a64768b5d7d3a4dc862479 to your computer and use it in GitHub Desktop.
JWTFunctionalityUT
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
package com.example.usergroups; | |
import com.auth0.jwt.JWT; | |
import com.auth0.jwt.JWTVerifier; | |
import com.auth0.jwt.algorithms.Algorithm; | |
import com.auth0.jwt.exceptions.InvalidClaimException; | |
import com.auth0.jwt.exceptions.JWTCreationException; | |
import com.auth0.jwt.interfaces.DecodedJWT; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import org.junit.Assert; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.boot.test.context.SpringBootTest; | |
import org.springframework.test.context.junit4.SpringRunner; | |
import java.io.UnsupportedEncodingException; | |
import java.util.Date; | |
@RunWith(SpringRunner.class) | |
@SpringBootTest | |
public class UsergroupsApplicationTests { | |
String token; | |
@Value("${secret}") | |
private String secret; | |
@Before | |
public void createToken() throws JSONException { | |
try { | |
System.out.println("Secret is : "+secret); | |
Algorithm algorithm = Algorithm.HMAC256(secret); | |
String subject = new JSONObject().put("userId", 1L).put("isAdmin", true).toString(); | |
token = JWT | |
.create() | |
.withIssuer("3clogic") | |
.withClaim("UserId", 1L) | |
.withClaim("isAdmin", true) | |
.withSubject(subject) | |
.withIssuedAt(new Date()) | |
.withNotBefore(new Date(2017,9,20)) | |
.withExpiresAt(new Date(2017,9,25)) | |
.sign(algorithm); | |
System.out.println("Token is : "+token); | |
} catch (UnsupportedEncodingException exception) { | |
//UTF-8 encoding not supported | |
} catch (JWTCreationException exception) { | |
//Invalid Signing configuration / Couldn't convert Claims. | |
} | |
} | |
@Test | |
public void givenJWTToken_whenVerified_shouldReturnDecodedJwt(){ | |
try { | |
DecodedJWT decodedJWT = verifyJwtToken(); | |
Assert.assertNotNull(decodedJWT); | |
} catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Test(expected = InvalidClaimException.class) | |
public void givenJWTToken_whenVerifiedWithWrongIssuer_shouldThrowException() throws UnsupportedEncodingException { | |
Algorithm algorithm = Algorithm.HMAC256(secret); | |
JWTVerifier verifier = JWT.require(algorithm) | |
.withIssuer("Not3clogic") | |
.build(); //Reusable verifier instance | |
DecodedJWT jwt = verifier.verify(token); | |
} | |
@Test | |
public void givenJWTToken_whenVerified_shouldProvideSubject() throws UnsupportedEncodingException, JSONException { | |
DecodedJWT jwt = verifyJwtToken(); | |
Assert.assertNotNull(jwt.getSubject()); | |
JSONObject jsonObject = new JSONObject(jwt.getSubject()); | |
Assert.assertTrue(jsonObject.has("userId")); | |
Assert.assertTrue(jsonObject.has("isAdmin")); | |
} | |
private DecodedJWT verifyJwtToken() throws UnsupportedEncodingException { | |
Algorithm algorithm = Algorithm.HMAC256(secret); | |
JWTVerifier verifier = JWT.require(algorithm) | |
.withIssuer("3clogic") | |
.build(); //Reusable verifier instance | |
DecodedJWT jwt = verifier.verify(token); | |
return jwt; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment