Skip to content

Commit 6c6824c

Browse files
committed
Add recoveryCodeEnabled and authenticators to user info
1 parent 3e17b9d commit 6c6824c

File tree

3 files changed

+102
-3
lines changed

3 files changed

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

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)