package org.cru.godtools.api.v2;

import org.ccci.util.time.Clock;
import org.cru.godtools.domain.authentication.AccessCodeRecord;
import org.cru.godtools.domain.authentication.AuthTokenGenerator;
import org.cru.godtools.domain.authentication.AuthorizationRecord;
import org.cru.godtools.domain.authentication.AuthorizationService;
import org.cru.godtools.domain.authentication.UnauthorizedException;
import org.jboss.logging.Logger;

import javax.inject.Inject;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import java.util.UUID;

@Path("/v2/auth")
public class AuthorizationResource
{
	@Inject
	Clock clock;

	@Inject
	AuthorizationService authorizationService;

	Logger log = Logger.getLogger(AuthorizationResource.class);

	@POST
	@Path("/admin")
	public Response getAdministratorToken(String adminPassphrase)
	{
		log.info("Requesting authorization token with translator access");

		AccessCodeRecord accessCodeRecord = authorizationService.getAccessCode(adminPassphrase);

		if(accessCodeRecord == null ||
				!accessCodeRecord.isCurrentlyActive(clock.currentDateTime()) ||
				!accessCodeRecord.isAdmin())
		{
			log.info("Authorization with admin passphrase was invalid.");
			log.info("Provided passphrase: " + adminPassphrase);

			// If the incorrect translator code is given, a 401 is returned
			throw new UnauthorizedException();
		}

		AuthorizationRecord authorizationRecord = createNewAuthorization();

		log.info(String.format("Saving authorization record with admin access w/ ID %s",
				authorizationRecord.getId()));

		authorizationService.recordNewAuthorization(authorizationRecord);

		return Response.noContent()
				.header("Authorization", authorizationRecord.getAuthToken())
				.build();
	}

	private AuthorizationRecord createNewAuthorization()
	{
		AuthorizationRecord authorizationRecord = new AuthorizationRecord();
		authorizationRecord.setId(UUID.randomUUID());
		authorizationRecord.setAuthToken(AuthTokenGenerator.generate());
		authorizationRecord.setAdmin(true);
		authorizationRecord.setDraftAccess(true);
		authorizationRecord.setGrantedTimestamp(clock.currentDateTime());
		authorizationRecord.setRevokedTimestamp(clock.currentDateTime().plusHours(4));

		return authorizationRecord;
	}
}

@@ -426,16 +426,49 @@
 .Logger;
+%0Aimport org.xml.sax.SAXException;
 %0A%0Aimport
@@ -539,16 +539,46 @@
 s.Path;%0A
+import javax.ws.rs.PathParam;%0A
 import j
@@ -603,16 +603,99 @@
 sponse;%0A
+import javax.xml.parsers.ParserConfigurationException;%0Aimport java.io.IOException;%0A
 import j
@@ -878,37 +878,1161 @@
 ger(
-AuthorizationResource.class);
+getClass());%0A%0A%09@POST%0A%09@Path(%22/%7BtranslatorCode%7D%22)%0A%09public Response getDraftAccessToken(@PathParam(%22translatorCode%22) String translatorCode) throws ParserConfigurationException, SAXException,IOException%0A%09%7B%0A%09%09log.info(%22Requesting authorization token with translator access%22);%0A%0A%09%09AccessCodeRecord accessCodeRecord = authorizationService.getAccessCode(translatorCode);%0A%0A%09%09AuthorizationRecord authorizationRecord = createNewAuthorization();%0A%0A%09%09if(accessCodeRecord == null %7C%7C !accessCodeRecord.isCurrentlyActive(clock.currentDateTime()))%0A%09%09%7B%0A%09%09%09log.info(%22Authorization with translator access code was invalid.%22);%0A%09%09%09log.info(%22Provided code: %22 + translatorCode);%0A%0A%09%09%09// If the incorrect translator code is given, a 401 is returned%0A%09%09%09throw new UnauthorizedException();%0A%09%09%7D%0A%0A%09%09authorizationRecord.setDraftAccess(true);%0A%09%09authorizationRecord.setAdmin(false);%0A%0A%09%09log.info(String.format(%22Saving authorization record with translator access w/ ID %25s%22,%0A%09%09%09%09authorizationRecord.getId()));%0A%0A%09%09authorizationService.recordNewAuthorization(authorizationRecord);%0A%0A%09%09return Response.noContent()%0A%09%09%09%09.header(%22Authorization%22, authorizationRecord.getAuthToken())%0A%09%09%09%09.build();%0A%09%7D
 %0A%0A%09@

Add getDraftToken endpoint to v2 API (iOS broken w/o it)