-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAUTH.cs
138 lines (135 loc) · 5 KB
/
AUTH.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Collections.Generic;
using System.Text;
using BeetleX.Buffers;
using System.Net;
using MailKit.Security;
namespace Infinispan.Hotrod.Core.Commands
{
public class AUTH : Command
{
public AUTH(string mech, NetworkCredential c)
{
Credential = c;
NetworkReceive = OnReceive;
SaslMechName = mech;
SaslMech = SaslMechanism.Create(mech, new Uri("hotrod://" + c.Domain), c);
}
public int TimeOut { get; set; }
public override string Name => "AUTH";
public override Byte Code => 0x23;
public NetworkCredential Credential;
public SaslMechanism SaslMech;
public string SaslMechName;
public byte Completed;
public byte[] Challenge = new byte[0];
private int Step = 0;
internal override void OnExecute(CommandContext ctx)
{
base.OnExecute(ctx);
}
internal override void Execute(CommandContext ctx, InfinispanClient client, PipeStream stream)
{
switch (this.SaslMechName)
{
case "DIGEST-MD5":
executeDigestMd5(ctx, client, stream);
break;
case "PLAIN":
executePlain(ctx, client, stream);
break;
default:
if (!this.SaslMechName.StartsWith("SCRAM-SHA-"))
{
Completed = 1;
break;
}
executeScram(ctx, client, stream);
break;
}
}
private void executeDigestMd5(CommandContext ctx, InfinispanClient client, PipeStream stream)
{
switch (Step)
{
case 0:
base.Execute(ctx, client, stream);
Codec.writeArray(Encoding.ASCII.GetBytes(SaslMechName), stream);
Codec.writeArray(Challenge, stream);
stream.Flush();
Step++;
break;
case 1:
base.Execute(ctx, client, stream);
Codec.writeArray(Encoding.ASCII.GetBytes(SaslMechName), stream);
var s = Convert.ToBase64String(Challenge);
s = SaslMech.Challenge(s);
Challenge = Convert.FromBase64String(s);
Codec.writeArray(Challenge, stream);
Completed = 1;
stream.Flush();
Step++;
break;
}
}
private void executePlain(CommandContext ctx, InfinispanClient client, PipeStream stream)
{
switch (Step)
{
case 0:
Step++;
goto case 1;
case 1:
base.Execute(ctx, client, stream);
Codec.writeArray(Encoding.ASCII.GetBytes(SaslMechName), stream);
var s = Convert.ToBase64String(Challenge);
s = SaslMech.Challenge(s);
Challenge = Convert.FromBase64String(s);
Codec.writeArray(Challenge, stream);
Completed = 1;
stream.Flush();
Step++;
break;
default:
Completed = 1;
break;
}
}
private void executeScram(CommandContext ctx, InfinispanClient client, PipeStream stream)
{
switch (Step)
{
case 0:
base.Execute(ctx, client, stream);
Codec.writeArray(Encoding.ASCII.GetBytes(SaslMechName), stream);
var s1 = Convert.ToBase64String(Challenge);
s1 = SaslMech.Challenge(s1);
Challenge = Convert.FromBase64String(s1);
Codec.writeArray(Challenge, stream);
stream.Flush();
Step++;
break;
case 1:
base.Execute(ctx, client, stream);
Codec.writeArray(Encoding.ASCII.GetBytes(SaslMechName), stream);
var s = Convert.ToBase64String(Challenge);
s = SaslMech.Challenge(s);
Challenge = Convert.FromBase64String(s);
Codec.writeArray(Challenge, stream);
Completed = 1;
stream.Flush();
Step++;
break;
default:
Completed = 1;
break;
}
}
public override Result OnReceive(InfinispanRequest request, ResponseStream stream)
{
var completed = (byte)stream.ReadByte();
Challenge = Codec.readArray(stream);
return new Result { Status = ResultStatus.Completed, ResultType = ResultType.Object };
}
}
}