Skip to content

Commit 36e7de8

Browse files
committed
Override CreateSocketAsync in BinaryNode
1 parent 2f45645 commit 36e7de8

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

Enyim.Caching/Memcached/MemcachedNode.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ protected internal virtual PooledSocket CreateSocket()
768768
}
769769

770770
}
771+
771772
protected internal virtual async Task<PooledSocket> CreateSocketAsync()
772773
{
773774
try

Enyim.Caching/Memcached/Protocol/Binary/BinaryNode.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Enyim.Collections;
88
using System.Security;
99
using Microsoft.Extensions.Logging;
10+
using System.Threading.Tasks;
1011

1112
namespace Enyim.Caching.Memcached.Protocol.Binary
1213
{
@@ -36,7 +37,21 @@ protected internal override PooledSocket CreateSocket()
3637
{
3738
var retval = base.CreateSocket();
3839

39-
if (this.authenticationProvider != null && !this.Auth(retval))
40+
if (this.authenticationProvider != null && !Auth(retval))
41+
{
42+
_logger.LogError("Authentication failed: " + this.EndPoint);
43+
44+
throw new SecurityException("auth failed: " + this.EndPoint);
45+
}
46+
47+
return retval;
48+
}
49+
50+
protected internal override async Task<PooledSocket> CreateSocketAsync()
51+
{
52+
var retval = await base.CreateSocketAsync();
53+
54+
if (this.authenticationProvider != null && !(await AuthAsync(retval)))
4055
{
4156
_logger.LogError("Authentication failed: " + this.EndPoint);
4257

@@ -76,6 +91,32 @@ private bool Auth(PooledSocket socket)
7691

7792
return true;
7893
}
94+
95+
private async Task<bool> AuthAsync(PooledSocket socket)
96+
{
97+
SaslStep currentStep = new SaslStart(this.authenticationProvider);
98+
99+
await socket.WriteAsync(currentStep.GetBuffer());
100+
101+
while (!(await currentStep.ReadResponseAsync(socket)).Success)
102+
{
103+
// challenge-response authentication
104+
if (currentStep.StatusCode == 0x21)
105+
{
106+
currentStep = new SaslContinue(this.authenticationProvider, currentStep.Data);
107+
await socket.WriteAsync(currentStep.GetBuffer());
108+
}
109+
else
110+
{
111+
_logger.LogWarning("Authentication failed, return code: 0x{0:x}", currentStep.StatusCode);
112+
113+
// invalid credentials or other error
114+
return false;
115+
}
116+
}
117+
118+
return true;
119+
}
79120
}
80121
}
81122

0 commit comments

Comments
 (0)