-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathAuthenticationClient.cs
More file actions
240 lines (207 loc) · 10.4 KB
/
Copy pathAuthenticationClient.cs
File metadata and controls
240 lines (207 loc) · 10.4 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Salesforce.Common.Models.Json;
namespace Salesforce.Common
{
public class AuthenticationClient : IAuthenticationClient, IDisposable
{
public string InstanceUrl { get; set; }
public string AccessToken { get; set; }
public string RefreshToken { get; set; }
public string Id { get; set; }
public string ApiVersion { get; set; }
private const string UserAgent = "forcedotcom-toolkit-dotnet";
private const string TokenRequestEndpointUrl = "https://login.salesforce.com/services/oauth2/token";
private readonly HttpClient _httpClient;
private readonly bool _disposeHttpClient;
public AuthenticationClient(string apiVersion = "v36.0")
: this(new HttpClient(), apiVersion)
{
}
public AuthenticationClient(HttpClient httpClient, string apiVersion = "v36.0", bool callerWillDisposeHttpClient = false)
{
if (httpClient == null) throw new ArgumentNullException("httpClient");
_httpClient = httpClient;
_disposeHttpClient = !callerWillDisposeHttpClient;
ApiVersion = apiVersion;
}
public Task UsernamePasswordAsync(string clientId, string clientSecret, string username, string password)
{
return UsernamePasswordAsync(clientId, clientSecret, username, password, TokenRequestEndpointUrl);
}
public async Task UsernamePasswordAsync(string clientId, string clientSecret, string username, string password, string tokenRequestEndpointUrl)
{
if (string.IsNullOrEmpty(clientId)) throw new ArgumentNullException("clientId");
if (string.IsNullOrEmpty(clientSecret)) throw new ArgumentNullException("clientSecret");
if (string.IsNullOrEmpty(username)) throw new ArgumentNullException("username");
if (string.IsNullOrEmpty(password)) throw new ArgumentNullException("password");
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", "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)
{
var authToken = JsonConvert.DeserializeObject<AuthToken>(response);
AccessToken = authToken.AccessToken;
InstanceUrl = authToken.InstanceUrl;
Id = authToken.Id;
}
else
{
var errorResponse = JsonConvert.DeserializeObject<AuthErrorResponse>(response);
throw new ForceAuthException(errorResponse.Error, errorResponse.ErrorDescription, responseMessage.StatusCode);
}
}
public Task WebServerAsync(string clientId, string clientSecret, string redirectUri, string code)
{
return WebServerAsync(clientId, clientSecret, redirectUri, code, TokenRequestEndpointUrl);
}
public async Task WebServerAsync(string clientId, string clientSecret, string redirectUri, string code, string 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)
{
var authToken = JsonConvert.DeserializeObject<AuthToken>(response);
AccessToken = authToken.AccessToken;
InstanceUrl = authToken.InstanceUrl;
Id = authToken.Id;
RefreshToken = authToken.RefreshToken;
}
else
{
try
{
var errorResponse = JsonConvert.DeserializeObject<AuthErrorResponse>(response);
throw new ForceAuthException(errorResponse.Error, errorResponse.ErrorDescription);
}
catch (Exception ex)
{
throw new ForceAuthException(Error.UnknownException, ex.Message);
}
}
}
public Task TokenRefreshAsync(string clientId, string refreshToken, string clientSecret = "")
{
return TokenRefreshAsync(clientId, refreshToken, clientSecret, TokenRequestEndpointUrl);
}
public async Task TokenRefreshAsync(string clientId, string refreshToken, string clientSecret, string tokenRequestEndpointUrl)
{
var url = Common.FormatRefreshTokenUrl(
tokenRequestEndpointUrl,
clientId,
refreshToken,
clientSecret);
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(url)
};
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)
{
var authToken = JsonConvert.DeserializeObject<AuthToken>(response);
AccessToken = authToken.AccessToken;
RefreshToken = refreshToken;
InstanceUrl = authToken.InstanceUrl;
Id = authToken.Id;
}
else
{
var errorResponse = JsonConvert.DeserializeObject<AuthErrorResponse>(response);
throw new ForceException(errorResponse.Error, errorResponse.ErrorDescription, responseMessage.StatusCode);
}
}
public async Task GetLatestVersionAsync()
{
try
{
string serviceURL = InstanceUrl + @"/services/data/";
HttpResponseMessage responseMessage = await _httpClient.GetAsync(serviceURL);
var response = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
if (responseMessage.IsSuccessStatusCode)
{
try
{
var jToken = JToken.Parse(response);
if (jToken.Type == JTokenType.Array)
{
var jArray = JArray.Parse(response);
List<Models.Json.Version> versionList = JsonConvert.DeserializeObject<List<Models.Json.Version>>(jArray.ToString());
if (versionList != null && versionList.Count > 0)
{
versionList.Sort();
if (!string.IsNullOrEmpty(versionList.Last().version))
ApiVersion = "v" + versionList.Last().version;
}
}
}
catch (Exception e)
{
throw new ForceException(Error.UnknownException, e.Message);
}
}
else
{
var errorResponse = JsonConvert.DeserializeObject<AuthErrorResponse>(response);
throw new ForceException(errorResponse.Error, errorResponse.ErrorDescription, responseMessage.StatusCode);
}
}
catch (Exception ex)
{
throw new ForceAuthException(Error.UnknownException, ex.Message);
}
}
public void Dispose()
{
if (_disposeHttpClient)
{
_httpClient.Dispose();
}
}
}
}