Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES-836 : Get oauth client endpoint should return client name language map. #901

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.*;

import com.fasterxml.jackson.databind.JsonNode;
import io.mosip.kernel.core.authmanager.authadapter.model.AuthUserDetails;
import io.mosip.pms.common.entity.*;
import io.mosip.pms.common.entity.ClientDetail;
Expand Down Expand Up @@ -74,6 +75,8 @@ public class ClientManagementServiceImpl implements ClientManagementService {
public static final String ACTIVE = "ACTIVE";
public static final String BLANK_STRING = "";
public static final String VERSION = "1.0";
public static final String NONE_LANG_KEY = "@none";
public static final String ENG_KEY = "eng";

@Value("${mosip.pms.api.id.oauth.clients.get}")
private String getClientsId;
Expand Down Expand Up @@ -123,19 +126,24 @@ public ClientDetailResponse createOIDCClient(ClientDetailCreateRequest createReq
response.setStatus(clientDetail.getStatus());
return response;
}

@Override
public ClientDetailResponse createOAuthClient(ClientDetailCreateRequestV2 createRequest) throws Exception {
ProcessedClientDetail processedClientDetail = processCreateOIDCClient(createRequest);
ClientDetail clientDetail = processedClientDetail.getClientDetail();
callEsignetService(clientDetail, environment.getProperty("mosip.pms.esignet.oauth-client-create-url"), true, createRequest.getClientNameLangMap());
String clientName=getClientNameLanguageMapAsJsonString(
createRequest.getClientNameLangMap(),
createRequest.getName()
);
clientDetail.setName(clientName);
publishClientData(processedClientDetail.getPartner(), processedClientDetail.getPolicy(), clientDetail);
clientDetailRepository.save(clientDetail);
var response = new ClientDetailResponse();
response.setClientId(clientDetail.getId());
response.setStatus(clientDetail.getStatus());
response.setStatus(clientDetail.getStatus());
return response;

}

public ProcessedClientDetail processCreateOIDCClient(ClientDetailCreateRequest createRequest) throws NoSuchAlgorithmException {
Expand Down Expand Up @@ -448,22 +456,27 @@ public ClientDetailResponse updateOIDCClient(String clientId, ClientDetailUpdate
response.setStatus(clientDetail.getStatus());
notify(MapperUtils.mapClientDataToPublishDto(clientDetail), EventType.OIDC_CLIENT_UPDATED);
return response;
}
}


@Override
public ClientDetailResponse updateOAuthClient(String clientId, ClientDetailUpdateRequestV2 updateRequest)
throws Exception {

ClientDetail clientDetail = processUpdateOIDCClient(clientId,updateRequest);
makeUpdateEsignetServiceCall(clientDetail, environment.getProperty("mosip.pms.esignet.oauth-client-update-url"), true, updateRequest.getClientNameLangMap());
String clientName=getClientNameLanguageMapAsJsonString(
updateRequest.getClientNameLangMap(),
updateRequest.getClientName()
);
clientDetail.setName(clientName);
clientDetail = clientDetailRepository.save(clientDetail);
var response = new ClientDetailResponse();
response.setClientId(clientDetail.getId());
response.setStatus(clientDetail.getStatus());
notify(MapperUtils.mapClientDataToPublishDto(clientDetail), EventType.OIDC_CLIENT_UPDATED);
return response;
}
}



Expand Down Expand Up @@ -615,6 +628,12 @@ public io.mosip.pms.oauth.client.dto.ClientDetail getClientDetails(String client
return dto;
}

private String getClientNameLanguageMapAsJsonString(Map<String, String> clientNameMap, String clientName) {
clientNameMap.put(NONE_LANG_KEY, clientName);
JSONObject clientNameObject = new JSONObject(clientNameMap);
return clientNameObject.toString();
}

@Override
public ResponseWrapperV2<List<OauthClientDto>> getClients() {
ResponseWrapperV2<List<OauthClientDto>> responseWrapper = new ResponseWrapperV2<>();
Expand Down Expand Up @@ -648,7 +667,7 @@ public ResponseWrapperV2<List<OauthClientDto>> getClients() {
oauthClientDto.setPartnerId(partnerId);
oauthClientDto.setUserId(userId);
oauthClientDto.setClientId(clientDetail.getId());
oauthClientDto.setClientName(clientDetail.getName());
oauthClientDto.setClientName(getClientName(clientDetail.getName()));
oauthClientDto.setPolicyGroupId(policyGroup.getId());
oauthClientDto.setPolicyGroupName(policyGroup.getName());
oauthClientDto.setPolicyGroupDescription(policyGroup.getDesc());
Expand Down Expand Up @@ -694,6 +713,29 @@ private String getUserId() {
private AuthUserDetails authUserDetails() {
return (AuthUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}

private String getClientName(String jsonString) {
try {
JsonNode jsonNode = objectMapper.readTree(jsonString);

JsonNode engNode = jsonNode.get(ENG_KEY);
if (engNode != null && engNode.isTextual()) {
return engNode.asText();
}

JsonNode noneNode = jsonNode.get(NONE_LANG_KEY);
if (noneNode != null && noneNode.isTextual()) {
return noneNode.asText();
}

// If neither "eng" nor "@none" is present, return the original string
return jsonString;
} catch (JsonProcessingException e) {
// If the string is not a valid JSON, return it as is
return jsonString;
}
}

/**
*
* @param commaSeparatedString
Expand Down
Loading