@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.
- package/bloom/Package.bloom +15 -0
- package/bloom/Resources/Localizable/ar-SA.json +108 -0
- package/bloom/Resources/Localizable/bn-BD.json +108 -0
- package/bloom/Resources/Localizable/ca-ES.json +111 -0
- package/bloom/Resources/Localizable/de-DE.json +108 -0
- package/bloom/Resources/Localizable/en-US.json +124 -0
- package/bloom/Resources/Localizable/es-ES.json +124 -0
- package/bloom/Resources/Localizable/fr-FR.json +108 -0
- package/bloom/Resources/Localizable/hi-IN.json +108 -0
- package/bloom/Resources/Localizable/id-ID.json +108 -0
- package/bloom/Resources/Localizable/it-IT.json +108 -0
- package/bloom/Resources/Localizable/ja-JP.json +108 -0
- package/bloom/Resources/Localizable/ko-KR.json +108 -0
- package/bloom/Resources/Localizable/pt-PT.json +108 -0
- package/bloom/Resources/Localizable/ru-RU.json +108 -0
- package/bloom/Resources/Localizable/tr-TR.json +108 -0
- package/bloom/Resources/Localizable/zh-CN.json +108 -0
- package/bloom/Sources/OxyAccountUI/SignInFlow.bloom +322 -0
- package/bloom/Sources/OxyAccountUI/SignInRules.bloom +135 -0
- package/bloom/Sources/OxyAccountUI/SignInService.bloom +141 -0
- package/bloom/Sources/OxyAccountUI/SignUpFlow.bloom +322 -0
- package/bloom/Sources/OxyAccountUI/SignUpRules.bloom +189 -0
- package/bloom/Sources/OxyAccountUIChecks/Checks.bloom +254 -0
- package/bloom/Sources/OxyAccountUIChecks/FakeAuth.bloom +123 -0
- package/bloom/Sources/OxyAccountUIChecks/SignUpChecks.bloom +193 -0
- package/lib/commonjs/ui/boot/runProviderColdBoot.js +43 -329
- package/lib/commonjs/ui/boot/runProviderColdBoot.js.map +1 -1
- package/lib/commonjs/ui/context/OxyContext.js +7 -1
- package/lib/commonjs/ui/context/OxyContext.js.map +1 -1
- package/lib/commonjs/ui/oauth/applicationSessionLane.js +12 -25
- package/lib/commonjs/ui/oauth/applicationSessionLane.js.map +1 -1
- package/lib/commonjs/ui/session/tokenTransport.js +7 -69
- package/lib/commonjs/ui/session/tokenTransport.js.map +1 -1
- package/lib/module/ui/boot/runProviderColdBoot.js +43 -329
- package/lib/module/ui/boot/runProviderColdBoot.js.map +1 -1
- package/lib/module/ui/context/OxyContext.js +8 -2
- package/lib/module/ui/context/OxyContext.js.map +1 -1
- package/lib/module/ui/oauth/applicationSessionLane.js +4 -23
- package/lib/module/ui/oauth/applicationSessionLane.js.map +1 -1
- package/lib/module/ui/session/tokenTransport.js +3 -68
- package/lib/module/ui/session/tokenTransport.js.map +1 -1
- package/lib/typescript/commonjs/ui/boot/runProviderColdBoot.d.ts +8 -31
- package/lib/typescript/commonjs/ui/boot/runProviderColdBoot.d.ts.map +1 -1
- package/lib/typescript/commonjs/ui/context/OxyContext.d.ts.map +1 -1
- package/lib/typescript/commonjs/ui/oauth/applicationSessionLane.d.ts +1 -5
- package/lib/typescript/commonjs/ui/oauth/applicationSessionLane.d.ts.map +1 -1
- package/lib/typescript/commonjs/ui/session/tokenTransport.d.ts +1 -33
- package/lib/typescript/commonjs/ui/session/tokenTransport.d.ts.map +1 -1
- package/lib/typescript/module/ui/boot/runProviderColdBoot.d.ts +8 -31
- package/lib/typescript/module/ui/boot/runProviderColdBoot.d.ts.map +1 -1
- package/lib/typescript/module/ui/context/OxyContext.d.ts.map +1 -1
- package/lib/typescript/module/ui/oauth/applicationSessionLane.d.ts +1 -5
- package/lib/typescript/module/ui/oauth/applicationSessionLane.d.ts.map +1 -1
- package/lib/typescript/module/ui/session/tokenTransport.d.ts +1 -33
- package/lib/typescript/module/ui/session/tokenTransport.d.ts.map +1 -1
- package/package.json +10 -5
- package/src/ui/boot/runProviderColdBoot.ts +40 -364
- package/src/ui/context/OxyContext.tsx +8 -1
- package/src/ui/oauth/applicationSessionLane.ts +7 -35
- package/src/ui/session/tokenTransport.ts +3 -75
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
// The sign-in model's checks: one program every backend runs (`checks/run.mjs` on TS; Kotlin
|
|
2
|
+
// through bloom-native), printing the same lines, compared with `checks/expected.txt`.
|
|
3
|
+
|
|
4
|
+
import OxyAccountUI
|
|
5
|
+
|
|
6
|
+
func stageName(_ stage: SignInStage) -> String {
|
|
7
|
+
switch stage {
|
|
8
|
+
case .start:
|
|
9
|
+
return "start"
|
|
10
|
+
case .checkEmail(_):
|
|
11
|
+
return "check-email"
|
|
12
|
+
case .choose(_, let accounts):
|
|
13
|
+
return "choose \(accounts.count)"
|
|
14
|
+
case .password(_):
|
|
15
|
+
return "password"
|
|
16
|
+
case .secondFactor(_, _):
|
|
17
|
+
return "second-factor"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/// What the person sees, with the identifier they typed left out.
|
|
22
|
+
func visible(_ flow: SignInFlow) -> String {
|
|
23
|
+
"\(stageName(flow.stage)) error=\(flow.error?.key ?? "-") notice=\(flow.noticeKey ?? "-") wait=\(flow.rateLimitSeconds) resend=\(flow.resendSeconds) pending=\(flow.pending)"
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
func failure(_ code: String, _ status: Int) -> SignInFailure {
|
|
27
|
+
signInFailure(code, status)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/// A known and an unknown identifier through the same server answers: start, a wrong code, the
|
|
31
|
+
/// password instead and a wrong password, then the sign-in email again.
|
|
32
|
+
func enumerationTrace(_ identifier: String) async -> [String] {
|
|
33
|
+
let auth = FakeAuth()
|
|
34
|
+
let flow = SignInFlow(service: auth)
|
|
35
|
+
var states: [String] = []
|
|
36
|
+
flow.setIdentifier(identifier)
|
|
37
|
+
await flow.submitIdentifier()
|
|
38
|
+
states.append(visible(flow))
|
|
39
|
+
auth.answers = [Answer.fail(failure("EMAIL_CODE_INVALID", 400))]
|
|
40
|
+
flow.setCode("000000")
|
|
41
|
+
await flow.submitCode()
|
|
42
|
+
states.append(visible(flow))
|
|
43
|
+
flow.toPassword()
|
|
44
|
+
states.append(visible(flow))
|
|
45
|
+
auth.answers = [Answer.fail(failure("SIGNIN_INVALID_CREDENTIALS", 401))]
|
|
46
|
+
flow.setPassword("not-the-password")
|
|
47
|
+
await flow.submitPassword()
|
|
48
|
+
states.append(visible(flow))
|
|
49
|
+
await flow.forgotPassword()
|
|
50
|
+
states.append(visible(flow))
|
|
51
|
+
return states
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public func runChecks() async -> [String] {
|
|
55
|
+
var out: [String] = []
|
|
56
|
+
|
|
57
|
+
// An empty identifier is refused here, and nothing is sent.
|
|
58
|
+
do {
|
|
59
|
+
let auth = FakeAuth()
|
|
60
|
+
let flow = SignInFlow(service: auth)
|
|
61
|
+
flow.setIdentifier(" \t ")
|
|
62
|
+
await flow.submitIdentifier()
|
|
63
|
+
out.append("empty identifier: \(visible(flow)) calls=\(auth.log.count)")
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Whitespace never reaches the identifier.
|
|
67
|
+
do {
|
|
68
|
+
let flow = SignInFlow(service: FakeAuth())
|
|
69
|
+
flow.setIdentifier(" ana @example.com\n")
|
|
70
|
+
out.append("identifier typed: \(flow.identifier)")
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Start: "Check your email", and the resend wait.
|
|
74
|
+
do {
|
|
75
|
+
let auth = FakeAuth()
|
|
76
|
+
let flow = SignInFlow(service: auth)
|
|
77
|
+
flow.setIdentifier("ana")
|
|
78
|
+
await flow.submitIdentifier()
|
|
79
|
+
out.append("start: \(visible(flow)) | \(auth.log.joined(separator: "; "))")
|
|
80
|
+
// A resend says so and keeps the field's kind.
|
|
81
|
+
flow.toggleLongCode()
|
|
82
|
+
for _ in 0..<30 { flow.tick() }
|
|
83
|
+
await flow.startEmail("ana")
|
|
84
|
+
out.append("resend: \(visible(flow)) long=\(flow.longCode)")
|
|
85
|
+
// A new request from the start starts at 6 digits.
|
|
86
|
+
flow.backToIdentifier()
|
|
87
|
+
await flow.submitIdentifier()
|
|
88
|
+
out.append("again: \(visible(flow)) long=\(flow.longCode)")
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// The send budget is spent: nothing was sent, try later.
|
|
92
|
+
do {
|
|
93
|
+
let auth = FakeAuth()
|
|
94
|
+
auth.starts = [EmailSignInStart(requestId: "req-1", requestSecret: "secret-1", retryLater: true)]
|
|
95
|
+
let flow = SignInFlow(service: auth)
|
|
96
|
+
flow.setIdentifier("ana")
|
|
97
|
+
await flow.submitIdentifier()
|
|
98
|
+
out.append("retry later: \(visible(flow))")
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// ADR 0030 D2: a known and an unknown identifier see the same states.
|
|
102
|
+
do {
|
|
103
|
+
let known = await enumerationTrace("ana@example.com")
|
|
104
|
+
let unknown = await enumerationTrace("nobody-here@example.com")
|
|
105
|
+
for line in known { out.append("enumeration: \(line)") }
|
|
106
|
+
out.append("enumeration, same for an unknown identifier: \(known == unknown ? "yes" : "NO")")
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// A partial code is never sent.
|
|
110
|
+
do {
|
|
111
|
+
let auth = FakeAuth()
|
|
112
|
+
let flow = SignInFlow(service: auth)
|
|
113
|
+
flow.setIdentifier("ana")
|
|
114
|
+
await flow.submitIdentifier()
|
|
115
|
+
flow.setCode("12345")
|
|
116
|
+
await flow.submitCode()
|
|
117
|
+
out.append("partial code: \(visible(flow)) calls=\(auth.log.count)")
|
|
118
|
+
// The long code, any case, with its dash, goes as typed.
|
|
119
|
+
flow.toggleLongCode()
|
|
120
|
+
auth.answers = [Answer.step(SignInStep.session(grant()))]
|
|
121
|
+
flow.setCode(" abcde-fghjk ")
|
|
122
|
+
await flow.submitCode()
|
|
123
|
+
out.append("long code: signedIn=\(flow.signedIn) | \(auth.log.joined(separator: "; "))")
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// The code, then the authenticator: signed in once.
|
|
127
|
+
do {
|
|
128
|
+
let auth = FakeAuth()
|
|
129
|
+
let flow = SignInFlow(service: auth)
|
|
130
|
+
flow.setIdentifier("ana")
|
|
131
|
+
await flow.submitIdentifier()
|
|
132
|
+
auth.answers = [Answer.step(SignInStep.secondFactor(challengeId: "ch-1"))]
|
|
133
|
+
flow.setCode("123456")
|
|
134
|
+
await flow.submitCode()
|
|
135
|
+
out.append("second factor: \(visible(flow))")
|
|
136
|
+
flow.setSecondFactorCode("12345")
|
|
137
|
+
await flow.submitSecondFactor()
|
|
138
|
+
out.append("second factor, partial: \(visible(flow))")
|
|
139
|
+
flow.toggleBackupCode()
|
|
140
|
+
auth.answers = [Answer.step(SignInStep.session(grant()))]
|
|
141
|
+
flow.setSecondFactorCode("abcdefghij")
|
|
142
|
+
await flow.submitSecondFactor()
|
|
143
|
+
out.append("second factor, backup: signedIn=\(flow.signedIn) commits=\(auth.commits) | \(auth.log.joined(separator: "; "))")
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// One email, several accounts (ADR 0040): which one.
|
|
147
|
+
do {
|
|
148
|
+
let auth = FakeAuth()
|
|
149
|
+
let flow = SignInFlow(service: auth)
|
|
150
|
+
flow.setIdentifier("ana@example.com")
|
|
151
|
+
await flow.submitIdentifier()
|
|
152
|
+
let accounts = [
|
|
153
|
+
AccountOption(id: "u-1", username: "ana", displayName: "Ana", avatarUrl: none),
|
|
154
|
+
AccountOption(id: "u-2", username: "ana.work", displayName: none, avatarUrl: none),
|
|
155
|
+
]
|
|
156
|
+
auth.answers = [Answer.step(SignInStep.chooseAccount(accounts))]
|
|
157
|
+
flow.setCode("123456")
|
|
158
|
+
await flow.submitCode()
|
|
159
|
+
out.append("choose: \(visible(flow))")
|
|
160
|
+
auth.answers = [Answer.step(SignInStep.secondFactor(challengeId: "ch-2"))]
|
|
161
|
+
await flow.choose(accounts[1])
|
|
162
|
+
out.append("chose: \(visible(flow)) choosing=\(flow.choosingId ?? "-") | \(auth.log.last ?? "-")")
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// A 429 is a wait, counted down, and holds every action back.
|
|
166
|
+
do {
|
|
167
|
+
let auth = FakeAuth()
|
|
168
|
+
let flow = SignInFlow(service: auth)
|
|
169
|
+
flow.setIdentifier("ana")
|
|
170
|
+
await flow.submitIdentifier()
|
|
171
|
+
auth.answers = [Answer.fail(SignInFailure(code: none, status: 429, retryAfterSeconds: 12.4, field: none, detail: none))]
|
|
172
|
+
flow.setCode("123456")
|
|
173
|
+
await flow.submitCode()
|
|
174
|
+
out.append("rate limited: \(visible(flow)) blocked=\(flow.blocked)")
|
|
175
|
+
let before = auth.log.count
|
|
176
|
+
flow.setCode("654321")
|
|
177
|
+
await flow.submitCode()
|
|
178
|
+
out.append("rate limited, held back: calls=\(auth.log.count - before)")
|
|
179
|
+
for _ in 0..<13 { flow.tick() }
|
|
180
|
+
out.append("rate limited, over: \(visible(flow)) blocked=\(flow.blocked)")
|
|
181
|
+
let other = SignInFlow(service: auth)
|
|
182
|
+
auth.starts = []
|
|
183
|
+
auth.answers = [Answer.fail(signInFailure(none, 429))]
|
|
184
|
+
other.setIdentifier("ana")
|
|
185
|
+
await other.submitIdentifier()
|
|
186
|
+
other.setCode("123456")
|
|
187
|
+
await other.submitCode()
|
|
188
|
+
out.append("rate limited, no wait named: wait=\(other.rateLimitSeconds)")
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// The link opened in this browser, and the typed code, race: one sign-in.
|
|
192
|
+
do {
|
|
193
|
+
let auth = FakeAuth()
|
|
194
|
+
let flow = SignInFlow(service: auth)
|
|
195
|
+
flow.setIdentifier("ana")
|
|
196
|
+
await flow.submitIdentifier()
|
|
197
|
+
auth.answers = [Answer.pending]
|
|
198
|
+
let waiting = await flow.pollLink()
|
|
199
|
+
auth.answers = [Answer.step(SignInStep.session(grant())), Answer.step(SignInStep.session(grant()))]
|
|
200
|
+
let again = await flow.pollLink()
|
|
201
|
+
flow.setCode("123456")
|
|
202
|
+
await flow.submitCode()
|
|
203
|
+
out.append("link: keep asking=\(waiting) then=\(again) signedIn=\(flow.signedIn) commits=\(auth.commits)")
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// A spent request is not asked for again; a failed ask is.
|
|
207
|
+
do {
|
|
208
|
+
let auth = FakeAuth()
|
|
209
|
+
let flow = SignInFlow(service: auth)
|
|
210
|
+
flow.setIdentifier("ana")
|
|
211
|
+
await flow.submitIdentifier()
|
|
212
|
+
auth.answers = [Answer.fail(failure("SIGNIN_REQUEST_INVALID", 400))]
|
|
213
|
+
let spent = await flow.pollLink()
|
|
214
|
+
auth.answers = [Answer.fail(signInFailure("NETWORK_ERROR", 0))]
|
|
215
|
+
let offline = await flow.pollLink()
|
|
216
|
+
flow.backToIdentifier()
|
|
217
|
+
let gone = await flow.pollLink()
|
|
218
|
+
out.append("link asks: spent=\(spent) offline=\(offline) step gone=\(gone)")
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// The password: refused empty, then signed in.
|
|
222
|
+
do {
|
|
223
|
+
let auth = FakeAuth()
|
|
224
|
+
let flow = SignInFlow(service: auth)
|
|
225
|
+
flow.setIdentifier("ana")
|
|
226
|
+
await flow.submitIdentifier()
|
|
227
|
+
flow.toPassword()
|
|
228
|
+
await flow.submitPassword()
|
|
229
|
+
out.append("password, empty: \(visible(flow))")
|
|
230
|
+
auth.answers = [Answer.step(SignInStep.session(grant()))]
|
|
231
|
+
flow.setPassword("correct horse battery")
|
|
232
|
+
await flow.submitPassword()
|
|
233
|
+
out.append("password: signedIn=\(flow.signedIn) | \(auth.log.joined(separator: "; "))")
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// Failures in the person's language: one sentence per kind.
|
|
237
|
+
let failures: [SignInFailure] = [
|
|
238
|
+
signInFailure("TIMEOUT", 0),
|
|
239
|
+
signInFailure(none, 0),
|
|
240
|
+
signInFailure(none, 503),
|
|
241
|
+
signInFailure("SOMETHING_NEW", 400),
|
|
242
|
+
failure("SIGNIN_LINK_INVALID", 400),
|
|
243
|
+
failure("TOTP_CODE_INVALID", 400),
|
|
244
|
+
failure("EMAIL_TICKET_REQUIRED", 400),
|
|
245
|
+
failure("BAD_REQUEST", 400),
|
|
246
|
+
]
|
|
247
|
+
for item in failures {
|
|
248
|
+
out.append("describe \(item.code ?? "-") \(item.status): \(describeFailure(item))")
|
|
249
|
+
}
|
|
250
|
+
for line in await runSignUpChecks() {
|
|
251
|
+
out.append(line)
|
|
252
|
+
}
|
|
253
|
+
return out
|
|
254
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// A scripted server for the checks: each call takes the next answer queued for it and is logged
|
|
2
|
+
// by name and its non-secret arguments. A request secret is never logged, only whether it was
|
|
3
|
+
// the one the start handed out.
|
|
4
|
+
|
|
5
|
+
import OxyAccountUI
|
|
6
|
+
|
|
7
|
+
/// What one call answers.
|
|
8
|
+
public enum Answer {
|
|
9
|
+
case step(SignInStep)
|
|
10
|
+
case pending
|
|
11
|
+
case fail(SignInFailure)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public final class FakeAuth: SignInService {
|
|
15
|
+
public var log: [String] = []
|
|
16
|
+
public var starts: [EmailSignInStart] = []
|
|
17
|
+
public var answers: [Answer] = []
|
|
18
|
+
public var commits = 0
|
|
19
|
+
public var issuedSecret = "secret-1"
|
|
20
|
+
/// Usernames the server says are taken.
|
|
21
|
+
public var taken: [String] = []
|
|
22
|
+
/// Thrown by the next sign-up verification start or confirm, once.
|
|
23
|
+
public var verifyFailure: SignInFailure? = none
|
|
24
|
+
|
|
25
|
+
public init() {}
|
|
26
|
+
|
|
27
|
+
func next() throws -> SignInStep? {
|
|
28
|
+
guard let answer = answers.first else { return none }
|
|
29
|
+
answers = Array(answers.dropFirst())
|
|
30
|
+
if case .fail(let failure) = answer { throw failure }
|
|
31
|
+
if case .step(let step) = answer { return step }
|
|
32
|
+
return none
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
func proves(_ request: EmailRequest) -> String {
|
|
36
|
+
request.requestSecret == issuedSecret ? "secret ok" : "secret WRONG"
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public func emailStart(_ identifier: String) async throws -> EmailSignInStart {
|
|
40
|
+
log.append("start \(identifier)")
|
|
41
|
+
if let first = starts.first {
|
|
42
|
+
starts = Array(starts.dropFirst())
|
|
43
|
+
return first
|
|
44
|
+
}
|
|
45
|
+
return EmailSignInStart(requestId: "req-1", requestSecret: issuedSecret, retryLater: false)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public func emailConfirm(_ request: EmailRequest, code: String) async throws -> SignInStep {
|
|
49
|
+
log.append("confirm \(request.requestId) \(code) \(proves(request))")
|
|
50
|
+
guard let step = try next() else { throw signInFailure("SIGNIN_REQUEST_INVALID", 400) }
|
|
51
|
+
return step
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public func emailCollect(_ request: EmailRequest) async throws -> SignInStep? {
|
|
55
|
+
log.append("collect \(request.requestId) \(proves(request))")
|
|
56
|
+
return try next()
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public func emailChoose(_ request: EmailRequest, userId: String) async throws -> SignInStep {
|
|
60
|
+
log.append("choose \(request.requestId) \(userId) \(proves(request))")
|
|
61
|
+
guard let step = try next() else { throw signInFailure("SIGNIN_REQUEST_INVALID", 400) }
|
|
62
|
+
return step
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public func passwordSignIn(identifier: String, password: String) async throws -> SignInStep {
|
|
66
|
+
log.append("password \(identifier) (\(password.count) characters)")
|
|
67
|
+
guard let step = try next() else { throw signInFailure("SIGNIN_INVALID_CREDENTIALS", 401) }
|
|
68
|
+
return step
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public func completeSecondFactor(challengeId: String, code: String) async throws -> SignInStep {
|
|
72
|
+
log.append("second-factor \(challengeId) \(code)")
|
|
73
|
+
guard let step = try next() else { throw signInFailure("SECOND_FACTOR_INVALID", 400) }
|
|
74
|
+
return step
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public func usernameAvailable(_ username: String) async throws -> Bool {
|
|
78
|
+
log.append("username \(username)")
|
|
79
|
+
return !taken.contains(username.lowercased())
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public func startSignupVerification(email: String) async throws -> String {
|
|
83
|
+
log.append("verify-start \(email)")
|
|
84
|
+
return "ver-\(log.count)"
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
public func confirmVerification(_ verificationId: String, code: String) async throws -> String {
|
|
88
|
+
log.append("verify-confirm \(verificationId) \(code)")
|
|
89
|
+
if let failure = verifyFailure {
|
|
90
|
+
verifyFailure = none
|
|
91
|
+
throw failure
|
|
92
|
+
}
|
|
93
|
+
return "ticket-1"
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
public func signUp(_ request: SignUpRequest) async throws -> SignInStep {
|
|
97
|
+
log.append("signup \(request.username) \(request.email) \(request.dateOfBirth) \(request.emailTicket) \(request.languages.joined(separator: ","))")
|
|
98
|
+
guard let step = try next() else { throw signInFailure(none, 500) }
|
|
99
|
+
return step
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
public func commit(_ grant: SessionGrant, firstFactor: FirstFactor) async throws {
|
|
103
|
+
commits += 1
|
|
104
|
+
log.append("commit \(firstFactor)")
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/// A session the fake server mints.
|
|
109
|
+
public func grant() -> SessionGrant {
|
|
110
|
+
let raw: JSONValue = ["sessionId": "s-1"]
|
|
111
|
+
return SessionGrant(raw: raw)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/// A clock the checks move by hand (epoch milliseconds).
|
|
115
|
+
public final class FakeClock {
|
|
116
|
+
public var now = 1_000_000.0
|
|
117
|
+
|
|
118
|
+
public init() {}
|
|
119
|
+
|
|
120
|
+
public func advance(seconds: Double) {
|
|
121
|
+
now += seconds * 1000.0
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
// The sign-up model's checks, printed after the sign-in's by `runChecks`.
|
|
2
|
+
|
|
3
|
+
import OxyAccountUI
|
|
4
|
+
|
|
5
|
+
func signUpStage(_ stage: SignUpStage) -> String {
|
|
6
|
+
switch stage {
|
|
7
|
+
case .username:
|
|
8
|
+
return "username"
|
|
9
|
+
case .birthdate:
|
|
10
|
+
return "birthdate"
|
|
11
|
+
case .email:
|
|
12
|
+
return "email"
|
|
13
|
+
case .code(let id):
|
|
14
|
+
return "code \(id)"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
func shown(_ message: FlowMessage?) -> String {
|
|
19
|
+
guard let m = message else { return "-" }
|
|
20
|
+
var text = m.key
|
|
21
|
+
for (name, value) in m.args {
|
|
22
|
+
text += " \(name)=\(value)"
|
|
23
|
+
}
|
|
24
|
+
return text
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
func signUpVisible(_ flow: SignUpFlow) -> String {
|
|
28
|
+
"\(signUpStage(flow.stage)) error=\(shown(flow.error)) notice=\(flow.noticeKey ?? "-") available=\(flow.showsAvailable) taken=\(flow.liveTaken) pending=\(flow.pending)"
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
func newSignUp(_ auth: FakeAuth, _ clock: FakeClock) -> SignUpFlow {
|
|
32
|
+
withBudget(auth, UsernameLookupBudget(clock: { clock.now }))
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/// A sign-up on a budget another screen shares: the app holds one, as the server counts one.
|
|
36
|
+
func withBudget(_ auth: FakeAuth, _ budget: UsernameLookupBudget) -> SignUpFlow {
|
|
37
|
+
SignUpFlow(service: auth, budget: budget, today: { "2026-12-01" }, languages: ["es-ES"])
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public func runSignUpChecks() async -> [String] {
|
|
41
|
+
var out: [String] = []
|
|
42
|
+
|
|
43
|
+
// The policy, in the person's words, before the server is asked.
|
|
44
|
+
for name in ["", "ab", "a-", "a--b", "1234", "ana_maria", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"] {
|
|
45
|
+
switch checkUsernameDraft(name) {
|
|
46
|
+
case .ok(let handle):
|
|
47
|
+
out.append("username '\(name)': ok \(handle)")
|
|
48
|
+
case .invalid(let rule):
|
|
49
|
+
out.append("username '\(name)': \(shown(rule))")
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
// The server's refusal of a reserved name names the rule; any other failure is not one.
|
|
53
|
+
let reserved = SignInFailure(code: "BAD_REQUEST", status: 400, retryAfterSeconds: none, field: "username", detail: RESERVED_USERNAME_MESSAGE)
|
|
54
|
+
out.append("refusal reserved: \(shown(describeUsernameRefusal(reserved, "admin")))")
|
|
55
|
+
out.append("refusal other: \(shown(describeUsernameRefusal(signInFailure("BAD_REQUEST", 400), "admin")))")
|
|
56
|
+
|
|
57
|
+
// Addresses, as the contract reads them.
|
|
58
|
+
for address in [" Ana@Example.COM ", "a@b.co", ".a@b.co", "a..b@c.co", "a'@b.co", "a@b", "a@-b.co", "a@b.c0"] {
|
|
59
|
+
out.append("email '\(address)': \(checkEmailAddress(address) ?? "invalid")")
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Ages, in UTC, with 29 February on 1 March.
|
|
63
|
+
out.append("age: \(ageInYears("2013-12-01", today: "2026-12-01")) \(ageInYears("2013-12-02", today: "2026-12-01")) \(ageInYears("2008-02-29", today: "2027-02-28")) \(ageInYears("2008-02-29", today: "2027-03-01"))")
|
|
64
|
+
|
|
65
|
+
// Typing keeps only the policy's characters.
|
|
66
|
+
do {
|
|
67
|
+
let flow = newSignUp(FakeAuth(), FakeClock())
|
|
68
|
+
flow.setUsername("ana maría!_1")
|
|
69
|
+
out.append("typed: \(flow.username)")
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// The whole way: username, date, email, code, signed in.
|
|
73
|
+
do {
|
|
74
|
+
let auth = FakeAuth()
|
|
75
|
+
let clock = FakeClock()
|
|
76
|
+
let flow = newSignUp(auth, clock)
|
|
77
|
+
flow.setUsername("ana")
|
|
78
|
+
await flow.checkAvailability()
|
|
79
|
+
out.append("signup live: \(signUpVisible(flow))")
|
|
80
|
+
await flow.submitUsername()
|
|
81
|
+
out.append("signup username: \(signUpVisible(flow))")
|
|
82
|
+
flow.submitBirthdate()
|
|
83
|
+
out.append("signup no date: \(signUpVisible(flow))")
|
|
84
|
+
flow.setDateOfBirth("2015-06-01")
|
|
85
|
+
out.append("signup guardian note: \(flow.needsGuardian)")
|
|
86
|
+
flow.submitBirthdate()
|
|
87
|
+
flow.setEmail("not an address")
|
|
88
|
+
await flow.submitEmail()
|
|
89
|
+
out.append("signup bad email: \(signUpVisible(flow))")
|
|
90
|
+
flow.setEmail(" Ana@Example.com")
|
|
91
|
+
await flow.submitEmail()
|
|
92
|
+
out.append("signup email: \(signUpVisible(flow)) email=\(flow.email)")
|
|
93
|
+
await flow.resendCode()
|
|
94
|
+
out.append("signup resend: \(signUpVisible(flow))")
|
|
95
|
+
flow.setCode("12 34")
|
|
96
|
+
await flow.submitCode()
|
|
97
|
+
out.append("signup short code: \(signUpVisible(flow))")
|
|
98
|
+
auth.answers = [Answer.step(SignInStep.session(grant()))]
|
|
99
|
+
flow.setCode("123-456")
|
|
100
|
+
await flow.submitCode()
|
|
101
|
+
out.append("signup done: signedIn=\(flow.signedIn) | \(auth.log.joined(separator: "; "))")
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Taken, live and at the submit; taken between typing and creating.
|
|
105
|
+
do {
|
|
106
|
+
let auth = FakeAuth()
|
|
107
|
+
auth.taken = ["ana"]
|
|
108
|
+
let flow = newSignUp(auth, FakeClock())
|
|
109
|
+
flow.setUsername("Ana")
|
|
110
|
+
await flow.checkAvailability()
|
|
111
|
+
out.append("taken live: \(signUpVisible(flow))")
|
|
112
|
+
await flow.submitUsername()
|
|
113
|
+
out.append("taken submit: \(signUpVisible(flow)) lookups=\(auth.log.count)")
|
|
114
|
+
}
|
|
115
|
+
do {
|
|
116
|
+
let auth = FakeAuth()
|
|
117
|
+
let flow = newSignUp(auth, FakeClock())
|
|
118
|
+
flow.setUsername("ana")
|
|
119
|
+
await flow.submitUsername()
|
|
120
|
+
flow.setDateOfBirth("1990-01-01")
|
|
121
|
+
flow.submitBirthdate()
|
|
122
|
+
flow.setEmail("ana@example.com")
|
|
123
|
+
await flow.submitEmail()
|
|
124
|
+
auth.answers = [Answer.fail(signInFailure("USERNAME_TAKEN", 409))]
|
|
125
|
+
flow.setCode("123456")
|
|
126
|
+
await flow.submitCode()
|
|
127
|
+
out.append("taken at creation: \(signUpVisible(flow))")
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// An expired ticket goes back to the email.
|
|
131
|
+
do {
|
|
132
|
+
let auth = FakeAuth()
|
|
133
|
+
let flow = newSignUp(auth, FakeClock())
|
|
134
|
+
flow.setUsername("ana")
|
|
135
|
+
await flow.submitUsername()
|
|
136
|
+
flow.setDateOfBirth("1990-01-01")
|
|
137
|
+
flow.submitBirthdate()
|
|
138
|
+
flow.setEmail("ana@example.com")
|
|
139
|
+
await flow.submitEmail()
|
|
140
|
+
auth.answers = [Answer.fail(signInFailure("EMAIL_TICKET_INVALID", 400))]
|
|
141
|
+
flow.setCode("123456")
|
|
142
|
+
await flow.submitCode()
|
|
143
|
+
out.append("ticket expired: \(signUpVisible(flow))")
|
|
144
|
+
auth.verifyFailure = signInFailure("EMAIL_CODE_INVALID", 400)
|
|
145
|
+
await flow.submitEmail()
|
|
146
|
+
flow.setCode("000000")
|
|
147
|
+
await flow.submitCode()
|
|
148
|
+
out.append("wrong code: \(signUpVisible(flow))")
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// The lookup budget: ten a minute, the live check never takes the last one, one answer reused.
|
|
152
|
+
do {
|
|
153
|
+
let auth = FakeAuth()
|
|
154
|
+
let clock = FakeClock()
|
|
155
|
+
let budget = UsernameLookupBudget(clock: { clock.now })
|
|
156
|
+
let flow = withBudget(auth, budget)
|
|
157
|
+
for i in 0..<9 {
|
|
158
|
+
flow.setUsername("name\(i)")
|
|
159
|
+
await flow.checkAvailability()
|
|
160
|
+
}
|
|
161
|
+
flow.setUsername("name9")
|
|
162
|
+
await flow.checkAvailability()
|
|
163
|
+
out.append("budget live: \(signUpVisible(flow)) lookups=\(auth.log.count)")
|
|
164
|
+
flow.setUsername("name3")
|
|
165
|
+
await flow.checkAvailability()
|
|
166
|
+
out.append("budget reuse: available=\(flow.showsAvailable) lookups=\(auth.log.count)")
|
|
167
|
+
flow.setUsername("name9")
|
|
168
|
+
await flow.submitUsername()
|
|
169
|
+
out.append("budget submit: \(signUpVisible(flow)) lookups=\(auth.log.count)")
|
|
170
|
+
let other = withBudget(auth, budget)
|
|
171
|
+
other.setUsername("other")
|
|
172
|
+
clock.advance(seconds: 20)
|
|
173
|
+
await other.submitUsername()
|
|
174
|
+
out.append("budget spent: \(signUpVisible(other))")
|
|
175
|
+
clock.advance(seconds: 41)
|
|
176
|
+
await other.submitUsername()
|
|
177
|
+
out.append("budget again: \(signUpVisible(other))")
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Back, step by step.
|
|
181
|
+
do {
|
|
182
|
+
let flow = newSignUp(FakeAuth(), FakeClock())
|
|
183
|
+
flow.setUsername("ana")
|
|
184
|
+
await flow.submitUsername()
|
|
185
|
+
flow.setDateOfBirth("1990-01-01")
|
|
186
|
+
flow.submitBirthdate()
|
|
187
|
+
flow.back()
|
|
188
|
+
let one = signUpStage(flow.stage)
|
|
189
|
+
flow.back()
|
|
190
|
+
out.append("back: \(one) then \(signUpStage(flow.stage))")
|
|
191
|
+
}
|
|
192
|
+
return out
|
|
193
|
+
}
|