package org.gluu.oxtrust.api.rest;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.gluu.oxtrust.exception.UmaProtectionException;
import org.gluu.oxtrust.ldap.service.PassportService;
import org.gluu.oxtrust.model.passport.PassportConfigResponse;
import org.gluu.oxtrust.model.passport.PassportStrategy;
import org.gluu.oxtrust.service.uma.PassportUmaProtectionService;
import org.gluu.oxtrust.service.uma.UmaPermissionService;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.log.Log;
import org.xdi.config.oxtrust.LdapOxPassportConfiguration;
import org.xdi.oxauth.model.uma.wrapper.Token;
import org.xdi.service.JsonService;
import org.xdi.util.Pair;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * PassportConfigurationEndPoint Implementation
 * 
 * @author Shekhar L.
 * @author Yuriy Movchan Date: 12/06/2016
 */

@Name("PassportConfigurationEndPoint")
@Path("/passport/config")
public class PassportRestWebService {

	@Logger
	private Log log;

	@In
	private PassportService passportService;

	@In
	private PassportUmaProtectionService pasportUmaProtectionService;

	@In
	private UmaPermissionService umaPermissionService;

	@In
	private JsonService jsonService;

	@POST
	@Produces({ MediaType.APPLICATION_JSON })
	public Response getPassportConfig(@HeaderParam("Authorization") String authorization) {
		Response authorizationResponse = processAuthorization(authorization);
		if (authorizationResponse != null) {
			return authorizationResponse;
		}

		PassportConfigResponse passportConfigResponse = new PassportConfigResponse();
		Map<String, PassportStrategy> passportStrategies = new HashMap<String, PassportStrategy>();

		LdapOxPassportConfiguration ldapOxPassportConfiguration = passportService.loadConfigurationFromLdap();
		for (org.xdi.model.passport.PassportConfiguration passportConfiguration : ldapOxPassportConfiguration.getPassportConfigurations()) {
			PassportStrategy passportStrategy = new PassportStrategy();
			passportStrategy.setClientId(passportConfiguration.getClientID());
			passportStrategy.setClientSecret(passportConfiguration.getClientSecret());

			passportStrategies.put(passportConfiguration.getProvider(), passportStrategy);
		}

		passportConfigResponse.setPassportStrategies(passportStrategies);

		String passportConfigResponseJson;
		try {
			passportConfigResponseJson = jsonService.objectToPerttyJson(passportConfigResponse);
		} catch (IOException ex) {
			return getErrorResponse(Response.Status.FORBIDDEN, "Failed to prepare configuration");
		}

		return Response.status(Response.Status.OK).entity(passportConfigResponseJson).build();
	}

	protected Response processAuthorization(String authorization) {
		if (!pasportUmaProtectionService.isEnabled()) {
			log.info("UMA authentication is disabled");
			return getErrorResponse(Response.Status.FORBIDDEN, "Passport configuration was disabled");
		}

		Token patToken;
		try {
			patToken = pasportUmaProtectionService.getPatToken();
		} catch (UmaProtectionException ex) {
			return getErrorResponse(Response.Status.FORBIDDEN, "Failed to obtain PAT token");
		}

		Pair<Boolean, Response> rptTokenValidationResult = umaPermissionService.validateRptToken(patToken,
				authorization, pasportUmaProtectionService.getUmaResourceId(),
				pasportUmaProtectionService.getUmaScope());
		if (rptTokenValidationResult.getFirst()) {
			if (rptTokenValidationResult.getSecond() != null) {
				return rptTokenValidationResult.getSecond();
			}
		} else {
			return getErrorResponse(Response.Status.FORBIDDEN, "Invalid GAT/RPT token");
		}

		return null;
	}

	protected Response getErrorResponse(Response.Status status, String detail) {
		return Response.status(status).entity(detail).build();
	}

}

@@ -108,13 +108,14 @@
 til.
-List;
+Map;%0D%0A
 %0D%0Aim
@@ -119,36 +119,36 @@
 %0Aimport java
-.util.Map;%0D%0A
+x.ws.rs.GET;
 %0D%0Aimport jav
@@ -174,34 +174,8 @@
 m;%0D%0A
-import javax.ws.rs.POST;%0D%0A
 impo
@@ -303,117 +303,8 @@
 %0D%0A%0D%0A
-import org.codehaus.jackson.JsonGenerationException;%0D%0Aimport org.codehaus.jackson.map.JsonMappingException;%0D%0A
 impo
@@ -994,123 +994,8 @@
 %0D%0A%0D%0A
-import com.fasterxml.jackson.core.JsonProcessingException;%0D%0Aimport com.fasterxml.jackson.databind.ObjectMapper;%0D%0A%0D%0A
 /**%0D
@@ -1498,11 +1498,10 @@
 %0D%0A%09@
-POS
+GE
 T%0D%0A%09

Use @GET to obtain Passport configuration