package ru.otus.kirillov.model.services.auth;

import ru.otus.kirillov.utils.AESSecurity;
import ru.otus.kirillov.utils.CommonUtils;

import java.util.Objects;
import java.util.UUID;

/**
 * Заглушка для сервиса авторизации и авторизации
 */
public class StubAuthServiceImpl implements AuthService {

    public static class NotAuthorizedException extends RuntimeException {
        public NotAuthorizedException(String userName, String password) {
            super(String.format("Wrong login/password (%s/%s) combination", userName, password));
        }
    }

    private final AESSecurity security;

    private final String username;

    private final String password;

    private String currentSessionId = null;

    public StubAuthServiceImpl(AESSecurity security) {
        this.security = CommonUtils.retunIfNotNull(security);
        username = "admin";
        password = security.encrypt("1234567890");
    }


    @Override
    public boolean isValidSession(String sessionId, String userName) {
        CommonUtils.requiredNotNull(sessionId, userName);
        return isSessionForUserExists(sessionId, userName);
    }

    @Override
    public String login(String userName, String password) {
        if (!Objects.equals(username, userName) || !Objects.equals(this.password, password)) {
            throw new NotAuthorizedException(userName, password);
        }
        return currentSessionId = UUID.randomUUID().toString();
    }

    @Override
    public void logout(String sessionId, String userName) {
        CommonUtils.requiredNotNull(sessionId, userName);
        if (isSessionForUserExists(sessionId, userName)) {
            currentSessionId = null;
        }
    }

    private boolean isSessionForUserExists(String sessionId, String userName) {
        return Objects.equals(currentSessionId, sessionId) && Objects.equals(username, userName);
    }
}

@@ -51,135 +51,368 @@
 ort 
-ru.otus.kirillov.utils.AESSecurity;%0Aimport ru.otus.kirillov.utils.CommonUtils;%0A%0Aimport java.util.Objects;%0Aimport java.util.UUID
+org.apache.logging.log4j.LogManager;%0Aimport org.apache.logging.log4j.Logger;%0Aimport ru.otus.kirillov.utils.AESSecurity;%0Aimport ru.otus.kirillov.utils.CommonUtils;%0A%0Aimport java.util.Collections;%0Aimport java.util.Objects;%0Aimport java.util.Set;%0Aimport java.util.UUID;%0Aimport java.util.concurrent.ConcurrentHashMap;%0Aimport java.util.concurrent.CopyOnWriteArrayList
 ;%0A%0A/
@@ -527,16 +527,79 @@
 vice %7B%0A%0A
+    private static final Logger log = LogManager.getLogger();%0A%0A
     publ
@@ -982,38 +982,89 @@
 ate 
+Set%3C
 String
- current
+%3E active
 Session
-Id = null
+s = Collections.newSetFromMap(new ConcurrentHashMap%3C%3E())
 ;%0A%0A 
@@ -1641,24 +1641,114 @@
 assword)) %7B%0A
+            log.error(%22Username = %7B%7D, password = %7B%7D not authorized%22, userName, password);%0A
             
@@ -1823,23 +1823,16 @@
     
-return currentS
+String s
 essi
@@ -1868,16 +1868,198 @@
 ring();%0A
+        activeSessions.add(sessionId);%0A        log.info(%22Username = %7B%7D, password = %7B%7D authorization success. Session = %7B%7D%22, userName, password, sessionId);%0A        return sessionId;%0A
     %7D%0A%0A 
@@ -2264,31 +2264,122 @@
     
-currentSessionId = null
+activeSessions.remove(sessionId);%0A            log.info(%22Username = %7B%7D, Session = %7B%7D. Logout%22, userName, sessionId)
 ;%0A  
@@ -2492,41 +2492,32 @@
 urn 
-Objects.equals(currentSessionId, 
+activeSessions.contains(
 sess

implement multisession Auth service