SignedTokenManager implements SimpleTokenManager { @Override public TokenStatus getValidity(SignedExpiringToken token) { Preconditions.checkNotNull(token); if (! isCorrectlySigned(token)) { return TokenStatus.INVALID; } if (isExpired(token)) { return TokenStatus.EXPIRED; } return TokenStatus.OK; } @Inject  SignedTokenManager(SignatureHandler signatureHandler, ZonedDateTimeProvider zonedDateTimeProvider); @Override TokenStatus getValidity(SignedExpiringToken token); @Override boolean isValid(SignedExpiringToken token);  }
@Test public void getValidityShouldThrowWhenTokenIsNull() throws Exception { assertThatThrownBy(() -> tokenManager.getValidity(null)) .isInstanceOf(NullPointerException.class); }
@Test public void getValidityShouldRecognizeValidTokens() throws Exception { zonedDateTimeProvider.setFixedDateTime(DATE); assertThat( tokenManager.getValidity( tokenFactory.generateContinuationToken("user"))) .isEqualTo(TokenStatus.OK); }
@Test public void getValidityShouldRecognizeTokenWhereUsernameIsModified() throws Exception { zonedDateTimeProvider.setFixedDateTime(DATE); ContinuationToken continuationToken = tokenFactory.generateContinuationToken("user"); ContinuationToken pirateContinuationToken = new ContinuationToken("pirate", continuationToken.getExpirationDate(), continuationToken.getSignature()); assertThat(tokenManager.getValidity(pirateContinuationToken)).isEqualTo(TokenStatus.INVALID); }
@Test public void getValidityhouldRecognizeTokenWhereExpirationDateIsModified() throws Exception { zonedDateTimeProvider.setFixedDateTime(DATE); ContinuationToken continuationToken = tokenFactory.generateContinuationToken("user"); ContinuationToken pirateContinuationToken = new ContinuationToken(continuationToken.getUsername(), continuationToken.getExpirationDate().plusHours(1), continuationToken.getSignature()); assertThat(tokenManager.getValidity(pirateContinuationToken)).isEqualTo(TokenStatus.INVALID); }
@Test public void getValidityShouldRecognizeTokenWhereSignatureIsModified() throws Exception { zonedDateTimeProvider.setFixedDateTime(DATE); ContinuationToken continuationToken = tokenFactory.generateContinuationToken("user"); ContinuationToken pirateContinuationToken = new ContinuationToken(continuationToken.getUsername(), continuationToken.getExpirationDate(), FAKE_SIGNATURE); assertThat(tokenManager.getValidity(pirateContinuationToken)).isEqualTo(TokenStatus.INVALID); }
@Test public void getValidityShouldReturnFalseWhenTokenIsOutdated() throws Exception { zonedDateTimeProvider.setFixedDateTime(DATE); ContinuationToken continuationToken = tokenFactory.generateContinuationToken("user"); zonedDateTimeProvider.setFixedDateTime(DATE.plusHours(1)); assertThat(tokenManager.getValidity(continuationToken)).isEqualTo(TokenStatus.EXPIRED); }
@Test public void getValidityShouldReturnFalseOnNonValidSignatures() throws Exception { zonedDateTimeProvider.setFixedDateTime(DATE); ContinuationToken pirateContinuationToken = new ContinuationToken("user", DATE.plusMinutes(15), "fake"); assertThat(tokenManager.getValidity(pirateContinuationToken)).isEqualTo(TokenStatus.INVALID); }