Create methods for consumer api app creation and get details of app

add_scope_mdm
Pasindu Rupasinghe 1 year ago
parent a513c13037
commit a8d45ee41a

@ -26,6 +26,12 @@ public interface APIApplicationServices {
APIApplicationKey createAndRetrieveApplicationCredentials() throws APIServicesException; APIApplicationKey createAndRetrieveApplicationCredentials() throws APIServicesException;
APIApplicationKey generateAndRetrieveApplicationKeys(String applicationName, String tags[],
String keyType, String username,
boolean isAllowedAllDomains,
String validityTime, String password)
throws APIServicesException;
AccessTokenInfo generateAccessTokenFromRegisteredApplication(String clientId, String clientSecret) throws APIServicesException; AccessTokenInfo generateAccessTokenFromRegisteredApplication(String clientId, String clientSecret) throws APIServicesException;
AccessTokenInfo generateAccessTokenFromRefreshToken(String refreshToken, String clientId, String clientSecret) throws APIServicesException; AccessTokenInfo generateAccessTokenFromRefreshToken(String refreshToken, String clientId, String clientSecret) throws APIServicesException;

@ -35,6 +35,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.apimgt.impl.APIManagerConfiguration; import org.wso2.carbon.apimgt.impl.APIManagerConfiguration;
import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder; import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder;
import java.io.IOException; import java.io.IOException;
public class APIApplicationServicesImpl implements APIApplicationServices { public class APIApplicationServicesImpl implements APIApplicationServices {
@ -78,6 +79,38 @@ public class APIApplicationServicesImpl implements APIApplicationServices {
} }
} }
@Override
public APIApplicationKey generateAndRetrieveApplicationKeys(String applicationName, String tags[],
String keyType, String username,
boolean isAllowedAllDomains,
String validityTime, String password)
throws APIServicesException {
String applicationEndpoint = config.getFirstProperty(Constants.DCR_END_POINT);
JSONObject jsonObject = new JSONObject();
jsonObject.put("callbackUrl", Constants.EMPTY_STRING);
jsonObject.put("clientName", username);
jsonObject.put("grantType", Constants.GRANT_TYPE);
jsonObject.put("owner", username);
jsonObject.put("saasApp", true);
RequestBody requestBody = RequestBody.Companion.create(jsonObject.toString(), JSON);
Request request = new Request.Builder()
.url(applicationEndpoint)
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Credentials.basic(username, password))
.post(requestBody)
.build();
try {
Response response = client.newCall(request).execute();
return gson.fromJson(response.body().string(), APIApplicationKey.class);
} catch (IOException e) {
msg = "Error occurred while processing the response";
log.error(msg, e);
throw new APIServicesException(e);
}
}
@Override @Override
public AccessTokenInfo generateAccessTokenFromRegisteredApplication(String consumerKey, String consumerSecret) public AccessTokenInfo generateAccessTokenFromRegisteredApplication(String consumerKey, String consumerSecret)
throws APIServicesException { throws APIServicesException {

@ -36,6 +36,10 @@ public interface ConsumerRESTAPIServices {
Application[] getAllApplications(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String appName) Application[] getAllApplications(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String appName)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;
Application getDetailsOfAnApplication(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo,
String applicationId)
throws APIServicesException, BadRequestException, UnexpectedResponseException;
Application createApplication(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, Application createApplication(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo,
Application application) Application application)
throws APIServicesException, BadRequestException, UnexpectedResponseException; throws APIServicesException, BadRequestException, UnexpectedResponseException;

@ -92,6 +92,45 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
} }
} }
@Override
public Application getDetailsOfAnApplication(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo,
String applicationId)
throws APIServicesException, BadRequestException, UnexpectedResponseException {
String getAllApplicationsUrl = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH + applicationId;
Request request = new Request.Builder()
.url(getAllApplicationsUrl)
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
+ accessTokenInfo.getAccess_token())
.get()
.build();
try {
Response response = client.newCall(request).execute();
if (HttpStatus.SC_OK == response.code()) {
return gson.fromJson(response.body().string(), Application.class);
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(),
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
//TODO: max attempt count
return getDetailsOfAnApplication(apiApplicationKey, refreshedAccessToken, applicationId);
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
String msg = "Bad Request, Invalid request";
log.error(msg);
throw new BadRequestException(msg);
} else {
String msg = "Response : " + response.code() + response.body();
throw new UnexpectedResponseException(msg);
}
} catch (IOException e) {
String msg = "Error occurred while processing the response";
log.error(msg, e);
throw new APIServicesException(msg, e);
}
}
@Override @Override
public Application createApplication(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, public Application createApplication(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo,
Application application) Application application)

@ -37,7 +37,7 @@ public class Application {
private int subscriptionCount; private int subscriptionCount;
private List<String> keys; private List<String> keys;
private JSONObject attributes; private JSONObject attributes;
private List<String> subscriptionScopes; private List<Scopes> subscriptionScopes;
private String owner; private String owner;
private boolean hashEnabled; private boolean hashEnabled;
@ -121,11 +121,11 @@ public class Application {
this.attributes = attributes; this.attributes = attributes;
} }
public List<String> getSubscriptionScopes() { public List<Scopes> getSubscriptionScopes() {
return subscriptionScopes; return subscriptionScopes;
} }
public void setSubscriptionScopes(List<String> subscriptionScopes) { public void setSubscriptionScopes(List<Scopes> subscriptionScopes) {
this.subscriptionScopes = subscriptionScopes; this.subscriptionScopes = subscriptionScopes;
} }

@ -0,0 +1,65 @@
/*
* Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer;
import java.util.List;
/**
* This class represents the scope data.
*/
public class Scopes {
private String key;
private String name;
private List<String> roles;
private String description;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getRoles() {
return roles;
}
public void setRoles(List<String> roles) {
this.roles = roles;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Loading…
Cancel
Save