Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. package com.example.usergroups;
  2.  
  3. import com.auth0.jwt.JWT;
  4. import com.auth0.jwt.JWTVerifier;
  5. import com.auth0.jwt.algorithms.Algorithm;
  6. import com.auth0.jwt.exceptions.InvalidClaimException;
  7. import com.auth0.jwt.exceptions.JWTCreationException;
  8. import com.auth0.jwt.interfaces.DecodedJWT;
  9. import org.json.JSONException;
  10. import org.json.JSONObject;
  11. import org.junit.Assert;
  12. import org.junit.Before;
  13. import org.junit.Test;
  14. import org.junit.runner.RunWith;
  15. import org.springframework.beans.factory.annotation.Value;
  16. import org.springframework.boot.test.context.SpringBootTest;
  17. import org.springframework.test.context.junit4.SpringRunner;
  18.  
  19. import java.io.UnsupportedEncodingException;
  20. import java.util.Date;
  21.  
  22. @RunWith(SpringRunner.class)
  23. @SpringBootTest
  24. public class UsergroupsApplicationTests {
  25.  
  26. String token;
  27.  
  28. @Value("${secret}")
  29. private String secret;
  30.  
  31. @Before
  32. public void createToken() throws JSONException {
  33. try {
  34. System.out.println("Secret is : "+secret);
  35. Algorithm algorithm = Algorithm.HMAC256(secret);
  36. String subject = new JSONObject().put("userId", 1L).put("isAdmin", true).toString();
  37. token = JWT
  38. .create()
  39. .withIssuer("3clogic")
  40. .withClaim("UserId", 1L)
  41. .withClaim("isAdmin", true)
  42. .withSubject(subject)
  43. .withIssuedAt(new Date())
  44. .withNotBefore(new Date(2017,9,20))
  45. .withExpiresAt(new Date(2017,9,25))
  46. .sign(algorithm);
  47. System.out.println("Token is : "+token);
  48. } catch (UnsupportedEncodingException exception) {
  49. //UTF-8 encoding not supported
  50. } catch (JWTCreationException exception) {
  51. //Invalid Signing configuration / Couldn't convert Claims.
  52. }
  53. }
  54.  
  55. @Test
  56. public void givenJWTToken_whenVerified_shouldReturnDecodedJwt(){
  57. try {
  58. DecodedJWT decodedJWT = verifyJwtToken();
  59. Assert.assertNotNull(decodedJWT);
  60. } catch (UnsupportedEncodingException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64.  
  65. @Test(expected = InvalidClaimException.class)
  66. public void givenJWTToken_whenVerifiedWithWrongIssuer_shouldThrowException() throws UnsupportedEncodingException {
  67. Algorithm algorithm = Algorithm.HMAC256(secret);
  68. JWTVerifier verifier = JWT.require(algorithm)
  69. .withIssuer("Not3clogic")
  70. .build(); //Reusable verifier instance
  71. DecodedJWT jwt = verifier.verify(token);
  72. }
  73.  
  74. @Test
  75. public void givenJWTToken_whenVerified_shouldProvideSubject() throws UnsupportedEncodingException, JSONException {
  76. DecodedJWT jwt = verifyJwtToken();
  77. Assert.assertNotNull(jwt.getSubject());
  78. JSONObject jsonObject = new JSONObject(jwt.getSubject());
  79. Assert.assertTrue(jsonObject.has("userId"));
  80. Assert.assertTrue(jsonObject.has("isAdmin"));
  81. }
  82.  
  83. private DecodedJWT verifyJwtToken() throws UnsupportedEncodingException {
  84. Algorithm algorithm = Algorithm.HMAC256(secret);
  85. JWTVerifier verifier = JWT.require(algorithm)
  86. .withIssuer("3clogic")
  87. .build(); //Reusable verifier instance
  88. DecodedJWT jwt = verifier.verify(token);
  89. return jwt;
  90. }
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement