Search in sources :

Example 6 with OrcidOauth2AuthoriziationCodeDetail

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());
}
Also used : Response(javax.ws.rs.core.Response) OrcidOauth2AuthoriziationCodeDetail(org.orcid.persistence.jpa.entities.OrcidOauth2AuthoriziationCodeDetail) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) DBUnitTest(org.orcid.test.DBUnitTest) Test(org.junit.Test)

Example 7 with OrcidOauth2AuthoriziationCodeDetail

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()));
}
Also used : Response(javax.ws.rs.core.Response) OrcidOauth2AuthoriziationCodeDetail(org.orcid.persistence.jpa.entities.OrcidOauth2AuthoriziationCodeDetail) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) DBUnitTest(org.orcid.test.DBUnitTest) Test(org.junit.Test)

Example 8 with OrcidOauth2AuthoriziationCodeDetail

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());
        }
    }
}
Also used : OrcidOauth2AuthoriziationCodeDetail(org.orcid.persistence.jpa.entities.OrcidOauth2AuthoriziationCodeDetail) Calendar(java.util.Calendar) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with OrcidOauth2AuthoriziationCodeDetail

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() });
}
Also used : OrcidOauth2AuthoriziationCodeDetail(org.orcid.persistence.jpa.entities.OrcidOauth2AuthoriziationCodeDetail) OrcidOauth2AuthInfo(org.orcid.core.oauth.OrcidOauth2AuthInfo)

Example 10 with OrcidOauth2AuthoriziationCodeDetail

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;
}
Also used : OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) OrcidOauth2AuthoriziationCodeDetail(org.orcid.persistence.jpa.entities.OrcidOauth2AuthoriziationCodeDetail) OrcidOauth2UserAuthentication(org.orcid.core.oauth.OrcidOauth2UserAuthentication) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) OrcidOauth2AuthInfo(org.orcid.core.oauth.OrcidOauth2AuthInfo)

Aggregations

OrcidOauth2AuthoriziationCodeDetail (org.orcid.persistence.jpa.entities.OrcidOauth2AuthoriziationCodeDetail)13 Test (org.junit.Test)4 DBUnitTest (org.orcid.test.DBUnitTest)4 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)4 MultivaluedMapImpl (com.sun.jersey.core.util.MultivaluedMapImpl)3 Response (javax.ws.rs.core.Response)3 ClientDetailsEntity (org.orcid.persistence.jpa.entities.ClientDetailsEntity)3 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)3 Authentication (org.springframework.security.core.Authentication)3 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)3 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)3 Calendar (java.util.Calendar)2 Date (java.util.Date)2 OrcidOauth2AuthInfo (org.orcid.core.oauth.OrcidOauth2AuthInfo)2 OrcidOauth2UserAuthentication (org.orcid.core.oauth.OrcidOauth2UserAuthentication)2 Transactional (org.springframework.transaction.annotation.Transactional)2 HashMap (java.util.HashMap)1 OrcidProfileUserDetails (org.orcid.core.oauth.OrcidProfileUserDetails)1 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)1 InvalidClientException (org.springframework.security.oauth2.common.exceptions.InvalidClientException)1