use of org.wso2.carbon.apimgt.core.models.AccessTokenRequest in project carbon-apimgt by wso2.
the class DefaultKeyManagerImplTestCase method testGetNewAccessTokenByAuthorizationCodeGrant.
@Test
public void testGetNewAccessTokenByAuthorizationCodeGrant() throws Exception {
DCRMServiceStub dcrmServiceStub = Mockito.mock(DCRMServiceStub.class);
OAuth2ServiceStubs oAuth2ServiceStub = Mockito.mock(OAuth2ServiceStubs.class);
OAuth2ServiceStubs.TokenServiceStub tokenStub = Mockito.mock(OAuth2ServiceStubs.TokenServiceStub.class);
ScopeRegistration scopeRegistration = Mockito.mock(ScopeRegistration.class);
DefaultKeyManagerImpl kmImpl = new DefaultKeyManagerImpl(dcrmServiceStub, oAuth2ServiceStub, scopeRegistration);
// happy path - 200 - authorization code grant type
// //request to key manager
AccessTokenRequest tokenRequest = createKeyManagerTokenRequest(consumerKey, consumerSecret, KeyManagerConstants.AUTHORIZATION_CODE_GRANT_TYPE, null, null, null, -2L, "xxx-auth-code-xxx", "http://test.callback/url", null, null);
// //mocked response from /token service
OAuth2TokenInfo oAuth2TokenInfo = createTokenServiceResponse(tokenRequest);
// //expected response from key manager
AccessTokenInfo accessTokenInfo = createExpectedKeyManagerResponse(oAuth2TokenInfo);
Response newTokenResponse = Response.builder().status(200).headers(new HashMap<>()).body(new Gson().toJson(oAuth2TokenInfo), Util.UTF_8).build();
Mockito.when(oAuth2ServiceStub.getTokenServiceStub()).thenReturn(tokenStub);
Mockito.when(oAuth2ServiceStub.getTokenServiceStub().generateAuthCodeGrantAccessToken(tokenRequest.getAuthorizationCode(), tokenRequest.getCallbackURI(), tokenRequest.getScopes(), tokenRequest.getValidityPeriod(), tokenRequest.getClientId(), tokenRequest.getClientSecret())).thenReturn(newTokenResponse);
try {
AccessTokenInfo newToken = kmImpl.getNewAccessToken(tokenRequest);
Assert.assertEquals(newToken, accessTokenInfo);
} catch (Exception ex) {
Assert.fail(ex.getMessage());
}
}
use of org.wso2.carbon.apimgt.core.models.AccessTokenRequest in project carbon-apimgt by wso2.
the class DefaultKeyManagerImplTestCase method testGetNewAccessTokenByPasswordGrant.
@Test
public void testGetNewAccessTokenByPasswordGrant() throws Exception {
DCRMServiceStub dcrmServiceStub = Mockito.mock(DCRMServiceStub.class);
OAuth2ServiceStubs oAuth2ServiceStub = Mockito.mock(OAuth2ServiceStubs.class);
OAuth2ServiceStubs.TokenServiceStub tokenStub = Mockito.mock(OAuth2ServiceStubs.TokenServiceStub.class);
OAuth2ServiceStubs.RevokeServiceStub revokeStub = Mockito.mock(OAuth2ServiceStubs.RevokeServiceStub.class);
ScopeRegistration scopeRegistration = Mockito.mock(ScopeRegistration.class);
DefaultKeyManagerImpl kmImpl = new DefaultKeyManagerImpl(dcrmServiceStub, oAuth2ServiceStub, scopeRegistration);
// happy path - 200 - password grant type
// //request to key manager
AccessTokenRequest tokenRequest = createKeyManagerTokenRequest(consumerKey, consumerSecret, KeyManagerConstants.PASSWORD_GRANT_TYPE, "user1", "pass1", "xxx-old-token-xxx", 7200L, null, null, null, null);
// //mocked response from /token service
OAuth2TokenInfo oAuth2TokenInfo = createTokenServiceResponse(tokenRequest);
// //expected response from key manager
AccessTokenInfo accessTokenInfo = createExpectedKeyManagerResponse(oAuth2TokenInfo);
Response revokeTokenResponse = Response.builder().status(200).headers(new HashMap<>()).body(new Gson().toJson(oAuth2TokenInfo), feign.Util.UTF_8).build();
Mockito.when(oAuth2ServiceStub.getRevokeServiceStub()).thenReturn(revokeStub);
Mockito.when(revokeStub.revokeAccessToken(tokenRequest.getTokenToRevoke(), tokenRequest.getClientId(), tokenRequest.getClientSecret())).thenReturn(revokeTokenResponse);
Response newTokenResponse = Response.builder().status(200).headers(new HashMap<>()).body(new Gson().toJson(oAuth2TokenInfo), feign.Util.UTF_8).build();
Mockito.when(oAuth2ServiceStub.getTokenServiceStub()).thenReturn(tokenStub);
Mockito.when(oAuth2ServiceStub.getTokenServiceStub().generatePasswordGrantAccessToken(tokenRequest.getResourceOwnerUsername(), tokenRequest.getResourceOwnerPassword(), tokenRequest.getScopes(), tokenRequest.getValidityPeriod(), tokenRequest.getClientId(), tokenRequest.getClientSecret())).thenReturn(newTokenResponse);
try {
AccessTokenInfo newToken = kmImpl.getNewAccessToken(tokenRequest);
Assert.assertEquals(newToken, accessTokenInfo);
} catch (Exception ex) {
Assert.fail(ex.getMessage());
}
}
use of org.wso2.carbon.apimgt.core.models.AccessTokenRequest in project carbon-apimgt by wso2.
the class ApplicationUtils method createAccessTokenRequest.
public static AccessTokenRequest createAccessTokenRequest(OAuthApplicationInfo oAuthApplication) throws APIManagementException {
AccessTokenRequest tokenRequest = new AccessTokenRequest();
if (oAuthApplication.getClientId() != null || oAuthApplication.getClientSecret() != null) {
tokenRequest.setClientId(oAuthApplication.getClientId());
tokenRequest.setClientSecret(oAuthApplication.getClientSecret());
} else {
throw new KeyManagementException("Consumer key or Consumer Secret is missing.");
}
if (oAuthApplication.getParameter(KeyManagerConstants.TOKEN_SCOPES) != null) {
String tokenScopes = (String) oAuthApplication.getParameter(KeyManagerConstants.TOKEN_SCOPES);
tokenRequest.setScopes(tokenScopes);
oAuthApplication.addParameter(KeyManagerConstants.OAUTH_CLIENT_TOKEN_SCOPE, tokenScopes);
}
tokenRequest.setGrantType(KeyManagerConstants.CLIENT_CREDENTIALS_GRANT_TYPE);
if (oAuthApplication.getParameter(KeyManagerConstants.VALIDITY_PERIOD) != null) {
tokenRequest.setValidityPeriod(Long.parseLong((String) oAuthApplication.getParameter(KeyManagerConstants.VALIDITY_PERIOD)));
} else {
throw new KeyManagementException("Validity period missing for generated oAuth keys");
}
return tokenRequest;
}
Aggregations