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
31 public static class InvalidStateException extends AuthenticationException {
32 public InvalidStateException(final String message) {
33 super(message);
34 }
35
36 public InvalidStateException(final State state) {
37 this("Invalid state: " + state.toString());
38 }
39 }
40
41 public SaslMechanism(final TagWriter tagWriter, final Account account, final SecureRandom rng) {
42 this.tagWriter = tagWriter;
43 this.account = account;
44 this.rng = rng;
45 }
46
47 /**
48 * The priority is used to pin the authentication mechanism. If authentication fails, it MAY be retried with another
49 * mechanism of the same priority, but MUST NOT be tried with a mechanism of lower priority (to prevent downgrade
50 * attacks).
51 * @return An arbitrary int representing the priority
52 */
53 public abstract int getPriority();
54
55 public abstract String getMechanism();
56 public String getClientFirstMessage() {
57 return "";
58 }
59 public String getResponse(final String challenge) throws AuthenticationException {
60 return "";
61 }
62}