mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
Add requested changes
This commit is contained in:
parent
40a5b00f80
commit
f21ee97d81
@ -22,10 +22,8 @@ import io.entgra.device.mgt.core.apimgt.application.extension.APIManagementProvi
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.api.util.APIUtil;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.api.util.RegistrationProfile;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.bean.ApiApplicationProfile;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.bean.IdnAuthenticationProfile;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.dto.ApiApplicationKey;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.bean.ApiApplicationKey;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.exception.APIManagerException;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.exception.IdnAuthenticationException;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException;
|
||||
import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException;
|
||||
@ -66,12 +64,6 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi
|
||||
String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm()
|
||||
.getRealmConfiguration().getAdminUserName();
|
||||
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(username);
|
||||
String password = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm()
|
||||
.getRealmConfiguration().getAdminPassword();
|
||||
|
||||
IdnAuthenticationProfile idnAuthenticationProfile = new IdnAuthenticationProfile();
|
||||
idnAuthenticationProfile.setUsername(username);
|
||||
idnAuthenticationProfile.setPassword(password);
|
||||
|
||||
ApiApplicationProfile apiApplicationProfile = new ApiApplicationProfile();
|
||||
apiApplicationProfile.setApplicationName(applicationName);
|
||||
@ -81,8 +73,7 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi
|
||||
|
||||
APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService();
|
||||
ApiApplicationKey apiApplicationKey =
|
||||
apiManagementProviderService.registerApiApplication(idnAuthenticationProfile,
|
||||
apiApplicationProfile);
|
||||
apiManagementProviderService.registerApiApplication(apiApplicationProfile);
|
||||
return Response.status(Response.Status.CREATED).entity(apiApplicationKey.toString()).build();
|
||||
} catch (APIManagerException e) {
|
||||
String msg = "Error occurred while registering an application '" + applicationName + "'";
|
||||
@ -96,7 +87,7 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi
|
||||
String msg = "Failed to retrieve the device service";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||
} catch (IdnAuthenticationException | BadRequestException | UnexpectedResponseException e) {
|
||||
} catch (BadRequestException | UnexpectedResponseException e) {
|
||||
String msg = "Error encountered while registering api application";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||
@ -118,10 +109,6 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi
|
||||
}
|
||||
APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService();
|
||||
|
||||
IdnAuthenticationProfile idnAuthenticationProfile = new IdnAuthenticationProfile();
|
||||
idnAuthenticationProfile.setUsername(registrationProfile.getUsername());
|
||||
idnAuthenticationProfile.setPassword(registrationProfile.getPassword());
|
||||
|
||||
ApiApplicationProfile apiApplicationProfile = new ApiApplicationProfile();
|
||||
apiApplicationProfile.setApplicationName(registrationProfile.getApplicationName());
|
||||
apiApplicationProfile.setTags(registrationProfile.getTags());
|
||||
@ -129,13 +116,8 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi
|
||||
apiApplicationProfile.setGrantTypes(String.join(" ", registrationProfile.getSupportedGrantTypes()));
|
||||
|
||||
ApiApplicationKey apiApplicationKey =
|
||||
apiManagementProviderService.registerApiApplication(idnAuthenticationProfile,
|
||||
apiApplicationProfile);
|
||||
apiManagementProviderService.registerApiApplication(apiApplicationProfile);
|
||||
return Response.status(Response.Status.CREATED).entity(apiApplicationKey).build();
|
||||
} catch (IdnAuthenticationException e) {
|
||||
String msg = "Failed to authenticate the user " + registrationProfile.getUsername();
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.UNAUTHORIZED).entity(msg).build();
|
||||
} catch (BadRequestException e) {
|
||||
String msg =
|
||||
"Received bad request for registering api application " + registrationProfile.getApplicationName();
|
||||
|
||||
@ -38,6 +38,7 @@ import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
|
||||
|
||||
@ -47,7 +48,6 @@ import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
|
||||
public class GsonMessageBodyHandler implements MessageBodyWriter<Object>, MessageBodyReader<Object> {
|
||||
|
||||
private Gson gson;
|
||||
private static final String UTF_8 = "UTF-8";
|
||||
|
||||
public boolean isReadable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
|
||||
return true;
|
||||
@ -65,12 +65,8 @@ public class GsonMessageBodyHandler implements MessageBodyWriter<Object>, Messag
|
||||
MultivaluedMap<String, String> stringStringMultivaluedMap, InputStream entityStream)
|
||||
throws IOException, WebApplicationException {
|
||||
|
||||
InputStreamReader reader = new InputStreamReader(entityStream, "UTF-8");
|
||||
|
||||
try {
|
||||
try (InputStreamReader reader = new InputStreamReader(entityStream, StandardCharsets.UTF_8)) {
|
||||
return getGson().fromJson(reader, type);
|
||||
} finally {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
|
||||
@ -86,11 +82,8 @@ public class GsonMessageBodyHandler implements MessageBodyWriter<Object>, Messag
|
||||
MultivaluedMap<String, Object> stringObjectMultivaluedMap, OutputStream entityStream)
|
||||
throws IOException, WebApplicationException {
|
||||
|
||||
OutputStreamWriter writer = new OutputStreamWriter(entityStream, UTF_8);
|
||||
try {
|
||||
try (OutputStreamWriter writer = new OutputStreamWriter(entityStream, StandardCharsets.UTF_8)) {
|
||||
getGson().toJson(object, type, writer);
|
||||
} finally {
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -37,12 +37,12 @@
|
||||
</servlet-mapping>
|
||||
<context-param>
|
||||
<param-name>doAuthentication</param-name>
|
||||
<param-value>false</param-value>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
<!--This is to support basic auth.-->
|
||||
<context-param>
|
||||
<param-name>basicAuth</param-name>
|
||||
<param-value>false</param-value>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
|
||||
<!--publish to apim-->
|
||||
|
||||
@ -132,7 +132,9 @@
|
||||
<Bundle-Description>API Management Application Bundle</Bundle-Description>
|
||||
<Private-Package>io.entgra.device.mgt.core.apimgt.application.extension.internal</Private-Package>
|
||||
<Import-Packages>
|
||||
com.google.gson.*;version="${google.gson.version}",
|
||||
com.google.gson.Gson;version="${google.gson.version}",
|
||||
com.google.gson.JsonArray;version="${google.gson.version}",
|
||||
com.google.gson.JsonElement;version="${google.gson.version}",
|
||||
io.entgra.device.mgt.core.apimgt.application.extension.bean,
|
||||
io.entgra.device.mgt.core.apimgt.application.extension.dto,
|
||||
io.entgra.device.mgt.core.apimgt.application.extension.exception,
|
||||
|
||||
@ -19,12 +19,10 @@ package io.entgra.device.mgt.core.apimgt.application.extension;
|
||||
|
||||
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.bean.ApiApplicationProfile;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.bean.IdnAuthenticationProfile;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.bean.Token;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.bean.TokenCreationProfile;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.dto.ApiApplicationKey;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.bean.ApiApplicationKey;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.exception.APIManagerException;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.exception.IdnAuthenticationException;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException;
|
||||
|
||||
@ -51,15 +49,20 @@ public interface APIManagementProviderService {
|
||||
|
||||
/**
|
||||
* Register API application base on {@link ApiApplicationProfile}
|
||||
* @param idnAuthenticationProfile Application owner's authentication user details
|
||||
* @param apiApplicationProfile {@link ApiApplicationProfile}
|
||||
* @return {@link ApiApplicationKey} result on a successful execution
|
||||
* @throws IdnAuthenticationException Throws when authentication error occurred
|
||||
* @throws APIManagerException Throws when error encountered while registering the application profile
|
||||
* @throws BadRequestException Throws when the application profile contains invalid attributes
|
||||
* @throws UnexpectedResponseException Throws when unexpected response received from the REST API client
|
||||
*/
|
||||
ApiApplicationKey registerApiApplication(IdnAuthenticationProfile idnAuthenticationProfile,
|
||||
ApiApplicationProfile apiApplicationProfile)
|
||||
throws IdnAuthenticationException, APIManagerException, BadRequestException, UnexpectedResponseException;
|
||||
ApiApplicationKey registerApiApplication(ApiApplicationProfile apiApplicationProfile)
|
||||
throws APIManagerException, BadRequestException, UnexpectedResponseException;
|
||||
|
||||
/**
|
||||
* Generate custom JWT token via extended JWT client
|
||||
* @param tokenCreationProfile {@link TokenCreationProfile}
|
||||
* @return Retrieve {@link Token} result on a successful execution
|
||||
* @throws APIManagerException Throws when error occurred while retrieving the token
|
||||
*/
|
||||
Token getCustomToken(TokenCreationProfile tokenCreationProfile) throws APIManagerException;
|
||||
}
|
||||
|
||||
@ -22,13 +22,11 @@ import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.bean.ApiApplicationProfile;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.bean.IdnAuthenticationProfile;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.bean.Token;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.bean.TokenCreationProfile;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.constants.ApiApplicationConstants;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.dto.ApiApplicationKey;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.bean.ApiApplicationKey;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.exception.APIManagerException;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.exception.IdnAuthenticationException;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.internal.APIApplicationManagerExtensionDataHolder;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.ConsumerRESTAPIServices;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.APIInfo;
|
||||
@ -39,11 +37,14 @@ import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Sub
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.constants.Constants;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.APIServicesException;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.OAuthClientException;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException;
|
||||
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
|
||||
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata;
|
||||
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManagementService;
|
||||
import io.entgra.device.mgt.core.identity.jwt.client.extension.JWTClient;
|
||||
import io.entgra.device.mgt.core.identity.jwt.client.extension.dto.AccessTokenInfo;
|
||||
import io.entgra.device.mgt.core.identity.jwt.client.extension.exception.JWTClientException;
|
||||
import io.entgra.device.mgt.core.identity.jwt.client.extension.service.JWTClientManagerService;
|
||||
import okhttp3.Credentials;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
@ -110,7 +111,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
|
||||
return requestBody;
|
||||
}
|
||||
|
||||
private static ApiApplicationKey registerApiApplication(ApiApplicationProfile apiApplicationProfile)
|
||||
private static ApiApplicationKey createApiApplication(ApiApplicationProfile apiApplicationProfile)
|
||||
throws APIManagerException, BadRequestException, UnexpectedResponseException {
|
||||
if (apiApplicationProfile.getGrantTypes().contains("authorization_code")
|
||||
&& StringUtils.isEmpty(apiApplicationProfile.getCallbackUrl())) {
|
||||
@ -140,7 +141,10 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
|
||||
updateAndRetrieveApplicationKeys(applications.get(0), apiApplicationProfile, apis);
|
||||
|
||||
} catch (APIServicesException e) {
|
||||
throw new RuntimeException(e);
|
||||
String msg =
|
||||
"Error encountered while creating API application : [ " + apiApplicationProfile.getApplicationName() + " ]";
|
||||
log.error(msg, e);
|
||||
throw new APIManagerException(msg, e);
|
||||
}
|
||||
|
||||
}
|
||||
@ -285,9 +289,8 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApiApplicationKey registerApiApplication(IdnAuthenticationProfile idnAuthenticationProfile,
|
||||
ApiApplicationProfile apiApplicationProfile)
|
||||
throws IdnAuthenticationException, APIManagerException, BadRequestException, UnexpectedResponseException {
|
||||
public ApiApplicationKey registerApiApplication(ApiApplicationProfile apiApplicationProfile) throws APIManagerException,
|
||||
BadRequestException, UnexpectedResponseException {
|
||||
String flowStartingDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
|
||||
MetadataManagementService metadataManagementService =
|
||||
APIApplicationManagerExtensionDataHolder.getInstance().getMetadataManagementService();
|
||||
@ -299,8 +302,10 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
|
||||
JsonArray tenants = gson.fromJson(metaData.getMetaValue(), JsonArray.class);
|
||||
|
||||
for (JsonElement tenant : tenants) {
|
||||
if (Objects.equals(tenant.getAsString(), idnAuthenticationProfile.getTenantDomain())) {
|
||||
flowStartingDomain = idnAuthenticationProfile.getTenantDomain();
|
||||
String currentTenantDomain =
|
||||
PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true);
|
||||
if (Objects.equals(tenant.getAsString(), currentTenantDomain)) {
|
||||
flowStartingDomain = currentTenantDomain;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -315,22 +320,41 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
|
||||
try {
|
||||
PrivilegedCarbonContext.startTenantFlow();
|
||||
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(flowStartingDomain, true);
|
||||
if (APIApplicationManagerExtensionDataHolder.getInstance().getIoAuthClientService().
|
||||
doAuthenticate(idnAuthenticationProfile.getUsername(), idnAuthenticationProfile.getPassword())) {
|
||||
apiApplicationProfile.setOwner(idnAuthenticationProfile.getUsername());
|
||||
return registerApiApplication(apiApplicationProfile);
|
||||
}
|
||||
return createApiApplication(apiApplicationProfile);
|
||||
|
||||
throw new IdnAuthenticationException(
|
||||
"Failed to authenticate the user : [ " + idnAuthenticationProfile.getUsername() + " ]");
|
||||
|
||||
} catch (OAuthClientException e) {
|
||||
String msg =
|
||||
"Error encountered while performing authentication for user : [ " + idnAuthenticationProfile.getUsername() + " ]";
|
||||
log.error(msg, e);
|
||||
throw new APIManagerException(msg, e);
|
||||
} finally {
|
||||
PrivilegedCarbonContext.endTenantFlow();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token getCustomToken(TokenCreationProfile tokenCreationProfile) throws APIManagerException {
|
||||
JWTClientManagerService jwtClientManagerService =
|
||||
APIApplicationManagerExtensionDataHolder.getInstance().getJwtClientManagerService();
|
||||
try {
|
||||
JWTClient jwtClient = jwtClientManagerService.getJWTClient();
|
||||
AccessTokenInfo accessTokenInfo = jwtClient.getAccessToken(tokenCreationProfile.getBasicAuthUsername(),
|
||||
tokenCreationProfile.getBasicAuthPassword(), tokenCreationProfile.getUsername(),
|
||||
tokenCreationProfile.getScope());
|
||||
|
||||
if (accessTokenInfo == null) {
|
||||
String msg = "Received a null token when generating a custom JWT token";
|
||||
log.error(msg);
|
||||
throw new APIManagerException(msg);
|
||||
}
|
||||
|
||||
Token token = new Token();
|
||||
token.setAccess_token(accessTokenInfo.getAccessToken());
|
||||
token.setRefresh_token(accessTokenInfo.getRefreshToken());
|
||||
token.setToken_type(accessTokenInfo.getTokenType());
|
||||
token.setScope(accessTokenInfo.getScopes());
|
||||
token.setExpires_in(accessTokenInfo.getExpiresIn());
|
||||
|
||||
return token;
|
||||
} catch (JWTClientException e) {
|
||||
String msg = "Error encountered while acquiring custom JWT token";
|
||||
log.error(msg, e);
|
||||
throw new APIManagerException(msg, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
* Copyright (c) 2018 - 2024, 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
|
||||
@ -14,9 +14,10 @@
|
||||
* 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.application.extension.dto;
|
||||
package io.entgra.device.mgt.core.apimgt.application.extension.bean;
|
||||
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.constants.ApiApplicationConstants;
|
||||
import org.json.simple.JSONObject;
|
||||
@ -1,58 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018 - 2024, 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.application.extension.bean;
|
||||
|
||||
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
|
||||
|
||||
public class IdnAuthenticationProfile {
|
||||
private String username;
|
||||
private String password;
|
||||
private String tenantDomain;
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
String []usernameParts = this.username.split("@(?=[^@]*$)");
|
||||
if (usernameParts.length == 2) {
|
||||
this.tenantDomain = usernameParts[usernameParts.length - 1];
|
||||
return;
|
||||
}
|
||||
this.tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getTenantDomain() {
|
||||
return tenantDomain;
|
||||
}
|
||||
|
||||
public void setTenantDomain(String tenantDomain) {
|
||||
this.tenantDomain = tenantDomain;
|
||||
}
|
||||
}
|
||||
@ -1,26 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018 - 2024, 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.application.extension.exception;
|
||||
|
||||
public class IdnAuthenticationException extends Exception {
|
||||
public IdnAuthenticationException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
}
|
||||
@ -38,14 +38,4 @@ public interface IOAuthClientService {
|
||||
*/
|
||||
OAuthClientResponse execute(Request request) throws OAuthClientException, BadRequestException,
|
||||
UnexpectedResponseException;
|
||||
|
||||
/**
|
||||
* Use to authenticate user against Identify Server
|
||||
*
|
||||
* @param username Username of the user
|
||||
* @param password Password of the User
|
||||
* @return Returns true if the requested user is authenticated user, otherwise false
|
||||
* @throws OAuthClientException Throws when error encountered while authenticating
|
||||
*/
|
||||
boolean doAuthenticate(String username, String password) throws OAuthClientException;
|
||||
}
|
||||
|
||||
@ -146,64 +146,6 @@ public class OAuthClient implements IOAuthClientService {
|
||||
return oAuthClientResponse;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doAuthenticate(String username, String password) throws OAuthClientException {
|
||||
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
|
||||
|
||||
CacheWrapper cacheWrapper = cache.computeIfAbsent(tenantDomain, key -> {
|
||||
CacheWrapper constructedWrapper = null;
|
||||
try {
|
||||
Keys keys = idnDynamicClientRegistration();
|
||||
Tokens tokens = idnTokenGeneration(keys);
|
||||
constructedWrapper = new CacheWrapper(keys, tokens);
|
||||
} catch (Exception e) {
|
||||
log.error("Error encountered while updating the cache", e);
|
||||
}
|
||||
return constructedWrapper;
|
||||
});
|
||||
|
||||
if (cacheWrapper != null) {
|
||||
String tokenRequestJsonStr = (new JSONObject())
|
||||
.put("grant_type", Constants.PASSWORD_GRANT_TYPE)
|
||||
.put("username", username)
|
||||
.put("password", password)
|
||||
.put("scope", Constants.SCOPES)
|
||||
.put("callbackUrl", Constants.PLACEHOLDING_CALLBACK_URL)
|
||||
.toString();
|
||||
|
||||
RequestBody requestBody = RequestBody.Companion.create(tokenRequestJsonStr, JSON);
|
||||
Request tokenRequest = new Request.Builder()
|
||||
.url(tokenEndpoint)
|
||||
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Credentials.basic(cacheWrapper.keys.consumerKey,
|
||||
cacheWrapper.keys.consumerSecret))
|
||||
.post(requestBody)
|
||||
.build();
|
||||
|
||||
try (Response response = client.newCall(tokenRequest).execute()) {
|
||||
if (response.isSuccessful()) {
|
||||
Tokens tokens = mapTokens(response.body());
|
||||
if (tokens.accessToken != null) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.info("IDN authentication success for user : [ " + username + " ]");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
String msg =
|
||||
"Error encountered while performing IDN authentication for received user : [ " + username +
|
||||
" ]";
|
||||
log.error(msg, e);
|
||||
throw new OAuthClientException(msg, e);
|
||||
|
||||
}
|
||||
}
|
||||
if (log.isDebugEnabled()) {
|
||||
log.info("IDN authentication failed for user : [ " + username + " ]");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dynamic client registration will be handled through here. These clients can be located under carbon console's
|
||||
* service provider section in respective tenants.
|
||||
|
||||
@ -67,7 +67,7 @@ import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.ssl.TrustStrategy;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.dto.ApiApplicationKey;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.bean.ApiApplicationKey;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.exception.APIManagerException;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException;
|
||||
|
||||
@ -20,10 +20,8 @@ package io.entgra.device.mgt.core.application.mgt.core.util;
|
||||
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.APIManagementProviderService;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.bean.ApiApplicationProfile;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.bean.IdnAuthenticationProfile;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.dto.ApiApplicationKey;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.bean.ApiApplicationKey;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.exception.APIManagerException;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.exception.IdnAuthenticationException;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException;
|
||||
import io.entgra.device.mgt.core.application.mgt.common.dto.ApiRegistrationProfile;
|
||||
@ -63,23 +61,17 @@ public class OAuthUtils {
|
||||
String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm()
|
||||
.getRealmConfiguration().getAdminUserName();
|
||||
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(username);
|
||||
String password = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm()
|
||||
.getRealmConfiguration().getAdminPassword();
|
||||
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||
APIManagementProviderService apiManagementProviderService = (APIManagementProviderService) ctx.
|
||||
getOSGiService(APIManagementProviderService.class, null);
|
||||
|
||||
IdnAuthenticationProfile idnAuthenticationProfile = new IdnAuthenticationProfile();
|
||||
idnAuthenticationProfile.setUsername(username);
|
||||
idnAuthenticationProfile.setPassword(password);
|
||||
|
||||
ApiApplicationProfile apiApplicationProfile = new ApiApplicationProfile();
|
||||
apiApplicationProfile.setApplicationName(registrationProfile.getApplicationName());
|
||||
apiApplicationProfile.setTags(registrationProfile.getTags());
|
||||
apiApplicationProfile.setGrantTypes("refresh_token client_credentials password");
|
||||
apiApplicationKeyInfo = apiManagementProviderService.
|
||||
registerApiApplication(idnAuthenticationProfile, apiApplicationProfile);
|
||||
} catch (IdnAuthenticationException | BadRequestException | UnexpectedResponseException e) {
|
||||
registerApiApplication(apiApplicationProfile);
|
||||
} catch (BadRequestException | UnexpectedResponseException e) {
|
||||
String msg = "Error encountered while registering api application";
|
||||
log.error(msg);
|
||||
throw new APIManagerException(msg, e);
|
||||
|
||||
@ -21,10 +21,8 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.APIManagementProviderService;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.bean.ApiApplicationProfile;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.bean.IdnAuthenticationProfile;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.dto.ApiApplicationKey;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.bean.ApiApplicationKey;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.exception.APIManagerException;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.exception.IdnAuthenticationException;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.bean.Token;
|
||||
import io.entgra.device.mgt.core.apimgt.application.extension.bean.TokenCreationProfile;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException;
|
||||
@ -956,28 +954,13 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
|
||||
try {
|
||||
ApiApplicationKey apiApplicationKey;
|
||||
try {
|
||||
String adminUserName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm()
|
||||
.getRealmConfiguration().getAdminUserName();
|
||||
String adminPassword = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm()
|
||||
.getRealmConfiguration().getAdminPassword();
|
||||
|
||||
IdnAuthenticationProfile idnAuthenticationProfile = new IdnAuthenticationProfile();
|
||||
idnAuthenticationProfile.setUsername(adminUserName);
|
||||
idnAuthenticationProfile.setPassword(adminPassword);
|
||||
|
||||
ApiApplicationProfile apiApplicationProfile = new ApiApplicationProfile();
|
||||
apiApplicationProfile.setApplicationName(applicationName);
|
||||
apiApplicationProfile.setTags(new String[] {"device_management"});
|
||||
apiApplicationProfile.setGrantTypes("client_credentials password refresh_token");
|
||||
|
||||
apiApplicationKey = apiManagementProviderService.registerApiApplication(idnAuthenticationProfile, apiApplicationProfile);
|
||||
} catch (UserStoreException e) {
|
||||
String msg = "Failed to retrieve the tenant" + tenantDomain + "'";
|
||||
log.error(msg, e);
|
||||
return Response.serverError().entity(
|
||||
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
||||
} catch (IdnAuthenticationException |
|
||||
io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException |
|
||||
apiApplicationKey = apiManagementProviderService.registerApiApplication(apiApplicationProfile);
|
||||
} catch (io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException |
|
||||
UnexpectedResponseException e) {
|
||||
String msg = "Error encountered while registering api application";
|
||||
log.error(msg, e);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user