Search in sources :

Example 1 with PerryTokenEntity

use of gov.ca.cwds.data.reissue.model.PerryTokenEntity in project perry by ca-cwds.

the class TokenService method issueAccessCode.

public String issueAccessCode(UniversalUserToken userToken, OAuth2AccessToken accessToken) {
    String accessCode = generator.generate();
    PerryTokenEntity perryTokenEntity = new PerryTokenEntity();
    perryTokenEntity.setUser(userToken.getUserId());
    perryTokenEntity.setAccessCode(accessCode);
    perryTokenEntity.writeAccessToken(accessToken);
    perryTokenEntity.setToken(userToken.getToken());
    deleteExpiredRecords();
    tokenRepository.save(perryTokenEntity);
    return accessCode;
}
Also used : PerryTokenEntity(gov.ca.cwds.data.reissue.model.PerryTokenEntity)

Example 2 with PerryTokenEntity

use of gov.ca.cwds.data.reissue.model.PerryTokenEntity in project perry by ca-cwds.

the class TokenService method getPerryTokenByAccessCode.

public String getPerryTokenByAccessCode(String accessCode) {
    List<PerryTokenEntity> tokens = tokenRepository.findByAccessCode(accessCode);
    if (tokens.isEmpty()) {
        throw new PerryException("Access Code: " + accessCode + " is not found");
    }
    if (tokens.size() > 1) {
        tokenRepository.delete(tokens);
        throw new PerryException("Access Code: " + accessCode + " is not unique");
    }
    PerryTokenEntity perryTokenEntity = tokens.get(0);
    if (new Date().after(DateUtils.addMinutes(perryTokenEntity.getCreatedDate(), properties.getJwt().getTimeout()))) {
        tokenRepository.delete(perryTokenEntity);
        throw new PerryException("Access Code: " + accessCode + " is expired");
    }
    perryTokenEntity.setAccessCode(null);
    return perryTokenEntity.getToken();
}
Also used : PerryTokenEntity(gov.ca.cwds.data.reissue.model.PerryTokenEntity) PerryException(gov.ca.cwds.rest.api.domain.PerryException) Date(java.util.Date)

Example 3 with PerryTokenEntity

use of gov.ca.cwds.data.reissue.model.PerryTokenEntity in project perry by ca-cwds.

the class TokenServiceTest method testLoginFlow.

@Test
public void testLoginFlow() {
    String accessCode = issueAccessCode();
    assert accessCode != null;
    PerryTokenEntity perryTokenEntity = tokenRepository.findOne(PERRY_TOKEN);
    assert perryTokenEntity != null;
    assert perryTokenEntity.readAccessToken().getValue().equals(ACCESS_TOKEN);
    assert perryTokenEntity.getCreatedDate() != null;
    assert perryTokenEntity.getAccessCode().equals(accessCode);
    assert perryTokenEntity.getToken().equals(PERRY_TOKEN);
    String perryToken = tokenService.getPerryTokenByAccessCode(accessCode);
    assert perryToken != null;
    assert perryToken.equals(PERRY_TOKEN);
    perryTokenEntity = tokenRepository.findOne(PERRY_TOKEN);
    assert perryTokenEntity.getAccessCode() == null;
}
Also used : PerryTokenEntity(gov.ca.cwds.data.reissue.model.PerryTokenEntity) Test(org.junit.Test) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest)

Aggregations

PerryTokenEntity (gov.ca.cwds.data.reissue.model.PerryTokenEntity)3 PerryException (gov.ca.cwds.rest.api.domain.PerryException)1 Date (java.util.Date)1 Test (org.junit.Test)1 DataJpaTest (org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest)1