use of org.orcid.persistence.jpa.entities.OrcidOauth2TokenDetail in project ORCID-Source by ORCID.
the class OrcidRefreshTokenTokenGranterTest method createRefreshTokenWithNarrowerScopesTest.
@Test
public void createRefreshTokenWithNarrowerScopesTest() {
// Create token, create refresh with narrower scopes, parent should be
// disabled, scopes should be narrower
long time = System.currentTimeMillis();
String parentScope = "/activities/update";
String refreshScope = "/orcid-works/create";
String tokenValue = "parent-token-" + time;
String refreshTokenValue = "refresh-token-" + time;
Boolean revokeOld = true;
Date parentTokenExpiration = new Date(time + 10000);
Long expireIn = null;
OrcidOauth2TokenDetail parent = createToken(CLIENT_ID_1, USER_ORCID, tokenValue, refreshTokenValue, parentTokenExpiration, parentScope);
OAuth2AccessToken refresh = generateRefreshToken(parent, null, revokeOld, expireIn, refreshScope);
assertNotNull(refresh);
OrcidOauth2TokenDetail parentToken = orcidOauth2TokenDetailService.findIgnoringDisabledByTokenValue(parent.getTokenValue());
assertNotNull(parentToken);
assertEquals(tokenValue, parentToken.getTokenValue());
assertTrue(parentToken.getTokenDisabled());
assertEquals(parentScope, parentToken.getScope());
assertNotNull(parentToken.getTokenExpiration());
OrcidOauth2TokenDetail refreshToken = orcidOauth2TokenDetailService.findIgnoringDisabledByTokenValue(refresh.getValue());
assertNotNull(refreshToken);
assertNotNull(refreshToken.getTokenValue());
assertNotNull(refreshToken.getRefreshTokenValue());
assertFalse(refreshToken.getTokenDisabled());
assertEquals(refreshScope, refreshToken.getScope());
assertNotNull(refreshToken.getTokenExpiration());
assertEquals(parentToken.getTokenExpiration().getTime(), refreshToken.getTokenExpiration().getTime());
}
use of org.orcid.persistence.jpa.entities.OrcidOauth2TokenDetail in project ORCID-Source by ORCID.
the class OrcidRefreshTokenTokenGranterTest method tryToCreateRefreshTokenWithInvalidRefreshTokenTest.
@Test
public void tryToCreateRefreshTokenWithInvalidRefreshTokenTest() {
// Create token, try to create refresh token with invalid refresh value,
// fail
long time = System.currentTimeMillis();
String parentScope = "/person/update";
String tokenValue = "parent-token-" + time;
String refreshTokenValue = "refresh-token-" + time;
Boolean revokeOld = true;
Date parentTokenExpiration = new Date(time + 10000);
Long expireIn = null;
OrcidOauth2TokenDetail parent = createToken(CLIENT_ID_1, USER_ORCID, tokenValue, refreshTokenValue, parentTokenExpiration, parentScope);
try {
//Change the value we are going to use for the refresh token
parent.setRefreshTokenValue("invalid-value");
generateRefreshToken(parent, null, revokeOld, expireIn, parentScope);
fail();
} catch (InvalidTokenException e) {
assertTrue(e.getMessage().contains("Token and refresh token does not match"));
} catch (Exception e) {
fail();
}
}
use of org.orcid.persistence.jpa.entities.OrcidOauth2TokenDetail in project ORCID-Source by ORCID.
the class OrcidRefreshTokenTokenGranterTest method tryToRefreshAnExpiredTokenTest.
@Test
public void tryToRefreshAnExpiredTokenTest() {
long time = System.currentTimeMillis();
String parentScope = "/person/update";
String tokenValue = "parent-token-" + time;
String refreshTokenValue = "refresh-token-" + time;
Boolean revokeOld = true;
Date parentTokenExpiration = new Date(time - 10000);
Long expireIn = null;
OrcidOauth2TokenDetail parent = createToken(CLIENT_ID_1, USER_ORCID, tokenValue, refreshTokenValue, parentTokenExpiration, parentScope);
try {
generateRefreshToken(parent, null, revokeOld, expireIn, parentScope);
fail();
} catch (InvalidTokenException e) {
assertTrue(e.getMessage().contains("Access token expired:"));
} catch (Exception e) {
fail();
}
}
use of org.orcid.persistence.jpa.entities.OrcidOauth2TokenDetail in project ORCID-Source by ORCID.
the class OrcidRefreshTokenTokenGranterTest method createRefreshTokenWithExpirationOf10Secs.
@Test
public void createRefreshTokenWithExpirationOf10Secs() {
// Create token, dont revoke parent and set expiration to 10 secs
long time = System.currentTimeMillis();
String parentScope = "/person/read-limited";
String refreshScope = "/orcid-bio/read-limited";
String tokenValue = "parent-token-" + time;
String refreshTokenValue = "refresh-token-" + time;
Boolean revokeOld = false;
Date parentTokenExpiration = new Date(time + 10000);
Long expireIn = 5L;
OrcidOauth2TokenDetail parent = createToken(CLIENT_ID_1, USER_ORCID, tokenValue, refreshTokenValue, parentTokenExpiration, parentScope);
OAuth2AccessToken refresh = generateRefreshToken(parent, null, revokeOld, expireIn, refreshScope);
assertNotNull(refresh);
OrcidOauth2TokenDetail parentToken = orcidOauth2TokenDetailService.findIgnoringDisabledByTokenValue(parent.getTokenValue());
assertNotNull(parentToken);
assertEquals(tokenValue, parentToken.getTokenValue());
assertFalse(parentToken.getTokenDisabled());
assertEquals(parentScope, parentToken.getScope());
assertNotNull(parentToken.getTokenExpiration());
OrcidOauth2TokenDetail refreshToken = orcidOauth2TokenDetailService.findIgnoringDisabledByTokenValue(refresh.getValue());
assertNotNull(refreshToken);
assertNotNull(refreshToken.getTokenValue());
assertNotNull(refreshToken.getRefreshTokenValue());
assertFalse(refreshToken.getTokenDisabled());
assertEquals(refreshScope, refreshToken.getScope());
assertNotNull(refreshToken.getTokenExpiration());
assertTrue(parentToken.getTokenExpiration().getTime() > refreshToken.getTokenExpiration().getTime());
// Assert that current time plus 6 secs is greather than refresh token
// expiration
assertTrue((time + 6000) > refreshToken.getTokenExpiration().getTime());
}
use of org.orcid.persistence.jpa.entities.OrcidOauth2TokenDetail in project ORCID-Source by ORCID.
the class OrcidOauth2TokenDetailServiceTest method createToken.
private OrcidOauth2TokenDetail createToken(String clientId, String tokenValue, String userOrcid, Date expirationDate, String scopes, boolean disabled) {
OrcidOauth2TokenDetail token = new OrcidOauth2TokenDetail();
token.setApproved(true);
token.setClientDetailsId(clientId);
token.setDateCreated(new Date());
token.setLastModified(new Date());
token.setProfile(new ProfileEntity(userOrcid));
token.setScope(scopes);
token.setTokenDisabled(disabled);
token.setTokenExpiration(expirationDate);
token.setTokenType("bearer");
token.setTokenValue(tokenValue);
orcidOauth2TokenDetailService.saveOrUpdate(token);
return token;
}
Aggregations