use of org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo in project carbon-apimgt by wso2.
the class DefaultKeyManagerImplTestCase method testGetNewAccessTokenByClientCredentialsGrant.
@Test
public void testGetNewAccessTokenByClientCredentialsGrant() 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 - client credentials grant type
// //request to key manager
AccessTokenRequest tokenRequest = createKeyManagerTokenRequest(consumerKey, consumerSecret, KeyManagerConstants.CLIENT_CREDENTIALS_GRANT_TYPE, null, null, null, -2L, null, null, 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().generateClientCredentialsGrantAccessToken(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.identity.jwt.client.extension.dto.AccessTokenInfo in project carbon-apimgt by wso2.
the class OAuth2AuthenticatorTestCase method testOauthAuthenticate.
@Test
public void testOauthAuthenticate() throws Exception {
HTTPCarbonMessage carbonMessage = Mockito.mock(HTTPCarbonMessage.class);
Request requestObj = new Request(carbonMessage);
Response responseObj = Mockito.mock(Response.class);
ServiceMethodInfo serviceMethodInfoObj = Mockito.mock(ServiceMethodInfo.class);
final String authorizationHttpHeader = "Bearer 7d33e3cd-60f0-3484-9651-cc31f2e09fb4";
final String accessToken = "7d33e3cd-60f0-3484-9651-cc31f2e09fb4";
Mockito.when(requestObj.getHeader(RestApiConstants.AUTHORIZATION_HTTP_HEADER)).thenReturn(authorizationHttpHeader);
AccessTokenInfo accessTokenInfo = new AccessTokenInfo();
accessTokenInfo.setTokenValid(true);
accessTokenInfo.setEndUserName("admin@carbon.super");
IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
APIManagerFactory instance = Mockito.mock(APIManagerFactory.class);
PowerMockito.mockStatic(APIManagerFactory.class);
PowerMockito.when(APIManagerFactory.getInstance()).thenReturn(instance);
Mockito.when(instance.getIdentityProvider()).thenReturn(identityProvider);
Mockito.when(identityProvider.getTokenMetaData(accessToken)).thenReturn(accessTokenInfo);
when((String) requestObj.getProperty(APIConstants.REQUEST_URL)).thenReturn("/api/am/publisher/");
OAuth2Authenticator oAuth2Authenticator = new OAuth2Authenticator();
oAuth2Authenticator.authenticate(requestObj, responseObj, serviceMethodInfoObj);
Assert.assertEquals(0, responseObj.getStatusCode());
}
use of org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo in project carbon-apimgt by wso2.
the class OAuth2Authenticator method validateTokenAndScopes.
private boolean validateTokenAndScopes(Request request, ServiceMethodInfo serviceMethodInfo, String accessToken) throws APIMgtSecurityException {
// Map<String, String> tokenInfo = validateToken(accessToken);
AccessTokenInfo accessTokenInfo = validateToken(accessToken);
String restAPIResource = getRestAPIResource(request);
// scope validation
return validateScopes(request, serviceMethodInfo, accessTokenInfo.getScopes(), restAPIResource);
}
use of org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo in project carbon-apimgt by wso2.
the class APIConsumerImplTest method testGetSubscribedIdentifiers.
@Test
public void testGetSubscribedIdentifiers() throws APIManagementException {
APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO);
Set<SubscribedAPI> originalSubscribedAPIs = new HashSet<>();
SubscribedAPI subscribedAPI = Mockito.mock(SubscribedAPI.class);
originalSubscribedAPIs.add(subscribedAPI);
Subscriber subscriber = new Subscriber("Subscriber");
APIIdentifier apiId1 = new APIIdentifier(API_PROVIDER, SAMPLE_API_NAME, SAMPLE_API_VERSION);
Tier tier = Mockito.mock(Tier.class);
when(apiMgtDAO.getSubscribedAPIs("testorg", subscriber, "testID")).thenReturn(originalSubscribedAPIs);
when(subscribedAPI.getTier()).thenReturn(tier);
when(tier.getName()).thenReturn("tier");
when(subscribedAPI.getApiId()).thenReturn(apiId1);
Application app = Mockito.mock(Application.class);
when(app.getId()).thenReturn(1);
when(subscribedAPI.getApplication()).thenReturn(app);
Set<APIKey> apiKeys = new HashSet<>();
APIKey apiKey = new APIKey();
apiKey.setType("Production");
apiKeys.add(apiKey);
Mockito.when(apiMgtDAO.getKeyMappingsFromApplicationId(Mockito.anyInt())).thenReturn(apiKeys);
AccessTokenInfo accessTokenInfo = new AccessTokenInfo();
accessTokenInfo.setAccessToken(UUID.randomUUID().toString());
Mockito.when(keyManager.getAccessTokenByConsumerKey(Mockito.anyString())).thenReturn(accessTokenInfo);
assertNotNull(apiConsumer.getSubscribedIdentifiers(subscriber, apiId1, "testID", "testorg"));
}
use of org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo in project carbon-apimgt by wso2.
the class APIConsumerImplTest method testGetApplicationKeys.
@Test
public void testGetApplicationKeys() throws APIManagementException {
APIKey apiKey1 = new APIKey();
apiKey1.setConsumerKey(UUID.randomUUID().toString());
apiKey1.setType(APIConstants.API_KEY_TYPE_PRODUCTION);
apiKey1.setState(UUID.randomUUID().toString());
APIKey apiKey2 = new APIKey();
apiKey2.setConsumerKey(UUID.randomUUID().toString());
apiKey2.setType(APIConstants.API_KEY_TYPE_SANDBOX);
apiKey2.setState(UUID.randomUUID().toString());
APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO);
Map<String, String> consumerKeyMap = new HashMap<>();
consumerKeyMap.put("default", apiKey1.getConsumerKey());
Set<APIKey> apiKeys = new HashSet<>();
apiKeys.add(apiKey1);
apiKeys.add(apiKey2);
Mockito.when(apiMgtDAO.getKeyMappingsFromApplicationId(Mockito.anyInt())).thenReturn(apiKeys);
Mockito.when(apiMgtDAO.getConsumerkeyByApplicationIdAndKeyType(Mockito.anyInt(), Mockito.anyString())).thenReturn(consumerKeyMap, consumerKeyMap);
AccessTokenInfo accessTokenInfo = new AccessTokenInfo();
accessTokenInfo.setAccessToken(UUID.randomUUID().toString());
Mockito.when(keyManager.getAccessTokenByConsumerKey(Mockito.anyString())).thenReturn(accessTokenInfo);
Mockito.when(keyManagerConfigurationDTO.isEnabled()).thenReturn(true);
assertNotNull(apiConsumer.getApplicationKeys(1));
assertEquals(apiConsumer.getApplicationKeys(1).size(), 2);
assertNotNull(apiConsumer.getApplicationKeys(1).iterator().next().getAccessToken());
}
Aggregations