de.adorsys.ledgers.keycloak.client.impl.KeycloakTokenServiceImpl Maven / Gradle / Ivy
package de.adorsys.ledgers.keycloak.client.impl;
import de.adorsys.ledgers.keycloak.client.api.KeycloakTokenService;
import de.adorsys.ledgers.keycloak.client.mapper.KeycloakAuthMapper;
import de.adorsys.ledgers.keycloak.client.model.TokenConfiguration;
import de.adorsys.ledgers.keycloak.client.rest.KeycloakTokenRestClient;
import de.adorsys.ledgers.middleware.api.domain.um.BearerTokenTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.keycloak.representations.AccessToken;
import org.keycloak.representations.AccessTokenResponse;
import org.keycloak.representations.JsonWebToken;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
@Slf4j
@Service
@RequiredArgsConstructor
public class KeycloakTokenServiceImpl implements KeycloakTokenService {
@Value("${keycloak.resource:}")
private String clientId;
@Value("${keycloak.credentials.secret:}")
private String clientSecret;
private final KeycloakTokenRestClient keycloakTokenRestClient;
private final KeycloakAuthMapper authMapper;
@Override
public BearerTokenTO login(String username, String password) {
MultiValueMap formParams = new LinkedMultiValueMap<>();
formParams.add("grant_type", "password");
formParams.add("username", username);
formParams.add("password", password);
formParams.add("client_id", clientId);
formParams.add("client_secret", clientSecret);
ResponseEntity