@Test public void testExpiredToken() { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.SECOND, 1); Key key = MacProvider.generateKey(); // new KeyPair("publickey","privatekey") String token = TokenUtil.getJWTString("Phil", new String[]{"user"}, 11, calendar.getTime(), key); assertNotNull(token); try { Thread.sleep(1000L * 3L); //1000 milliseconds is one second. } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } assertEquals("Token should have expired", false, TokenUtil.isValid(token, key)); }
@Test public void testNonExpiredToken() { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.MINUTE, 15); Key key = MacProvider.generateKey(); // new KeyPair("publickey","privatekey") String token = TokenUtil.getJWTString("Phil", new String[]{"user"}, 12, calendar.getTime(), key); assertNotNull(token); try { Thread.sleep(1000L * 2L); //1000 milliseconds is one second. } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } assertEquals("Token should have been valid", true, TokenUtil.isValid(token, key)); }
public static JwtBuilder builder(KeyEncrypt keyEncrypt) throws KeyOperationException { SecretKey secretKey = MacProvider.generateKey(SignatureAlgorithm.HS256); byte[] encryptedKey = keyEncrypt.encrypt(secretKey.getEncoded()); return Jwts.builder() .setHeaderParam("kid", DatatypeConverter.printBase64Binary(encryptedKey)) .signWith(SignatureAlgorithm.HS256, secretKey); }
@Test public void testSigninService() { UserSigninReq reqObj = new UserSigninReq(); // reqObj.setAppName("samyh"); // reqObj.setDeviceType("android"); // reqObj.setVersion("0.0.1"); // reqObj.setUserPhone("13900000014"); reqObj.setPassword("123456789"); reqObj.setDeviceInfo("XXXXXXXXXXX"); String jsonReq = JSON.toJSONString(reqObj); logger.info("Reuqest json String:" + jsonReq); String url = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/user/signin.json").build().toUriString(); logger.info("Request URL:" + url); Key key = MacProvider.generateKey(); String s = createJWT(key, jsonReq); logger.info("jwt:" + s); String str = decodeJWT(key, s); logger.info("jwt:" + str); MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>(); params.add("jsonReq", jsonReq); String resp = restTemplate.postForObject(url, params, String.class); logger.info("ResponseBody:" + resp); assertEquals(jsonReq, str); }
protected javax.ws.rs.core.Application configure() { enable(TestProperties.LOG_TRAFFIC); enable(TestProperties.DUMP_ENTITY); final BookDao bookDao = new BookDao(); final UserDao userDao = new UserDao(); userDao.addUser(AdminUser); userDao.addUser(NormalUser); book1Id = bookDao.addBook(book1).getId(); book2Id = bookDao.addBook(book2).getId(); final Key key = MacProvider.generateKey(); return new com.sample.Application(bookDao, userDao, key); }
@Test(expected = NullPointerException.class) public void testGetJWTString_invalidUserName() { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.MINUTE, 15); String username = null; String[] roles = new String[]{"user"}; int version = 0; Date expires = calendar.getTime(); Key key = MacProvider.generateKey(); TokenUtil.getJWTString(username, roles, version, expires, key); }
@Test(expected = NullPointerException.class) public void testGetJWTString_invalidRoles() { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.MINUTE, 15); String username = "Bob"; String[] roles = null; int version = 0; Date expires = calendar.getTime(); Key key = MacProvider.generateKey(); TokenUtil.getJWTString(username, roles, version, expires, key); }
@Test(expected = NullPointerException.class) public void testGetJWTString_invalidExpires() { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.MINUTE, 15); String username = "Bob"; String[] roles = new String[]{"user"}; int version = 0; Date expires = null; Key key = MacProvider.generateKey(); TokenUtil.getJWTString(username, roles, version, expires, key); }
@Test public void testGetVersionGood() { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.MINUTE, 15); String username = "Bob"; String[] roles = new String[]{"user"}; int version = 3; Date expires = calendar.getTime(); Key key = MacProvider.generateKey(); String jwtString = TokenUtil.getJWTString(username, roles, version, expires, key); int version1 = TokenUtil.getVersion(jwtString, key); assertEquals(version, version1); }
@Test public void testGetVersionBad() { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.MINUTE, 15); String username = "Bob"; String[] roles = new String[]{"user"}; int version = 3; Date expires = calendar.getTime(); Key key = MacProvider.generateKey(); String jwtString = TokenUtil.getJWTString(username, roles, version, expires, key); int version1 = TokenUtil.getVersion(jwtString, MacProvider.generateKey()); assertEquals(-1, version1); }
@Test public void testGetRolesGood() { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.MINUTE, 15); String username = "Bob"; String[] roles = new String[]{"user"}; int version = 3; Date expires = calendar.getTime(); Key key = MacProvider.generateKey(); String jwtString = TokenUtil.getJWTString(username, roles, version, expires, key); String[] roles1 = TokenUtil.getRoles(jwtString, key); assertEquals(Arrays.toString(roles), Arrays.toString(roles1)); }
@Test public void testGetRolesBad() { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.MINUTE, 15); String username = "Bob"; String[] roles = new String[]{"user"}; int version = 3; Date expires = calendar.getTime(); Key key = MacProvider.generateKey(); String jwtString = TokenUtil.getJWTString(username, roles, version, expires, key); String[] roles1 = TokenUtil.getRoles(jwtString, MacProvider.generateKey()); assertEquals(Arrays.toString(new String[]{}), Arrays.toString(roles1)); }
@Test public void testGetNamesGood() { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.MINUTE, 15); String username = "Bob"; String[] roles = new String[]{"user"}; int version = 3; Date expires = calendar.getTime(); Key key = MacProvider.generateKey(); String jwtString = TokenUtil.getJWTString(username, roles, version, expires, key); String username1 = TokenUtil.getName(jwtString, key); assertEquals(username, username1); }
@Test public void testGetNamesBad() { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.MINUTE, 15); String username = "Bob"; String[] roles = new String[]{"user"}; int version = 3; Date expires = calendar.getTime(); Key key = MacProvider.generateKey(); String jwtString = TokenUtil.getJWTString(username, roles, version, expires, key); String username1 = TokenUtil.getName(jwtString, MacProvider.generateKey()); assertNull(username1); }
public static String createJsonWebToken(String subject, Date expiration, Collection<String> permissions) { Key key = MacProvider.generateKey(); userToKeyMap.put(subject, key); String permissionsString = StringUtils.join(permissions, "|"); Map<String, Object> claims = new HashMap<>(); claims.put("permissions", permissionsString); return Jwts.builder() .setClaims(claims) .setSubject(subject) .setExpiration(expiration) .signWith(SignatureAlgorithm.HS512, key) .compact(); }
public JwtTokenService() { key = MacProvider.generateKey(); }
@Override public Key getKey() { return MacProvider.generateKey(); }
public static HttpServer startServer() { Key key = MacProvider.generateKey(); final ResourceConfig rc = new Application(new BookDao(), new UserDao(), key); return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc); }