Skip to content

Commit cfde737

Browse files
Add recoveryCodeEnabled and authenticators to user info
ref DEV-3027
2 parents 77f834c + fdfa95a commit cfde737

File tree

3 files changed

+96
-3
lines changed

3 files changed

+96
-3
lines changed

example/lib/main.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -872,8 +872,10 @@ class _MyAppState extends State<MyApp> {
872872
builder: (BuildContext context) {
873873
return AlertDialog(
874874
title: const Text("User Info"),
875-
content:
876-
Text(const JsonEncoder.withIndent(" ").convert(userInfo.raw)),
875+
content: SingleChildScrollView(
876+
child: Text(
877+
const JsonEncoder.withIndent(" ").convert(userInfo.raw)),
878+
),
877879
actions: [
878880
TextButton(
879881
child: const Text("OK"),

lib/src/authenticator.dart

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
class Authenticator {
2+
final DateTime createdAt;
3+
final DateTime updatedAt;
4+
final AuthenticatorType type;
5+
final AuthenticatorKind kind;
6+
7+
Authenticator.fromJSON(dynamic json)
8+
: createdAt = DateTime.parse(json["created_at"]),
9+
updatedAt = DateTime.parse(json["updated_at"]),
10+
type = AuthenticatorTypeExtension.parse(json["type"]),
11+
kind = AuthenticatorKindExtension.parse(json["kind"]);
12+
}
13+
14+
enum AuthenticatorType {
15+
password,
16+
oobOtpEmail,
17+
oobOtpSms,
18+
totp,
19+
}
20+
21+
extension AuthenticatorTypeExtension on AuthenticatorType {
22+
String get value {
23+
switch (this) {
24+
case AuthenticatorType.password:
25+
return "password";
26+
case AuthenticatorType.oobOtpEmail:
27+
return "oob_otp_email";
28+
case AuthenticatorType.oobOtpSms:
29+
return "oob_otp_sms";
30+
case AuthenticatorType.totp:
31+
return "totp";
32+
}
33+
}
34+
35+
static AuthenticatorType parse(String value) {
36+
switch (value) {
37+
case "password":
38+
return AuthenticatorType.password;
39+
case "oob_otp_email":
40+
return AuthenticatorType.oobOtpEmail;
41+
case "oob_otp_sms":
42+
return AuthenticatorType.oobOtpSms;
43+
case "totp":
44+
return AuthenticatorType.totp;
45+
default:
46+
throw Exception("unknown authenticator type: $value");
47+
}
48+
}
49+
}
50+
51+
enum AuthenticatorKind {
52+
primary,
53+
secondary,
54+
}
55+
56+
extension AuthenticatorKindExtension on AuthenticatorKind {
57+
String get value {
58+
switch (this) {
59+
case AuthenticatorKind.primary:
60+
return "primary";
61+
case AuthenticatorKind.secondary:
62+
return "secondary";
63+
}
64+
}
65+
66+
static AuthenticatorKind parse(String value) {
67+
switch (value) {
68+
case "primary":
69+
return AuthenticatorKind.primary;
70+
case "secondary":
71+
return AuthenticatorKind.secondary;
72+
default:
73+
throw Exception("unknown authenticator kind: $value");
74+
}
75+
}
76+
}

lib/src/type.dart

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'authenticator.dart';
2+
13
enum SessionState { unknown, noSession, authenticated }
24

35
enum SessionStateChangeReason {
@@ -196,11 +198,19 @@ List<String>? parseRoles(dynamic roles) {
196198
return null;
197199
}
198200

201+
List<Authenticator>? parseAuthenticators(dynamic authenticators) {
202+
if (authenticators is List) {
203+
return authenticators.map((e) => Authenticator.fromJSON(e)).toList();
204+
}
205+
return null;
206+
}
207+
199208
class UserInfo {
200209
final String sub;
201210
final bool isAnonymous;
202211
final bool isVerified;
203212
final bool canReauthenticate;
213+
final bool? recoveryCodeEnabled;
204214
final List<String>? roles;
205215

206216
final Map<String, dynamic> raw;
@@ -224,13 +234,16 @@ class UserInfo {
224234
final String? zoneinfo;
225235
final String? locale;
226236
final UserInfoAddress? address;
237+
final List<Authenticator>? authenticators;
227238

228239
UserInfo.fromJSON(dynamic json)
229240
: sub = json["sub"],
230241
isAnonymous = json["https://authgear.com/claims/user/is_anonymous"],
231242
isVerified = json["https://authgear.com/claims/user/is_verified"],
232243
canReauthenticate =
233244
json["https://authgear.com/claims/user/can_reauthenticate"],
245+
recoveryCodeEnabled =
246+
json["https://authgear.com/claims/user/recovery_code_enabled"],
234247
roles = parseRoles(json["https://authgear.com/claims/user/roles"]),
235248
raw = json,
236249
customAttributes = json["custom_attributes"] ?? {},
@@ -253,7 +266,9 @@ class UserInfo {
253266
locale = json["locale"],
254267
address = json["address"] != null
255268
? UserInfoAddress.fromJSON(json["address"])
256-
: null;
269+
: null,
270+
authenticators = parseAuthenticators(
271+
json["https://authgear.com/claims/user/authenticators"]);
257272
}
258273

259274
class OIDCConfiguration {

0 commit comments

Comments
 (0)