use of org.wso2.carbon.apimgt.api.model.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;
}
use of org.wso2.carbon.apimgt.api.model.AccessTokenRequest in project carbon-apimgt by wso2.
the class AbstractKeyManager method buildAccessTokenRequestFromOAuthApp.
public AccessTokenRequest buildAccessTokenRequestFromOAuthApp(OAuthApplicationInfo oAuthApplication, AccessTokenRequest tokenRequest) throws APIManagementException {
if (oAuthApplication == null) {
return tokenRequest;
}
if (tokenRequest == null) {
tokenRequest = new AccessTokenRequest();
}
if (oAuthApplication.getClientId() == null || oAuthApplication.getClientSecret() == null) {
throw new APIManagementException("Consumer key or Consumer Secret missing.");
}
tokenRequest.setClientId(oAuthApplication.getClientId());
tokenRequest.setClientSecret(oAuthApplication.getClientSecret());
if (oAuthApplication.getParameter("tokenScope") != null) {
String[] tokenScopes = (String[]) oAuthApplication.getParameter("tokenScope");
tokenRequest.setScope(tokenScopes);
oAuthApplication.addParameter("tokenScope", Arrays.toString(tokenScopes));
}
if (oAuthApplication.getParameter(ApplicationConstants.VALIDITY_PERIOD) != null) {
tokenRequest.setValidityPeriod(Long.parseLong((String) oAuthApplication.getParameter(ApplicationConstants.VALIDITY_PERIOD)));
}
return tokenRequest;
}
use of org.wso2.carbon.apimgt.api.model.AccessTokenRequest in project carbon-apimgt by wso2.
the class APIConsumerImplTest method testMapExistingOAuthClient.
@Test
public void testMapExistingOAuthClient() throws APIManagementException {
APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO);
apiConsumer.tenantDomain = "carbon.super";
OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
OAuthAppRequest oAuthAppRequest = new OAuthAppRequest();
oAuthAppRequest.setOAuthApplicationInfo(oAuthApplicationInfo);
BDDMockito.when(ApplicationUtils.createOauthAppRequest(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(oAuthAppRequest);
Mockito.when(apiMgtDAO.isKeyMappingExistsForConsumerKeyOrApplication(Mockito.anyInt(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(true, false);
Mockito.when(keyManager.mapOAuthApplication((OAuthAppRequest) Mockito.any())).thenReturn(oAuthApplicationInfo);
Mockito.doNothing().when(apiMgtDAO).createApplicationKeyTypeMappingForManualClients(Mockito.anyString(), Mockito.anyInt(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
KeyManagerConfigurationDTO keyManagerConfigurationsDto = new KeyManagerConfigurationDTO();
keyManagerConfigurationsDto.setUuid(UUID.randomUUID().toString());
keyManagerConfigurationsDto.setEnabled(true);
Mockito.when(apiMgtDAO.isKeyManagerConfigurationExistByName("default", "carbon.super")).thenReturn(true);
Mockito.when(apiMgtDAO.getKeyManagerConfigurationByName("carbon.super", "default")).thenReturn(keyManagerConfigurationsDto);
AccessTokenRequest accessTokenRequest = new AccessTokenRequest();
AccessTokenInfo accessTokenInfo = new AccessTokenInfo();
KeyManagerConfiguration keyManagerConfiguration = new KeyManagerConfiguration();
Mockito.when(keyManager.getKeyManagerConfiguration()).thenReturn(keyManagerConfiguration);
BDDMockito.when(ApplicationUtils.createAccessTokenRequest(keyManager, oAuthApplicationInfo, null)).thenReturn(accessTokenRequest);
Mockito.when(keyManager.getNewApplicationAccessToken(accessTokenRequest)).thenReturn(accessTokenInfo);
try {
apiConsumer.mapExistingOAuthClient("", "admin", "1", "app1", "refresh", "DEFAULT", "Resident Key Manager", "carbon.super");
Assert.fail("Exception is not thrown when client id is already mapped to an application");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Key Mappings already exists for application"));
}
Assert.assertEquals(8, apiConsumer.mapExistingOAuthClient("", "admin", "1", "app1", "PRODUCTION", "DEFAULT", "Resident Key Manager", "carbon.super").size());
}
use of org.wso2.carbon.apimgt.api.model.AccessTokenRequest in project carbon-apimgt by wso2.
the class AbstractKeyManagerTestCase method buildAccessTokenRequestFromOAuthAppTest.
@Test
public void buildAccessTokenRequestFromOAuthAppTest() throws APIManagementException {
AbstractKeyManager keyManager = new AMDefaultKeyManagerImpl();
// test null flow
assertNull(keyManager.buildAccessTokenRequestFromOAuthApp(null, null));
// test without client id and secret
try {
keyManager.buildAccessTokenRequestFromOAuthApp(new OAuthApplicationInfo(), new AccessTokenRequest());
assertTrue(false);
} catch (APIManagementException e) {
assertEquals("Consumer key or Consumer Secret missing.", e.getMessage());
}
// test with all the parameters
OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
oAuthApplicationInfo.setClientId("XBPcXSfGK47WiEX7enchoP2Dcvga");
oAuthApplicationInfo.setClientSecret("4UD8VX8NaQMtrHCwqzI1tHJLPoca");
oAuthApplicationInfo.addParameter("tokenScope", new String[] { "view", "update" });
oAuthApplicationInfo.addParameter("validityPeriod", "1200");
AccessTokenRequest accessTokenRequest = keyManager.buildAccessTokenRequestFromOAuthApp(oAuthApplicationInfo, null);
assertNotNull(accessTokenRequest);
assertEquals("XBPcXSfGK47WiEX7enchoP2Dcvga", accessTokenRequest.getClientId());
assertEquals("4UD8VX8NaQMtrHCwqzI1tHJLPoca", accessTokenRequest.getClientSecret());
assertEquals(1200, accessTokenRequest.getValidityPeriod());
}
use of org.wso2.carbon.apimgt.api.model.AccessTokenRequest in project carbon-apimgt by wso2.
the class AbstractKeyManagerTestCase method buildAccessTokenRequestFromJSONTest.
@Test
public void buildAccessTokenRequestFromJSONTest() throws APIManagementException {
String jsonPayload = "{ \"callbackUrl\": \"www.google.lk\", \"clientName\": \"rest_api_publisher\", " + "\"tokenScope\": \"Production\", \"owner\": \"admin\", \"grantType\": \"password refresh_token\", " + "\"saasApp\": true }";
AbstractKeyManager keyManager = new AMDefaultKeyManagerImpl();
// test AccessTokenRequest null scenario
AccessTokenRequest accessTokenRequest1 = keyManager.buildAccessTokenRequestFromJSON(jsonPayload, null);
Assert.notNull(accessTokenRequest1);
// test json payload without required parameters
AccessTokenRequest accessTokenRequest2 = keyManager.buildAccessTokenRequestFromJSON(jsonPayload, accessTokenRequest1);
Assert.notNull(accessTokenRequest2);
assertNull(accessTokenRequest2.getClientId());
// test json payload null
assertNull(keyManager.buildAccessTokenRequestFromJSON(null, null));
String jsonPayload2 = "{ \"callbackUrl\": \"www.google.lk\", \"client_id\": \"XBPcXSfGK47WiEX7enchoP2Dcvga\"," + "\"client_secret\": \"4UD8VX8NaQMtrHCwqzI1tHJLPoca\", \"owner\": \"admin\", \"grantType\": \"password" + " refresh_token\", " + "\"validityPeriod\": \"3600\" }";
AccessTokenRequest accessTokenRequest3 = keyManager.buildAccessTokenRequestFromJSON(jsonPayload2, new AccessTokenRequest());
assertEquals("XBPcXSfGK47WiEX7enchoP2Dcvga", accessTokenRequest3.getClientId());
assertEquals("4UD8VX8NaQMtrHCwqzI1tHJLPoca", accessTokenRequest3.getClientSecret());
assertEquals(3600, accessTokenRequest3.getValidityPeriod());
// Error path with invalid json
try {
keyManager.buildAccessTokenRequestFromJSON("{dd}", null);
assertTrue(false);
} catch (APIManagementException e) {
assertEquals("Error occurred while parsing JSON String", e.getMessage());
}
// Error path with empty JSON
assertNull(keyManager.buildAccessTokenRequestFromJSON("{}", null));
keyManager.buildAccessTokenRequestFromJSON(null, new AccessTokenRequest());
}
Aggregations