88import io .mosip .esignet .core .util .IdentityProviderUtil ;
99import lombok .extern .slf4j .Slf4j ;
1010import org .springframework .beans .factory .annotation .Autowired ;
11- import org .springframework .beans .factory .annotation .Qualifier ;
1211import org .springframework .beans .factory .annotation .Value ;
1312import org .springframework .core .ParameterizedTypeReference ;
14- import org .springframework .core .env .Environment ;
15- import org .springframework .http .HttpEntity ;
16- import org .springframework .http .HttpMethod ;
17- import org .springframework .http .ResponseEntity ;
18- import org .springframework .scheduling .annotation .Async ;
13+ import org .springframework .http .*;
1914import org .springframework .stereotype .Component ;
2015import org .springframework .util .CollectionUtils ;
16+ import org .springframework .util .LinkedMultiValueMap ;
17+ import org .springframework .util .MultiValueMap ;
2118import org .springframework .util .StringUtils ;
2219import org .springframework .web .client .RestClientException ;
2320import org .springframework .web .client .RestTemplate ;
21+ import org .springframework .web .multipart .MultipartFile ;
22+
2423
25- import java .util .Base64 ;
26- import java .util .List ;
2724import java .util .Map ;
2825
2926@ Component
@@ -37,35 +34,66 @@ public class IdentityAPIClient {
3734 private String userInfoUrl ;
3835
3936 @ Autowired
40- @ Qualifier ("selfTokenRestTemplate" )
41- private RestTemplate selfTokenRestTemplate ;
42-
43- @ Autowired
44- private Environment environment ;
37+ private RestTemplate restTemplate ;
4538
4639 @ Value ("${mosip.esignet.send-notification.endpoint}" )
4740 private String sendNotificationEndpoint ;
4841
49- @ Value ("{${mosip.esignet.default-language}" )
50- private String defaultLanguage ;
42+ @ Value ("${mosip.esignet.client.secret}" )
43+ private String clientSecret ;
44+
45+ @ Value ("${mosip.compass.client.secret}" )
46+ private String compassClientSecret ;
5147
52- @ Value ("#{ ${mosip.esignet.sms-notification-template.encoded-langcodes} }" )
53- private List < String > encodedLangCodes ;
48+ @ Value ("${mosip.esignet.get-auth.endpoint }" )
49+ private String getAuthTokenEndpoint ;
5450
55- @ Value ("${mosip.signup.identifier.prefix:}" )
56- private String identifierPrefix ;
51+ public String getAuthToken (String client_id ,String client_secret ,String grant_type )
52+ {
53+ HttpHeaders headers = new HttpHeaders ();
54+ headers .setContentType (MediaType .APPLICATION_FORM_URLENCODED );
55+ MultiValueMap <String , String > body = new LinkedMultiValueMap <>();
56+ body .add ("client_id" , client_id );
57+ body .add ("client_secret" , client_secret );
58+ body .add ("grant_type" , grant_type );
59+
60+ HttpEntity <MultiValueMap <String , String >> requestEntity = new HttpEntity <>(body , headers );
61+
62+ try {
63+ ResponseEntity <Map > response = restTemplate .postForEntity (getAuthTokenEndpoint , requestEntity , Map .class );
64+ if (response .getStatusCode ().is2xxSuccessful ()) {
65+ Map <String , Object > responseBody = response .getBody ();
66+ if (responseBody != null && responseBody .containsKey ("access_token" )) {
67+ return responseBody .get ("access_token" ).toString ();
68+ }
69+ }
70+ } catch (Exception e ) {
71+ throw new EsignetException (e .getMessage ());
72+ }
73+ throw new EsignetException ("Error fetching auth token" );
74+ }
5775
5876 public String generateOTPChallenge (String challengeTransactionId ) throws SendOtpException {
5977 OtpRequest otpRequest = new OtpRequest ();
6078 otpRequest .setKey (challengeTransactionId );
6179 RequestWrapper <OtpRequest > restRequestWrapper = new RequestWrapper <>();
6280 restRequestWrapper .setRequestTime (IdentityProviderUtil .getUTCDateTime ());
6381 restRequestWrapper .setRequest (otpRequest );
82+ String token = getAuthToken ("mosip-signup-client" , clientSecret , "client_credentials" );
83+ if (token == null || token .isEmpty ()) {
84+ throw new SendOtpException ("Token retrieval failed" );
85+ }
86+
87+ HttpHeaders headers = new HttpHeaders ();
88+ headers .setContentType (MediaType .APPLICATION_JSON );
89+ headers .set ("Cookie" , "Authorization=" +token );
90+
91+ HttpEntity <RequestWrapper <OtpRequest >> entity = new HttpEntity <>(restRequestWrapper , headers );
6492
6593 try {
66- ResponseWrapper <OtpResponse > responseWrapper = selfTokenRestTemplate
94+ ResponseWrapper <OtpResponse > responseWrapper = restTemplate
6795 .exchange (generateChallengeUrl , HttpMethod .POST ,
68- new HttpEntity <>( restRequestWrapper ) ,
96+ entity ,
6997 new ParameterizedTypeReference <ResponseWrapper <OtpResponse >>() {
7098 })
7199 .getBody ();
@@ -86,48 +114,56 @@ public String generateOTPChallenge(String challengeTransactionId) throws SendOtp
86114 }
87115
88116
89- public void sendSMSNotification
90- (String number , String locale , String templateKey , Map <String , String > params ) throws SendOtpException {
91-
92- locale = locale != null ? locale : defaultLanguage ;
117+ public void sendSMSNotification (String [] mailTo ,
118+ String [] mailCc ,
119+ String [] mailSubject ,
120+ String [] mailContent ,
121+ MultipartFile [] attachments ) throws SendOtpException {
93122
94- String message = encodedLangCodes .contains (locale )?
95- new String (Base64 .getDecoder ().decode (environment .getProperty (templateKey + "." + locale ))):
96- environment .getProperty (templateKey + "." + locale );
97-
98- if (params != null && message != null ) {
99- for (Map .Entry <String , String > entry : params .entrySet ()) {
100- message = message .replace (entry .getKey (), entry .getValue ());
101- }
102- }
103-
104- NotificationRequest notificationRequest = new NotificationRequest (number .substring (identifierPrefix .length ()), message );
123+ NotificationRequest notificationRequest = new NotificationRequest (mailTo , mailCc , mailSubject , mailContent , attachments );
105124
106125 RequestWrapper <NotificationRequest > restRequestWrapper = new RequestWrapper <>();
107126 restRequestWrapper .setRequestTime (IdentityProviderUtil .getUTCDateTime ());
108127 restRequestWrapper .setRequest (notificationRequest );
128+ String token = getAuthToken ("mosip-signup-client" , clientSecret , "client_credentials" );
129+ if (token == null || token .isEmpty ()) {
130+ throw new SendOtpException ("Token retrieval failed" );
131+ }
132+
133+ HttpHeaders headers = new HttpHeaders ();
134+ headers .setContentType (MediaType .APPLICATION_JSON );
135+ headers .set ("Cookie" , "Authorization=" +token );
136+
137+ HttpEntity <RequestWrapper <NotificationRequest >> entity = new HttpEntity <>(restRequestWrapper , headers );
109138
110139 try {
111- ResponseWrapper <NotificationResponse > responseWrapper = selfTokenRestTemplate .exchange (sendNotificationEndpoint ,
140+ ResponseWrapper <NotificationResponse > responseWrapper = restTemplate .exchange (sendNotificationEndpoint ,
112141 HttpMethod .POST ,
113- new HttpEntity <>( restRequestWrapper ) ,
142+ entity ,
114143 new ParameterizedTypeReference <ResponseWrapper <NotificationResponse >>(){}).getBody ();
115144 log .debug ("Notification response -> {}" , responseWrapper );
116145 } catch (RestClientException e ){
117146 throw new SendOtpException ("otp_notification_failed" );
118147 }
119148 }
120149
121- @ Async
122- public void sendSMSNotificationAsync
123- (String number , String locale , String templateKey , Map <String , String > params ) throws SendOtpException {
124- sendSMSNotification (number , locale , templateKey , params );
125- }
126-
127150 public UserInfo getUserInfoByNationalUid (String nationalUid ) {
128151 try {
129- ResponseEntity <UserInfo > responseEntity = selfTokenRestTemplate .getForEntity (
152+ String token = getAuthToken ("compass-admin" , compassClientSecret , "client_credentials" );
153+ if (token == null || token .isEmpty ()) {
154+ throw new SendOtpException ("Token retrieval failed" );
155+ }
156+
157+ HttpHeaders headers = new HttpHeaders ();
158+ headers .setContentType (MediaType .APPLICATION_JSON );
159+ headers .set ("Authorization" ,"Bearer " +token );
160+
161+ HttpEntity <String > entity = new HttpEntity <>(headers );
162+
163+ ResponseEntity <UserInfo > responseEntity = restTemplate .exchange (
130164 userInfoUrl + "/{nationalUid}" ,
165+ HttpMethod .GET ,
166+ entity ,
131167 UserInfo .class ,
132168 nationalUid
133169 );
0 commit comments