forked from thenoobsbr/RestSharp.Authenticators.Digest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDigestAuthenticatorManager.cs
More file actions
214 lines (186 loc) · 7.29 KB
/
DigestAuthenticatorManager.cs
File metadata and controls
214 lines (186 loc) · 7.29 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Reflection;
using System.Security.Authentication;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace RestSharp.Authenticators.Digest;
/// <summary>
/// DigestAuthenticatorManager class.
/// </summary>
internal class DigestAuthenticatorManager
{
private static readonly Version _assemblyVersion;
private readonly Uri _host;
private readonly string _password;
private readonly TimeSpan _timeout;
private readonly ILogger _logger;
private readonly string _username;
/// <summary>
/// The cnonce that is generated randomly by the application.
/// </summary>
private string? _cnonce;
/// <summary>
/// The nonce that is returned by the first digest request (without the data).
/// </summary>
private string? _nonce;
/// <summary>
/// The qop that is returned by the first digest request (without the data).
/// </summary>
private string? _qop;
/// <summary>
/// The Realm that is returned by the first digest request (without the data).
/// </summary>
private string? _realm;
private readonly RestClientOptions? _handshakeClientOptions;
/// <summary>
/// The opaque that is returned by the first digest request (without the data).
/// </summary>
private string? _opaque;
static DigestAuthenticatorManager()
{
_assemblyVersion = Assembly.GetAssembly(typeof(DigestAuthenticatorManager)).GetName().Version;
}
/// <summary>
/// Creates a new instance of <see cref="DigestAuthenticatorManager" /> class.
/// </summary>
/// <param name="host">The host.</param>
/// <param name="username">The username.</param>
/// <param name="password">The password.</param>
/// <param name="timeout">The timeout.</param>
/// <param name="logger"></param>
public DigestAuthenticatorManager(Uri host, string username, string password, TimeSpan timeout, RestClientOptions? handshakeClientOptions, ILogger logger)
{
if (string.IsNullOrWhiteSpace(username))
{
throw new ArgumentException("Value cannot be null or whitespace.", nameof(username));
}
if (string.IsNullOrWhiteSpace(password))
{
throw new ArgumentException("Value cannot be null or whitespace.", nameof(password));
}
if (timeout.Ticks <= 0)
{
throw new ArgumentOutOfRangeException(nameof(timeout));
}
_host = host ?? throw new ArgumentNullException(nameof(host));
_username = username;
_password = password;
_timeout = timeout;
_logger = logger;
_handshakeClientOptions = handshakeClientOptions;
}
/// <summary>
/// Gets the digest auth header.
/// </summary>
/// <param name="path">The request path.</param>
/// <param name="method">The request method.</param>
/// <param name="proxy">The request proxy.</param>
public async Task GetDigestAuthHeader(
string path,
Method method,
IWebProxy? proxy = null)
{
_logger.LogDebug("Initiating GetDigestAuthHeader");
var uri = new Uri(_host, path);
var request = new RestRequest(uri, method);
request.AddOrUpdateHeader("Connection", "Keep-Alive");
request.AddOrUpdateHeader("Accept", "*/*");
request.AddOrUpdateHeader("User-Agent", $"RestSharp.Authenticators.Digest/{_assemblyVersion}");
request.AddOrUpdateHeader("Accept-Encoding", "gzip, deflate, br");
request.Timeout = _timeout;
RestClient client;
if (_handshakeClientOptions != null)
{
client = new RestClient(_handshakeClientOptions);
}
else
{
client = new RestClient(new RestClientOptions() { Proxy = proxy });
}
var response = await client.ExecuteAsync(request).ConfigureAwait(false);
client.Dispose();
GetDigestDataFromFailResponse(response);
_logger.LogDebug("GetDigestAuthHeader completed");
}
/// <summary>
/// Gets the digest header.
/// </summary>
/// <param name="digestUri">The digest uri.</param>
/// <param name="method">The method.</param>
/// <returns>The digest header.</returns>
public string GetDigestHeader(string digestUri, Method method)
{
var hash1 = GenerateMD5($"{_username}:{_realm}:{_password}");
var hash2 = GenerateMD5($"{method.ToString().ToUpperInvariant()}:{digestUri}");
var digestResponse =
GenerateMD5($"{hash1}:{_nonce}:{DigestHeader.NONCE_COUNT:00000000}:{_cnonce}:{_qop}:{hash2}");
return $"Digest username=\"{_username}\"," +
$"realm=\"{_realm}\"," +
$"nonce=\"{_nonce}\"," +
$"uri=\"{digestUri}\"," +
"algorithm=MD5," +
$"response=\"{digestResponse}\"," +
(!string.IsNullOrWhiteSpace(_opaque) ? $"opaque=\"{_opaque}\"," : string.Empty) +
$"qop={_qop}," +
$"nc={DigestHeader.NONCE_COUNT:00000000}," +
$"cnonce=\"{_cnonce}\"";
}
/// <summary>
/// Generate the MD5 Hash.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>The MD5.</returns>
private static string GenerateMD5(string input)
{
var inputBytes = Encoding.ASCII.GetBytes(input);
var hash = MD5.Create().ComputeHash(inputBytes);
var stringBuilder = new StringBuilder();
hash.ToList().ForEach(b => stringBuilder.Append(b.ToString("x2")));
return stringBuilder.ToString();
}
private void GetDigestDataFromFailResponse(RestResponse response)
{
if (response.IsSuccessful)
{
_logger.LogInformation("Request is already authenticated");
return;
}
if (response is not { StatusCode: HttpStatusCode.Unauthorized })
{
_logger.LogWarning("Response status code not supported for fail authentication. {StatusCode}", response.StatusCode);
throw new Exception(response.ErrorMessage);
}
var header = response
.Headers?
.First(h => string.Equals(h.Name, "WWW-Authenticate", StringComparison.OrdinalIgnoreCase))
.Value?
.ToString();
if (string.IsNullOrWhiteSpace(header))
{
_logger.LogError("Any WWW-Authenticate header is not found");
throw new AuthenticationException("WWW-Authenticate header is empty.");
}
var digestHeader = new DigestHeader(header!, _logger);
_cnonce = new Random()
.Next(123400, 9999999)
.ToString(CultureInfo.InvariantCulture);
_realm = digestHeader.Realm;
_nonce = digestHeader.Nonce;
_qop = digestHeader.Qop;
if (!string.IsNullOrWhiteSpace(_qop) && _qop!.Contains(','))
{
_qop = _qop!
.Split(',')
.Select(q => q.Trim())
.FirstOrDefault(q => q.Equals("auth", StringComparison.OrdinalIgnoreCase)) // prefer "auth" if the server offered several
?? _qop.Split(',')[0].Trim();
}
_opaque = digestHeader.Opaque;
}
}