use of org.orcid.persistence.jpa.entities.OrcidOauth2AuthoriziationCodeDetail in project ORCID-Source by ORCID.
the class OrcidClientCredentialEndPointDelegatorTest method generateRefreshTokenThatExpireAfterParentTokenTest.
@Test
public void generateRefreshTokenThatExpireAfterParentTokenTest() {
//Generate the access token
SecurityContextTestUtils.setUpSecurityContextForClientOnly(CLIENT_ID_1, ScopePathType.ACTIVITIES_UPDATE, ScopePathType.READ_LIMITED);
OrcidOauth2AuthoriziationCodeDetail authCode = createAuthorizationCode("code-1", CLIENT_ID_1, "http://www.APP-5555555555555555.com/redirect/oauth", false, "/activities/update");
MultivaluedMap<String, String> formParams = new MultivaluedMapImpl();
formParams.add("client_id", CLIENT_ID_1);
formParams.add("client_secret", "DhkFj5EI0qp6GsUKi55Vja+h+bsaKpBx");
formParams.add("grant_type", "authorization_code");
formParams.add("redirect_uri", "http://www.APP-5555555555555555.com/redirect/oauth");
formParams.add("code", authCode.getId());
Response response = orcidClientCredentialEndPointDelegator.obtainOauth2Token(null, formParams);
assertNotNull(response);
assertNotNull(response.getEntity());
DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken) response.getEntity();
assertNotNull(token);
assertTrue(!PojoUtil.isEmpty(token.getValue()));
assertNotNull(token.getRefreshToken());
assertTrue(!PojoUtil.isEmpty(token.getRefreshToken().getValue()));
//Generate the refresh token that expires after parent token
MultivaluedMap<String, String> refreshTokenformParams = new MultivaluedMapImpl();
refreshTokenformParams.add("client_id", CLIENT_ID_1);
refreshTokenformParams.add("client_secret", "DhkFj5EI0qp6GsUKi55Vja+h+bsaKpBx");
refreshTokenformParams.add("grant_type", "refresh_token");
refreshTokenformParams.add("redirect_uri", "http://www.APP-5555555555555555.com/redirect/oauth");
refreshTokenformParams.add("refresh_token", token.getRefreshToken().getValue());
refreshTokenformParams.add("expires_in", String.valueOf(2 * 60 * 60));
String authorization = "bearer " + token.getValue();
try {
orcidClientCredentialEndPointDelegator.obtainOauth2Token(authorization, refreshTokenformParams);
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("Token expiration can't be after"));
}
//Try again with a valid expiration value
refreshTokenformParams = new MultivaluedMapImpl();
refreshTokenformParams.add("client_id", CLIENT_ID_1);
refreshTokenformParams.add("client_secret", "DhkFj5EI0qp6GsUKi55Vja+h+bsaKpBx");
refreshTokenformParams.add("grant_type", "refresh_token");
refreshTokenformParams.add("redirect_uri", "http://www.APP-5555555555555555.com/redirect/oauth");
refreshTokenformParams.add("refresh_token", token.getRefreshToken().getValue());
refreshTokenformParams.add("expires_in", String.valueOf(60 * 30));
response = orcidClientCredentialEndPointDelegator.obtainOauth2Token(authorization, refreshTokenformParams);
assertNotNull(response);
assertNotNull(response.getEntity());
DefaultOAuth2AccessToken refreshToken = (DefaultOAuth2AccessToken) response.getEntity();
assertNotNull(refreshToken);
assertTrue(!PojoUtil.isEmpty(refreshToken.getValue()));
assertNotNull(refreshToken.getRefreshToken());
assertTrue(!PojoUtil.isEmpty(refreshToken.getRefreshToken().getValue()));
assertTrue(token.getExpiration().getTime() > refreshToken.getExpiration().getTime());
}
use of org.orcid.persistence.jpa.entities.OrcidOauth2AuthoriziationCodeDetail in project ORCID-Source by ORCID.
the class OrcidClientCredentialEndPointDelegatorTest method generateAccessTokenTest.
@Test
public void generateAccessTokenTest() {
SecurityContextTestUtils.setUpSecurityContextForClientOnly(CLIENT_ID_1, ScopePathType.ACTIVITIES_UPDATE, ScopePathType.READ_LIMITED);
OrcidOauth2AuthoriziationCodeDetail authCode = createAuthorizationCode("code-1", CLIENT_ID_1, "http://www.APP-5555555555555555.com/redirect/oauth", true, "/activities/update");
MultivaluedMap<String, String> formParams = new MultivaluedMapImpl();
formParams.add("client_id", CLIENT_ID_1);
formParams.add("client_secret", "DhkFj5EI0qp6GsUKi55Vja+h+bsaKpBx");
formParams.add("grant_type", "authorization_code");
formParams.add("redirect_uri", "http://www.APP-5555555555555555.com/redirect/oauth");
formParams.add("code", authCode.getId());
Response response = orcidClientCredentialEndPointDelegator.obtainOauth2Token(null, formParams);
assertNotNull(response);
assertNotNull(response.getEntity());
DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken) response.getEntity();
assertNotNull(token);
assertTrue(!PojoUtil.isEmpty(token.getValue()));
assertNotNull(token.getRefreshToken());
assertTrue(!PojoUtil.isEmpty(token.getRefreshToken().getValue()));
}
use of org.orcid.persistence.jpa.entities.OrcidOauth2AuthoriziationCodeDetail in project ORCID-Source by ORCID.
the class AuthorizationCodeCleanerCronJobImpl method cleanExpiredAuthorizationCodes.
@Transactional
public void cleanExpiredAuthorizationCodes() {
List<OrcidOauth2AuthoriziationCodeDetail> allAuthorizationCodes = orcidOauth2AuthoriziationCodeDetailDao.getAll();
Date now = new Date();
for (OrcidOauth2AuthoriziationCodeDetail authorizationCode : allAuthorizationCodes) {
Date creationDate = authorizationCode.getDateCreated();
Calendar c = Calendar.getInstance();
c.setTime(creationDate);
c.add(Calendar.MINUTE, authorizationCodeExpiration);
Date expirationDate = c.getTime();
if (expirationDate.before(now)) {
LOG.info("Authorization code is expired and will be deleted: " + authorizationCode.getId());
orcidOauth2AuthoriziationCodeDetailDao.remove(authorizationCode.getId());
}
}
}
use of org.orcid.persistence.jpa.entities.OrcidOauth2AuthoriziationCodeDetail in project ORCID-Source by ORCID.
the class OrcidAuthorizationCodeServiceImpl method store.
@Override
protected void store(String code, OAuth2Authentication authentication) {
OrcidOauth2AuthoriziationCodeDetail detail = getDetailFromAuthorization(code, authentication);
if (detail == null) {
throw new IllegalArgumentException("Cannot persist the authorisation code as the user and/or client " + "cannot be found");
}
orcidOauth2AuthoriziationCodeDetailDao.persist(detail);
OrcidOauth2AuthInfo authInfo = new OrcidOauth2AuthInfo(authentication);
LOGGER.info("Storing authorization code: code={}, clientId={}, scopes={}, userOrcid={}", new Object[] { code, authInfo.getClientId(), authInfo.getScopes(), authInfo.getUserOrcid() });
}
use of org.orcid.persistence.jpa.entities.OrcidOauth2AuthoriziationCodeDetail in project ORCID-Source by ORCID.
the class OrcidAuthorizationCodeServiceImpl method remove.
@Override
protected OAuth2Authentication remove(String code) {
OrcidOauth2AuthoriziationCodeDetail detail = orcidOauth2AuthoriziationCodeDetailDao.removeAndReturn(code);
if (detail == null) {
LOGGER.info("No such authorization code to remove: code={}", new Object[] { code });
return null;
}
OrcidOauth2AuthInfo authInfo = new OrcidOauth2AuthInfo(detail.getClientDetailsEntity().getId(), detail.getScopes(), detail.getProfileEntity().getId());
LOGGER.info("Removed authorization code: code={}, clientId={}, scopes={}, userOrcid={}", new Object[] { code, authInfo.getClientId(), authInfo.getScopes(), authInfo.getUserOrcid() });
OAuth2Request oAuth2Request = new OAuth2Request(Collections.<String, String>emptyMap(), authInfo.getClientId(), Collections.<GrantedAuthority>emptyList(), true, authInfo.getScopes(), detail.getResourceIds(), detail.getRedirectUri(), new HashSet<String>(Arrays.asList(detail.getResponseType())), Collections.<String, Serializable>emptyMap());
Authentication userAuth = getUserAuthentication(detail);
OAuth2Authentication result = new OAuth2Authentication(oAuth2Request, userAuth);
return result;
}
Aggregations