add vpp user services

otp-db-change
inoshperera 1 year ago
parent 9ccf3a5fa5
commit cc8470d3c0

@ -0,0 +1,76 @@
/*
* 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.application.mgt.common.dto;
public class VppPaginationDTO {
int currentPageIndex;
int size;
String tokenExpirationDate;
int totalPages;
String uId;
String versionId;
public int getCurrentPageIndex() {
return currentPageIndex;
}
public void setCurrentPageIndex(int currentPageIndex) {
this.currentPageIndex = currentPageIndex;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getTokenExpirationDate() {
return tokenExpirationDate;
}
public void setTokenExpirationDate(String tokenExpirationDate) {
this.tokenExpirationDate = tokenExpirationDate;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public String getuId() {
return uId;
}
public void setuId(String uId) {
this.uId = uId;
}
public String getVersionId() {
return versionId;
}
public void setVersionId(String versionId) {
this.versionId = versionId;
}
}

@ -73,4 +73,20 @@ public class VppUserDTO extends VppItuneUserDTO {
this.tmpPassword = tmpPassword; this.tmpPassword = tmpPassword;
} }
@Override
public String toString() {
return "VppUserDTO{" +
"id=" + id +
", dmUsername='" + dmUsername + '\'' +
", tenantId=" + tenantId +
", createdTime='" + createdTime + '\'' +
", lastUpdatedTime='" + lastUpdatedTime + '\'' +
", tmpPassword='" + tmpPassword + '\'' +
", clientUserId='" + clientUserId + '\'' +
", inviteCode='" + inviteCode + '\'' +
", status='" + status + '\'' +
", email='" + email + '\'' +
", managedId='" + managedId + '\'' +
'}';
}
} }

@ -26,9 +26,13 @@ import java.io.IOException;
public interface VPPApplicationManager { public interface VPPApplicationManager {
VppUserDTO getUserByDMUsername() throws ApplicationManagementException;
VppUserDTO addUser(VppUserDTO userDTO) throws ApplicationManagementException; VppUserDTO addUser(VppUserDTO userDTO) throws ApplicationManagementException;
VppUserDTO getUserByDMUsername(String emmUsername) throws ApplicationManagementException;
void updateUser(VppUserDTO userDTO) throws ApplicationManagementException;
void syncUsers(String clientId) throws ApplicationManagementException;
ProxyResponse callVPPBackend(String url, String payload, String accessToken, String method) throws IOException; ProxyResponse callVPPBackend(String url, String payload, String accessToken, String method) throws IOException;
} }

@ -24,17 +24,17 @@ import java.util.List;
public class VppItuneUserRequestWrapper { public class VppItuneUserRequestWrapper {
List<VppItuneUserDTO> user; List<VppItuneUserDTO> users;
public VppItuneUserRequestWrapper() { public VppItuneUserRequestWrapper() {
user = new ArrayList<>(); users = new ArrayList<>();
} }
public List<VppItuneUserDTO> getUser() { public List<VppItuneUserDTO> getUser() {
return user; return users;
} }
public void setUser(List<VppItuneUserDTO> user) { public void setUser(List<VppItuneUserDTO> users) {
this.user = user; this.users = users;
} }
} }

@ -0,0 +1,40 @@
/*
* 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.application.mgt.common.wrapper;
import io.entgra.device.mgt.core.application.mgt.common.dto.VppItuneUserDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.VppPaginationDTO;
import java.util.ArrayList;
import java.util.List;
public class VppItuneUserResponseWrapper extends VppPaginationDTO {
List<VppItuneUserDTO> users;
public VppItuneUserResponseWrapper() {
users = new ArrayList<>();
}
public List<VppItuneUserDTO> getUser() {
return users;
}
public void setUser(List<VppItuneUserDTO> users) {
this.users = users;
}
}

@ -18,13 +18,17 @@
package io.entgra.device.mgt.core.application.mgt.core.impl; package io.entgra.device.mgt.core.application.mgt.core.impl;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.entgra.device.mgt.core.application.mgt.common.dto.ProxyResponse; import io.entgra.device.mgt.core.application.mgt.common.dto.ProxyResponse;
import io.entgra.device.mgt.core.application.mgt.common.dto.VppItuneUserDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.VppItuneUserDTO;
import io.entgra.device.mgt.core.application.mgt.common.wrapper.VppItuneUserRequestWrapper; import io.entgra.device.mgt.core.application.mgt.common.wrapper.VppItuneUserRequestWrapper;
import io.entgra.device.mgt.core.application.mgt.common.dto.VppUserDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.VppUserDTO;
import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException;
import io.entgra.device.mgt.core.application.mgt.common.services.VPPApplicationManager; import io.entgra.device.mgt.core.application.mgt.common.services.VPPApplicationManager;
import io.entgra.device.mgt.core.application.mgt.common.wrapper.VppItuneUserResponseWrapper;
import io.entgra.device.mgt.core.application.mgt.core.dao.ApplicationDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.ApplicationDAO;
import io.entgra.device.mgt.core.application.mgt.core.dao.SPApplicationDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.SPApplicationDAO;
import io.entgra.device.mgt.core.application.mgt.core.dao.VisibilityDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.VisibilityDAO;
@ -42,7 +46,9 @@ import java.io.IOException;
public class VppApplicationManagerImpl implements VPPApplicationManager { public class VppApplicationManagerImpl implements VPPApplicationManager {
private static final String APP_API = "https://vpp.itunes.apple.com/mdm/v2"; private static final String APP_API = "https://vpp.itunes.apple.com/mdm/v2";
private static final String ASSETS = APP_API + "/assets"; private static final String ASSETS = APP_API + "/assets";
private static final String USER_CREATE = APP_API + "users/create"; private static final String USER_CREATE = APP_API + "/users/create";
private static final String USER_UPDATE = APP_API + "/users/update";
private static final String USER_GET = APP_API + "/users";
private static final String TOKEN = ""; private static final String TOKEN = "";
private static final Log log = LogFactory.getLog(VppApplicationManagerImpl.class); private static final Log log = LogFactory.getLog(VppApplicationManagerImpl.class);
@ -62,13 +68,6 @@ public class VppApplicationManagerImpl implements VPPApplicationManager {
this.spApplicationDAO = ApplicationManagementDAOFactory.getSPApplicationDAO(); this.spApplicationDAO = ApplicationManagementDAOFactory.getSPApplicationDAO();
} }
@Override
public VppUserDTO getUserByDMUsername() throws ApplicationManagementException {
// TODO: Return from DAO in a tenanted manner
return null;
}
@Override @Override
public VppUserDTO addUser(VppUserDTO userDTO) throws ApplicationManagementException { public VppUserDTO addUser(VppUserDTO userDTO) throws ApplicationManagementException {
// Call the API to add // Call the API to add
@ -78,23 +77,90 @@ public class VppApplicationManagerImpl implements VPPApplicationManager {
wrapper.getUser().add(ituneUserDTO); wrapper.getUser().add(ituneUserDTO);
Gson gson = new Gson(); Gson gson = new Gson();
// Gson gson = new GsonBuilder()
// .setExclusionStrategies(new NullEmptyExclusionStrategy())
// .create();
String userPayload = gson.toJson(wrapper); String userPayload = gson.toJson(wrapper);
ProxyResponse proxyResponse = callVPPBackend(USER_CREATE, userPayload, TOKEN, Constants.POST); ProxyResponse proxyResponse = callVPPBackend(USER_CREATE, userPayload, TOKEN, Constants.VPP.POST);
if (proxyResponse.getCode() == HttpStatus.SC_OK || proxyResponse.getCode() == HttpStatus.SC_CREATED) { if ((proxyResponse.getCode() == HttpStatus.SC_OK || proxyResponse.getCode() ==
// TODO: Save the user in the DAO HttpStatus.SC_CREATED) && proxyResponse.getData().contains(Constants.VPP.EVENT_ID)) {
// Create user does not return any useful data. Its needed to call the backend again
ProxyResponse getUserResponse = callVPPBackend(USER_GET + Constants.VPP.CLIENT_USER_ID_PARAM +
userDTO.getClientUserId(), userPayload, TOKEN, Constants.VPP.GET);
if ((getUserResponse.getCode() == HttpStatus.SC_OK || getUserResponse.getCode() ==
HttpStatus.SC_CREATED) && getUserResponse.getData().contains(Constants.VPP.TOTAL_PAGES)) {
VppItuneUserResponseWrapper vppItuneUserResponseWrapper = gson.fromJson
(getUserResponse.getData(), VppItuneUserResponseWrapper.class);
userDTO.setInviteCode(vppItuneUserResponseWrapper.getUser().get(0)
.getInviteCode());
userDTO.setStatus(vppItuneUserResponseWrapper.getUser().get(0).getStatus());
log.error("userDTO " + userDTO.toString());
// TODO: Save the userDTO in the DAO
return userDTO;
}
}
} catch (IOException e) {
String msg = "Error while calling VPP backend to add user";
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
}
return null;
}
@Override
public VppUserDTO getUserByDMUsername(String emmUsername) throws ApplicationManagementException {
// TODO: Return from DAO in a tenanted manner
return null;
}
@Override
public void updateUser(VppUserDTO userDTO) throws ApplicationManagementException {
VppItuneUserDTO ituneUserDTO = userDTO;
VppItuneUserRequestWrapper wrapper = new VppItuneUserRequestWrapper();
wrapper.getUser().add(ituneUserDTO);
Gson gson = new Gson();
String userPayload = gson.toJson(wrapper);
try {
ProxyResponse proxyResponse = callVPPBackend(USER_UPDATE, userPayload, TOKEN, Constants.VPP.POST);
if ((proxyResponse.getCode() == HttpStatus.SC_OK || proxyResponse.getCode() ==
HttpStatus.SC_CREATED) && proxyResponse.getData().contains(Constants.VPP.EVENT_ID)) {
log.error("userDTO " + userDTO.toString());
// TODO: Save the userDTO in the DAO
} }
} catch (IOException e) { } catch (IOException e) {
String msg = "Error while callng VPP backend"; String msg = "Error while callng VPP backend to update";
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} }
}
@Override
public void syncUsers(String clientId) throws ApplicationManagementException {
ProxyResponse proxyResponse = null;
try {
proxyResponse = callVPPBackend(USER_GET, null, TOKEN, Constants
.VPP.GET);
if ((proxyResponse.getCode() == HttpStatus.SC_OK || proxyResponse.getCode() ==
HttpStatus.SC_CREATED) && proxyResponse.getData().contains(Constants.VPP.TOTAL_PAGES)) {
log.error("proxyResponse " + proxyResponse.getData());
Gson gson = new Gson();
VppItuneUserResponseWrapper vppUserResponseWrapper = gson.fromJson
(proxyResponse.getData(), VppItuneUserResponseWrapper.class);
}
} catch (IOException e) {
String msg = "Error while syncing VPP users with backend";
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
}
return null;
} }
@Override @Override
@ -104,4 +170,5 @@ public class VppApplicationManagerImpl implements VPPApplicationManager {
String method) throws IOException { String method) throws IOException {
return VppHttpUtil.execute(url, payload, accessToken, method); return VppHttpUtil.execute(url, payload, accessToken, method);
} }
} }

@ -100,17 +100,23 @@ public class Constants {
} }
public static final Map<String, String> AGENT_FILE_NAMES = Collections.unmodifiableMap(AGENT_DATA); public static final Map<String, String> AGENT_FILE_NAMES = Collections.unmodifiableMap(AGENT_DATA);
public static final String BEARER = "Bearer "; public static final class VPP {
public static final String GET = "GET";
public static final String EXECUTOR_EXCEPTION_PREFIX = "ExecutorException-"; public static final String BEARER = "Bearer ";
public static final String TOKEN_IS_EXPIRED = "ACCESS_TOKEN_IS_EXPIRED"; public static final String EXECUTOR_EXCEPTION_PREFIX = "ExecutorException-";
public static final String TOKEN_IS_EXPIRED = "ACCESS_TOKEN_IS_EXPIRED";
public static final int INTERNAL_ERROR_CODE = 500; public static final int INTERNAL_ERROR_CODE = 500;
public static final String POST = "POST";
public static final String GET = "GET"; public static final String PUT = "PUT";
public static final String POST = "POST"; public static final String DELETE = "DELETE";
public static final String PUT = "PUT"; public static final String CLIENT_USER_ID = "clientUserId";
public static final String DELETE = "DELETE"; public static final String EVENT_ID = "eventId";
public static final String CLIENT_USER_ID_PARAM = "?clientUserId=";
public static final String TOTAL_PAGES = "totalPages";
private VPP() {
}
}
/** /**

@ -55,13 +55,13 @@ public class VppHttpUtil {
String method) throws IOException { String method) throws IOException {
HttpRequestBase endpoint = null; HttpRequestBase endpoint = null;
if (Constants.GET.equalsIgnoreCase(method) || Constants.DELETE.equalsIgnoreCase(method)) { if (Constants.VPP.GET.equalsIgnoreCase(method) || Constants.VPP.DELETE.equalsIgnoreCase(method)) {
endpoint = new HttpGet(url); endpoint = new HttpGet(url);
addHeaders(endpoint, accessToken); addHeaders(endpoint, accessToken);
return VppHttpUtil.execute(endpoint); return VppHttpUtil.execute(endpoint);
} else if (Constants.POST.equalsIgnoreCase(method)) { } else if (Constants.VPP.POST.equalsIgnoreCase(method)) {
endpoint = new HttpPost(url); endpoint = new HttpPost(url);
} else if (Constants.PUT.equalsIgnoreCase(method)) { } else if (Constants.VPP.PUT.equalsIgnoreCase(method)) {
endpoint = new HttpPut(url); endpoint = new HttpPut(url);
} }
addHeaders(endpoint, accessToken); addHeaders(endpoint, accessToken);
@ -69,9 +69,9 @@ public class VppHttpUtil {
if (payload != null) { if (payload != null) {
HttpEntity forwardRequestBody = new StringEntity(payload, ContentType.APPLICATION_JSON.toString(), "utf-8"); HttpEntity forwardRequestBody = new StringEntity(payload, ContentType.APPLICATION_JSON.toString(), "utf-8");
if (Constants.POST.equalsIgnoreCase(method)) { if (Constants.VPP.POST.equalsIgnoreCase(method)) {
((HttpPost) endpoint).setEntity(forwardRequestBody); ((HttpPost) endpoint).setEntity(forwardRequestBody);
} else if (Constants.PUT.equalsIgnoreCase(method)) { } else if (Constants.VPP.PUT.equalsIgnoreCase(method)) {
((HttpPut) endpoint).setEntity(forwardRequestBody); ((HttpPut) endpoint).setEntity(forwardRequestBody);
} }
@ -85,7 +85,7 @@ public class VppHttpUtil {
private static void addHeaders(HttpRequestBase endpoint, String accessToken) { private static void addHeaders(HttpRequestBase endpoint, String accessToken) {
endpoint.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()); endpoint.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
endpoint.setHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.toString()); endpoint.setHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.toString());
endpoint.setHeader(HttpHeaders.AUTHORIZATION, Constants.BEARER + accessToken); endpoint.setHeader(HttpHeaders.AUTHORIZATION, Constants.VPP.BEARER + accessToken);
} }
@ -103,9 +103,9 @@ public class VppHttpUtil {
if (response == null) { if (response == null) {
log.error("Received null response for http request : " + httpRequest.getMethod() + " " + httpRequest log.error("Received null response for http request : " + httpRequest.getMethod() + " " + httpRequest
.getURI().toString()); .getURI().toString());
proxyResponse.setCode(Constants.INTERNAL_ERROR_CODE); proxyResponse.setCode(Constants.VPP.INTERNAL_ERROR_CODE);
proxyResponse.setExecutorResponse(Constants.EXECUTOR_EXCEPTION_PREFIX + getStatusKey( proxyResponse.setExecutorResponse(Constants.VPP.EXECUTOR_EXCEPTION_PREFIX + getStatusKey(
Constants.INTERNAL_ERROR_CODE)); Constants.VPP.INTERNAL_ERROR_CODE));
return proxyResponse; return proxyResponse;
} else { } else {
int statusCode = response.getStatusLine().getStatusCode(); int statusCode = response.getStatusLine().getStatusCode();
@ -126,7 +126,7 @@ public class VppHttpUtil {
if (jsonString.contains("Access token expired") || jsonString if (jsonString.contains("Access token expired") || jsonString
.contains("Invalid input. Access token validation failed")) { .contains("Invalid input. Access token validation failed")) {
proxyResponse.setCode(statusCode); proxyResponse.setCode(statusCode);
proxyResponse.setExecutorResponse(Constants.TOKEN_IS_EXPIRED); proxyResponse.setExecutorResponse(Constants.VPP.TOKEN_IS_EXPIRED);
return proxyResponse; return proxyResponse;
} else { } else {
log.error( log.error(
@ -135,7 +135,7 @@ public class VppHttpUtil {
proxyResponse.setCode(statusCode); proxyResponse.setCode(statusCode);
proxyResponse.setData(jsonString); proxyResponse.setData(jsonString);
proxyResponse.setExecutorResponse( proxyResponse.setExecutorResponse(
Constants.EXECUTOR_EXCEPTION_PREFIX + getStatusKey(statusCode)); Constants.VPP.EXECUTOR_EXCEPTION_PREFIX + getStatusKey(statusCode));
return proxyResponse; return proxyResponse;
} }
} }
@ -145,7 +145,7 @@ public class VppHttpUtil {
proxyResponse.setCode(statusCode); proxyResponse.setCode(statusCode);
proxyResponse.setData(jsonString); proxyResponse.setData(jsonString);
proxyResponse proxyResponse
.setExecutorResponse(Constants.EXECUTOR_EXCEPTION_PREFIX + getStatusKey(statusCode)); .setExecutorResponse(Constants.VPP.EXECUTOR_EXCEPTION_PREFIX + getStatusKey(statusCode));
return proxyResponse; return proxyResponse;
} }
} }
@ -205,8 +205,8 @@ public class VppHttpUtil {
if (proxyResponse == null) { if (proxyResponse == null) {
proxyResponse = new ProxyResponse(); proxyResponse = new ProxyResponse();
proxyResponse.setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR); proxyResponse.setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
proxyResponse.setExecutorResponse(Constants.EXECUTOR_EXCEPTION_PREFIX + VppHttpUtil proxyResponse.setExecutorResponse(Constants.VPP.EXECUTOR_EXCEPTION_PREFIX + VppHttpUtil
.getStatusKey(Constants.INTERNAL_ERROR_CODE)); .getStatusKey(Constants.VPP.INTERNAL_ERROR_CODE));
} }
resp.setStatus(proxyResponse.getCode()); resp.setStatus(proxyResponse.getCode());
resp.setContentType(ContentType.APPLICATION_JSON.getMimeType()); resp.setContentType(ContentType.APPLICATION_JSON.getMimeType());
@ -230,7 +230,7 @@ public class VppHttpUtil {
ProxyResponse proxyResponse = new ProxyResponse(); ProxyResponse proxyResponse = new ProxyResponse();
proxyResponse.setCode(errorCode); proxyResponse.setCode(errorCode);
proxyResponse.setExecutorResponse( proxyResponse.setExecutorResponse(
Constants.EXECUTOR_EXCEPTION_PREFIX + VppHttpUtil.getStatusKey(errorCode)); Constants.VPP.EXECUTOR_EXCEPTION_PREFIX + VppHttpUtil.getStatusKey(errorCode));
VppHttpUtil.handleError(resp, proxyResponse); VppHttpUtil.handleError(resp, proxyResponse);
} }

Loading…
Cancel
Save