Skip to content

Commit

Permalink
ES-836 : Get oauth client endpoint should return client name language…
Browse files Browse the repository at this point in the history
… map. (#901)

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

Signed-off-by: sudeep <[email protected]>

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

Signed-off-by: sudeep <[email protected]>

---------

Signed-off-by: sudeep <[email protected]>
  • Loading branch information
Sudeep7353 authored Oct 3, 2024
1 parent 3dd35ab commit 5cd3a91
Showing 1 changed file with 51 additions and 9 deletions.
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

0 comments on commit 5cd3a91

Please sign in to comment.