package com.github.dkanellis.fikeyserverexample.views.login;


import com.github.dkanellis.fikey.Authenticator;
import com.github.dkanellis.fikey.FiKeyAuth;
import com.github.dkanellis.fikey.exceptions.DeviceCompromisedException;
import com.github.dkanellis.fikey.exceptions.InvalidPasswordException;
import com.github.dkanellis.fikey.exceptions.NoEligibleDevicesException;
import com.github.dkanellis.fikey.exceptions.UserDoesNotExistException;
import com.github.dkanellis.fikey.storage.Users;
import com.github.dkanellis.fikeyserverexample.FiKeyApplication;
import io.dropwizard.views.View;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

/**
 * @author Dimitris Kanellis
 */
@Path("/")
@Produces(MediaType.TEXT_HTML)
public class AuthenticateResource {

    private Authenticator fiKeyAuth;
    private Users users;

    public AuthenticateResource() {
        this.fiKeyAuth = new FiKeyAuth(FiKeyApplication.APP_ID);
        users = Users.getInstance();
    }

    @Path("startAuthentication")
    @GET
    public View startRegistration(@QueryParam("username") String username, @QueryParam("password") String password) {
        try {
            fiKeyAuth.authenticateUser(username, password);
            return new StartAuthenticationView(username);
        } catch (UserDoesNotExistException e) {
            return new AuthenticationFailedView(username, e);
        } catch (InvalidPasswordException e) {
            return new AuthenticationFailedView(username, e);
        }
    }

    @Path("startDeviceAuthentication")
    @GET
    public View startDeviceAuthentication(@QueryParam("username") String username) {
        try {
            String authenticateRequestData = fiKeyAuth.startDeviceAuthentication(username);
            return new StartDeviceAuthenticationView(username, authenticateRequestData);
        } catch (NoEligibleDevicesException e) {
            return new AuthenticationFailedView(username, e);
        } catch (UserDoesNotExistException e) {
            return new AuthenticationFailedView(username, e);
        }
    }

    @Path("finishDeviceAuthentication")
    @POST
    public View finishDeviceAuthentication(@FormParam("tokenResponse") String response,
                                           @FormParam("username") String username) {
        try {
            String loginInfo = fiKeyAuth.finishDeviceAuthentication(response, username);
            return new FinishDeviceAuthenticationView(username, loginInfo, false);
        } catch (UserDoesNotExistException e) {
            return new AuthenticationFailedView(username, e);
        } catch (DeviceCompromisedException e) {
            return new AuthenticationFailedView(username, e);
        }
    }
}

@@ -443,57 +443,8 @@
 on;%0A
-import com.github.dkanellis.fikey.storage.Users;%0A
 impo
@@ -752,33 +752,8 @@
 uth;
-%0A    private Users users;
 %0A%0A  
@@ -855,45 +855,8 @@
 D);%0A
-        users = Users.getInstance();%0A
     

Remove unused users