forked from community/device-mgt-core
parent
5f94113adf
commit
4d06dfd4d4
@ -1,33 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.AccessTokenInfo;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.APIServicesException;
|
||||
|
||||
public interface APIApplicationServices {
|
||||
|
||||
APIApplicationKey createAndRetrieveApplicationCredentials() throws APIServicesException;
|
||||
|
||||
AccessTokenInfo generateAccessTokenFromRegisteredApplication(String clientId, String clientSecret) throws APIServicesException;
|
||||
|
||||
AccessTokenInfo generateAccessTokenFromRefreshToken(String refreshToken, String clientId, String clientSecret) throws APIServicesException;
|
||||
|
||||
}
|
@ -1,149 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.json.JSONObject;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.util.HttpsTrustManagerUtils;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.constants.Constants;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.AccessTokenInfo;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.APIServicesException;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Credentials;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.apimgt.impl.APIConstants;
|
||||
import org.wso2.carbon.apimgt.impl.APIManagerConfiguration;
|
||||
import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class APIApplicationServicesImpl implements APIApplicationServices {
|
||||
|
||||
private static final Log log = LogFactory.getLog(APIApplicationServicesImpl.class);
|
||||
private static final OkHttpClient client = new OkHttpClient(HttpsTrustManagerUtils.getSSLClient().newBuilder());
|
||||
private static final Gson gson = new Gson();
|
||||
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
||||
String msg = null;
|
||||
APIManagerConfiguration config = ServiceReferenceHolder.getInstance().
|
||||
getAPIManagerConfigurationService().getAPIManagerConfiguration();
|
||||
|
||||
@Override
|
||||
public APIApplicationKey createAndRetrieveApplicationCredentials() throws APIServicesException {
|
||||
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
|
||||
String serverUser = getScopePublishUserName(tenantDomain);
|
||||
String serverPassword = getScopePublishUserPassword(tenantDomain);
|
||||
|
||||
String applicationEndpoint = config.getFirstProperty(Constants.DCR_END_POINT);
|
||||
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("callbackUrl", Constants.EMPTY_STRING);
|
||||
jsonObject.put("clientName", Constants.CLIENT_NAME);
|
||||
jsonObject.put("grantType", Constants.GRANT_TYPE);
|
||||
jsonObject.put("owner", serverUser);
|
||||
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(serverUser, serverPassword))
|
||||
.post(requestBody)
|
||||
.build();
|
||||
|
||||
try {
|
||||
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
|
||||
public AccessTokenInfo generateAccessTokenFromRegisteredApplication(String consumerKey, String consumerSecret)
|
||||
throws APIServicesException {
|
||||
|
||||
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
|
||||
String userName = getScopePublishUserName(tenantDomain);
|
||||
String userPassword = getScopePublishUserPassword(tenantDomain);
|
||||
|
||||
JSONObject params = new JSONObject();
|
||||
params.put(Constants.GRANT_TYPE_PARAM_NAME, Constants.PASSWORD_GRANT_TYPE);
|
||||
params.put(Constants.PASSWORD_GRANT_TYPE_USERNAME, userName);
|
||||
params.put(Constants.PASSWORD_GRANT_TYPE_PASSWORD, userPassword);
|
||||
params.put(Constants.SCOPE_PARAM_NAME, Constants.SCOPES);
|
||||
return getToken(params, consumerKey, consumerSecret);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccessTokenInfo generateAccessTokenFromRefreshToken(String refreshToken, String consumerKey,
|
||||
String consumerSecret) throws APIServicesException {
|
||||
JSONObject params = new JSONObject();
|
||||
params.put(Constants.GRANT_TYPE_PARAM_NAME, Constants.REFRESH_TOKEN_GRANT_TYPE);
|
||||
params.put(Constants.REFRESH_TOKEN_GRANT_TYPE_PARAM_NAME, refreshToken);
|
||||
params.put(Constants.SCOPE_PARAM_NAME, Constants.SCOPES);
|
||||
return getToken(params, consumerKey, consumerSecret);
|
||||
}
|
||||
|
||||
public AccessTokenInfo getToken(JSONObject nameValuePairs, String clientId, String clientSecret)
|
||||
throws APIServicesException {
|
||||
|
||||
String tokenEndPoint = config.getFirstProperty(Constants.TOKE_END_POINT);
|
||||
|
||||
RequestBody requestBody = RequestBody.Companion.create(nameValuePairs.toString(), JSON);
|
||||
Request request = new Request.Builder()
|
||||
.url(tokenEndPoint)
|
||||
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Credentials.basic(clientId, clientSecret))
|
||||
.post(requestBody)
|
||||
.build();
|
||||
|
||||
try {
|
||||
Response response = client.newCall(request).execute();
|
||||
return gson.fromJson(response.body().string(), AccessTokenInfo.class);
|
||||
} catch (IOException e) {
|
||||
msg = "Error occurred while processing the response";
|
||||
log.error(msg, e);
|
||||
throw new APIServicesException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private String getScopePublishUserName(String tenantDomain) {
|
||||
if(APIConstants.SUPER_TENANT_DOMAIN.equals(tenantDomain)) {
|
||||
return config.getFirstProperty(Constants.SERVER_USER);
|
||||
} else {
|
||||
return Constants.SCOPE_PUBLISH_RESERVED_USER_NAME + "@" + tenantDomain;
|
||||
}
|
||||
}
|
||||
|
||||
private String getScopePublishUserPassword(String tenantDomain) {
|
||||
if(APIConstants.SUPER_TENANT_DOMAIN.equals(tenantDomain)) {
|
||||
return config.getFirstProperty(Constants.SERVER_PASSWORD);
|
||||
} else {
|
||||
return Constants.SCOPE_PUBLISH_RESERVED_USER_PASSWORD;
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* 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.extension.rest.api.bean;
|
||||
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.AccessTokenInfo;
|
||||
|
||||
public class PublisherOauthApp {
|
||||
private final APIApplicationKey apiApplicationKey;
|
||||
private final AccessTokenInfo accessTokenInfo;
|
||||
|
||||
public PublisherOauthApp(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo) {
|
||||
this.apiApplicationKey = apiApplicationKey;
|
||||
this.accessTokenInfo = accessTokenInfo;
|
||||
}
|
||||
|
||||
public AccessTokenInfo getAccessTokenInfo() {
|
||||
return accessTokenInfo;
|
||||
}
|
||||
|
||||
public APIApplicationKey getApiApplicationKey() {
|
||||
return apiApplicationKey;
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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.extension.rest.api.bean;
|
||||
|
||||
public class PublisherRESTAPIClientResponse {
|
||||
private final int code;
|
||||
private final String body;
|
||||
private final boolean isSuccessful;
|
||||
|
||||
public PublisherRESTAPIClientResponse(int code, String body, boolean isSuccessful) {
|
||||
this.code = code;
|
||||
this.body = body;
|
||||
this.isSuccessful = isSuccessful;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getBody() {
|
||||
return body;
|
||||
}
|
||||
|
||||
public boolean isSuccessful() {
|
||||
return isSuccessful;
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* 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.extension.rest.api.exceptions;
|
||||
|
||||
public class PublisherRESTAPIOauthClientException extends Exception {
|
||||
private static final long serialVersionUID = -896103750774855894L;
|
||||
|
||||
public PublisherRESTAPIOauthClientException(String errorMessage) {
|
||||
super(errorMessage);
|
||||
}
|
||||
|
||||
public PublisherRESTAPIOauthClientException(String errorMessage, Throwable t) {
|
||||
super(errorMessage, t);
|
||||
}
|
||||
}
|
@ -0,0 +1,203 @@
|
||||
/*
|
||||
* 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.extension.rest.api.util;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.PublisherOauthApp;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.PublisherRESTAPIClientResponse;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.constants.Constants;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.AccessTokenInfo;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.PublisherRESTAPIOauthClientException;
|
||||
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException;
|
||||
import okhttp3.*;
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONObject;
|
||||
import org.wso2.carbon.apimgt.impl.APIConstants;
|
||||
import org.wso2.carbon.apimgt.impl.APIManagerConfiguration;
|
||||
import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class PublisherRESTAPIOauthClient {
|
||||
private static final Log log = LogFactory.getLog(PublisherRESTAPIOauthClient.class);
|
||||
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
||||
private static final Gson gson = new Gson();
|
||||
private static final OkHttpClient client = new OkHttpClient(HttpsTrustManagerUtils.getSSLClient().newBuilder());
|
||||
private static final Map<String, PublisherOauthApp> publisherOauthAppCache = new ConcurrentHashMap<>();
|
||||
private static final APIManagerConfiguration config = ServiceReferenceHolder.getInstance().
|
||||
getAPIManagerConfigurationService().getAPIManagerConfiguration();
|
||||
|
||||
public static PublisherRESTAPIClientResponse execute(Request request) throws PublisherRESTAPIOauthClientException
|
||||
, BadRequestException,
|
||||
UnexpectedResponseException {
|
||||
try {
|
||||
request = addAuthorizationHeader(request);
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
if (response.isSuccessful()) {
|
||||
return map(response);
|
||||
}
|
||||
|
||||
if (response.code() == HttpStatus.SC_UNAUTHORIZED) {
|
||||
updateCacheWithNewToken();
|
||||
return execute(addAuthorizationHeader(request));
|
||||
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
||||
String msg = "Encountered a bad request! Request failed with code : [ " + response.code() + " ] &" +
|
||||
" body : [ " + (response.body() != null ?
|
||||
response.body().string() : " empty body received!") + " ]";
|
||||
log.error(msg);
|
||||
throw new BadRequestException(msg);
|
||||
} else {
|
||||
String msg =
|
||||
"Request failed with code : [ " + response.code() + " ] & body : [ " +
|
||||
(response.body() != null ?
|
||||
response.body().string() : " empty body received!") + " ]";
|
||||
log.error(msg);
|
||||
throw new UnexpectedResponseException(msg);
|
||||
}
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
String msg = "Error occurred while executing the request : [ " + request.method() + ":" + request.url() +
|
||||
" ]";
|
||||
throw new PublisherRESTAPIOauthClientException(msg, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static APIApplicationKey createOauthApplication() throws IOException {
|
||||
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
|
||||
String serverUser = getScopePublishUserName(tenantDomain);
|
||||
String serverPassword = getScopePublishUserPassword(tenantDomain);
|
||||
String applicationEndpoint = config.getFirstProperty(Constants.DCR_END_POINT);
|
||||
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("callbackUrl", Constants.EMPTY_STRING);
|
||||
jsonObject.put("clientName", Constants.CLIENT_NAME + "_for_" + tenantDomain);
|
||||
jsonObject.put("grantType", Constants.GRANT_TYPE);
|
||||
jsonObject.put("owner", serverUser);
|
||||
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(serverUser, serverPassword))
|
||||
.post(requestBody)
|
||||
.build();
|
||||
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
return gson.fromJson(response.body() != null ? response.body().string() : null, APIApplicationKey.class);
|
||||
}
|
||||
}
|
||||
|
||||
private static AccessTokenInfo getAccessToken() throws IOException {
|
||||
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
|
||||
APIApplicationKey apiApplicationKey = createOauthApplication();
|
||||
String userName = getScopePublishUserName(tenantDomain);
|
||||
String userPassword = getScopePublishUserPassword(tenantDomain);
|
||||
|
||||
JSONObject params = new JSONObject();
|
||||
params.put(Constants.GRANT_TYPE_PARAM_NAME, Constants.PASSWORD_GRANT_TYPE);
|
||||
params.put(Constants.PASSWORD_GRANT_TYPE_USERNAME, userName);
|
||||
params.put(Constants.PASSWORD_GRANT_TYPE_PASSWORD, userPassword);
|
||||
params.put(Constants.SCOPE_PARAM_NAME, Constants.SCOPES);
|
||||
return getToken(params, apiApplicationKey);
|
||||
}
|
||||
|
||||
public static void updateCacheWithNewToken() throws IOException {
|
||||
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
|
||||
publisherOauthAppCache.computeIfPresent(tenantDomain, (key, value) -> {
|
||||
PublisherOauthApp publisherOauthApp = value;
|
||||
try {
|
||||
APIApplicationKey apiApplicationKey = value.getApiApplicationKey();
|
||||
JSONObject params = new JSONObject();
|
||||
params.put(Constants.GRANT_TYPE_PARAM_NAME, Constants.REFRESH_TOKEN_GRANT_TYPE);
|
||||
params.put(Constants.REFRESH_TOKEN_GRANT_TYPE_PARAM_NAME,
|
||||
value.getAccessTokenInfo().getRefresh_token());
|
||||
params.put(Constants.SCOPE_PARAM_NAME, Constants.SCOPES);
|
||||
AccessTokenInfo accessTokenInfo = getToken(params, apiApplicationKey);
|
||||
publisherOauthApp = new PublisherOauthApp(apiApplicationKey, accessTokenInfo);
|
||||
} catch (IOException e) {
|
||||
log.error("Error encountered while creating publisher OAuth application", e);
|
||||
}
|
||||
return publisherOauthApp;
|
||||
});
|
||||
}
|
||||
|
||||
private static AccessTokenInfo getToken(JSONObject params, APIApplicationKey apiApplicationKey) throws IOException {
|
||||
String tokenEndPoint = config.getFirstProperty(Constants.TOKE_END_POINT);
|
||||
RequestBody requestBody = RequestBody.Companion.create(params.toString(), JSON);
|
||||
Request request = new Request.Builder()
|
||||
.url(tokenEndPoint)
|
||||
.addHeader(Constants.AUTHORIZATION_HEADER_NAME,
|
||||
Credentials.basic(apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()))
|
||||
.post(requestBody)
|
||||
.build();
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
return gson.fromJson(response.body() != null ? response.body().string() : null, AccessTokenInfo.class);
|
||||
}
|
||||
}
|
||||
|
||||
private static PublisherOauthApp getOauthApp() {
|
||||
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
|
||||
return publisherOauthAppCache.computeIfAbsent(tenantDomain, key -> {
|
||||
PublisherOauthApp publisherOauthApp = null;
|
||||
try {
|
||||
APIApplicationKey apiApplicationKey = createOauthApplication();
|
||||
AccessTokenInfo accessTokenInfo = getAccessToken();
|
||||
publisherOauthApp = new PublisherOauthApp(apiApplicationKey, accessTokenInfo);
|
||||
} catch (IOException e) {
|
||||
log.error("Error encountered while creating publisher OAuth application", e);
|
||||
}
|
||||
return publisherOauthApp;
|
||||
});
|
||||
}
|
||||
|
||||
private static Request addAuthorizationHeader(Request request) throws IOException {
|
||||
return request.newBuilder().addHeader(Constants.AUTHORIZATION_HEADER_NAME,
|
||||
Constants.AUTHORIZATION_HEADER_PREFIX_BEARER + getOauthApp().getAccessTokenInfo().
|
||||
getAccess_token()).build();
|
||||
}
|
||||
|
||||
private static String getScopePublishUserName(String tenantDomain) {
|
||||
if (APIConstants.SUPER_TENANT_DOMAIN.equals(tenantDomain)) {
|
||||
return config.getFirstProperty(Constants.SERVER_USER);
|
||||
} else {
|
||||
return Constants.SCOPE_PUBLISH_RESERVED_USER_NAME + "@" + tenantDomain;
|
||||
}
|
||||
}
|
||||
|
||||
private static String getScopePublishUserPassword(String tenantDomain) {
|
||||
if (APIConstants.SUPER_TENANT_DOMAIN.equals(tenantDomain)) {
|
||||
return config.getFirstProperty(Constants.SERVER_PASSWORD);
|
||||
} else {
|
||||
return Constants.SCOPE_PUBLISH_RESERVED_USER_PASSWORD;
|
||||
}
|
||||
}
|
||||
|
||||
private static PublisherRESTAPIClientResponse map(Response response) throws IOException {
|
||||
return new PublisherRESTAPIClientResponse(response.code(),
|
||||
response.body() != null ? response.body().string() : null, response.isSuccessful());
|
||||
}
|
||||
}
|
Loading…
Reference in new issue