package net.davidvoid.thor.lightning.service;

import net.davidvoid.thor.lightning.data.access.UserStore;
import net.davidvoid.thor.lightning.entity.User;
import net.davidvoid.thor.lightning.exception.DuplicateUserException;
import net.davidvoid.thor.lightning.exception.ResourceNotFoundException;
import net.davidvoid.thor.lightning.exception.AuthenticationException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Auth {
    @Autowired
    UserStore store = null;

    volatile User user = null;
    
    
    public void authenticate(String token) {
        ensureUserLoaded();

        if (user == null || !token.equals(this.user.getPassword())) {
            throw new AuthenticationException("incorrect credential provided");
        }
    }
    
    synchronized public void register(String name, String password) {
        ensureUserLoaded();
        if (user != null) throw new DuplicateUserException("the user already registered");
        
        User user = new User();
        user.setName(name);
        user.setPassword(password);
        store.add(user);
        this.user = user;
    }

    public User getUser() {
        ensureUserLoaded();
        return user;
    }

    private void ensureUserLoaded() {
        // double checked locking with volatile variable
        if (user == null) {
            synchronized(this) {
                if (user == null) {
                    user = retrieve_token();
                }
            }
            
        }
    }

    private User retrieve_token() {
        try {
            User user = store.getUser();
            return user;
        } catch (ResourceNotFoundException e) {
        }
        
        assert false : "should never reach here";
        return null;
    }
}

@@ -605,22 +605,16 @@
 %0D%0A    %0D%0A
-    %0D%0A
     publ
@@ -641,21 +641,41 @@
 (String 
-token
+username, String password
 ) %7B%0D%0A   
@@ -731,21 +731,82 @@
 ull %7C%7C !
-token
+username.equals(this.user.getName())%0D%0A                %7C%7C !password
 .equals(
@@ -885,37 +885,35 @@
 on(%22
-incorrect credential provided
+failed to authenticate user
 %22);%0D

add service to authenticate a user