Commit 3a51d851 authored by Noel Alonso's avatar Noel Alonso
Browse files

Añade endpoint para obtener token de superset

parent 0b71de9c
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -12,6 +12,10 @@ services:
      RECAPTCHA_SECRET:
      PROPERTY_CONTACTREDMIC:
      JAVA_OPTS:
      SUPERSET_PRIVATE_DASHBOARD_USERNAME:
      SUPERSET_PRIVATE_DASHBOARD_PASSWORD:
      SUPERSET_PUBLIC_DASHBOARD_USERNAME:
      SUPERSET_PUBLIC_DASHBOARD_PASSWORD:
    networks:
      postgres-net:
      redmic-net:
+49 −0
Original line number Diff line number Diff line
package es.redmic.user.embedded.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;

/*-
 * #%L
 * User
 * %%
 * Copyright (C) 2025 REDMIC Project / Server
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import es.redmic.user.embedded.service.SupersetEmbeddedService;


@RestController
@RequestMapping(value = "${controller.mapping.SUPERSET_EMBEDDED}")
public class SupersetEmbeddedController {

	SupersetEmbeddedService service;

	public SupersetEmbeddedController(SupersetEmbeddedService service) {

		this.service = service;
	}

	@RequestMapping(value = "/get-token/{dashboardid}", method = RequestMethod.GET)
	public String getToken(@PathVariable("dashboardid") String dashboardid) {

		return service.getToken(dashboardid);
	}
}
+145 −0
Original line number Diff line number Diff line
package es.redmic.user.embedded.service;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import es.redmic.user.manager.model.User;
import es.redmic.user.manager.service.UserProfileService;

/*-
 * #%L
 * User
 * %%
 * Copyright (C) 2025 REDMIC Project / Server
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

@Service
public class SupersetEmbeddedService {

	@Value("${property.SUPERSET_API_URL}")
	private String supersetApiUrl;

	@Value("${property.SUPERSET_API_BASE_PATH}")
	private String supersetApiBasePath;

	@Value("${property.SUPERSET_PRIVATE_DASHBOARD_USERNAME}")
	private String supersetPrivateDashboardUsername;

	@Value("${property.SUPERSET_PRIVATE_DASHBOARD_PASSWORD}")
	private String supersetPrivateDashboardPassword;

	@Value("${property.SUPERSET_PUBLIC_DASHBOARD_USERNAME}")
	private String supersetPublicDashboardUsername;

	@Value("${property.SUPERSET_PUBLIC_DASHBOARD_PASSWORD}")
	private String supersetPublicDashboardPassword;

	@Autowired
	UserProfileService userProfileService;

	List<MediaType> acceptableMediaTypes = new ArrayList<>();

	HttpHeaders headers = new HttpHeaders();

	public SupersetEmbeddedService() {

		acceptableMediaTypes.add(MediaType.APPLICATION_JSON);

		headers.setContentType(MediaType.APPLICATION_JSON);
		headers.setAccept(acceptableMediaTypes);
	}

	public String getToken(String dashboardid) {

		String username = userProfileService.getUsername();
		User profile = userProfileService.findProfileByUsername(username);

		Long roleId = profile.getRole().getId();

		//TODO: Cuando se realice la integración Superset + ECOMARCAN + OpenId, comprobar acceso del usuario al dashboard específico,
		// no de forma genérica como está ahora.
		try {
			if (roleId <= 2) {
				// Se trata de un usuario con permisos, por lo que se loguea contra superset con usuario embbeded
				return fetchGuestToken(supersetPrivateDashboardUsername, supersetPrivateDashboardPassword, dashboardid);
			} else if (roleId > 2 ) {
				// Se trata de un usuario sin permisos, por lo que se loguea contra superset con usuario guest
				return fetchGuestToken(supersetPublicDashboardUsername, supersetPublicDashboardPassword, dashboardid);
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}

		return null;
	}

	private String fetchGuestToken(String user, String password, String dashboardid) throws JSONException {
		String url = supersetApiUrl + supersetApiBasePath + "guest_token/";
		String accessToken = login(user, password);

		RestTemplate restTemplate = new RestTemplate();

		JSONObject body = new JSONObject(
			"{'resources': [{id': " + dashboardid + ", 'type': 'dashboard'}], 'rls': [], 'user': {'username': " + user + "}}");

		HttpHeaders authHeaders = headers;
		authHeaders.set(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken);
		HttpEntity<JSONObject> request = new HttpEntity<>(body, authHeaders);

		JSONObject response = restTemplate.postForObject(url, request, JSONObject.class);

		if (response != null) {
			Object token = response.get("token");
			return (token != null) ? token.toString() : null;
		} else {
			return null;
		}
	}

	private String login(String user, String password) throws JSONException {

		String url = supersetApiUrl + supersetApiBasePath + "login";

		RestTemplate restTemplate = new RestTemplate();

		JSONObject body = new JSONObject(
			"{'username': " + user
			+ ", 'password': " + password
			+ ", 'provider': 'db', 'refresh': 'true'}");

		HttpEntity<JSONObject> request = new HttpEntity<>(body, headers);

		JSONObject response = restTemplate.postForObject(url, request, JSONObject.class);

		if (response != null) {
			Object accessToken = response.get("access_token");
			return (accessToken != null) ? accessToken.toString() : null;
		} else {
			return null;
		}
	}
}
+2 −0
Original line number Diff line number Diff line
@@ -5,4 +5,6 @@ oauth.server=http://oauth:8081
spring.datasource.name=redmic
spring.datasource.url=jdbc:postgresql://db:5432/redmic?currentSchema=app

property.SUPERSET_API_URL=https://superset.ecomarcan.grafcan.es

logging.level.es.redmic=debug
+2 −0
Original line number Diff line number Diff line
@@ -5,4 +5,6 @@ oauth.server=http://oauth:8081
spring.datasource.name=redmic
spring.datasource.url=jdbc:postgresql://db:5432/redmic?currentSchema=app

property.SUPERSET_API_URL=https://superset.ecomarcan.grafcan.es

logging.level.es.redmic=warn
Loading