|
7 | 7 | using Enyim.Collections;
|
8 | 8 | using System.Security;
|
9 | 9 | using Microsoft.Extensions.Logging;
|
| 10 | +using System.Threading.Tasks; |
10 | 11 |
|
11 | 12 | namespace Enyim.Caching.Memcached.Protocol.Binary
|
12 | 13 | {
|
@@ -36,7 +37,21 @@ protected internal override PooledSocket CreateSocket()
|
36 | 37 | {
|
37 | 38 | var retval = base.CreateSocket();
|
38 | 39 |
|
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))) |
40 | 55 | {
|
41 | 56 | _logger.LogError("Authentication failed: " + this.EndPoint);
|
42 | 57 |
|
@@ -76,6 +91,32 @@ private bool Auth(PooledSocket socket)
|
76 | 91 |
|
77 | 92 | return true;
|
78 | 93 | }
|
| 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 | + } |
79 | 120 | }
|
80 | 121 | }
|
81 | 122 |
|
|
0 commit comments