forked from anthonyreilly/NetCoreForce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthenticationClient.cs
332 lines (295 loc) · 16.7 KB
/
AuthenticationClient.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using Newtonsoft.Json;
using System.Threading.Tasks;
using NetCoreForce.Client.Models;
namespace NetCoreForce.Client
{
public class AuthenticationClient : IDisposable
{
private string DefaultApiVersion { get { return "v57.0"; } }
public string ApiVersion { get; set; }
/// <summary>
/// The access token response from a successful authentication.
/// </summary>
/// <returns><see cref="AccessTokenResponse" /></returns>
public AccessTokenResponse AccessInfo { get; private set; }
private const string UserAgent = "netcoreforce-client";
private const string TokenRequestEndpointUrl = "https://login.salesforce.com/services/oauth2/token";
private readonly HttpClient _httpClient;
/// <summary>
/// Initialize the AuthenticationClient with the libary's default Salesforce API version, and default HttpClient
/// <para>See the DefaultApiVersion property</para>
/// </summary>
public AuthenticationClient() : this(null, null) { }
/// <summary>
/// Initialize the AuthenticationClient with the specified Salesforce API version and/or HttpClient
/// </summary>
/// <param name="apiVersion">Target Salesforce API version</param>
/// <param name="httpClient">Custom HttpClient (Optional)</param>
public AuthenticationClient(string apiVersion = null, HttpClient httpClient = null)
{
if (!string.IsNullOrEmpty(apiVersion))
{
ApiVersion = apiVersion;
}
else
{
ApiVersion = DefaultApiVersion;
}
_httpClient = httpClient ?? new HttpClient();
}
/// <summary>
/// Authenticate using the "Username and Password" auth flow, synchronously
/// <para>Uses a default Token request endpoint URL: https://login.salesforce.com/services/oauth2/token</para>
/// </summary>
/// <param name="clientId">Client ID, a.k.a. Consumer Key</param>
/// <param name="clientSecret">Client Secret, a.k.a. Consumer Secret</param>
/// <param name="username">Username</param>
/// <param name="password">Password</param>
/// <exception cref="ForceAuthException">Thrown if the authentication fails</exception>
public void UsernamePassword(string clientId, string clientSecret, string username, string password)
{
UsernamePassword(clientId, clientSecret, username, password, TokenRequestEndpointUrl);
}
/// <summary>
/// Authenticate using the "Username and Password" auth flow, synchronously
/// <para>Uses a default Token request endpoint URL: https://login.salesforce.com/services/oauth2/token</para>
/// </summary>
/// <param name="clientId">Client ID, a.k.a. Consumer Key</param>
/// <param name="clientSecret">Client Secret, a.k.a. Consumer Secret</param>
/// <param name="username">Username</param>
/// <param name="password">Password</param>
/// <param name="tokenRequestEndpointUrl">Token request endpoint URL, e.g. https://login.salesforce.com/services/oauth2/token</param>
/// <exception cref="ForceAuthException">Thrown if the authentication fails</exception>
public void UsernamePassword(string clientId, string clientSecret, string username, string password, string tokenRequestEndpointUrl)
{
try
{
var task = UsernamePasswordAsync(clientId, clientSecret, username, password, tokenRequestEndpointUrl);
task.Wait();
}
catch (AggregateException ex)
{
// Will typically be a single ForceAuthException exception - unwrap and throw
if (ex.InnerException != null && ex.InnerExceptions != null && ex.InnerExceptions.Count == 1)
{
throw ex.InnerException;
}
//otherwise throw the original AggregateException as-is
throw;
}
}
/// <summary>
/// Authenticate using the "Username and Password" auth flow
/// <para>Uses a default Token request endpoint URL: https://login.salesforce.com/services/oauth2/token</para>
/// </summary>
/// <param name="clientId">Client ID, a.k.a. Consumer Key</param>
/// <param name="clientSecret">Client Secret, a.k.a. Consumer Secret</param>
/// <param name="username">Username</param>
/// <param name="password">Password</param>
/// <exception cref="ForceAuthException">Thrown if the authentication fails</exception>
public Task UsernamePasswordAsync(string clientId, string clientSecret, string username, string password)
{
return UsernamePasswordAsync(clientId, clientSecret, username, password, TokenRequestEndpointUrl);
}
/// <summary>
/// Authenticate using the "Username and Password" auth flow
/// </summary>
/// <param name="clientId">Client ID, a.k.a. Consumer Key</param>
/// <param name="clientSecret">Client Secret, a.k.a. Consumer Secret</param>
/// <param name="username">Username</param>
/// <param name="password">Password</param>
/// <param name="tokenRequestEndpointUrl">Token request endpoint URL, e.g. https://login.salesforce.com/services/oauth2/token</param>
/// <exception cref="ForceAuthException">Thrown if the authentication fails</exception>
public async Task UsernamePasswordAsync(string clientId, string clientSecret, string username, string password, string tokenRequestEndpointUrl)
{
#if DEBUG
Stopwatch sw = new Stopwatch();
sw.Start();
#endif
if (string.IsNullOrEmpty(clientId)) throw new ArgumentNullException("clientId", "Client ID is null or empty");
if (string.IsNullOrEmpty(clientSecret)) throw new ArgumentNullException("clientSecret", "Client Secret is null or empty");
if (string.IsNullOrEmpty(username)) throw new ArgumentNullException("username", "Username is null or empty");
if (string.IsNullOrEmpty(password)) throw new ArgumentNullException("password", "Password is null or empty");
if (string.IsNullOrEmpty(tokenRequestEndpointUrl)) throw new ArgumentNullException("tokenRequestEndpointUrl", "Token Request Endpoint is null or empty");
if (!Uri.IsWellFormedUriString(tokenRequestEndpointUrl, UriKind.Absolute)) throw new FormatException("Invalid tokenRequestEndpointUrl");
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("client_id", clientId),
new KeyValuePair<string, string>("client_secret", clientSecret),
new KeyValuePair<string, string>("username", username),
new KeyValuePair<string, string>("password", password)
});
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(tokenRequestEndpointUrl),
Content = content
};
request.Headers.UserAgent.ParseAdd(string.Concat(UserAgent, "/", ApiVersion));
var responseMessage = await _httpClient.SendAsync(request).ConfigureAwait(false);
var response = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
if (responseMessage.IsSuccessStatusCode)
{
this.AccessInfo = JsonConvert.DeserializeObject<AccessTokenResponse>(response);
}
else if (responseMessage.StatusCode == HttpStatusCode.NotFound)
{
// Unable to reach the auth/token url
throw new ForceAuthException(Error.Unknown.ToString(), "Error reaching Login URL", responseMessage.StatusCode);
}
else
{
var errorResponse = JsonConvert.DeserializeObject<AuthErrorResponse>(response);
throw new ForceAuthException(errorResponse.Error, errorResponse.ErrorDescription, responseMessage.StatusCode);
}
#if DEBUG
sw.Stop();
Debug.WriteLine(string.Format("Login completed in {0}ms", sw.ElapsedMilliseconds.ToString()));
#endif
return;
}
/// <summary>
/// Web Server OAuth Authentication Flow
/// <para>https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_understanding_web_server_oauth_flow.htm</para>
/// </summary>
/// <param name="clientId">The Consumer Key from the connected app definition.</param>
/// <param name="clientSecret">The Consumer Secret from the connected app definition. Required unless the Require Secret for Web Server Flow setting is not enabled in the connected app definition.</param>
/// <param name="redirectUri">The Callback URL from the connected app definition.</param>
/// <param name="code">Authorization code the consumer must use to obtain the access and refresh tokens.</param>
/// <param name="tokenRequestEndpointUrl">Salesforce token request endpoint</param>
/// <returns><see cref="AccessTokenResponse" /></returns>
public async Task WebServerAsync(string clientId, string clientSecret, string redirectUri, string code, string tokenRequestEndpointUrl = TokenRequestEndpointUrl)
{
if (string.IsNullOrEmpty(clientId)) throw new ArgumentNullException("clientId");
if (string.IsNullOrEmpty(clientSecret)) throw new ArgumentNullException("clientSecret");
if (string.IsNullOrEmpty(redirectUri)) throw new ArgumentNullException("redirectUri");
if (!Uri.IsWellFormedUriString(redirectUri, UriKind.Absolute)) throw new FormatException("redirectUri");
if (string.IsNullOrEmpty(code)) throw new ArgumentNullException("code");
if (string.IsNullOrEmpty(tokenRequestEndpointUrl)) throw new ArgumentNullException("tokenRequestEndpointUrl");
if (!Uri.IsWellFormedUriString(tokenRequestEndpointUrl, UriKind.Absolute)) throw new FormatException("tokenRequestEndpointUrl");
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "authorization_code"),
new KeyValuePair<string, string>("client_id", clientId),
new KeyValuePair<string, string>("client_secret", clientSecret),
new KeyValuePair<string, string>("redirect_uri", redirectUri),
new KeyValuePair<string, string>("code", code)
});
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(tokenRequestEndpointUrl),
Content = content
};
request.Headers.UserAgent.ParseAdd(string.Concat(UserAgent, "/", ApiVersion));
var responseMessage = await _httpClient.SendAsync(request).ConfigureAwait(false);
var response = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
if (responseMessage.IsSuccessStatusCode)
{
this.AccessInfo = JsonConvert.DeserializeObject<AccessTokenResponse>(response);
}
else
{
try
{
var errorResponse = JsonConvert.DeserializeObject<AuthErrorResponse>(response);
throw new ForceAuthException(errorResponse.Error, errorResponse.ErrorDescription, responseMessage.StatusCode);
}
catch (Exception ex)
{
throw new ForceAuthException("Unknown", ex.Message, responseMessage.StatusCode);
}
}
}
/// <summary>
/// Obtain a new access token using a refresh token
/// </summary>
/// <param name="refreshToken">The refresh token the client application already received.</param>
/// <param name="clientId">The Consumer Key from the connected app definition.</param>
/// <param name="clientSecret">The Consumer Secret from the connected app definition. Required unless the Require Secret for Web Server Flow setting is not enabled in the connected app definition.</param>
/// <param name="tokenRequestEndpointUrl"></param>
/// <returns></returns>
public async Task TokenRefreshAsync(string refreshToken, string clientId, string clientSecret = "", string tokenRequestEndpointUrl = TokenRequestEndpointUrl)
{
var uri = UriFormatter.RefreshTokenUrl(
tokenRequestEndpointUrl,
refreshToken,
clientId,
clientSecret);
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = uri
};
request.Headers.UserAgent.ParseAdd(string.Concat(UserAgent, "/", ApiVersion));
var responseMessage = await _httpClient.SendAsync(request).ConfigureAwait(false);
var response = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
if (responseMessage.IsSuccessStatusCode)
{
this.AccessInfo = JsonConvert.DeserializeObject<AccessTokenResponse>(response);
this.AccessInfo.RefreshToken = refreshToken; //not included in reponse
}
else
{
var errorResponse = JsonConvert.DeserializeObject<AuthErrorResponse>(response);
throw new ForceAuthException(errorResponse.Error, errorResponse.ErrorDescription, responseMessage.StatusCode);
}
}
/// <summary>
/// Client credentials OAuth Authentication Flow
/// <para>https://help.salesforce.com/s/articleView?id=sf.remoteaccess_oauth_client_credentials_flow.htm</para>
/// </summary>
/// <param name="clientId">The Consumer Key from the connected app definition.</param>
/// <param name="clientSecret">The Consumer Secret from the connected app definition. Required unless the Require Secret for Web Server Flow setting is not enabled in the connected app definition.</param>
/// <param name="tokenRequestEndpointUrl">Salesforce token request endpoint</param>
/// <returns><see cref="AccessTokenResponse" /></returns>
public async Task ClientCredentialsAsync(string clientId, string clientSecret,string tokenRequestEndpointUrl = TokenRequestEndpointUrl)
{
if (string.IsNullOrEmpty(clientId)) throw new ArgumentNullException("clientId");
if (string.IsNullOrEmpty(clientSecret)) throw new ArgumentNullException("clientSecret");
if (string.IsNullOrEmpty(tokenRequestEndpointUrl)) throw new ArgumentNullException("tokenRequestEndpointUrl");
if (!Uri.IsWellFormedUriString(tokenRequestEndpointUrl, UriKind.Absolute)) throw new FormatException("tokenRequestEndpointUrl");
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "client_credentials"),
new KeyValuePair<string, string>("client_id", clientId),
new KeyValuePair<string, string>("client_secret", clientSecret),
});
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(tokenRequestEndpointUrl),
Content = content
};
request.Headers.UserAgent.ParseAdd(string.Concat(UserAgent, "/", ApiVersion));
var responseMessage = await _httpClient.SendAsync(request).ConfigureAwait(false);
var response = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
if (responseMessage.IsSuccessStatusCode)
{
this.AccessInfo = JsonConvert.DeserializeObject<AccessTokenResponse>(response);
}
else
{
try
{
var errorResponse = JsonConvert.DeserializeObject<AuthErrorResponse>(response);
throw new ForceAuthException(errorResponse.Error, errorResponse.ErrorDescription, responseMessage.StatusCode);
}
catch (Exception ex)
{
throw new ForceAuthException("Unknown", ex.Message, responseMessage.StatusCode);
}
}
}
public void Dispose()
{
_httpClient.Dispose();
}
}
}