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.impl.ProtonSaslPlainImpl;
12 
13 import hunt.amqp.sasl.impl.ProtonSaslMechanismImpl;
14 import hunt.amqp.sasl.ProtonSaslMechanism;
15 
16 class ProtonSaslPlainImpl : ProtonSaslMechanismImpl {
17 
18   public static  string MECH_NAME = "PLAIN";
19 
20   public int getPriority() {
21     return PRIORITY.LOWER.getValue();
22   }
23 
24   public string getName() {
25     return MECH_NAME;
26   }
27 
28   public byte[] getInitialResponse() {
29 
30     string username = getUsername();
31     string password = getPassword();
32 
33     if (username is null) {
34       username = "";
35     }
36 
37     if (password is null) {
38       password = "";
39     }
40 
41     byte[] usernameBytes = cast(byte[])username;
42     byte[] passwordBytes = cast(byte[])password;
43     byte[] data = new byte[usernameBytes.length + passwordBytes.length + 2];
44     //System.arraycopy(usernameBytes, 0, data, 1, usernameBytes.length);
45     data[1 .. 1+usernameBytes.length] = usernameBytes[0 ..usernameBytes.length];
46     //System.arraycopy(passwordBytes, 0, data, 2 + usernameBytes.length, passwordBytes.length);
47     data[2 + usernameBytes.length .. 2 + usernameBytes.length + passwordBytes.length] = passwordBytes[0 ..passwordBytes.length ];
48     return data;
49   }
50 
51   public byte[] getChallengeResponse(byte[] challenge) {
52     return EMPTY;
53   }
54 
55   public bool isApplicable(string username, string password) {
56     return username !is null && username.length > 0 && password !is null && password.length > 0;
57   }
58 }