1 /*
2  * hunt-amqp: AMQP library for D programming language, based on hunt-net.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 module hunt.amqp.sasl.ProtonSaslMechanism;
12 
13 import  hunt.util.Common;
14 import  hunt.Enum;
15 import std.concurrency : initOnce;
16 
17 class PRIORITY  : AbstractEnum!PRIORITY
18 {
19   //LOWEST(0), LOWER(1), LOW(2), MEDIUM(3), HIGH(4), HIGHER(5), HIGHEST(6);
20 
21   static PRIORITY  LOWEST() {
22     __gshared PRIORITY  inst;
23     return initOnce!inst(new PRIORITY("LOWEST" ,0));
24   }
25 
26   static PRIORITY  LOWER() {
27     __gshared PRIORITY  inst;
28     return initOnce!inst(new PRIORITY("LOWER" ,1));
29   }
30   static PRIORITY  LOW() {
31     __gshared PRIORITY  inst;
32     return initOnce!inst(new PRIORITY("LOW" ,2));
33   }
34 
35   static PRIORITY  MEDIUM() {
36     __gshared PRIORITY  inst;
37     return initOnce!inst(new PRIORITY("MEDIUM" ,3));
38   }
39 
40   static PRIORITY  HIGH() {
41     __gshared PRIORITY  inst;
42     return initOnce!inst(new PRIORITY("HIGH" ,4));
43   }
44 
45   static PRIORITY  HIGHER() {
46     __gshared PRIORITY  inst;
47     return initOnce!inst(new PRIORITY("HIGHER" ,5));
48   }
49 
50   static PRIORITY  HIGHEST() {
51     __gshared PRIORITY  inst;
52     return initOnce!inst(new PRIORITY("HIGHEST" ,6));
53   }
54 
55 
56   private int value;
57 
58   this(string name , int value) {
59     super(name ,value);
60     this.value = value;
61   }
62 
63   public int getValue() {
64     return value;
65   }
66 
67 };
68 
69 
70 interface ProtonSaslMechanism : Comparable!ProtonSaslMechanism {
71 
72   /**
73    * Relative priority values used to arrange the found SASL mechanisms in a preferred order where the level of security
74    * generally defines the preference.
75    */
76 
77 
78   /**
79    * @return return the relative priority of this SASL mechanism.
80    */
81   int getPriority();
82 
83   /**
84    * @return the well known name of this SASL mechanism.
85    */
86   string getName();
87 
88   /**
89    * Create an initial response based on selected mechanism.
90    *
91    * May be null if there is no initial response.
92    *
93    * @return the initial response, or null if there isn't one.
94    * @throws SaslException
95    *           if an error occurs computing the response.
96    */
97   byte[] getInitialResponse() ;
98 
99   /**
100    * Create a response based on a given challenge from the remote peer.
101    *
102    * @param challenge
103    *          the challenge that this Mechanism should response to.
104    *
105    * @return the response that answers the given challenge.
106    * @throws SaslException
107    *           if an error occurs computing the response.
108    */
109   byte[] getChallengeResponse(byte[] challenge);
110 
111   /**
112    * Sets the user name value for this Mechanism. The Mechanism can ignore this value if it does not utilize user name
113    * in it's authentication processing.
114    *
115    * @param username
116    *          The user name given.
117    * @return the mechanism.
118    */
119   ProtonSaslMechanism setUsername(string username);
120 
121   /**
122    * Returns the configured user name value for this Mechanism.
123    *
124    * @return the currently set user name value for this Mechanism.
125    */
126   string getUsername();
127 
128   /**
129    * Sets the password value for this Mechanism. The Mechanism can ignore this value if it does not utilize a password
130    * in it's authentication processing.
131    *
132    * @param username
133    *          The user name given.
134    * @return the mechanism.
135    */
136   ProtonSaslMechanism setPassword(string username);
137 
138   /**
139    * Returns the configured password value for this Mechanism.
140    *
141    * @return the currently set password value for this Mechanism.
142    */
143   string getPassword();
144 
145   /**
146    * Checks whether a given mechanism is suitable for use in light of the available credentials.
147    *
148    * @param username
149    *          the username
150    * @param password
151    *          the password
152    * @return whether mechanism is applicable
153    */
154   bool isApplicable(string username, string password);
155 }