Skip to content

Commit d433d92

Browse files
authored
Merge pull request #128 from cnblogs/hotfix/127-authentication-failure
Override CreateSocketAsync in BinaryNode
2 parents e124b56 + 49a1681 commit d433d92

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

Enyim.Caching/EnyimMemcachedApplicationBuilderExtensions.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ public static IApplicationBuilder UseEnyimMemcached(this IApplicationBuilder app
1717
var logger = app.ApplicationServices.GetService<ILogger<IMemcachedClient>>();
1818
try
1919
{
20-
var client = app.ApplicationServices.GetRequiredService<IMemcachedClient>();
21-
client.GetValueAsync<string>("UseEnyimMemcached").Wait();
22-
Console.WriteLine("EnyimMemcached connected memcached servers.");
20+
var client = app.ApplicationServices.GetRequiredService<IMemcachedClient>();
2321
}
2422
catch (Exception ex)
2523
{

Enyim.Caching/Memcached/IAuthenticator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public interface IAuthenticator
1111
#region [ License information ]
1212
/* ************************************************************
1313
*
14-
* Copyright (c) 2010 Attila Kiskó, enyim.com
14+
* Copyright (c) 2010 Attila Kisk? enyim.com
1515
*
1616
* Licensed under the Apache License, Version 2.0 (the "License");
1717
* you may not use this file except in compliance with the License.

Enyim.Caching/Memcached/MemcachedNode.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ private async Task<PooledSocket> CreateSocketAsync()
369369

370370
return ps;
371371
}
372+
372373
private PooledSocket CreateSocket()
373374
{
374375
var ps = this.ownerNode.CreateSocket();
@@ -767,6 +768,7 @@ protected internal virtual PooledSocket CreateSocket()
767768
}
768769

769770
}
771+
770772
protected internal virtual async Task<PooledSocket> CreateSocketAsync()
771773
{
772774
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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected override BinaryRequest Build()
3333
#region [ License information ]
3434
/* ************************************************************
3535
*
36-
* Copyright (c) 2010 Attila Kiskó, enyim.com
36+
* Copyright (c) 2010 Attila Kisk? enyim.com
3737
*
3838
* Licensed under the Apache License, Version 2.0 (the "License");
3939
* you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)