SaslMechanism.java

 1package eu.siacs.conversations.crypto.sasl;
 2
 3import java.security.SecureRandom;
 4
 5import eu.siacs.conversations.entities.Account;
 6import eu.siacs.conversations.xml.TagWriter;
 7
 8public abstract class SaslMechanism {
 9
10    final protected TagWriter tagWriter;
11    final protected Account account;
12    final protected SecureRandom rng;
13
14    protected enum State {
15        INITIAL,
16        AUTH_TEXT_SENT,
17        RESPONSE_SENT,
18        VALID_SERVER_RESPONSE,
19    }
20
21    public static class AuthenticationException extends Exception {
22        public AuthenticationException(final String message) {
23            super(message);
24        }
25
26        public AuthenticationException(final Exception inner) {
27            super(inner);
28        }
29
30        public AuthenticationException(final String message, final Exception exception) {
31            super(message, exception);
32        }
33    }
34
35    public static class InvalidStateException extends AuthenticationException {
36        public InvalidStateException(final String message) {
37            super(message);
38        }
39
40        public InvalidStateException(final State state) {
41            this("Invalid state: " + state.toString());
42        }
43    }
44
45    public SaslMechanism(final TagWriter tagWriter, final Account account, final SecureRandom rng) {
46        this.tagWriter = tagWriter;
47        this.account = account;
48        this.rng = rng;
49    }
50
51    /**
52     * The priority is used to pin the authentication mechanism. If authentication fails, it MAY be retried with another
53     * mechanism of the same priority, but MUST NOT be tried with a mechanism of lower priority (to prevent downgrade
54     * attacks).
55     *
56     * @return An arbitrary int representing the priority
57     */
58    public abstract int getPriority();
59
60    public abstract String getMechanism();
61
62    public String getClientFirstMessage() {
63        return "";
64    }
65
66    public String getResponse(final String challenge) throws AuthenticationException {
67        return "";
68    }
69}