@Test public void test_getApiTokenContents_expired() throws InterruptedException { String compactJws = Jwts .builder() .setSubject(USERNAME) .setExpiration(new Date((new Date()).getTime() + 1)) .signWith(SignatureAlgorithm.HS512, propertiesManager.getProperty(Properties.SIGNING_KEY)).compact(); TimeUnit.SECONDS.sleep(1); IApiTokenContents tokenContents = serviceToTest.getApiTokenContents(compactJws); Assert.assertTrue(tokenContents.isExpired()); }
@Override public IApiTokenContents getApiTokenContents(String token) { IApiTokenContents contents = new ApiTokenContents(); contents.setExpired(true); try { Jws<Claims> jws = Jwts.parser().setSigningKey(propertiesManager.getProperty(Properties.SIGNING_KEY)).parseClaimsJws(token); Claims claims = jws.getBody(); contents.setUsername(claims.getSubject()); Date expirationTime = claims.getExpiration(); if (expirationTime != null) { contents.setExpired(expirationTime.before(new Date())); } } catch (io.jsonwebtoken.ExpiredJwtException e) { messageHandler.handleMessage("Token is expired.", e, MessageType.INFO); contents.setExpired(true); } catch (SignatureException e) { messageHandler.handleMessage("Token signature not correct.", e, MessageType.WARNING); return null; } return contents; }
TokenService implements ITokenService { @Override public IApiTokenContents getApiTokenContents(String token) { IApiTokenContents contents = new ApiTokenContents(); contents.setExpired(true); try { Jws<Claims> jws = Jwts.parser().setSigningKey(propertiesManager.getProperty(Properties.SIGNING_KEY)).parseClaimsJws(token); Claims claims = jws.getBody(); contents.setUsername(claims.getSubject()); Date expirationTime = claims.getExpiration(); if (expirationTime != null) { contents.setExpired(expirationTime.before(new Date())); } } catch (io.jsonwebtoken.ExpiredJwtException e) { messageHandler.handleMessage("Token is expired.", e, MessageType.INFO); contents.setExpired(true); } catch (SignatureException e) { messageHandler.handleMessage("Token signature not correct.", e, MessageType.WARNING); return null; } return contents; } }
TokenService implements ITokenService { @Override public IApiTokenContents getApiTokenContents(String token) { IApiTokenContents contents = new ApiTokenContents(); contents.setExpired(true); try { Jws<Claims> jws = Jwts.parser().setSigningKey(propertiesManager.getProperty(Properties.SIGNING_KEY)).parseClaimsJws(token); Claims claims = jws.getBody(); contents.setUsername(claims.getSubject()); Date expirationTime = claims.getExpiration(); if (expirationTime != null) { contents.setExpired(expirationTime.before(new Date())); } } catch (io.jsonwebtoken.ExpiredJwtException e) { messageHandler.handleMessage("Token is expired.", e, MessageType.INFO); contents.setExpired(true); } catch (SignatureException e) { messageHandler.handleMessage("Token signature not correct.", e, MessageType.WARNING); return null; } return contents; }  }
TokenService implements ITokenService { @Override public IApiTokenContents getApiTokenContents(String token) { IApiTokenContents contents = new ApiTokenContents(); contents.setExpired(true); try { Jws<Claims> jws = Jwts.parser().setSigningKey(propertiesManager.getProperty(Properties.SIGNING_KEY)).parseClaimsJws(token); Claims claims = jws.getBody(); contents.setUsername(claims.getSubject()); Date expirationTime = claims.getExpiration(); if (expirationTime != null) { contents.setExpired(expirationTime.before(new Date())); } } catch (io.jsonwebtoken.ExpiredJwtException e) { messageHandler.handleMessage("Token is expired.", e, MessageType.INFO); contents.setExpired(true); } catch (SignatureException e) { messageHandler.handleMessage("Token signature not correct.", e, MessageType.WARNING); return null; } return contents; }  @Override String generateApiToken(User user); @Override IApiTokenContents getApiTokenContents(String token); @Override IAppToken generateAppToken(IRegisteredApp app); @Override IAppToken getAppTokenContents(String token); }
TokenService implements ITokenService { @Override public IApiTokenContents getApiTokenContents(String token) { IApiTokenContents contents = new ApiTokenContents(); contents.setExpired(true); try { Jws<Claims> jws = Jwts.parser().setSigningKey(propertiesManager.getProperty(Properties.SIGNING_KEY)).parseClaimsJws(token); Claims claims = jws.getBody(); contents.setUsername(claims.getSubject()); Date expirationTime = claims.getExpiration(); if (expirationTime != null) { contents.setExpired(expirationTime.before(new Date())); } } catch (io.jsonwebtoken.ExpiredJwtException e) { messageHandler.handleMessage("Token is expired.", e, MessageType.INFO); contents.setExpired(true); } catch (SignatureException e) { messageHandler.handleMessage("Token signature not correct.", e, MessageType.WARNING); return null; } return contents; }  @Override String generateApiToken(User user); @Override IApiTokenContents getApiTokenContents(String token); @Override IAppToken generateAppToken(IRegisteredApp app); @Override IAppToken getAppTokenContents(String token);  }