-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOAuthHandler.cs
More file actions
699 lines (593 loc) · 25.7 KB
/
OAuthHandler.cs
File metadata and controls
699 lines (593 loc) · 25.7 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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography;
using System.Threading.Tasks;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Utils;
using Contentstack.Management.Core.Services.OAuth;
namespace Contentstack.Management.Core
{
/// <summary>
/// Handles OAuth 2.0 authentication flow for Contentstack Management API.
/// Supports both traditional OAuth flow (with client secret) and PKCE flow (without client secret).
/// </summary>
public class OAuthHandler
{
#region Private Fields
private readonly ContentstackClient _client;
private readonly OAuthOptions _options;
private readonly string _clientId;
private string codeVerifier = "";
private string codeChallenge = "";
#endregion
#region Constructor
/// <summary>
/// Initializes a new instance of the OAuthHandler class.
/// </summary>
/// <param name="client">The Contentstack client instance.</param>
/// <param name="options">The OAuth configuration options.</param>
/// <exception cref="ArgumentNullException">Thrown when client or options is null.</exception>
/// <exception cref="ArgumentException">Thrown when options are invalid.</exception>
public OAuthHandler(ContentstackClient client, OAuthOptions options)
{
if (client == null)
throw new ArgumentNullException(nameof(client), "Contentstack client cannot be null.");
if (options == null)
throw new ArgumentNullException(nameof(options), "OAuth options cannot be null.");
// Validate OAuth options and throw specific exception if invalid
options.Validate();
_client = client;
_options = options;
_clientId = options.ClientId;
}
#endregion
#region Public Properties
public string ClientId => _clientId;
public string AppId => _options.AppId;
public string RedirectUri => _options.RedirectUri;
public bool UsePkce => _options.UsePkce;
public string[] Scope => _options.Scope;
#endregion
#region Helper Methods
private OAuthTokens ValidateAndGetTokens()
{
var tokens = GetCurrentTokens();
if (tokens == null)
{
throw new Exceptions.OAuthException("No OAuth tokens found. Please authenticate first.");
}
return tokens;
}
private void SetClientOAuthTokens(OAuthTokens tokens)
{
GetClient().contentstackOptions.Authtoken = tokens.AccessToken;
GetClient().contentstackOptions.IsOAuthToken = true;
}
private void UpdateTokenProperty<T>(Action<OAuthTokens, T> setter, T value)
{
var tokens = GetCurrentTokens();
if (tokens == null)
{
tokens = new OAuthTokens { ClientId = _clientId };
}
setter(tokens, value);
StoreTokens(tokens);
}
private string LogoutInternal()
{
var currentTokens = ValidateAndGetTokens();
// Try to revoke the OAuth app authorization (optional - if it fails, we still clear tokens)
// Only attempt revocation if we have valid tokens
if (!string.IsNullOrEmpty(currentTokens.AccessToken))
{
try
{
var authorizationId = GetOauthAppAuthorizationAsync().GetAwaiter().GetResult();
RevokeOauthAppAuthorizationAsync(authorizationId).GetAwaiter().GetResult();
}
catch
{
// If revocation fails, continue with logout
// This is common in OAuth implementations where revocation is optional
}
}
// Clear tokens from memory store
ClearTokens();
// Return success message
return "Logged out successfully";
}
private Exceptions.OAuthException HandleOAuthException(Exception ex, string operation)
{
if (ex is Exceptions.OAuthException)
return (Exceptions.OAuthException)ex;
return new Exceptions.OAuthException($"Failed to {operation}: {ex.Message}", ex);
}
#endregion
#region Public Methods
public OAuthTokens GetCurrentTokens()
{
return _client.GetStoredOAuthTokens(_clientId);
}
public bool HasValidTokens()
{
var tokens = _client.GetStoredOAuthTokens(_clientId);
return tokens?.IsValid == true;
}
public bool HasTokens()
{
return _client.HasStoredOAuthTokens(_clientId);
}
public void ClearTokens()
{
_client.ClearStoredOAuthTokens(_clientId);
}
private void StoreTokens(OAuthTokens tokens)
{
_client.StoreOAuthTokens(_clientId, tokens);
}
public override string ToString()
{
return $"OAuthHandler: ClientId={_clientId}, AppId={_options.AppId}, UsePkce={_options.UsePkce}, HasTokens={HasTokens()}";
}
#region Token Getter Methods
public string GetAccessToken()
{
var tokens = GetCurrentTokens();
return tokens?.AccessToken;
}
public string GetRefreshToken()
{
var tokens = GetCurrentTokens();
return tokens?.RefreshToken;
}
public string GetOrganizationUID()
{
var tokens = GetCurrentTokens();
return tokens?.OrganizationUid;
}
public string GetUserUID()
{
var tokens = GetCurrentTokens();
return tokens?.UserUid;
}
public DateTime? GetTokenExpiryTime()
{
var tokens = GetCurrentTokens();
return tokens?.ExpiresAt;
}
#endregion
#region Token Setter Methods
public void SetAccessToken(string token)
{
if (string.IsNullOrEmpty(token))
throw new ArgumentException("Access token is required.", nameof(token));
UpdateTokenProperty((t, v) => t.AccessToken = v, token);
}
public void SetRefreshToken(string token)
{
if (string.IsNullOrEmpty(token))
throw new ArgumentException("Refresh token is required.", nameof(token));
UpdateTokenProperty((t, v) => t.RefreshToken = v, token);
}
public void SetOrganizationUID(string organizationUID)
{
if (string.IsNullOrEmpty(organizationUID))
throw new ArgumentException("Organization UID is required.", nameof(organizationUID));
UpdateTokenProperty((t, v) => t.OrganizationUid = v, organizationUID);
}
public void SetUserUID(string userUID)
{
if (string.IsNullOrEmpty(userUID))
throw new ArgumentException("User UID is required.", nameof(userUID));
UpdateTokenProperty((t, v) => t.UserUid = v, userUID);
}
public void SetTokenExpiryTime(DateTime expiryTime)
{
if (expiryTime == default(DateTime))
throw new ArgumentException("Token expiry time is required.", nameof(expiryTime));
UpdateTokenProperty((t, v) => t.ExpiresAt = v, expiryTime);
}
#endregion
#endregion
#region Protected Methods
protected ContentstackClient GetClient()
{
return _client;
}
protected OAuthOptions GetOptions()
{
return _options;
}
#endregion
#region OAuth Flow Methods
/// <summary>
/// Generates the OAuth authorization URL for user authentication.
/// </summary>
public async Task<string> AuthorizeAsync()
{
// AppId validation is now handled by OAuthOptions.Validate() in constructor
try
{
// Build the base authorization URL using the correct OAuth hostname
// Transform api.contentstack.io -> app.contentstack.com for OAuth authorization
var oauthHost = GetOAuthHost(GetClient().contentstackOptions.Host);
var baseUrl = $"https://{oauthHost}/#!/apps/{_options.AppId}/authorize";
var authUrl = new UriBuilder(baseUrl);
// Add required OAuth parameters
var queryParams = new List<string>
{
$"response_type={Uri.EscapeDataString(_options.ResponseType)}",
$"client_id={Uri.EscapeDataString(_options.ClientId)}",
$"redirect_uri={Uri.EscapeDataString(_options.RedirectUri)}"
};
// Add scopes if provided
if (_options.Scope != null && _options.Scope.Length > 0)
{
var scopeString = string.Join(" ", _options.Scope);
queryParams.Add($"scope={Uri.EscapeDataString(scopeString)}");
}
// Handle PKCE vs Traditional OAuth flow
if (_options.UsePkce)
{
// PKCE flow - generate code verifier and challenge
this.codeVerifier = PkceHelper.GenerateCodeVerifier();
this.codeChallenge = PkceHelper.GenerateCodeChallenge(codeVerifier);
// Add PKCE parameters
queryParams.Add($"code_challenge={Uri.EscapeDataString(codeChallenge)}");
queryParams.Add("code_challenge_method=S256");
}
// Traditional OAuth flow - no additional parameters needed
// Build the complete URL
authUrl.Query = string.Join("&", queryParams);
return await Task.FromResult(authUrl.ToString());
}
catch (Exception ex) when (!(ex is Exceptions.OAuthException))
{
throw new Exceptions.OAuthAuthorizationException($"Failed to generate OAuth authorization URL: {ex.Message}", ex);
}
}
/// <summary>
/// Exchanges an authorization code for OAuth access and refresh tokens.
/// </summary>
public async Task<OAuthTokens> ExchangeCodeForTokenAsync(string authorizationCode)
{
if (string.IsNullOrEmpty(authorizationCode))
{
throw new ArgumentException("Authorization code is required.", nameof(authorizationCode));
}
try
{
// Create the OAuth token service for authorization code exchange
OAuthTokenService tokenService;
if (_options.UsePkce)
{
// PKCE code verifier should be available from the instance
if (string.IsNullOrEmpty(this.codeVerifier))
{
throw new Exceptions.OAuthConfigurationException(
"PKCE code verifier not found. Make sure to call AuthorizeAsync() before ExchangeCodeForTokenAsync().");
}
}
if (_options.UsePkce && !string.IsNullOrEmpty(this.codeVerifier))
{
tokenService = OAuthTokenService.CreateForAuthorizationCode(
serializer: GetClient().serializer,
authorizationCode: authorizationCode,
clientId: _options.ClientId,
redirectUri: _options.RedirectUri,
codeVerifier: this.codeVerifier
);
}
else
{
if (string.IsNullOrEmpty(_options.ClientSecret))
{
throw new Exceptions.OAuthConfigurationException(
"Client secret is required for traditional OAuth flow. Set ClientSecret in OAuth options or use PKCE flow.");
}
tokenService = OAuthTokenService.CreateForAuthorizationCode(
serializer: GetClient().serializer,
authorizationCode: authorizationCode,
clientId: _options.ClientId,
redirectUri: _options.RedirectUri,
clientSecret: _options.ClientSecret
);
}
// Make the token exchange request
var response = await GetClient().InvokeAsync<OAuthTokenService, ContentstackResponse>(tokenService);
// Parse the OAuth response from the ContentstackResponse
var oauthResponse = response.OpenTResponse<OAuthResponse>();
// Create OAuth tokens from the response
var tokens = new OAuthTokens
{
AccessToken = oauthResponse.AccessToken,
RefreshToken = oauthResponse.RefreshToken,
ExpiresAt = DateTime.UtcNow.AddSeconds(oauthResponse.ExpiresIn - 60), // 60 second buffer
OrganizationUid = oauthResponse.OrganizationUid,
UserUid = oauthResponse.UserUid,
ClientId = _clientId,
AppId = _options.AppId
};
// Store tokens in memory for future use
StoreTokens(tokens);
// Set OAuth tokens in the client for authenticated requests
SetClientOAuthTokens(tokens);
return tokens;
}
catch (Exception ex) when (!(ex is ArgumentException || ex is Exceptions.OAuthException))
{
throw HandleOAuthException(ex, "exchange authorization code for tokens");
}
}
/// <summary>
/// Refreshes the OAuth access token using the refresh token.
/// </summary>
public async Task<OAuthTokens> RefreshTokenAsync(string refreshToken = null)
{
// Get the refresh token to use
string tokenToUse = refreshToken;
if (string.IsNullOrEmpty(tokenToUse))
{
// Get refresh token from stored tokens
var storedTokens = GetCurrentTokens();
if (storedTokens?.RefreshToken == null)
{
throw new Exceptions.OAuthTokenRefreshException(
"No refresh token available. Please authenticate first.");
}
tokenToUse = storedTokens.RefreshToken;
}
try
{
// Create the OAuth token service for token refresh
OAuthTokenService tokenService;
if (_options.UsePkce)
{
// PKCE flow - no client secret needed
tokenService = OAuthTokenService.CreateForRefreshToken(
serializer: GetClient().serializer,
refreshToken: tokenToUse,
clientId: _options.ClientId
);
}
else
{
// Traditional OAuth flow - use client secret
if (string.IsNullOrEmpty(_options.ClientSecret))
{
throw new Exceptions.OAuthConfigurationException(
"Client secret is required for traditional OAuth flow.");
}
tokenService = OAuthTokenService.CreateForRefreshToken(
serializer: GetClient().serializer,
refreshToken: tokenToUse,
clientId: _options.ClientId,
clientSecret: _options.ClientSecret
);
}
// Make the token refresh request
var response = await GetClient().InvokeAsync<OAuthTokenService, ContentstackResponse>(tokenService);
// Parse the OAuth response from the ContentstackResponse
var oauthResponse = response.OpenTResponse<OAuthResponse>();
// Create new OAuth tokens from the response
var newTokens = new OAuthTokens
{
AccessToken = oauthResponse.AccessToken,
RefreshToken = oauthResponse.RefreshToken ?? tokenToUse, // Keep existing refresh token if not provided
ExpiresAt = DateTime.UtcNow.AddSeconds(oauthResponse.ExpiresIn - 60), // 60 second buffer
OrganizationUid = oauthResponse.OrganizationUid,
UserUid = oauthResponse.UserUid,
ClientId = _clientId,
AppId = _options.AppId
};
// Store the new tokens in memory
StoreTokens(newTokens);
// Set OAuth tokens in the client for authenticated requests
SetClientOAuthTokens(newTokens);
return newTokens;
}
catch (Exception ex) when (!(ex is Exceptions.OAuthException))
{
throw HandleOAuthException(ex, "refresh OAuth tokens");
}
}
/// <summary>
/// Logs out the user by clearing OAuth tokens.
/// </summary>
public async Task<string> LogoutAsync()
{
try
{
var currentTokens = ValidateAndGetTokens();
// Try to revoke the OAuth app authorization (optional - if it fails, we still clear tokens)
// Only attempt revocation if we have valid tokens
if (!string.IsNullOrEmpty(currentTokens.AccessToken))
{
try
{
var authorizationId = await GetOauthAppAuthorizationAsync();
await RevokeOauthAppAuthorizationAsync(authorizationId);
}
catch
{
// If revocation fails, continue with logout
// This is common in OAuth implementations where revocation is optional
}
}
// Clear tokens from memory store
ClearTokens();
// Return success message
return "Logged out successfully";
}
catch (Exception ex) when (!(ex is Exceptions.OAuthException))
{
throw new InvalidOperationException($"Failed to logout: {ex.Message}", ex);
}
}
/// <summary>
/// Logs out the user synchronously by clearing OAuth tokens.
/// </summary>
public string Logout()
{
try
{
return LogoutInternal();
}
catch (Exception ex) when (!(ex is InvalidOperationException))
{
throw new InvalidOperationException($"Failed to logout: {ex.Message}", ex);
}
}
/// <summary>
/// Handles the redirect URL after OAuth authorization and exchanges the authorization code for tokens.
/// </summary>
public async Task HandleRedirectAsync(string url)
{
if (string.IsNullOrEmpty(url))
throw new ArgumentException("Redirect URL is required.", nameof(url));
try
{
// Parse the URL to extract the authorization code
var uri = new Uri(url);
var query = uri.Query.TrimStart('?');
var queryParams = new Dictionary<string, string>();
if (!string.IsNullOrEmpty(query))
{
foreach (var param in query.Split('&'))
{
var parts = param.Split('=');
if (parts.Length == 2)
{
queryParams[Uri.UnescapeDataString(parts[0])] = Uri.UnescapeDataString(parts[1]);
}
}
}
var code = queryParams.ContainsKey("code") ? queryParams["code"] : null;
if (string.IsNullOrEmpty(code))
{
throw new Exceptions.OAuthException("Authorization code not found in redirect URL.");
}
// Exchange the authorization code for tokens
await ExchangeCodeForTokenAsync(code);
}
catch (Exception ex) when (!(ex is ArgumentException || ex is Exceptions.OAuthException))
{
throw HandleOAuthException(ex, "handle redirect URL");
}
}
#endregion
#region Private Methods
private static string GetOAuthHost(string baseHost)
{
if (string.IsNullOrEmpty(baseHost))
return baseHost;
// Extract hostname from URL if it contains protocol
var oauthHost = baseHost;
if (oauthHost.StartsWith("https://"))
{
oauthHost = oauthHost.Substring(8); // Remove "https://"
}
else if (oauthHost.StartsWith("http://"))
{
oauthHost = oauthHost.Substring(7); // Remove "http://"
}
// Transform api.contentstack.io -> app.contentstack.com
// Replace .io with .com
if (oauthHost.EndsWith(".io"))
{
oauthHost = oauthHost.Replace(".io", ".com");
}
// Replace 'api' with 'app'
if (oauthHost.Contains("api."))
{
oauthHost = oauthHost.Replace("api.", "app.");
}
return oauthHost;
}
private async Task<string> GetOauthAppAuthorizationAsync()
{
var tokens = ValidateAndGetTokens();
try
{
// Create a service to get OAuth app authorizations
var service = new OAuthAppAuthorizationService(
GetClient().serializer,
_options.AppId,
tokens.OrganizationUid
);
SetClientOAuthTokens(tokens);
try
{
// Make the API call to get authorizations
var response = await GetClient().InvokeAsync<OAuthAppAuthorizationService, ContentstackResponse>(service);
var authResponse = response.OpenTResponse<OAuthAppAuthorizationResponse>();
if (authResponse?.Data?.Length > 0)
{
var userUid = tokens.UserUid;
var currentUserAuthorization = authResponse.Data.FirstOrDefault(auth => auth.User?.Uid == userUid);
if (currentUserAuthorization == null)
{
throw new Exceptions.OAuthException("No authorizations found for current user.");
}
return currentUserAuthorization.AuthorizationUid;
}
else
{
throw new Exceptions.OAuthException("No authorizations found for the app.");
}
}
catch (Exception ex) when (!(ex is Exceptions.OAuthException))
{
throw HandleOAuthException(ex, "get OAuth app authorization");
}
}
catch (Exception ex) when (!(ex is Exceptions.OAuthException))
{
throw HandleOAuthException(ex, "get OAuth app authorization");
}
}
private async Task RevokeOauthAppAuthorizationAsync(string authorizationId)
{
if (string.IsNullOrEmpty(authorizationId))
{
throw new ArgumentException("Authorization ID is required.", nameof(authorizationId));
}
try
{
// Get current tokens to access organization UID
var tokens = GetCurrentTokens();
var organizationUid = tokens?.OrganizationUid;
// Create a service to revoke OAuth app authorization
var service = new Services.OAuth.OAuthAppRevocationService(
GetClient().serializer,
_options.AppId,
authorizationId,
organizationUid
);
SetClientOAuthTokens(tokens);
try
{
// Make the API call to revoke authorization
var response = await GetClient().InvokeAsync<OAuthAppRevocationService, ContentstackResponse>(service);
}
catch (Exception ex)
{
throw ex;
}
finally
{
// Clear OAuth tokens after successful revocation (for logout scenario)
GetClient().contentstackOptions.Authtoken = null;
GetClient().contentstackOptions.IsOAuthToken = false;
}
}
catch (Exception ex) when (!(ex is ArgumentException || ex is Exceptions.OAuthException))
{
throw HandleOAuthException(ex, "revoke OAuth app authorization");
}
}
#endregion
}
}