package de.bornemisza.rest.security;

import de.bornemisza.rest.exception.UnauthorizedException;
import static de.bornemisza.rest.security.Auth.Scheme.COOKIE_CSRFTOKEN;
import static de.bornemisza.rest.security.Auth.Scheme.USERNAME_PASSWORD;

public class Auth {

    private final Scheme scheme;
    private final String userName, cookie, csrfToken;
    private String password;

    public enum Scheme { USERNAME_PASSWORD, COOKIE_CSRFTOKEN }

    public Auth(BasicAuthCredentials creds) {
        this.scheme = USERNAME_PASSWORD;
        this.userName = creds.getUserName();
        this.password = creds.getPassword();
        this.cookie = null;
        this.csrfToken = null;
    }

    public Auth(DoubleSubmitToken dsToken) {
        this.scheme = COOKIE_CSRFTOKEN;
        this.userName = null;
        this.password = null;
        this.cookie = dsToken.getCookie();
        this.csrfToken = dsToken.getCtoken();
    }

    public Scheme getScheme() {
        return scheme;
    }

    /**
     * @return the username to be used for authentication or null, if no username is part of the authentication scheme
     */
    public String getUsername() {
        return this.userName;
    }

    /**
     * @return the password to be used for authentication or null, if no password is part of the authentication scheme
     */
    public String getPassword() {
        return this.password;
    }

    /**
     * @return the cookie to be used for authentication or null, if no cookie is part of the authentication scheme
     */
    public String getCookie() {
        return this.cookie;
    }

    /**
     * @return the csrf token to be used for authentication or null, if no csrf token is part of the authentication scheme
     */
    public String getCsrfToken() {
        return this.csrfToken;
    }

    public void changePassword(String newPassword) {
        this.password = newPassword;
    }

    public String checkTokenValidity(HashProvider hashProvider) throws UnauthorizedException {
        return new DoubleSubmitToken(cookie, csrfToken).checkValidity(hashProvider);
    }

}

@@ -31,16 +31,72 @@
 urity;%0A%0A
+import java.io.Serializable;%0Aimport java.util.Objects;%0A%0A
 import d
@@ -310,17 +310,95 @@
 ss Auth 
-%7B
+implements Serializable %7B%0A%0A    private static final long serialVersionUID = 1L;
 %0A%0A    pr
@@ -2224,10 +2224,1138 @@
 %0A    %7D%0A%0A
+    @Override%0A    public int hashCode() %7B%0A        int hash = 7;%0A        hash = 97 * hash + Objects.hashCode(this.scheme);%0A        hash = 97 * hash + Objects.hashCode(this.userName);%0A        hash = 97 * hash + Objects.hashCode(this.cookie);%0A        hash = 97 * hash + Objects.hashCode(this.csrfToken);%0A        hash = 97 * hash + Objects.hashCode(this.password);%0A        return hash;%0A    %7D%0A%0A    @Override%0A    public boolean equals(Object obj) %7B%0A        if (this == obj) %7B%0A            return true;%0A        %7D%0A        if (obj == null) %7B%0A            return false;%0A        %7D%0A        if (getClass() != obj.getClass()) %7B%0A            return false;%0A        %7D%0A        final Auth other = (Auth) obj;%0A        if (!Objects.equals(this.userName, other.userName)) %7B%0A            return false;%0A        %7D%0A        if (!Objects.equals(this.cookie, other.cookie)) %7B%0A            return false;%0A        %7D%0A        if (!Objects.equals(this.csrfToken, other.csrfToken)) %7B%0A            return false;%0A        %7D%0A        if (!Objects.equals(this.password, other.password)) %7B%0A            return false;%0A        %7D%0A        return this.scheme == other.scheme;%0A    %7D%0A%0A
 %7D%0A

make it serializable