package ru.fitgraph.rest.resources;

import org.hibernate.validator.constraints.NotEmpty;
import ru.fitgraph.database.users.User;
import ru.fitgraph.database.users.UserController;
import ru.fitgraph.engine.secure.AuthController;
import ru.fitgraph.engine.vkapi.VkAuth;
import ru.fitgraph.engine.vkapi.exceptions.VkSideError;

import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import java.net.MalformedURLException;
import java.net.URISyntaxException;

/**
 * Resource which is used for auth and work with profile.
 *
 * If client must be authorized for calling method but it is not, unauthorized response code will be
 * returned to client, and body will contain error object with description.
 *
 * @author Morgen Matvey
 */
@Path("/profile")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public class ProfileResource {
    /**
     * Field storing the session id of the user if the user has no session it is null.
     *
     * Can be null only in permitted for all methods.
     */
    @CookieParam(value = "JSESSIONID")
    private String sessionId;

    /**
     * Field storing the vk id of the user if the user not authorized it is null.
     *
     * Can be null only in permitted for all methods.
     */
    @CookieParam(value = "vkId")
    private Long vkId;

    /**
     * Return uri referring to which the client will receive the code necessary
     * to authenticate and from which they will be redirected back to our
     * resource for authentication. Method wouldn't redirect
     *
     * Method allowed for all.
     * @param uriInfo information about resource, uri injected by context.
     * @return redirect uri as string.
     */
    @GET
    @PermitAll
    @Path("/getVkRequestUri")
    @Produces(MediaType.TEXT_PLAIN)
    public String getVkRequestUri(@Context UriInfo uriInfo) {
        return VkAuth.getClientAuthUri(uriInfo.getBaseUriBuilder()
                .path(ProfileResource.class) // Add class path
                .path(ProfileResource.class, "auth").build());
    }

    /**
     * Auth user by code and set the client cookie.
     *
     * Yo should call {@link #getVkRequestUri(javax.ws.rs.core.UriInfo)}
     * for get the url which client must call, to get the code.
     * @param code code that vk returned to the client.
     * @param request information about request, injected by context.
     * @return response with new cookie, or error object with description.
     * @throws MalformedURLException
     * @throws VkSideError
     * @throws URISyntaxException
     */
    @GET
    @PermitAll
    @Path("/auth")
    public Response auth(
            @NotEmpty @QueryParam("code") String code,
            @Context HttpServletRequest request) throws MalformedURLException, VkSideError, URISyntaxException {
        String uri = request.getRequestURL().toString();

        NewCookie vkIdCookie = new NewCookie("vkId", AuthController.auth(code, uri, sessionId).toString(), "/", null,
                null, 2629744, false); // Valid for a month
        return Response.ok()
                .cookie(vkIdCookie)
                .build();
    }

    /**
     * Return user profile object. Only for authorized users.
     * @return profile object.
     */
    @GET
    public User getProfile() {
        return UserController.getUserByVkAndSession(vkId, sessionId);
    }
}

@@ -497,16 +497,37 @@
 eption;%0A
+import java.net.URI;%0A
 import j
@@ -3204,32 +3204,1250 @@
 build();%0A    %7D%0A%0A
+    /**%0A     * Auth user by code and set the client cookie. Same as the %7B@link #auth%7D but response contains script%0A     * which should close the window.%0A     *%0A     * Yo should call %7B@link #getVkRequestUri(javax.ws.rs.core.UriInfo)%7D%0A     * for get the url which client must call, to get the code.%0A     * @param code code that vk returned to the client.%0A     * @param request information about request, injected by context.%0A     * @return response with new cookie, or error object with description.%0A     * @throws MalformedURLException%0A     * @throws VkSideError%0A     * @throws URISyntaxException%0A     */%0A    @GET%0A    @PermitAll%0A    @Path(%22/auth%22)%0A    @Produces(MediaType.TEXT_PLAIN)%0A    public Response authBrowser(%0A            @NotEmpty @QueryParam(%22code%22) String code,%0A            @Context HttpServletRequest request) throws MalformedURLException, VkSideError, URISyntaxException %7B%0A        String uri = request.getRequestURL().toString();%0A%0A        NewCookie vkIdCookie = new NewCookie(%22vkId%22, AuthController.auth(code, uri, sessionId).toString(), %22/%22, null,%0A                null, 2629744, false); // Valid for a month%0A        return Response.ok()%0A                .cookie(vkIdCookie)%0A                .build();%0A    %7D%0A%0A
     /**%0A     * R

Add auth method for browser based application.