-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathClient.cs
918 lines (716 loc) · 26.2 KB
/
Client.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
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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Newtonsoft.Json;
using Supabase.Gotrue.Exceptions;
using Supabase.Gotrue.Interfaces;
using Supabase.Gotrue.Mfa;
using static Supabase.Gotrue.Constants;
using static Supabase.Gotrue.Constants.AuthState;
using static Supabase.Gotrue.Exceptions.FailureHint.Reason;
namespace Supabase.Gotrue
{
/// <inheritdoc />
public class Client : IGotrueClient<User, Session>
{
/// <summary>
/// The underlying API requests object that sends the requests
/// </summary>
private readonly IGotrueApi<User, Session> _api;
/// <summary>
/// Handlers for notifications of state changes.
/// </summary>
private readonly List<IGotrueClient<User, Session>.AuthEventHandler> _authEventHandlers =
new List<IGotrueClient<User, Session>.AuthEventHandler>();
/// <summary>
/// Gets notifications if there is a failure not visible by exceptions (e.g. background thread refresh failure)
/// </summary>
private DebugNotification? _debugNotification;
/// <summary>
/// Object called to persist the session (e.g. filesystem or cookie)
/// </summary>
private IGotruePersistenceListener<Session>? _sessionPersistence;
/// <summary>
/// Get the TokenRefresh object, if it exists
/// </summary>
public TokenRefresh? TokenRefresh { get; }
/// <summary>
/// Initializes the GoTrue stateful client.
///
/// You will likely want to at least specify a <see>
/// <cref>ClientOptions.Url</cref>
/// </see>
///
/// Sessions are not automatically retrieved when this object is created.
///
/// If you want to load the session from your persistence store, <see>
/// <cref>GotrueSessionPersistence</cref>
/// </see>.
///
/// If you want to load/refresh the session, <see>
/// <cref>RetrieveSessionAsync</cref>
/// </see>.
///
/// For a typical client application, you'll want to load the session from persistence
/// and then refresh it. If your application is listening for session changes, you'll
/// get two SignIn notifications if the persisted session is valid - one for the
/// session loaded from disk, and a second on a successful session refresh.
///
/// <remarks></remarks>
/// <example>
/// var client = new Supabase.Gotrue.Client(options);
/// client.LoadSession();
/// await client.RetrieveSessionAsync();
/// </example>
/// </summary>
/// <param name="options"></param>
public Client(ClientOptions? options = null)
{
options ??= new ClientOptions();
Options = options;
_api = new Api(options.Url, options.Headers);
if (options.AutoRefreshToken)
{
TokenRefresh = new TokenRefresh(this);
_authEventHandlers.Add(TokenRefresh.ManageAutoRefresh);
}
}
/// <inheritdoc />
public void SetPersistence(IGotrueSessionPersistence<Session> persistence)
{
if (_sessionPersistence != null) _authEventHandlers.Remove(_sessionPersistence.EventHandler);
_sessionPersistence = new PersistenceListener(persistence);
_authEventHandlers.Add(_sessionPersistence.EventHandler);
}
/// <inheritdoc />
public ClientOptions Options { get; }
/// <inheritdoc />
public Task<User?> GetUser(string jwt) => _api.GetUser(jwt);
/// <inheritdoc />
public void NotifyAuthStateChange(AuthState stateChanged)
{
foreach (var handler in _authEventHandlers)
{
try
{
handler.Invoke(this, stateChanged);
}
catch (Exception e)
{
_debugNotification?.Log("Auth State Change Handler Failure", e);
}
}
}
/// <inheritdoc />
public User? CurrentUser
{
get => CurrentSession?.User;
}
/// <inheritdoc />
public void AddStateChangedListener(IGotrueClient<User, Session>.AuthEventHandler authEventHandler)
{
if (_authEventHandlers.Contains(authEventHandler)) return;
_authEventHandlers.Add(authEventHandler);
}
/// <inheritdoc />
public void RemoveStateChangedListener(IGotrueClient<User, Session>.AuthEventHandler authEventHandler)
{
if (!_authEventHandlers.Contains(authEventHandler)) return;
_authEventHandlers.Remove(authEventHandler);
}
/// <inheritdoc />
public void ClearStateChangedListeners()
{
_authEventHandlers.Clear();
}
/// <inheritdoc />
public bool Online { get; set; } = true;
/// <inheritdoc />
public Session? CurrentSession { get; private set; }
/// <inheritdoc />
public Task<Session?> SignUp(string email, string password, SignUpOptions? options = null) =>
SignUp(SignUpType.Email, email, password, options);
/// <inheritdoc />
public async Task<Session?> SignUp(SignUpType type, string identifier, string password,
SignUpOptions? options = null)
{
if (!Online)
throw new GotrueException("Only supported when online", Offline);
DestroySession();
var session = type switch
{
SignUpType.Email => await _api.SignUpWithEmail(identifier, password, options),
SignUpType.Phone => await _api.SignUpWithPhone(identifier, password, options),
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
};
if (session?.User?.ConfirmedAt != null || session?.User != null && Options.AllowUnconfirmedUserSessions)
{
UpdateSession(session);
NotifyAuthStateChange(SignedIn);
return CurrentSession;
}
return session;
}
/// <inheritdoc />
public async Task<bool> SignIn(string email, SignInOptions? options = null)
{
if (!Online)
throw new GotrueException("Only supported when online", Offline);
await _api.SendMagicLinkEmail(email, options);
return true;
}
/// <inheritdoc />
public async Task<Session?> SignInWithIdToken(Provider provider, string idToken, string? accessToken = null, string? nonce = null,
string? captchaToken = null)
{
if (!Online)
throw new GotrueException("Only supported when online", Offline);
DestroySession();
var result = await _api.SignInWithIdToken(provider, idToken, accessToken, nonce, captchaToken);
UpdateSession(result);
NotifyAuthStateChange(SignedIn);
return result;
}
/// <inheritdoc />
public async Task<PasswordlessSignInState> SignInWithOtp(SignInWithPasswordlessEmailOptions options)
{
if (!Online)
throw new GotrueException("Only supported when online", Offline);
DestroySession();
return await _api.SignInWithOtp(options);
}
/// <inheritdoc />
public async Task<PasswordlessSignInState> SignInWithOtp(SignInWithPasswordlessPhoneOptions options)
{
if (!Online)
throw new GotrueException("Only supported when online", Offline);
DestroySession();
return await _api.SignInWithOtp(options);
}
/// <inheritdoc />
public Task<bool> SendMagicLink(string email, SignInOptions? options = null) => SignIn(email, options);
/// <inheritdoc />
public Task<Session?> SignIn(string email, string password) => SignIn(SignInType.Email, email, password);
/// <inheritdoc />
public Task<Session?> SignInWithPassword(string email, string password) => SignIn(email, password);
/// <inheritdoc />
public async Task<Session?> SignIn(SignInType type, string identifierOrToken, string? password = null,
string? scopes = null)
{
if (!Online)
throw new GotrueException("Only supported when online", Offline);
Session? newSession;
switch (type)
{
case SignInType.Email:
newSession = await _api.SignInWithEmail(identifierOrToken, password!);
UpdateSession(newSession);
break;
case SignInType.Phone:
if (string.IsNullOrEmpty(password))
{
await _api.SendMobileOTP(identifierOrToken);
return null;
}
newSession = await _api.SignInWithPhone(identifierOrToken, password!);
UpdateSession(newSession);
break;
case SignInType.RefreshToken:
if (CurrentSession == null || string.IsNullOrEmpty(CurrentSession.AccessToken))
throw new GotrueException("Not logged in.", NoSessionFound);
await RefreshToken(CurrentSession.AccessToken!, identifierOrToken);
return CurrentSession;
default: throw new ArgumentOutOfRangeException(nameof(type), type, null);
}
// Handle case when a user registers and has not confirmed email (and options do not allow for this), return null for session.
if (newSession?.User?.ConfirmedAt == null &&
(newSession?.User == null || !Options.AllowUnconfirmedUserSessions))
return null;
NotifyAuthStateChange(SignedIn);
return CurrentSession;
}
/// <inheritdoc />
public Task<ProviderAuthState> SignIn(Provider provider, SignInOptions? options = null)
{
if (!Online)
throw new GotrueException("Only supported when online", Offline);
DestroySession();
var providerUri = _api.GetUriForProvider(provider, options);
return Task.FromResult(providerUri);
}
/// <inheritdoc />
public Task<SSOResponse?> SignInWithSSO(Guid providerId, SignInWithSSOOptions? options = null)
{
if (!Online)
throw new GotrueException("Only supported when online", Offline);
DestroySession();
return _api.SignInWithSSO(providerId, options);
}
/// <inheritdoc />
public Task<SSOResponse?> SignInWithSSO(string domain, SignInWithSSOOptions? options = null)
{
if (!Online)
throw new GotrueException("Only supported when online", Offline);
DestroySession();
return _api.SignInWithSSO(domain, options);
}
/// <inheritdoc />
public async Task<Session?> SignInAnonymously(SignInAnonymouslyOptions? options = null)
{
if (!Online)
throw new GotrueException("Only supported when online", Offline);
DestroySession();
var newSession = await _api.SignInAnonymously(options);
UpdateSession(newSession);
NotifyAuthStateChange(SignedIn);
return CurrentSession;
}
/// <inheritdoc />
public async Task<Session?> VerifyOTP(string phone, string token, MobileOtpType type = MobileOtpType.SMS)
{
if (!Online)
throw new GotrueException("Only supported when online", Offline);
DestroySession();
var session = await _api.VerifyMobileOTP(phone, token, type);
if (session?.AccessToken != null)
{
UpdateSession(session);
NotifyAuthStateChange(SignedIn);
return session;
}
return null;
}
/// <inheritdoc />
public async Task<Session?> VerifyOTP(string email, string token, EmailOtpType type = EmailOtpType.MagicLink)
{
if (!Online)
throw new GotrueException("Only supported when online", Offline);
DestroySession();
var session = await _api.VerifyEmailOTP(email, token, type);
if (session?.AccessToken != null)
{
UpdateSession(session);
NotifyAuthStateChange(SignedIn);
return session;
}
return null;
}
/// <inheritdoc />
public async Task<Session?> VerifyTokenHash(string tokenHash, EmailOtpType type = EmailOtpType.Email)
{
if (!Online)
throw new GotrueException("Only supported when online", Offline);
DestroySession();
var session = await _api.VerifyTokenHash(tokenHash, type);
if (session?.AccessToken != null)
{
UpdateSession(session);
NotifyAuthStateChange(SignedIn);
return session;
}
return null;
}
/// <inheritdoc />
public Task<ProviderAuthState> LinkIdentity(Provider provider, SignInOptions options)
{
if (!Online)
throw new GotrueException("Only supported when online", Offline);
if (CurrentSession == null || CurrentUser == null)
throw new GotrueException("A valid session is required.", NoSessionFound);
if (options.FlowType != OAuthFlowType.PKCE)
throw new GotrueException("PKCE flow type is required for this action.", InvalidFlowType);
return _api.LinkIdentity(CurrentSession.AccessToken!, provider, options);
}
/// <inheritdoc />
public Task<bool> UnlinkIdentity(UserIdentity userIdentity)
{
if (!Online)
throw new GotrueException("Only supported when online", Offline);
if (CurrentSession == null || CurrentUser == null)
throw new GotrueException("A valid session is required.", NoSessionFound);
return _api.UnlinkIdentity(CurrentSession.AccessToken!, userIdentity);
}
/// <inheritdoc />
public async Task SignOut(SignOutScope scope = SignOutScope.Global)
{
if (CurrentSession?.AccessToken != null) await _api.SignOut(CurrentSession.AccessToken, scope);
UpdateSession(null);
NotifyAuthStateChange(SignedOut);
}
/// <inheritdoc />
public async Task<User?> Update(UserAttributes attributes)
{
if (CurrentSession == null || string.IsNullOrEmpty(CurrentSession.AccessToken))
throw new GotrueException("Not Logged in.");
if (!Online)
throw new GotrueException("Only supported when online", Offline);
var result = await _api.UpdateUser(CurrentSession.AccessToken!, attributes);
CurrentSession.User = result;
NotifyAuthStateChange(UserUpdated);
return result;
}
/// <inheritdoc />
public async Task<bool> Reauthenticate()
{
if (CurrentSession == null || string.IsNullOrEmpty(CurrentSession.AccessToken))
throw new GotrueException("Not Logged in.", NoSessionFound);
if (!Online)
throw new GotrueException("Only supported when online", Offline);
var response = await _api.Reauthenticate(CurrentSession.AccessToken!);
return response.ResponseMessage?.IsSuccessStatusCode ?? false;
}
/// <inheritdoc />
public async Task<bool> ResetPasswordForEmail(string email)
{
var result = await _api.ResetPasswordForEmail(email);
result.ResponseMessage?.EnsureSuccessStatusCode();
return true;
}
/// <inheritdoc />
public async Task<ResetPasswordForEmailState> ResetPasswordForEmail(ResetPasswordForEmailOptions options)
{
var state = await _api.ResetPasswordForEmail(options);
return state;
}
/// <inheritdoc />
public async Task<Session?> RefreshSession()
{
if (CurrentSession == null || string.IsNullOrEmpty(CurrentSession.AccessToken))
throw new GotrueException("Not Logged in.", NoSessionFound);
if (!Online)
throw new GotrueException("Only supported when online", Offline);
await RefreshToken();
var user = await _api.GetUser(CurrentSession.AccessToken!);
CurrentSession.User = user;
return CurrentSession;
}
/// <inheritdoc />
public async Task<Session> SetSession(string accessToken, string refreshToken, bool forceAccessTokenRefresh = false)
{
DestroySession();
if (string.IsNullOrEmpty(accessToken) || string.IsNullOrEmpty(refreshToken))
throw new GotrueException("`accessToken` and `refreshToken` cannot be empty.", NoSessionFound);
var payload = new JwtSecurityTokenHandler().ReadJwtToken(accessToken).Payload;
if (payload == null || payload.ValidTo == DateTime.MinValue)
throw new GotrueException("`accessToken`'s payload was of an unknown structure.", NoSessionFound);
if (payload.ValidTo < DateTime.UtcNow || forceAccessTokenRefresh)
{
var result = await _api.RefreshAccessToken(accessToken, refreshToken);
if (result == null || string.IsNullOrEmpty(result.AccessToken))
throw new GotrueException("Could not generate a session given the provided parameters.", NoSessionFound);
CurrentSession = result;
NotifyAuthStateChange(SignedIn);
return CurrentSession;
}
CurrentSession = new Session
{
AccessToken = accessToken,
RefreshToken = refreshToken,
TokenType = "bearer",
ExpiresIn = payload.Expiration!.Value,
User = await _api.GetUser(accessToken)
};
NotifyAuthStateChange(SignedIn);
return CurrentSession;
}
/// <summary>
/// Parses a <see cref="Session"/> out of a <see cref="Uri"/>'s Query parameters.
/// </summary>
/// <param name="uri"></param>
/// <param name="storeSession"></param>
/// <returns></returns>
public async Task<Session?> GetSessionFromUrl(Uri uri, bool storeSession = true)
{
var query = string.IsNullOrEmpty(uri.Fragment)
? HttpUtility.ParseQueryString(uri.Query)
: HttpUtility.ParseQueryString('?' + uri.Fragment.TrimStart('#'));
var errorDescription = query.Get("error_description");
if (!string.IsNullOrEmpty(errorDescription)) throw new GotrueException(errorDescription, BadSessionUrl);
var accessToken = query.Get("access_token");
if (string.IsNullOrEmpty(accessToken))
throw new GotrueException("No access_token detected.", BadSessionUrl);
var expiresIn = query.Get("expires_in");
if (string.IsNullOrEmpty(expiresIn)) throw new GotrueException("No expires_in detected.", BadSessionUrl);
var refreshToken = query.Get("refresh_token");
if (string.IsNullOrEmpty(refreshToken))
throw new GotrueException("No refresh_token detected.", BadSessionUrl);
var tokenType = query.Get("token_type");
if (string.IsNullOrEmpty(tokenType)) throw new GotrueException("No token_type detected.", BadSessionUrl);
var user = await _api.GetUser(accessToken);
var session = new Session
{
AccessToken = accessToken,
ExpiresIn = long.Parse(expiresIn),
RefreshToken = refreshToken,
TokenType = tokenType,
User = user
};
if (storeSession)
{
UpdateSession(session);
NotifyAuthStateChange(SignedIn);
if (query.Get("type") == "recovery") NotifyAuthStateChange(PasswordRecovery);
}
return session;
}
/// <inheritdoc />
public async Task<Session?> RetrieveSessionAsync()
{
// No session, so just return.
if (CurrentSession == null)
return null;
// Check to see if the session has expired. If so go ahead and destroy it.
if (CurrentSession != null && CurrentSession.Expired())
{
_debugNotification?.Log($"Loaded session has expired");
DestroySession();
return null;
}
// If we aren't online, we can't refresh the token
if (!Online)
{
throw new GotrueException("Only supported when online", Offline);
}
// We have a session, and hasn't expired, and we seem to be online. Let's try to refresh it.
if (Options.AutoRefreshToken && CurrentSession?.RefreshToken != null)
{
try
{
await RefreshToken();
return CurrentSession;
}
catch (Exception e)
{
_debugNotification?.Log($"Failed to refresh token ({e.Message})", e);
_debugNotification?.Log(JsonConvert.SerializeObject(CurrentSession, Formatting.Indented));
DestroySession();
return null;
}
}
return CurrentSession;
}
/// <inheritdoc />
public async Task<Session?> ExchangeCodeForSession(string codeVerifier, string authCode)
{
var result = await _api.ExchangeCodeForSession(codeVerifier, authCode);
if (result != null)
{
UpdateSession(result);
NotifyAuthStateChange(SignedIn);
return CurrentSession;
}
return null;
}
/// <summary>
/// Headers sent to the API on every request.
/// </summary>
public Func<Dictionary<string, string>>? GetHeaders
{
get => _api.GetHeaders;
set => _api.GetHeaders = value;
}
/// <inheritdoc />
public void AddDebugListener(Action<string, Exception?> listener)
{
_debugNotification ??= new DebugNotification();
_debugNotification.AddDebugListener(listener);
}
/// <summary>
/// Saves the session
/// </summary>
/// <param name="session"></param>
private void UpdateSession(Session? session)
{
if (session == null)
{
CurrentSession = null;
NotifyAuthStateChange(SignedOut);
return;
}
var dirty = CurrentSession != session;
CurrentSession = session;
if (dirty) NotifyAuthStateChange(UserUpdated);
}
/// <summary>
/// Clears the session
/// </summary>
private void DestroySession()
{
UpdateSession(null);
}
/// <inheritdoc />
public async Task RefreshToken(string accessToken, string refreshToken)
{
if (string.IsNullOrEmpty(accessToken) || string.IsNullOrEmpty(refreshToken))
throw new GotrueException("No token provided", NoSessionFound);
var result = await _api.RefreshAccessToken(accessToken, refreshToken);
if (result == null || string.IsNullOrEmpty(result.AccessToken))
throw new GotrueException("Could not refresh token from provided session.", NoSessionFound);
CurrentSession = result;
NotifyAuthStateChange(TokenRefreshed);
}
/// <inheritdoc />
public async Task RefreshToken()
{
if (!Online)
throw new GotrueException("Only supported when online", Offline);
if (CurrentSession == null || string.IsNullOrEmpty(CurrentSession?.AccessToken) || string.IsNullOrEmpty(CurrentSession?.RefreshToken))
throw new GotrueException("No current session.", NoSessionFound);
if (CurrentSession!.Expired())
throw new GotrueException("Session expired", ExpiredRefreshToken);
var result = await _api.RefreshAccessToken(CurrentSession.AccessToken!, CurrentSession.RefreshToken!);
if (result == null || string.IsNullOrEmpty(result.AccessToken))
throw new GotrueException("Could not refresh token from provided session.", NoSessionFound);
CurrentSession = result;
NotifyAuthStateChange(TokenRefreshed);
}
/// <inheritdoc />
public void LoadSession()
{
if (_sessionPersistence != null) UpdateSession(_sessionPersistence.Persistence.LoadSession());
}
/// <inheritdoc />
public Task<Settings?> Settings()
{
if (!Online)
return Task.FromResult<Settings?>(null);
return _api.Settings();
}
/// <inheritdoc />
public void Debug(string message, Exception? e = null)
{
_debugNotification?.Log(message, e);
}
/// <inheritdoc />
public void Shutdown()
{
NotifyAuthStateChange(AuthState.Shutdown);
}
/// <inheritdoc />
public async Task<MfaEnrollResponse?> Enroll(MfaEnrollParams mfaEnrollParams)
{
if (CurrentSession == null || string.IsNullOrEmpty(CurrentSession.AccessToken))
throw new GotrueException("Not Logged in.", NoSessionFound);
if (!Online)
throw new GotrueException("Only supported when online", Offline);
return await _api.Enroll(CurrentSession.AccessToken, mfaEnrollParams);
}
/// <inheritdoc />
public async Task<MfaChallengeResponse?> Challenge(MfaChallengeParams mfaChallengeParams)
{
if (CurrentSession == null || string.IsNullOrEmpty(CurrentSession.AccessToken))
throw new GotrueException("Not Logged in.", NoSessionFound);
if (!Online)
throw new GotrueException("Only supported when online", Offline);
return await _api.Challenge(CurrentSession.AccessToken, mfaChallengeParams);
}
/// <inheritdoc />
public async Task<Session?> Verify(MfaVerifyParams mfaVerifyParams)
{
if (CurrentSession == null || string.IsNullOrEmpty(CurrentSession.AccessToken))
throw new GotrueException("Not Logged in.", NoSessionFound);
if (!Online)
throw new GotrueException("Only supported when online", Offline);
var result = await _api.Verify(CurrentSession.AccessToken, mfaVerifyParams);
if (result == null || string.IsNullOrEmpty(result.AccessToken))
throw new GotrueException("Could not verify MFA.", MfaChallengeUnverified);
var session = new Session
{
AccessToken = result.AccessToken,
RefreshToken = result.RefreshToken,
TokenType = "bearer",
ExpiresIn = result.ExpiresIn,
User = result.User
};
UpdateSession(session);
NotifyAuthStateChange(MfaChallengeVerified);
return session;
}
/// <inheritdoc />
public async Task<Session?> ChallengeAndVerify(MfaChallengeAndVerifyParams mfaChallengeAndVerifyParams)
{
if (CurrentSession == null || string.IsNullOrEmpty(CurrentSession.AccessToken))
throw new GotrueException("Not Logged in.", NoSessionFound);
if (!Online)
throw new GotrueException("Only supported when online", Offline);
var challengeResponse = await _api.Challenge(CurrentSession.AccessToken, new MfaChallengeParams
{
FactorId = mfaChallengeAndVerifyParams.FactorId
});
if (challengeResponse == null)
{
return null;
}
var result = await _api.Verify(CurrentSession.AccessToken, new MfaVerifyParams
{
FactorId = mfaChallengeAndVerifyParams.FactorId,
Code = mfaChallengeAndVerifyParams.Code,
ChallengeId = challengeResponse.Id
});
if (result == null || string.IsNullOrEmpty(result.AccessToken))
throw new GotrueException("Could not verify MFA.", MfaChallengeUnverified);
var session = new Session
{
AccessToken = result.AccessToken,
RefreshToken = result.RefreshToken,
TokenType = "bearer",
ExpiresIn = result.ExpiresIn,
User = result.User
};
UpdateSession(session);
NotifyAuthStateChange(MfaChallengeVerified);
return session;
}
/// <inheritdoc />
public async Task<MfaUnenrollResponse?> Unenroll(MfaUnenrollParams mfaUnenrollParams)
{
if (CurrentSession == null || string.IsNullOrEmpty(CurrentSession.AccessToken))
throw new GotrueException("Not Logged in.", NoSessionFound);
if (!Online)
throw new GotrueException("Only supported when online", Offline);
return await _api.Unenroll(CurrentSession.AccessToken, mfaUnenrollParams);
}
/// <inheritdoc />
public Task<MfaListFactorsResponse?> ListFactors()
{
if (CurrentSession == null || string.IsNullOrEmpty(CurrentSession.AccessToken))
throw new GotrueException("Not Logged in.", NoSessionFound);
var response = new MfaListFactorsResponse()
{
All = CurrentSession.User!.Factors,
Totp = CurrentSession.User!.Factors?.Where(x => x.FactorType == "totp" && x.Status == "verified").ToList()
};
return Task.FromResult(response);
}
public Task<MfaGetAuthenticatorAssuranceLevelResponse?> GetAuthenticatorAssuranceLevel()
{
if (CurrentSession == null || string.IsNullOrEmpty(CurrentSession.AccessToken))
throw new GotrueException("Not Logged in.", NoSessionFound);
var payload = new JwtSecurityTokenHandler().ReadJwtToken(CurrentSession.AccessToken).Payload;
if (payload == null || payload.ValidTo == DateTime.MinValue)
throw new GotrueException("`accessToken`'s payload was of an unknown structure.", NoSessionFound);
AuthenticatorAssuranceLevel? currentLevel = null;
if (payload.ContainsKey("aal"))
{
currentLevel = Enum.TryParse(payload["aal"].ToString(), out AuthenticatorAssuranceLevel parsedLevel) ? parsedLevel : (AuthenticatorAssuranceLevel?)null;
}
AuthenticatorAssuranceLevel? nextLevel = currentLevel;
var verifiedFactors = CurrentSession.User!.Factors?.Where(factor => factor.Status == "verified").ToList() ?? new List<Factor>();
if (verifiedFactors.Count > 0)
{
nextLevel = AuthenticatorAssuranceLevel.aal2;
}
var currentAuthenticationMethods = payload.Amr.Select(x => JsonConvert.DeserializeObject<AmrEntry>(x));
var response = new MfaGetAuthenticatorAssuranceLevelResponse
{
CurrentLevel = currentLevel,
NextLevel = nextLevel,
CurrentAuthenticationMethods = currentAuthenticationMethods.ToArray()
};
return Task.FromResult(response);
}
}
}