use of org.wso2.carbon.apimgt.impl.kmclient.model.ClientInfo in project carbon-apimgt by wso2.
the class AMDefaultKeyManagerImplTest method testCreateApplication.
@Test
public void testCreateApplication() throws APIManagementException, KeyManagerClientException {
PowerMockito.mockStatic(APIUtil.class);
System.setProperty("carbon.home", "jhkjn");
PowerMockito.mockStatic(PrivilegedCarbonContext.class);
OAuthAppRequest oauthRequest = new OAuthAppRequest();
OAuthApplicationInfo oauthApplication = new OAuthApplicationInfo();
oauthApplication.setAppOwner(APP_OWNER);
oauthApplication.setCallBackURL(StringUtils.join(REDIRECT_URIS, ","));
oauthApplication.setClientName(APP_NAME);
oauthApplication.addParameter(ApplicationConstants.OAUTH_CLIENT_USERNAME, APP_OWNER);
oauthApplication.addParameter(ApplicationConstants.APP_KEY_TYPE, KEY_TYPE);
oauthApplication.setJsonString(getJSONString());
oauthRequest.setMappingId("123");
oauthRequest.setOAuthApplicationInfo(oauthApplication);
PowerMockito.when(APIUtil.isCrossTenantSubscriptionsEnabled()).thenReturn(false);
PrivilegedCarbonContext privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
ClientInfo response = new ClientInfo();
response.setClientId(CLIENT_ID);
response.setClientName(APP_UUID);
response.setClientSecret(CLIENT_SECRET);
response.setRedirectUris(Arrays.asList(REDIRECT_URIS));
response.setGrantTypes(Arrays.asList(GRANT_TYPES));
Mockito.when(dcrClient.createApplication(Mockito.any(ClientInfo.class))).thenReturn(response);
PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
Mockito.when(privilegedCarbonContext.getTenantDomain()).thenReturn(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
Mockito.when(APIUtil.getApplicationUUID(Mockito.anyString(), Mockito.anyString())).thenReturn(APP_UUID);
OAuthApplicationInfo oauthApplicationResponse = keyManager.createApplication(oauthRequest);
Assert.assertEquals(StringUtils.join(REDIRECT_URIS, ","), oauthApplicationResponse.getCallBackURL());
Assert.assertEquals(APP_UUID, oauthApplicationResponse.getClientName());
}
use of org.wso2.carbon.apimgt.impl.kmclient.model.ClientInfo in project carbon-apimgt by wso2.
the class AMDefaultKeyManagerImpl method createClientInfo.
/**
* Construct ClientInfo object for application create request
*
* @param info The OAuthApplicationInfo object
* @param oauthClientName The name of the OAuth application to be created
* @param isUpdate To determine whether the ClientInfo object is related to application update call
* @return constructed ClientInfo object
* @throws JSONException for errors in parsing the OAuthApplicationInfo json string
* @throws APIManagementException if an error occurs while constructing the ClientInfo object
*/
private ClientInfo createClientInfo(OAuthApplicationInfo info, String oauthClientName, boolean isUpdate) throws JSONException, APIManagementException {
ClientInfo clientInfo = new ClientInfo();
JSONObject infoJson = new JSONObject(info.getJsonString());
String applicationOwner = (String) info.getParameter(ApplicationConstants.OAUTH_CLIENT_USERNAME);
if (infoJson.has(ApplicationConstants.OAUTH_CLIENT_GRANT)) {
// this is done as there are instances where the grant string begins with a comma character.
String grantString = infoJson.getString(ApplicationConstants.OAUTH_CLIENT_GRANT);
if (grantString.startsWith(",")) {
grantString = grantString.substring(1);
}
String[] grantTypes = grantString.split(",");
clientInfo.setGrantTypes(Arrays.asList(grantTypes));
}
if (StringUtils.isNotEmpty(info.getCallBackURL())) {
String callBackURL = info.getCallBackURL();
String[] callbackURLs = callBackURL.trim().split("\\s*,\\s*");
clientInfo.setRedirectUris(Arrays.asList(callbackURLs));
}
clientInfo.setClientName(oauthClientName);
// todo: run tests by commenting the type
if (StringUtils.isEmpty(info.getTokenType())) {
clientInfo.setTokenType(APIConstants.TOKEN_TYPE_JWT);
} else {
clientInfo.setTokenType(info.getTokenType());
}
// being exposed in the JWT token.
if (APIUtil.isCrossTenantSubscriptionsEnabled() && !tenantDomain.equals(MultitenantUtils.getTenantDomain(applicationOwner))) {
clientInfo.setApplication_owner(APIUtil.retrieveDefaultReservedUsername());
} else {
clientInfo.setApplication_owner(MultitenantUtils.getTenantAwareUsername(applicationOwner));
}
if (StringUtils.isNotEmpty(info.getClientId())) {
if (isUpdate) {
clientInfo.setClientId(info.getClientId());
} else {
clientInfo.setPresetClientId(info.getClientId());
}
}
if (StringUtils.isNotEmpty(info.getClientSecret())) {
if (isUpdate) {
clientInfo.setClientId(info.getClientSecret());
} else {
clientInfo.setPresetClientSecret(info.getClientSecret());
}
}
Object parameter = info.getParameter(APIConstants.JSON_ADDITIONAL_PROPERTIES);
Map<String, Object> additionalProperties = new HashMap<>();
if (parameter instanceof String) {
additionalProperties = new Gson().fromJson((String) parameter, Map.class);
}
if (additionalProperties.containsKey(APIConstants.KeyManager.APPLICATION_ACCESS_TOKEN_EXPIRY_TIME)) {
Object expiryTimeObject = additionalProperties.get(APIConstants.KeyManager.APPLICATION_ACCESS_TOKEN_EXPIRY_TIME);
if (expiryTimeObject instanceof String) {
if (!APIConstants.KeyManager.NOT_APPLICABLE_VALUE.equals(expiryTimeObject)) {
try {
long expiry = Long.parseLong((String) expiryTimeObject);
if (expiry < 0) {
throw new APIManagementException("Invalid application access token expiry time given for " + oauthClientName, ExceptionCodes.INVALID_APPLICATION_PROPERTIES);
}
clientInfo.setApplicationAccessTokenLifeTime(expiry);
} catch (NumberFormatException e) {
// No need to throw as its due to not a number sent.
}
}
}
}
if (additionalProperties.containsKey(APIConstants.KeyManager.USER_ACCESS_TOKEN_EXPIRY_TIME)) {
Object expiryTimeObject = additionalProperties.get(APIConstants.KeyManager.USER_ACCESS_TOKEN_EXPIRY_TIME);
if (expiryTimeObject instanceof String) {
if (!APIConstants.KeyManager.NOT_APPLICABLE_VALUE.equals(expiryTimeObject)) {
try {
long expiry = Long.parseLong((String) expiryTimeObject);
if (expiry < 0) {
throw new APIManagementException("Invalid user access token expiry time given for " + oauthClientName, ExceptionCodes.INVALID_APPLICATION_PROPERTIES);
}
clientInfo.setUserAccessTokenLifeTime(expiry);
} catch (NumberFormatException e) {
// No need to throw as its due to not a number sent.
}
}
}
}
if (additionalProperties.containsKey(APIConstants.KeyManager.REFRESH_TOKEN_EXPIRY_TIME)) {
Object expiryTimeObject = additionalProperties.get(APIConstants.KeyManager.REFRESH_TOKEN_EXPIRY_TIME);
if (expiryTimeObject instanceof String) {
if (!APIConstants.KeyManager.NOT_APPLICABLE_VALUE.equals(expiryTimeObject)) {
try {
long expiry = Long.parseLong((String) expiryTimeObject);
clientInfo.setRefreshTokenLifeTime(expiry);
} catch (NumberFormatException e) {
// No need to throw as its due to not a number sent.
}
}
}
}
if (additionalProperties.containsKey(APIConstants.KeyManager.ID_TOKEN_EXPIRY_TIME)) {
Object expiryTimeObject = additionalProperties.get(APIConstants.KeyManager.ID_TOKEN_EXPIRY_TIME);
if (expiryTimeObject instanceof String) {
if (!APIConstants.KeyManager.NOT_APPLICABLE_VALUE.equals(expiryTimeObject)) {
try {
long expiry = Long.parseLong((String) expiryTimeObject);
clientInfo.setIdTokenLifeTime(expiry);
} catch (NumberFormatException e) {
// No need to throw as its due to not a number sent.
}
}
}
}
if (additionalProperties.containsKey(APIConstants.KeyManager.PKCE_MANDATORY)) {
Object pkceMandatoryValue = additionalProperties.get(APIConstants.KeyManager.PKCE_MANDATORY);
if (pkceMandatoryValue instanceof String) {
if (!APIConstants.KeyManager.PKCE_MANDATORY.equals(pkceMandatoryValue)) {
try {
Boolean pkceMandatory = Boolean.parseBoolean((String) pkceMandatoryValue);
clientInfo.setPkceMandatory(pkceMandatory);
} catch (NumberFormatException e) {
// No need to throw as its due to not a number sent.
}
}
}
}
if (additionalProperties.containsKey(APIConstants.KeyManager.PKCE_SUPPORT_PLAIN)) {
Object pkceSupportPlainValue = additionalProperties.get(APIConstants.KeyManager.PKCE_SUPPORT_PLAIN);
if (pkceSupportPlainValue instanceof String) {
if (!APIConstants.KeyManager.PKCE_SUPPORT_PLAIN.equals(pkceSupportPlainValue)) {
try {
Boolean pkceSupportPlain = Boolean.parseBoolean((String) pkceSupportPlainValue);
clientInfo.setPkceSupportPlain(pkceSupportPlain);
} catch (NumberFormatException e) {
// No need to throw as its due to not a number sent.
}
}
}
}
if (additionalProperties.containsKey(APIConstants.KeyManager.BYPASS_CLIENT_CREDENTIALS)) {
Object bypassClientCredentialsValue = additionalProperties.get(APIConstants.KeyManager.BYPASS_CLIENT_CREDENTIALS);
if (bypassClientCredentialsValue instanceof String) {
if (!APIConstants.KeyManager.BYPASS_CLIENT_CREDENTIALS.equals(bypassClientCredentialsValue)) {
try {
Boolean bypassClientCredentials = Boolean.parseBoolean((String) bypassClientCredentialsValue);
clientInfo.setBypassClientCredentials(bypassClientCredentials);
} catch (NumberFormatException e) {
// No need to throw as its due to not a number sent.
}
}
}
}
// Set the display name of the application. This name would appear in the consent page of the app.
clientInfo.setApplicationDisplayName(info.getClientName());
return clientInfo;
}
Aggregations