|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
3 | 3 | using System.IdentityModel.Tokens.Jwt;
|
| 4 | +using System.Linq; |
4 | 5 | using System.Threading.Tasks;
|
5 | 6 | using System.Web;
|
6 | 7 | using Newtonsoft.Json;
|
7 | 8 | using Supabase.Gotrue.Exceptions;
|
8 | 9 | using Supabase.Gotrue.Interfaces;
|
| 10 | +using Supabase.Gotrue.Mfa; |
9 | 11 | using static Supabase.Gotrue.Constants;
|
10 | 12 | using static Supabase.Gotrue.Constants.AuthState;
|
11 | 13 | using static Supabase.Gotrue.Exceptions.FailureHint.Reason;
|
@@ -586,7 +588,6 @@ public async Task<Session> SetSession(string accessToken, string refreshToken, b
|
586 | 588 | return session;
|
587 | 589 | }
|
588 | 590 |
|
589 |
| - |
590 | 591 | /// <inheritdoc />
|
591 | 592 | public async Task<Session?> RetrieveSessionAsync()
|
592 | 593 | {
|
@@ -752,5 +753,166 @@ public void Shutdown()
|
752 | 753 | {
|
753 | 754 | NotifyAuthStateChange(AuthState.Shutdown);
|
754 | 755 | }
|
| 756 | + |
| 757 | + /// <inheritdoc /> |
| 758 | + public async Task<MfaEnrollResponse?> Enroll(MfaEnrollParams mfaEnrollParams) |
| 759 | + { |
| 760 | + if (CurrentSession == null || string.IsNullOrEmpty(CurrentSession.AccessToken)) |
| 761 | + throw new GotrueException("Not Logged in.", NoSessionFound); |
| 762 | + |
| 763 | + if (!Online) |
| 764 | + throw new GotrueException("Only supported when online", Offline); |
| 765 | + |
| 766 | + return await _api.Enroll(CurrentSession.AccessToken, mfaEnrollParams); |
| 767 | + } |
| 768 | + |
| 769 | + /// <inheritdoc /> |
| 770 | + public async Task<MfaChallengeResponse?> Challenge(MfaChallengeParams mfaChallengeParams) |
| 771 | + { |
| 772 | + if (CurrentSession == null || string.IsNullOrEmpty(CurrentSession.AccessToken)) |
| 773 | + throw new GotrueException("Not Logged in.", NoSessionFound); |
| 774 | + |
| 775 | + if (!Online) |
| 776 | + throw new GotrueException("Only supported when online", Offline); |
| 777 | + |
| 778 | + return await _api.Challenge(CurrentSession.AccessToken, mfaChallengeParams); |
| 779 | + } |
| 780 | + |
| 781 | + /// <inheritdoc /> |
| 782 | + public async Task<Session?> Verify(MfaVerifyParams mfaVerifyParams) |
| 783 | + { |
| 784 | + if (CurrentSession == null || string.IsNullOrEmpty(CurrentSession.AccessToken)) |
| 785 | + throw new GotrueException("Not Logged in.", NoSessionFound); |
| 786 | + |
| 787 | + if (!Online) |
| 788 | + throw new GotrueException("Only supported when online", Offline); |
| 789 | + |
| 790 | + var result = await _api.Verify(CurrentSession.AccessToken, mfaVerifyParams); |
| 791 | + |
| 792 | + if (result == null || string.IsNullOrEmpty(result.AccessToken)) |
| 793 | + throw new GotrueException("Could not verify MFA.", MfaChallengeUnverified); |
| 794 | + |
| 795 | + var session = new Session |
| 796 | + { |
| 797 | + AccessToken = result.AccessToken, |
| 798 | + RefreshToken = result.RefreshToken, |
| 799 | + TokenType = "bearer", |
| 800 | + ExpiresIn = result.ExpiresIn, |
| 801 | + User = result.User |
| 802 | + }; |
| 803 | + |
| 804 | + UpdateSession(session); |
| 805 | + NotifyAuthStateChange(MfaChallengeVerified); |
| 806 | + |
| 807 | + return session; |
| 808 | + } |
| 809 | + |
| 810 | + /// <inheritdoc /> |
| 811 | + public async Task<Session?> ChallengeAndVerify(MfaChallengeAndVerifyParams mfaChallengeAndVerifyParams) |
| 812 | + { |
| 813 | + if (CurrentSession == null || string.IsNullOrEmpty(CurrentSession.AccessToken)) |
| 814 | + throw new GotrueException("Not Logged in.", NoSessionFound); |
| 815 | + |
| 816 | + if (!Online) |
| 817 | + throw new GotrueException("Only supported when online", Offline); |
| 818 | + |
| 819 | + var challengeResponse = await _api.Challenge(CurrentSession.AccessToken, new MfaChallengeParams |
| 820 | + { |
| 821 | + FactorId = mfaChallengeAndVerifyParams.FactorId |
| 822 | + }); |
| 823 | + |
| 824 | + if (challengeResponse == null) |
| 825 | + { |
| 826 | + return null; |
| 827 | + } |
| 828 | + |
| 829 | + var result = await _api.Verify(CurrentSession.AccessToken, new MfaVerifyParams |
| 830 | + { |
| 831 | + FactorId = mfaChallengeAndVerifyParams.FactorId, |
| 832 | + Code = mfaChallengeAndVerifyParams.Code, |
| 833 | + ChallengeId = challengeResponse.Id |
| 834 | + }); |
| 835 | + |
| 836 | + if (result == null || string.IsNullOrEmpty(result.AccessToken)) |
| 837 | + throw new GotrueException("Could not verify MFA.", MfaChallengeUnverified); |
| 838 | + |
| 839 | + var session = new Session |
| 840 | + { |
| 841 | + AccessToken = result.AccessToken, |
| 842 | + RefreshToken = result.RefreshToken, |
| 843 | + TokenType = "bearer", |
| 844 | + ExpiresIn = result.ExpiresIn, |
| 845 | + User = result.User |
| 846 | + }; |
| 847 | + |
| 848 | + UpdateSession(session); |
| 849 | + NotifyAuthStateChange(MfaChallengeVerified); |
| 850 | + |
| 851 | + return session; |
| 852 | + } |
| 853 | + |
| 854 | + /// <inheritdoc /> |
| 855 | + public async Task<MfaUnenrollResponse?> Unenroll(MfaUnenrollParams mfaUnenrollParams) |
| 856 | + { |
| 857 | + if (CurrentSession == null || string.IsNullOrEmpty(CurrentSession.AccessToken)) |
| 858 | + throw new GotrueException("Not Logged in.", NoSessionFound); |
| 859 | + |
| 860 | + if (!Online) |
| 861 | + throw new GotrueException("Only supported when online", Offline); |
| 862 | + |
| 863 | + return await _api.Unenroll(CurrentSession.AccessToken, mfaUnenrollParams); |
| 864 | + } |
| 865 | + |
| 866 | + /// <inheritdoc /> |
| 867 | + public Task<MfaListFactorsResponse?> ListFactors() |
| 868 | + { |
| 869 | + if (CurrentSession == null || string.IsNullOrEmpty(CurrentSession.AccessToken)) |
| 870 | + throw new GotrueException("Not Logged in.", NoSessionFound); |
| 871 | + |
| 872 | + var response = new MfaListFactorsResponse() |
| 873 | + { |
| 874 | + All = CurrentSession.User!.Factors, |
| 875 | + Totp = CurrentSession.User!.Factors?.Where(x => x.FactorType == "totp" && x.Status == "verified").ToList() |
| 876 | + }; |
| 877 | + |
| 878 | + return Task.FromResult(response); |
| 879 | + } |
| 880 | + |
| 881 | + public Task<MfaGetAuthenticatorAssuranceLevelResponse?> GetAuthenticatorAssuranceLevel() |
| 882 | + { |
| 883 | + if (CurrentSession == null || string.IsNullOrEmpty(CurrentSession.AccessToken)) |
| 884 | + throw new GotrueException("Not Logged in.", NoSessionFound); |
| 885 | + |
| 886 | + var payload = new JwtSecurityTokenHandler().ReadJwtToken(CurrentSession.AccessToken).Payload; |
| 887 | + |
| 888 | + if (payload == null || payload.ValidTo == DateTime.MinValue) |
| 889 | + throw new GotrueException("`accessToken`'s payload was of an unknown structure.", NoSessionFound); |
| 890 | + |
| 891 | + AuthenticatorAssuranceLevel? currentLevel = null; |
| 892 | + |
| 893 | + if (payload.ContainsKey("aal")) |
| 894 | + { |
| 895 | + currentLevel = Enum.TryParse(payload["aal"].ToString(), out AuthenticatorAssuranceLevel parsedLevel) ? parsedLevel : (AuthenticatorAssuranceLevel?)null; |
| 896 | + } |
| 897 | + |
| 898 | + AuthenticatorAssuranceLevel? nextLevel = currentLevel; |
| 899 | + |
| 900 | + var verifiedFactors = CurrentSession.User!.Factors?.Where(factor => factor.Status == "verified").ToList() ?? new List<Factor>(); |
| 901 | + if (verifiedFactors.Count > 0) |
| 902 | + { |
| 903 | + nextLevel = AuthenticatorAssuranceLevel.aal2; |
| 904 | + } |
| 905 | + |
| 906 | + var currentAuthenticationMethods = payload.Amr.Select(x => JsonConvert.DeserializeObject<AmrEntry>(x)); |
| 907 | + |
| 908 | + var response = new MfaGetAuthenticatorAssuranceLevelResponse |
| 909 | + { |
| 910 | + CurrentLevel = currentLevel, |
| 911 | + NextLevel = nextLevel, |
| 912 | + CurrentAuthenticationMethods = currentAuthenticationMethods.ToArray() |
| 913 | + }; |
| 914 | + |
| 915 | + return Task.FromResult(response); |
| 916 | + } |
755 | 917 | }
|
756 | 918 | }
|
0 commit comments