Search in sources :

Example 56 with DefaultOAuth2AccessToken

use of org.springframework.security.oauth2.common.DefaultOAuth2AccessToken in project ORCID-Source by ORCID.

the class OrcidClientCredentialEndPointDelegatorTest method generateClientCredentialsAccessTokenTest.

@Test
public void generateClientCredentialsAccessTokenTest() {
    SecurityContextTestUtils.setUpSecurityContextForClientOnly(CLIENT_ID_1, ScopePathType.ACTIVITIES_UPDATE, ScopePathType.READ_LIMITED);
    MultivaluedMap<String, String> formParams = new MultivaluedMapImpl();
    formParams.add("client_id", CLIENT_ID_1);
    formParams.add("client_secret", "DhkFj5EI0qp6GsUKi55Vja+h+bsaKpBx");
    formParams.add("grant_type", "client_credentials");
    formParams.add("redirect_uri", "http://www.APP-5555555555555555.com/redirect/oauth");
    formParams.add("scope", "/orcid-profile/create");
    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) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) DBUnitTest(org.orcid.test.DBUnitTest) Test(org.junit.Test)

Example 57 with DefaultOAuth2AccessToken

use of org.springframework.security.oauth2.common.DefaultOAuth2AccessToken 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 58 with DefaultOAuth2AccessToken

use of org.springframework.security.oauth2.common.DefaultOAuth2AccessToken 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 59 with DefaultOAuth2AccessToken

use of org.springframework.security.oauth2.common.DefaultOAuth2AccessToken in project ORCID-Source by ORCID.

the class OrcidTokenStoreServiceTest method testRemoveAccessToken.

@Test
@Transactional
public void testRemoveAccessToken() throws Exception {
    OAuth2AccessToken accessToken = new DefaultOAuth2AccessToken("code3");
    orcidTokenStoreService.removeAccessToken(accessToken);
    OAuth2AccessToken oAuth2AccessToken = orcidTokenStoreService.readAccessToken("code3");
    assertNull(oAuth2AccessToken);
}
Also used : DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) DBUnitTest(org.orcid.test.DBUnitTest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 60 with DefaultOAuth2AccessToken

use of org.springframework.security.oauth2.common.DefaultOAuth2AccessToken in project ORCID-Source by ORCID.

the class OrcidRandomValueTokenServicesImpl method toOAuth2AccessToken.

private OAuth2AccessToken toOAuth2AccessToken(OrcidOauth2TokenDetail token) {
    DefaultOAuth2AccessToken result = new DefaultOAuth2AccessToken(token.getTokenValue());
    result.setExpiration(token.getTokenExpiration());
    result.setRefreshToken(new DefaultOAuth2RefreshToken(token.getRefreshTokenValue()));
    result.setScope(OAuth2Utils.parseParameterList(token.getScope()));
    result.setTokenType(token.getTokenType());
    result.setValue(token.getTokenValue());
    Map<String, Object> additionalInfo = new HashMap<String, Object>();
    if (token.getProfile() != null) {
        additionalInfo.put(OrcidOauth2Constants.ORCID, token.getProfile().getId());
        additionalInfo.put(OrcidOauth2Constants.NAME, profileEntityManager.retrivePublicDisplayName(token.getProfile().getId()));
    }
    result.setAdditionalInformation(additionalInfo);
    return result;
}
Also used : DefaultOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken) HashMap(java.util.HashMap) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)

Aggregations

DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)95 Test (org.junit.Test)78 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)52 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)48 Date (java.util.Date)27 TokenRequest (org.springframework.security.oauth2.provider.TokenRequest)13 HashMap (java.util.HashMap)12 Authentication (org.springframework.security.core.Authentication)12 DefaultOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken)12 URI (java.net.URI)9 DefaultExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken)9 AuthorizationCodeResourceDetails (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails)8 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)8 DBUnitTest (org.orcid.test.DBUnitTest)7 ExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken)6 AuthorizationRequest (org.springframework.security.oauth2.provider.AuthorizationRequest)6 Before (org.junit.Before)5 BaseOAuth2ProtectedResourceDetails (org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails)5 OAuth2ProtectedResourceDetails (org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails)5 ClientDetails (org.springframework.security.oauth2.provider.ClientDetails)5