-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathIdentityVerificationOTPAuthState.cs
More file actions
211 lines (184 loc) · 7.49 KB
/
IdentityVerificationOTPAuthState.cs
File metadata and controls
211 lines (184 loc) · 7.49 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
using Cysharp.Threading.Tasks;
using DCL.Diagnostics;
using DCL.Utilities;
using DCL.Web3.Authenticators;
using DCL.Web3.Identities;
using MVC;
using System;
using System.Threading;
using static DCL.AuthenticationScreenFlow.AuthenticationScreenController;
using static DCL.UI.UIAnimationHashes;
namespace DCL.AuthenticationScreenFlow
{
public class IdentityVerificationOTPAuthState : AuthStateBase, IPayloadedState<(string email, CancellationToken ct)>
{
private readonly MVCStateMachine<AuthStateBase> machine;
private readonly AuthenticationScreenController controller;
private readonly ReactiveProperty<AuthStatus> currentState;
private readonly ICompositeWeb3Provider compositeWeb3Provider;
private readonly VerificationOTPAuthView view;
public event Action<string, bool>? OTPVerified;
public event Action? OTPResend;
private string email;
private CancellationToken loginCt;
private Exception? loginException;
public IdentityVerificationOTPAuthState(
MVCStateMachine<AuthStateBase> machine,
AuthenticationScreenView viewInstance,
AuthenticationScreenController controller,
ReactiveProperty<AuthStatus> currentState,
ICompositeWeb3Provider compositeWeb3Provider) : base(viewInstance)
{
view = viewInstance.VerificationOTPAuthView;
this.machine = machine;
this.controller = controller;
this.currentState = currentState;
this.compositeWeb3Provider = compositeWeb3Provider;
email = string.Empty;
loginCt = CancellationToken.None;
}
public void Enter((string email, CancellationToken ct) payload)
{
base.Enter();
loginException = null;
currentState.Value = AuthStatus.VerificationRequested;
email = payload.email;
loginCt = payload.ct;
AuthenticateAsync(payload.email, payload.ct).Forget();
}
public override void Exit()
{
if (loginException == null)
view.Hide(OUT);
else
{
view.Hide(SLIDE);
spanErrorInfo = loginException switch
{
OperationCanceledException => new SpanErrorInfo("Login process was cancelled by user"),
SignatureExpiredException ex => new SpanErrorInfo("Web3 signature expired during authentication", ex),
Web3SignatureException ex => new SpanErrorInfo("Web3 signature validation failed", ex),
CodeVerificationException ex => new SpanErrorInfo("Code verification failed during authentication", ex),
InvalidEmailException ex => new SpanErrorInfo("Invalid email provided during authentication", ex),
Exception ex => new SpanErrorInfo("Unexpected error during authentication flow", ex),
};
if (loginException is not OperationCanceledException)
ReportHub.LogException(loginException, new ReportData(ReportCategory.AUTHENTICATION));
}
// Listeners
view.BackButton.onClick.RemoveAllListeners();
view.ResendCodeButton.onClick.RemoveAllListeners();
view.InputField.CodeEntered -= OnOTPEntered;
email = string.Empty;
loginCt = CancellationToken.None;
base.Exit();
}
private async UniTaskVoid AuthenticateAsync(string email, CancellationToken ct)
{
try
{
controller.CurrentRequestID = string.Empty;
compositeWeb3Provider.OTPSendSuccess += OnOTPSendSuccess;
// awaits OTP code being entered
IWeb3Identity identity = await compositeWeb3Provider.LoginAsync(LoginPayload.ForOtpFlow(email), ct);
machine.Enter<ProfileFetchingAuthState, ProfileFetchingPayload>(new (email, identity, false, ct));
}
catch (OperationCanceledException e)
{
loginException = e;
machine.Enter<LoginSelectionAuthState, int>(SLIDE);
}
catch (SignatureExpiredException e)
{
loginException = e;
machine.Enter<LoginSelectionAuthState, int>(SLIDE);
}
catch (Web3SignatureException e)
{
loginException = e;
machine.Enter<LoginSelectionAuthState, int>(SLIDE);
}
catch (CodeVerificationException e)
{
loginException = e;
machine.Enter<LoginSelectionAuthState, int>(SLIDE);
}
catch (InvalidEmailException e)
{
loginException = e;
machine.Enter<LoginSelectionAuthState, ErrorType>(ErrorType.INVALID_EMAIL);
}
catch (Exception e)
{
loginException = e;
machine.Enter<LoginSelectionAuthState, ErrorType>(ErrorType.CONNECTION_ERROR);
}
finally{ compositeWeb3Provider.OTPSendSuccess -= OnOTPSendSuccess; }
}
private void OnOTPSendSuccess(string emailAddress)
{
view.Show(emailAddress);
// Listeners
view.BackButton.onClick.AddListener(controller.CancelLoginProcess);
view.ResendCodeButton.onClick.AddListener(ResendOtp);
view.InputField.CodeEntered += OnOTPEntered;
}
private void OnOTPEntered(string otp)
{
OnOtpEnteredAsync(loginCt).Forget();
return;
async UniTask OnOtpEnteredAsync(CancellationToken ct)
{
try
{
await compositeWeb3Provider.SubmitOtpAsync(otp, ct);
ShowOtpResult(true);
}
catch (OperationCanceledException)
{ /* Expected on cancellation */
}
catch (CodeVerificationException)
{
ShowOtpResult(false);
}
catch (Exception e)
{
ReportHub.LogException(e, new ReportData(ReportCategory.AUTHENTICATION));
controller.CancelLoginProcess();
}
}
}
private void ShowOtpResult(bool isSuccess)
{
OTPVerified?.Invoke(email, isSuccess);
if (isSuccess)
view.InputField.SetSuccess();
else
view.InputField.SetFailure();
}
private void ResendOtp()
{
ResendOtpAsync(loginCt).Forget();
return;
async UniTaskVoid ResendOtpAsync(CancellationToken ct)
{
view.ResendCodeButton.interactable = false;
try
{
await compositeWeb3Provider.ResendOtpAsync(ct);
OTPResend?.Invoke();
view.InputField.Clear();
}
catch (OperationCanceledException)
{ /* Expected on cancellation */
}
catch (Exception e)
{
ReportHub.LogException(e, new ReportData(ReportCategory.AUTHENTICATION));
controller.CancelLoginProcess();
}
finally { view.ResendCodeButton.interactable = true; }
}
}
}
}