@oxy.so/services 15.1.4 → 15.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/bloom/Package.bloom +15 -0
  2. package/bloom/Resources/Localizable/ar-SA.json +108 -0
  3. package/bloom/Resources/Localizable/bn-BD.json +108 -0
  4. package/bloom/Resources/Localizable/ca-ES.json +111 -0
  5. package/bloom/Resources/Localizable/de-DE.json +108 -0
  6. package/bloom/Resources/Localizable/en-US.json +124 -0
  7. package/bloom/Resources/Localizable/es-ES.json +124 -0
  8. package/bloom/Resources/Localizable/fr-FR.json +108 -0
  9. package/bloom/Resources/Localizable/hi-IN.json +108 -0
  10. package/bloom/Resources/Localizable/id-ID.json +108 -0
  11. package/bloom/Resources/Localizable/it-IT.json +108 -0
  12. package/bloom/Resources/Localizable/ja-JP.json +108 -0
  13. package/bloom/Resources/Localizable/ko-KR.json +108 -0
  14. package/bloom/Resources/Localizable/pt-PT.json +108 -0
  15. package/bloom/Resources/Localizable/ru-RU.json +108 -0
  16. package/bloom/Resources/Localizable/tr-TR.json +108 -0
  17. package/bloom/Resources/Localizable/zh-CN.json +108 -0
  18. package/bloom/Sources/OxyAccountUI/SignInFlow.bloom +322 -0
  19. package/bloom/Sources/OxyAccountUI/SignInRules.bloom +135 -0
  20. package/bloom/Sources/OxyAccountUI/SignInService.bloom +141 -0
  21. package/bloom/Sources/OxyAccountUI/SignUpFlow.bloom +322 -0
  22. package/bloom/Sources/OxyAccountUI/SignUpRules.bloom +189 -0
  23. package/bloom/Sources/OxyAccountUIChecks/Checks.bloom +254 -0
  24. package/bloom/Sources/OxyAccountUIChecks/FakeAuth.bloom +123 -0
  25. package/bloom/Sources/OxyAccountUIChecks/SignUpChecks.bloom +193 -0
  26. package/lib/commonjs/ui/boot/runProviderColdBoot.js +43 -329
  27. package/lib/commonjs/ui/boot/runProviderColdBoot.js.map +1 -1
  28. package/lib/commonjs/ui/context/OxyContext.js +7 -1
  29. package/lib/commonjs/ui/context/OxyContext.js.map +1 -1
  30. package/lib/commonjs/ui/oauth/applicationSessionLane.js +12 -25
  31. package/lib/commonjs/ui/oauth/applicationSessionLane.js.map +1 -1
  32. package/lib/commonjs/ui/session/tokenTransport.js +7 -69
  33. package/lib/commonjs/ui/session/tokenTransport.js.map +1 -1
  34. package/lib/module/ui/boot/runProviderColdBoot.js +43 -329
  35. package/lib/module/ui/boot/runProviderColdBoot.js.map +1 -1
  36. package/lib/module/ui/context/OxyContext.js +8 -2
  37. package/lib/module/ui/context/OxyContext.js.map +1 -1
  38. package/lib/module/ui/oauth/applicationSessionLane.js +4 -23
  39. package/lib/module/ui/oauth/applicationSessionLane.js.map +1 -1
  40. package/lib/module/ui/session/tokenTransport.js +3 -68
  41. package/lib/module/ui/session/tokenTransport.js.map +1 -1
  42. package/lib/typescript/commonjs/ui/boot/runProviderColdBoot.d.ts +8 -31
  43. package/lib/typescript/commonjs/ui/boot/runProviderColdBoot.d.ts.map +1 -1
  44. package/lib/typescript/commonjs/ui/context/OxyContext.d.ts.map +1 -1
  45. package/lib/typescript/commonjs/ui/oauth/applicationSessionLane.d.ts +1 -5
  46. package/lib/typescript/commonjs/ui/oauth/applicationSessionLane.d.ts.map +1 -1
  47. package/lib/typescript/commonjs/ui/session/tokenTransport.d.ts +1 -33
  48. package/lib/typescript/commonjs/ui/session/tokenTransport.d.ts.map +1 -1
  49. package/lib/typescript/module/ui/boot/runProviderColdBoot.d.ts +8 -31
  50. package/lib/typescript/module/ui/boot/runProviderColdBoot.d.ts.map +1 -1
  51. package/lib/typescript/module/ui/context/OxyContext.d.ts.map +1 -1
  52. package/lib/typescript/module/ui/oauth/applicationSessionLane.d.ts +1 -5
  53. package/lib/typescript/module/ui/oauth/applicationSessionLane.d.ts.map +1 -1
  54. package/lib/typescript/module/ui/session/tokenTransport.d.ts +1 -33
  55. package/lib/typescript/module/ui/session/tokenTransport.d.ts.map +1 -1
  56. package/package.json +10 -5
  57. package/src/ui/boot/runProviderColdBoot.ts +40 -364
  58. package/src/ui/context/OxyContext.tsx +8 -1
  59. package/src/ui/oauth/applicationSessionLane.ts +7 -35
  60. package/src/ui/session/tokenTransport.ts +3 -75
@@ -0,0 +1,108 @@
1
+ {
2
+ "accountSwitcher.commonsNotInstalled": "没有 Commons?获取应用以使用您的 Oxy ID 登录。",
3
+ "accountSwitcher.continueWithOxy": "使用 Oxy 继续",
4
+ "accountSwitcher.getCommons": "获取 Commons",
5
+ "accountSwitcher.linkOpenFailed": "无法打开链接,请重试。",
6
+ "accountSwitcher.signInWithOxy": "使用 Oxy 登录",
7
+ "emailCode.changeEmail": "使用其他邮箱",
8
+ "emailCode.errors.codeInvalid": "验证码不正确或已过期。",
9
+ "emailCode.errors.expired": "耗时过长,请重新开始。",
10
+ "emailCode.errors.rateLimited": "验证码请求过多,请稍后再试。",
11
+ "emailCode.errors.tooManyAttempts": "错误次数过多,请重新获取验证码。",
12
+ "emailCode.errors.unavailable": "Oxy 暂时无法发送邮件,请稍后再试。",
13
+ "emailCode.label": "验证码",
14
+ "emailCode.resend": "发送新验证码",
15
+ "emailCode.resent": "我们已发送新验证码。",
16
+ "emailCode.sentTo": "我们已向 {email} 发送了 6 位验证码。",
17
+ "emailCode.title": "查看你的邮箱",
18
+ "signin.actions.back": "返回",
19
+ "signin.actions.continue": "继续",
20
+ "signin.addAccountSubtitle": "使用另一个账户登录",
21
+ "signin.addAccountTitle": "添加其他账号",
22
+ "signin.checkEmail.codeDigitsOnly": "验证码只有数字吗?",
23
+ "signin.checkEmail.codeHasLetters": "验证码里有字母吗?",
24
+ "signin.checkEmail.codeHint": "输入邮件中的验证码,或在此浏览器中打开邮件里的链接。",
25
+ "signin.checkEmail.codeLabel": "验证码",
26
+ "signin.checkEmail.description": "如果有与 {identifier} 匹配的账户,我们已向其发送验证码和登录链接。",
27
+ "signin.checkEmail.differentAccount": "使用其他账户",
28
+ "signin.checkEmail.resend": "重新发送邮件",
29
+ "signin.checkEmail.resendIn": "{seconds} 秒后可重新发送邮件",
30
+ "signin.checkEmail.resent": "我们已发送新邮件。",
31
+ "signin.checkEmail.retryLater": "我们已发送了多封邮件。请等几分钟后再试。",
32
+ "signin.checkEmail.title": "查看你的邮箱",
33
+ "signin.checkEmail.usePassword": "改用密码",
34
+ "signin.chooser.continueAs": "以 {name} 的身份继续",
35
+ "signin.chooser.subtitle": "使用下方的账户继续,或使用其他账户。",
36
+ "signin.chooser.subtitleToApp": "以继续前往 {app}",
37
+ "signin.chooser.title": "选择账户",
38
+ "signin.chooser.useAnother": "使用其他账户",
39
+ "signin.createAccount": "创建账号",
40
+ "signin.createAccountLink": "初次使用 Oxy?创建账户",
41
+ "signin.device.codeHint": "请确认手机上显示的是这个代码",
42
+ "signin.emailAccounts.description": "{identifier} 有多个账号。请选择要登录的账号。",
43
+ "signin.emailAccounts.title": "选择账号",
44
+ "signin.errors.codeInvalid": "验证码不正确或已过期。",
45
+ "signin.errors.emailAccountsFull": "此邮箱的账号数量已达上限。",
46
+ "signin.errors.failed": "无法开始登录",
47
+ "signin.errors.failedDescription": "出了点问题,请重试。",
48
+ "signin.errors.generic": "出了点问题,请重试。",
49
+ "signin.errors.invalidCredentials": "用户名、邮箱或密码不正确。 如果多个账号使用此邮箱,请用用户名登录。",
50
+ "signin.errors.notConfigured": "无法登录",
51
+ "signin.errors.notConfiguredDescription": "{app} 尚未配置登录功能。请联系该应用的开发者。",
52
+ "signin.errors.originNotAllowed": "此处无法登录。",
53
+ "signin.errors.rateLimited": "尝试次数过多。请在 {seconds} 秒后重试。",
54
+ "signin.errors.requestExpired": "此次登录已过期。请重新发送邮件。",
55
+ "signin.errors.secondFactorInvalid": "验证码不正确。请使用当前的验证码。",
56
+ "signin.identifier.label": "邮箱或用户名",
57
+ "signin.identifier.placeholder": "you@example.com",
58
+ "signin.identifier.required": "请输入你的邮箱或用户名。",
59
+ "signin.link.approvedDescription": "返回你请求登录的应用,它会自动继续。你可以关闭此标签页。",
60
+ "signin.link.approvedTitle": "你已登录",
61
+ "signin.link.invalidDescription": "链接已过期或已被使用。请在应用中重新获取邮件,或输入最新一封邮件中的验证码。",
62
+ "signin.link.invalidTitle": "此链接无法使用",
63
+ "signin.link.otherDeviceDescription": "此链接只能在你请求登录的浏览器中使用。请在那里打开,或在应用中输入邮件里的验证码。",
64
+ "signin.link.otherDeviceTitle": "请在同一浏览器中打开链接",
65
+ "signin.noAccount": "还没有账号?",
66
+ "signin.orContinueWith": "或使用以下方式继续",
67
+ "signin.password.forgot": "忘记密码?通过邮件获取验证码",
68
+ "signin.password.label": "密码",
69
+ "signin.password.required": "请输入你的密码。",
70
+ "signin.password.title": "输入你的密码",
71
+ "signin.qr.caption": "使用 Commons 扫码登录",
72
+ "signin.qr.renew": "显示新的二维码",
73
+ "signin.secondFactor.backupDescription": "输入一个备用验证码。每个只能使用一次。",
74
+ "signin.secondFactor.backupLabel": "备用验证码",
75
+ "signin.secondFactor.description": "输入身份验证器应用中的验证码。",
76
+ "signin.secondFactor.label": "身份验证器验证码",
77
+ "signin.secondFactor.title": "两步验证",
78
+ "signin.secondFactor.useAuthenticator": "使用身份验证器应用",
79
+ "signin.secondFactor.useBackup": "使用备用验证码",
80
+ "signin.status.signingIn": "正在登录…",
81
+ "signin.subtitle": "使用你的 Oxy 账号",
82
+ "signin.subtitleToApp": "以继续前往 {app}",
83
+ "signin.terms.and": "和",
84
+ "signin.terms.before": "继续即表示你同意我们的",
85
+ "signin.terms.privacyLink": "隐私政策",
86
+ "signin.terms.termsLink": "服务条款",
87
+ "signin.title": "登录",
88
+ "signup.backToSignInLink": "已有账户?登录",
89
+ "signup.birthdate.guardianNote": "如果你未满 13 岁,你的账号需要一位家长或监护人。创建账号后即可邀请对方。",
90
+ "signup.birthdate.label": "出生日期",
91
+ "signup.birthdate.required": "请选择你的出生日期。",
92
+ "signup.birthdate.subtitle": "用于启用适合你年龄的保护措施。其他人看不到。",
93
+ "signup.birthdate.title": "你的出生日期是?",
94
+ "signup.commonsSubtitle": "或者在 Commons 中创建你的身份,自己保管密钥。",
95
+ "signup.createInCommons": "改为在 Commons 中创建",
96
+ "signup.email.invalid": "请输入有效的邮箱地址。",
97
+ "signup.email.label": "邮箱",
98
+ "signup.email.placeholder": "you@example.com",
99
+ "signup.email.subtitle": "你将用它登录:每次我们都会发送一个验证码。其他人看不到它。",
100
+ "signup.email.title": "你的邮箱是什么?",
101
+ "signup.laterNote": "你之后可以在账户的安全设置中添加密码或身份验证器应用。",
102
+ "signup.subtitle": "选择你的用户名,然后我们确认你的邮箱。",
103
+ "signup.title": "创建你的账户",
104
+ "signup.username.label": "用户名",
105
+ "signup.username.placeholder": "yourname",
106
+ "signup.username.required": "请选择用户名。",
107
+ "signup.username.taken": "该用户名已被占用。"
108
+ }
@@ -0,0 +1,322 @@
1
+ // THE sign-in screen's state, as `OxySignInPanel` keeps it, for every backend: the steps, what is
2
+ // typed, what is pending, what went wrong and the waits. The screen draws it and calls its
3
+ // methods; the model makes the calls (`SignInService`) and decides the next step.
4
+ //
5
+ // start "Email or username", then Continue
6
+ // checkEmail one email carries a code and a link: the code is typed here (6 digits, or the
7
+ // 10-character long code), and the link opened in this browser is collected
8
+ // choose the email holds several accounts and was just proven: which one
9
+ // password the alternative for an account that has one
10
+ // secondFactor the authenticator's code, or a backup code
11
+ //
12
+ // Every answer is the same whether or not the account exists (ADR 0030 D2): an accepted start
13
+ // always leads to `checkEmail`, and every failure is one generic sentence per kind, so no state,
14
+ // string or wait here depends on the account. `OxyAccountUIChecks` holds it to that.
15
+ //
16
+ // The waits are counted by `tick()`, once a second, from the screen's own timer, and the link is
17
+ // asked for by `pollLink()` every `EMAIL_SIGNIN_POLL_MS`: no timer lives in the model, so a test
18
+ // drives time itself.
19
+
20
+ /// A step of the sign-in screen.
21
+ public enum SignInStage: Equatable {
22
+ case start
23
+ case checkEmail(EmailRequest)
24
+ case choose(EmailRequest, [AccountOption])
25
+ case password(identifier: String)
26
+ case secondFactor(challengeId: String, identifier: String)
27
+ }
28
+
29
+ @Observable
30
+ public final class SignInFlow {
31
+ public private(set) var stage: SignInStage = .start
32
+ public private(set) var identifier = ""
33
+ public private(set) var code = ""
34
+ public private(set) var password = ""
35
+ public private(set) var secondFactorCode = ""
36
+ /// The second-factor field takes a backup code.
37
+ public private(set) var useBackupCode = false
38
+ /// The code field takes the 10-character long code ("Does your code have letters?").
39
+ public private(set) var longCode = false
40
+ public private(set) var pending = false
41
+ /// The account of `choose` being signed in.
42
+ public private(set) var choosingId: String? = none
43
+ /// What went wrong, shown in the field's error line.
44
+ public private(set) var error: FlowMessage? = none
45
+ /// The catalog key of a notice ("We sent you a new email").
46
+ public private(set) var noticeKey: String? = none
47
+ /// Seconds left of a 429's wait, counted down in a notice above the action it holds back.
48
+ public private(set) var rateLimitSeconds = 0
49
+ /// Seconds before "Send a new email" may be pressed again.
50
+ public private(set) var resendSeconds = 0
51
+ /// This screen signed the client in.
52
+ public private(set) var signedIn = false
53
+
54
+ let service: SignInService
55
+ /// One sign-in ends once: the typed code and the opened link can race.
56
+ var finishing = false
57
+
58
+ public init(service: SignInService, loginHint: String? = none) {
59
+ self.service = service
60
+ self.identifier = loginHint ?? ""
61
+ }
62
+
63
+ /// Nothing may be sent: a call is running, or a 429's wait is not over.
64
+ public var blocked: Bool {
65
+ pending || rateLimitSeconds > 0
66
+ }
67
+
68
+ /// One second passed: the waits count down.
69
+ public func tick() {
70
+ if rateLimitSeconds > 0 { rateLimitSeconds -= 1 }
71
+ if resendSeconds > 0 { resendSeconds -= 1 }
72
+ }
73
+
74
+ // MARK: typing
75
+
76
+ public func setIdentifier(_ value: String) {
77
+ identifier = withoutWhitespace(value)
78
+ error = none
79
+ }
80
+
81
+ public func setCode(_ value: String) {
82
+ code = value
83
+ error = none
84
+ }
85
+
86
+ public func setPassword(_ value: String) {
87
+ password = value
88
+ error = none
89
+ }
90
+
91
+ public func setSecondFactorCode(_ value: String) {
92
+ secondFactorCode = value
93
+ error = none
94
+ }
95
+
96
+ /// "Does your code have letters?" and back: the field changes length and starts empty.
97
+ public func toggleLongCode() {
98
+ longCode = !longCode
99
+ code = ""
100
+ error = none
101
+ }
102
+
103
+ /// "Use a backup code" and back.
104
+ public func toggleBackupCode() {
105
+ useBackupCode = !useBackupCode
106
+ secondFactorCode = ""
107
+ error = none
108
+ }
109
+
110
+ // MARK: steps
111
+
112
+ /// Continue on "Email or username".
113
+ public func submitIdentifier() async {
114
+ let name = identifier.trimmed()
115
+ if name.isEmpty {
116
+ error = message("signin.identifier.required")
117
+ return
118
+ }
119
+ await startEmail(name)
120
+ }
121
+
122
+ /// Send the sign-in email and show "Check your email"; again from there, "Send a new email".
123
+ public func startEmail(_ name: String) async {
124
+ if blocked { return }
125
+ error = none
126
+ noticeKey = none
127
+ pending = true
128
+ var resending = false
129
+ if case .checkEmail(_) = stage { resending = true }
130
+ do {
131
+ let started = try await service.emailStart(name)
132
+ if started.retryLater {
133
+ error = message("signin.checkEmail.retryLater")
134
+ } else {
135
+ code = ""
136
+ resendSeconds = EMAIL_RESEND_COOLDOWN_SECONDS
137
+ // A resend keeps the field as the person set it; a new request starts at 6 digits.
138
+ if resending {
139
+ noticeKey = "signin.checkEmail.resent"
140
+ } else {
141
+ longCode = false
142
+ }
143
+ stage = .checkEmail(EmailRequest(identifier: name, requestId: started.requestId, requestSecret: started.requestSecret))
144
+ }
145
+ } catch {
146
+ fail(error)
147
+ }
148
+ pending = false
149
+ }
150
+
151
+ /// Continue on "Check your email", or the code field filled.
152
+ public func submitCode() async {
153
+ guard case .checkEmail(let request) = stage else { return }
154
+ if blocked { return }
155
+ let typed = code
156
+ if !isCompleteSignInCode(typed) {
157
+ error = message("signin.errors.codeInvalid")
158
+ return
159
+ }
160
+ error = none
161
+ noticeKey = none
162
+ pending = true
163
+ do {
164
+ let step = try await service.emailConfirm(request, code: typed.trimmed())
165
+ try await finish(step, from: request.identifier, request: request, firstFactor: .emailCode)
166
+ } catch {
167
+ code = ""
168
+ fail(error)
169
+ }
170
+ pending = false
171
+ }
172
+
173
+ /// Whether the link was opened in this browser: one ask. `false` once there is nothing left to
174
+ /// ask for (the step went, the request is spent or gone, or it signed in).
175
+ public func pollLink() async -> Bool {
176
+ guard case .checkEmail(let request) = stage else { return false }
177
+ if finishing { return false }
178
+ do {
179
+ guard let step = try await service.emailCollect(request) else { return true }
180
+ // A session the server handed over is committed even if the step went meanwhile: the
181
+ // request is spent, so it would otherwise be lost.
182
+ try await finish(step, from: request.identifier, request: request, firstFactor: .emailCode)
183
+ return false
184
+ } catch let failure as SignInFailure {
185
+ // A spent, expired or unknown request never approves: stop asking.
186
+ return failure.code != SignInCode.requestInvalid
187
+ } catch {
188
+ return true
189
+ }
190
+ }
191
+
192
+ /// One of the email's accounts, chosen.
193
+ public func choose(_ account: AccountOption) async {
194
+ guard case .choose(let request, _) = stage else { return }
195
+ if blocked { return }
196
+ let name = account.username ?? request.identifier
197
+ error = none
198
+ pending = true
199
+ choosingId = account.id
200
+ do {
201
+ let step = try await service.emailChoose(request, userId: account.id)
202
+ try await finish(step, from: name, request: none, firstFactor: .emailCode)
203
+ } catch {
204
+ fail(error)
205
+ }
206
+ pending = false
207
+ choosingId = none
208
+ }
209
+
210
+ /// "Use your password instead".
211
+ public func toPassword() {
212
+ guard case .checkEmail(let request) = stage else { return }
213
+ error = none
214
+ noticeKey = none
215
+ password = ""
216
+ stage = .password(identifier: request.identifier)
217
+ }
218
+
219
+ /// Continue on "Enter your password".
220
+ public func submitPassword() async {
221
+ guard case .password(let name) = stage else { return }
222
+ if blocked { return }
223
+ if password.isEmpty {
224
+ error = message("signin.password.required")
225
+ return
226
+ }
227
+ error = none
228
+ pending = true
229
+ do {
230
+ let step = try await service.passwordSignIn(identifier: name, password: password)
231
+ try await finish(step, from: name, request: none, firstFactor: .password)
232
+ } catch {
233
+ fail(error)
234
+ }
235
+ pending = false
236
+ }
237
+
238
+ /// "Forgot password?": the sign-in email, for the same identifier.
239
+ public func forgotPassword() async {
240
+ guard case .password(let name) = stage else { return }
241
+ await startEmail(name)
242
+ }
243
+
244
+ /// Continue on "Two-step verification", or its field filled.
245
+ public func submitSecondFactor() async {
246
+ guard case .secondFactor(let challengeId, let name) = stage else { return }
247
+ if blocked { return }
248
+ let value = secondFactorCode.trimmed()
249
+ if !isCompleteSecondFactorCode(value, backup: useBackupCode) {
250
+ error = message("signin.errors.secondFactorInvalid")
251
+ return
252
+ }
253
+ error = none
254
+ pending = true
255
+ do {
256
+ let step = try await service.completeSecondFactor(challengeId: challengeId, code: value)
257
+ try await finish(step, from: name, request: none, firstFactor: .emailCode)
258
+ } catch {
259
+ secondFactorCode = ""
260
+ fail(error)
261
+ }
262
+ pending = false
263
+ }
264
+
265
+ /// "Use a different account": back to "Email or username", as typed.
266
+ public func backToIdentifier() {
267
+ error = none
268
+ noticeKey = none
269
+ code = ""
270
+ longCode = false
271
+ password = ""
272
+ stage = .start
273
+ }
274
+
275
+ // MARK: ends
276
+
277
+ func finish(_ step: SignInStep, from name: String, request: EmailRequest?, firstFactor: FirstFactor) async throws {
278
+ if finishing { return }
279
+ switch step {
280
+ case .chooseAccount(let accounts):
281
+ guard let proven = request else { return }
282
+ error = none
283
+ noticeKey = none
284
+ stage = .choose(proven, accounts)
285
+ case .secondFactor(let challengeId):
286
+ error = none
287
+ secondFactorCode = ""
288
+ useBackupCode = false
289
+ stage = .secondFactor(challengeId: challengeId, identifier: name)
290
+ case .session(let grant):
291
+ finishing = true
292
+ do {
293
+ try await service.commit(grant, firstFactor: firstFactor)
294
+ } catch {
295
+ finishing = false
296
+ throw error
297
+ }
298
+ signedIn = true
299
+ }
300
+ }
301
+
302
+ /// A 429 is a wait, counted down; anything else is one sentence in the error line.
303
+ func fail(_ reason: Error) {
304
+ let failure = asFailure(reason)
305
+ if isRateLimited(failure) {
306
+ rateLimitSeconds = retryAfter(failure)
307
+ } else {
308
+ error = message(describeFailure(failure))
309
+ }
310
+ }
311
+ }
312
+
313
+ /// Any error as the screens read it: a `SignInFailure`, or a generic one.
314
+ func asFailure(_ reason: Error) -> SignInFailure {
315
+ do {
316
+ throw reason
317
+ } catch let failure as SignInFailure {
318
+ return failure
319
+ } catch {
320
+ return signInFailure(none, -1)
321
+ }
322
+ }
@@ -0,0 +1,135 @@
1
+ // The sign-in contract's numbers and codes the screens follow, from `@oxy.so/contracts/client`
2
+ // (`signIn.ts`, `accountEmail.ts`). Kept equal to them by `checks/run.mjs`.
3
+
4
+ /// Digits in an emailed sign-in or confirmation code.
5
+ public let EMAIL_CODE_LENGTH = 6
6
+ /// Characters in the long code (`XXXXX-XXXXX`) an account past its daily ceiling is sent.
7
+ public let EMAIL_SIGNIN_LONG_CODE_LENGTH = 10
8
+ /// The long code's alphabet: no 0, 1, I, L, O or U.
9
+ public let EMAIL_SIGNIN_LONG_CODE_ALPHABET = "23456789ABCDEFGHJKMNPQRSTVWXYZ"
10
+ /// Digits in an authenticator's code.
11
+ public let TOTP_DIGITS = 6
12
+ /// Characters in a backup code (`xxxxx-xxxxx`, its dash not counted).
13
+ public let BACKUP_CODE_LENGTH = 10
14
+ public let PASSWORD_MAX_LENGTH = 256
15
+ public let SIGN_IN_IDENTIFIER_MAX_LENGTH = 254
16
+ /// How long after an email "Send a new email" waits.
17
+ public let EMAIL_RESEND_COOLDOWN_SECONDS = 30
18
+ /// How often "Check your email" asks whether the link was opened in this browser (ms).
19
+ public let EMAIL_SIGNIN_POLL_MS = 2000
20
+ /// How long a screen waits after a 429 that names no wait of its own.
21
+ public let RATE_LIMIT_SECONDS = 60
22
+
23
+ /// The API's machine codes the screens word (`SIGN_IN_ERROR_CODES`,
24
+ /// `EMAIL_VERIFICATION_ERROR_CODES`, core's `ErrorCodes`).
25
+ public struct SignInCode {
26
+ public static let invalidCredentials = "SIGNIN_INVALID_CREDENTIALS"
27
+ public static let requestInvalid = "SIGNIN_REQUEST_INVALID"
28
+ public static let linkInvalid = "SIGNIN_LINK_INVALID"
29
+ public static let secondFactorInvalid = "SECOND_FACTOR_INVALID"
30
+ public static let totpCodeInvalid = "TOTP_CODE_INVALID"
31
+ public static let originNotAllowed = "SIGNIN_ORIGIN_NOT_ALLOWED"
32
+ public static let usernameTaken = "USERNAME_TAKEN"
33
+ public static let emailAccountsFull = "EMAIL_ACCOUNTS_FULL"
34
+ public static let emailCodeInvalid = "EMAIL_CODE_INVALID"
35
+ public static let emailCodeTooManyAttempts = "EMAIL_CODE_TOO_MANY_ATTEMPTS"
36
+ public static let emailTicketInvalid = "EMAIL_TICKET_INVALID"
37
+ public static let emailTicketRequired = "EMAIL_TICKET_REQUIRED"
38
+ public static let emailUnavailable = "EMAIL_UNAVAILABLE"
39
+ public static let badRequest = "BAD_REQUEST"
40
+ /// A request that never got an answer: offline, or timed out.
41
+ public static let offline: [String] = ["NETWORK_ERROR", "CONNECTION_FAILED", "TIMEOUT"]
42
+ }
43
+
44
+ /// `normalizeEmailSignInCode`: no spaces or dashes, upper case.
45
+ public func normalizeSignInCode(_ code: String) -> String {
46
+ code.trimmed().replacingOccurrences(of: " ", with: "").replacingOccurrences(of: "-", with: "").uppercased()
47
+ }
48
+
49
+ /// Whether every character of `text` is a digit.
50
+ func allDigits(_ text: String) -> Bool {
51
+ for ch in text {
52
+ if !"0123456789".contains(String(ch)) { return false }
53
+ }
54
+ return true
55
+ }
56
+
57
+ /// A typed sign-in code is complete: 6 digits, or the 10-character long code in its alphabet
58
+ /// (any case, with or without its dash).
59
+ public func isCompleteSignInCode(_ typed: String) -> Bool {
60
+ let trimmed = typed.trimmed()
61
+ if trimmed.count == EMAIL_CODE_LENGTH && allDigits(trimmed) { return true }
62
+ let long = normalizeSignInCode(trimmed)
63
+ if long.count != EMAIL_SIGNIN_LONG_CODE_LENGTH { return false }
64
+ for ch in long {
65
+ if !EMAIL_SIGNIN_LONG_CODE_ALPHABET.contains(String(ch)) { return false }
66
+ }
67
+ return true
68
+ }
69
+
70
+ /// Every box of the second-factor field is filled: a partial code is never sent.
71
+ public func isCompleteSecondFactorCode(_ typed: String, backup: Bool) -> Bool {
72
+ typed.trimmed().count == (backup ? BACKUP_CODE_LENGTH : TOTP_DIGITS)
73
+ }
74
+
75
+ /// `withoutWhitespace`: an identifier holds no spaces, tabs or line breaks.
76
+ public func withoutWhitespace(_ text: String) -> String {
77
+ var out = ""
78
+ for ch in text {
79
+ if !ch.isWhitespace { out += String(ch) }
80
+ }
81
+ return out
82
+ }
83
+
84
+ /// A failed call in the person's language: the catalog key of `describeApiError`'s sentence.
85
+ /// Never says whether an account exists: every "no" is the same key.
86
+ public func describeFailure(_ failure: SignInFailure) -> String {
87
+ if let code = failure.code {
88
+ if SignInCode.offline.contains(code) { return "signin.errors.network" }
89
+ switch code {
90
+ case "SIGNIN_INVALID_CREDENTIALS":
91
+ return "signin.errors.invalidCredentials"
92
+ case "SIGNIN_REQUEST_INVALID", "SIGNIN_LINK_INVALID":
93
+ return "signin.errors.requestExpired"
94
+ case "SECOND_FACTOR_INVALID", "TOTP_CODE_INVALID":
95
+ return "signin.errors.secondFactorInvalid"
96
+ case "SIGNIN_ORIGIN_NOT_ALLOWED":
97
+ return "signin.errors.originNotAllowed"
98
+ case "USERNAME_TAKEN":
99
+ return "signup.username.taken"
100
+ case "EMAIL_ACCOUNTS_FULL":
101
+ return "signin.errors.emailAccountsFull"
102
+ case "EMAIL_CODE_INVALID":
103
+ return "emailCode.errors.codeInvalid"
104
+ case "EMAIL_CODE_TOO_MANY_ATTEMPTS":
105
+ return "emailCode.errors.tooManyAttempts"
106
+ case "EMAIL_TICKET_INVALID", "EMAIL_TICKET_REQUIRED":
107
+ return "emailCode.errors.expired"
108
+ case "EMAIL_UNAVAILABLE":
109
+ return "emailCode.errors.unavailable"
110
+ case "BAD_REQUEST":
111
+ return "signin.errors.invalidInput"
112
+ default:
113
+ break
114
+ }
115
+ }
116
+ if failure.status == 0 { return "signin.errors.network" }
117
+ if failure.status >= 500 && failure.status < 600 { return "signin.errors.server" }
118
+ return "signin.errors.generic"
119
+ }
120
+
121
+ /// Whether the failure is a 429: a wait, not a mistake.
122
+ public func isRateLimited(_ failure: SignInFailure) -> Bool {
123
+ failure.status == 429
124
+ }
125
+
126
+ /// How long to wait after a 429, whole seconds.
127
+ public func retryAfter(_ failure: SignInFailure) -> Int {
128
+ guard let seconds = failure.retryAfterSeconds else { return RATE_LIMIT_SECONDS }
129
+ return Int(seconds.ceil())
130
+ }
131
+
132
+ /// Whether the email's ticket is gone and the sign-up must confirm the email again.
133
+ public func isTicketExpired(_ failure: SignInFailure) -> Bool {
134
+ failure.code == SignInCode.emailTicketInvalid || failure.code == SignInCode.emailTicketRequired
135
+ }