Skip to content
Snippets Groups Projects
Unverified Commit 8b23a25b authored by Matej Hrica's avatar Matej Hrica
Browse files

Pass auth header to api in weather

parent 273b26ce
No related branches found
No related tags found
No related merge requests found
Pipeline #
......@@ -3,12 +3,17 @@ package cz.muni.fi.pa165.weather.server.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import cz.muni.fi.pa165.core.client.AirportApi;
import cz.muni.fi.pa165.core.client.invoker.ApiClient;
import cz.muni.fi.pa165.core.client.invoker.ApiException;
import cz.muni.fi.pa165.core.client.model.AirportDto;
import cz.muni.fi.pa165.weather.server.data.FlightCreationAdvice;
import cz.muni.fi.pa165.weather.server.data.HourlyWeatherForecast;
import cz.muni.fi.pa165.weather.server.data.WeatherForecast;
import cz.muni.fi.pa165.weather.server.data.WeatherReason;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthentication;
import org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionAuthenticatedPrincipal;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
......@@ -19,7 +24,6 @@ public class WeatherServiceImpl implements WeatherService {
private final RestTemplate restTemplate = new RestTemplate();
private final ObjectMapper objectMapper = new ObjectMapper();
private final AirportApi airportClient = new AirportApi();
private static final String TEMPERATURE_ATTRIBUTE_NAME = "temperature_2m";
private static final String RAIN_ATTRIBUTE_NAME = "rain";
......@@ -50,6 +54,13 @@ public class WeatherServiceImpl implements WeatherService {
OffsetDateTime arrivalTime
) {
try {
var authentication = SecurityContextHolder.getContext().getAuthentication();
var token = (OAuth2AccessToken) (((BearerTokenAuthentication) authentication).getToken());
var client = new ApiClient();
client.setRequestInterceptor((builder -> {
builder.header("Authorization", "Bearer " + token.getTokenValue());
}));
AirportApi airportClient = new AirportApi(client);
AirportDto departureAirportDto = airportClient.getAirportById(departureAirportId);
AirportDto arrivalAirportDto = airportClient.getAirportById(arrivalAirportId);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment