io.mosip.certify.mosipid.integration.helper.AuthTransactionHelper Maven / Gradle / Ivy
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package io.mosip.certify.mosipid.integration.helper;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.mosip.certify.mosipid.integration.dto.ClientIdSecretKeyRequest;
import io.mosip.kernel.core.http.RequestWrapper;
import io.mosip.kernel.core.http.ResponseWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.time.LocalDateTime;
@Component
@Slf4j
@ConditionalOnProperty(value = "mosip.certify.integration.vci-plugin", havingValue = "IdaVCIssuancePluginImpl")
public class AuthTransactionHelper {
private static final String AUTH_TOKEN_CACHE = "authtokens";
@Autowired
private ObjectMapper objectMapper;
@Autowired
private RestTemplate restTemplate;
@Value("${mosip.certify.authenticator.ida.auth-token-url}")
private String authTokenUrl;
@Value("${mosip.certify.authenticator.ida.client-id}")
private String clientId;
@Value("${mosip.certify.authenticator.ida.secret-key}")
private String secretKey;
@Value("${mosip.certify.authenticator.ida.app-id}")
private String appId;
@Cacheable(value = AUTH_TOKEN_CACHE, key = "#root.target.AUTH_TOKEN_CACHE_KEY")
public String getAuthToken() throws Exception {
log.info("Started to get auth-token with appId : {} && clientId : {}",
appId, clientId);
RequestWrapper authRequest = new RequestWrapper<>();
authRequest.setRequesttime(LocalDateTime.now());
ClientIdSecretKeyRequest clientIdSecretKeyRequest = new ClientIdSecretKeyRequest(clientId, secretKey, appId);
authRequest.setRequest(clientIdSecretKeyRequest);
String requestBody = objectMapper.writeValueAsString(authRequest);
RequestEntity requestEntity = RequestEntity
.post(UriComponentsBuilder.fromUriString(authTokenUrl).build().toUri())
.contentType(MediaType.APPLICATION_JSON)
.body(requestBody);
ResponseEntity responseEntity = restTemplate.exchange(requestEntity,
new ParameterizedTypeReference() {});
String authToken = responseEntity.getHeaders().getFirst("authorization");
return authToken;
}
@CacheEvict(value = AUTH_TOKEN_CACHE, allEntries = true)
public void purgeAuthTokenCache() {
log.info("Evicting entry from AUTH_TOKEN_CACHE");
}
}