-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathauthentication_model.dart
More file actions
366 lines (314 loc) · 9.85 KB
/
authentication_model.dart
File metadata and controls
366 lines (314 loc) · 9.85 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
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
import 'dart:typed_data';
import 'package:cedar/cedar.dart';
import 'package:celest_cloud/src/proto.dart' as pb;
import 'package:celest_cloud_auth/src/context.dart';
import 'package:celest_cloud_auth/src/model/interop.dart';
import 'package:celest_cloud_auth/src/util/typeid.dart';
import 'package:celest_core/celest_core.dart';
import 'package:corks_cedar/corks_cedar.dart';
import 'package:protobuf/protobuf.dart';
sealed class AuthenticationFactor {
const AuthenticationFactor();
factory AuthenticationFactor.fromProto(pb.AuthenticationFactor proto) {
return switch (proto.whichFactor()) {
pb.AuthenticationFactor_Factor.emailOtp =>
AuthenticationFactorEmailOtp.fromProto(proto.emailOtp),
pb.AuthenticationFactor_Factor.smsOtp =>
AuthenticationFactorSmsOtp.fromProto(proto.smsOtp),
final unknown => throw ArgumentError.value(
unknown,
'factor',
'Invalid AuthenticationFactor. Expected one of: emailOtp, smsOtp',
),
};
}
pb.AuthenticationFactor toProto() => switch (this) {
final AuthenticationFactorEmailOtp emailOtp => pb.AuthenticationFactor(
emailOtp: emailOtp.toValueProto(),
),
final AuthenticationFactorSmsOtp smsOtp => pb.AuthenticationFactor(
smsOtp: smsOtp.toValueProto(),
),
};
GeneratedMessage toValueProto();
@override
String toString() => toProto().toString();
}
final class AuthenticationFactorEmailOtp extends AuthenticationFactor {
const AuthenticationFactorEmailOtp({
required this.email,
this.code,
});
factory AuthenticationFactorEmailOtp.fromProto(
pb.AuthenticationFactorEmailOtp emailOtp,
) {
if (!emailOtp.hasEmail()) {
throw const BadRequestException(
'Missing email in AuthenticationFactorEmailOtp',
);
}
return AuthenticationFactorEmailOtp(
email: emailOtp.email,
code: emailOtp.hasCode() ? emailOtp.code : null,
);
}
final String email;
final String? code;
@override
pb.AuthenticationFactorEmailOtp toValueProto() =>
pb.AuthenticationFactorEmailOtp(
email: email,
code: code,
);
}
final class AuthenticationFactorSmsOtp extends AuthenticationFactor {
const AuthenticationFactorSmsOtp({
required this.phoneNumber,
this.code,
});
factory AuthenticationFactorSmsOtp.fromProto(
pb.AuthenticationFactorSmsOtp smsOtp,
) {
if (!smsOtp.hasPhoneNumber()) {
throw const BadRequestException(
'Missing phoneNumber in AuthenticationFactorSmsOtp',
);
}
return AuthenticationFactorSmsOtp(
phoneNumber: smsOtp.phoneNumber,
code: smsOtp.hasCode() ? smsOtp.code : null,
);
}
final String phoneNumber;
final String? code;
@override
pb.AuthenticationFactorSmsOtp toValueProto() => pb.AuthenticationFactorSmsOtp(
phoneNumber: phoneNumber,
code: code,
);
}
final class SessionClient {
const SessionClient({
required this.clientId,
this.clientType,
required this.callbacks,
});
factory SessionClient.fromProto(pb.SessionClient client) {
return SessionClient(
clientId: client.clientId,
clientType: client.hasClientType() ? client.clientType : null,
callbacks: SessionCallbacks.fromProto(client.callbacks),
);
}
final String clientId;
final pb.ClientType? clientType;
final SessionCallbacks callbacks;
pb.SessionClient toProto() => pb.SessionClient(
clientId: clientId,
clientType: clientType,
callbacks: callbacks.toProto(),
);
@override
String toString() => toProto().toString();
}
final class SessionCallbacks {
const SessionCallbacks({
required this.successUri,
this.errorUri,
});
factory SessionCallbacks.fromProto(pb.SessionCallbacks callbacks) {
return SessionCallbacks(
successUri:
callbacks.hasSuccessUri() ? Uri.parse(callbacks.successUri) : null,
errorUri: callbacks.hasErrorUri() ? Uri.parse(callbacks.errorUri) : null,
);
}
final Uri? successUri;
final Uri? errorUri;
pb.SessionCallbacks toProto() => pb.SessionCallbacks(
successUri: successUri.toString(),
errorUri: errorUri?.toString(),
);
@override
String toString() => toProto().toString();
}
sealed class SessionState {
const SessionState();
void apply(pb.Session session);
GeneratedMessage toProto();
@override
String toString() => toProto().toString();
}
final class SessionStateSuccess extends SessionState {
const SessionStateSuccess({
required this.cork,
required this.user,
required this.isNewUser,
});
factory SessionStateSuccess.fromProto(pb.AuthenticationSuccess success) {
return SessionStateSuccess(
cork: CedarCork.parse(success.identityToken),
user: success.user.toModel(),
isNewUser: success.isNewUser,
);
}
final CedarCork cork;
String get identityToken => cork.toString();
final User user;
final bool isNewUser;
@override
void apply(pb.Session session) {
session.success = toProto();
}
@override
pb.AuthenticationSuccess toProto() => pb.AuthenticationSuccess(
identityToken: cork.toString(),
user: user.toProto(),
isNewUser: isNewUser,
);
}
sealed class SessionStateNextStep extends SessionState {
const SessionStateNextStep();
factory SessionStateNextStep.fromProto(pb.AuthenticationStep proto) {
return switch (proto.whichStep()) {
pb.AuthenticationStep_Step.needsProof =>
SessionStateNeedsProof.fromProto(proto.needsProof),
pb.AuthenticationStep_Step.pendingConfirmation =>
SessionStatePendingConfirmation.fromProto(proto.pendingConfirmation),
final unknown => throw ArgumentError.value(
unknown,
'step',
'Invalid AuthenticationStep. Expected one of: needsProof, pendingConfirmation',
),
};
}
@override
pb.AuthenticationStep toProto() => switch (this) {
final SessionStateNeedsProof needsProof => pb.AuthenticationStep(
needsProof: needsProof.toValueProto(),
),
final SessionStatePendingConfirmation pendingConfirmation =>
pb.AuthenticationStep(
pendingConfirmation: pendingConfirmation.toValueProto(),
),
};
@override
void apply(pb.Session session) {
session.nextStep = toProto();
}
}
final class SessionStateNeedsProof extends SessionStateNextStep {
const SessionStateNeedsProof({
required this.factor,
});
factory SessionStateNeedsProof.fromProto(pb.AuthenticationFactor proto) {
return SessionStateNeedsProof(
factor: AuthenticationFactor.fromProto(proto),
);
}
final AuthenticationFactor factor;
pb.AuthenticationFactor toValueProto() => factor.toProto();
}
final class SessionStatePendingConfirmation extends SessionStateNextStep {
const SessionStatePendingConfirmation({
this.linkExistingUser,
this.registerUser,
});
factory SessionStatePendingConfirmation.fromProto(
pb.AuthenticationPendingConfirmation proto,
) {
return SessionStatePendingConfirmation(
linkExistingUser:
proto.hasLinkExistingUser() ? proto.linkExistingUser.toModel() : null,
registerUser:
proto.hasRegisterUser() ? proto.registerUser.toModel() : null,
);
}
final User? linkExistingUser;
final User? registerUser;
pb.AuthenticationPendingConfirmation toValueProto() =>
pb.AuthenticationPendingConfirmation(
linkExistingUser: linkExistingUser?.toProto(),
registerUser: registerUser?.toProto(),
);
}
final class Session {
Session({
EntityUid? parent,
required String sessionId,
required this.cryptoKeyId,
required this.userId,
required this.expireTime,
this.sessionToken,
required this.authenticationFactor,
this.state,
this.clientInfo,
this.ipAddress,
this.externalSessionId,
}) : parent = parent ?? context.rootEntity,
sessionId = TypeId<Session>.decode(sessionId);
const Session._({
required this.parent,
required this.sessionId,
required this.cryptoKeyId,
required this.userId,
required this.expireTime,
required this.sessionToken,
required this.authenticationFactor,
required this.state,
required this.clientInfo,
required this.ipAddress,
required this.externalSessionId,
});
final EntityUid? parent;
final TypeId<Session> sessionId;
final Uint8List cryptoKeyId;
final String userId;
final DateTime expireTime;
final String? sessionToken;
CedarCork get sessionCork => CedarCork.parse(sessionToken!);
final AuthenticationFactor authenticationFactor;
final SessionState? state;
final SessionClient? clientInfo;
final String? ipAddress;
final String? externalSessionId;
pb.Session toProto({
String? sessionToken,
}) {
final session = pb.Session(
parent: parent?.id,
sessionId: sessionId.encoded,
sessionToken: sessionToken ?? this.sessionToken,
expireTime: expireTime.toProto(),
client: clientInfo?.toProto(),
);
state?.apply(session);
return session;
}
Session copyWith({
EntityUid? parent,
Uint8List? cryptoKeyId,
String? userId,
DateTime? expireTime,
String? sessionToken,
AuthenticationFactor? authenticationFactor,
SessionState? state,
SessionClient? clientInfo,
String? ipAddress,
String? externalSessionId,
}) {
return Session._(
parent: parent ?? this.parent,
sessionId: sessionId,
cryptoKeyId: cryptoKeyId ?? this.cryptoKeyId,
userId: userId ?? this.userId,
expireTime: expireTime ?? this.expireTime,
sessionToken: sessionToken ?? this.sessionToken,
authenticationFactor: authenticationFactor ?? this.authenticationFactor,
state: state ?? this.state,
clientInfo: clientInfo ?? this.clientInfo,
ipAddress: ipAddress ?? this.ipAddress,
externalSessionId: externalSessionId ?? this.externalSessionId,
);
}
}