Skip to content

Commit 52b6978

Browse files
Add authenticators and recoveryCodeEnabled to user info
ref DEV-3027
2 parents 9d7383f + 96d7eb3 commit 52b6978

File tree

2 files changed

+134
-1
lines changed

2 files changed

+134
-1
lines changed

packages/authgear-core/src/types.test.ts

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { _decodeUserInfo } from "./types";
1+
import { _decodeUserInfo, AuthenticatorType, AuthenticatorKind } from "./types";
22

33
const USER_INFO = `
44
{
@@ -7,6 +7,33 @@ const USER_INFO = `
77
"https://authgear.com/claims/user/is_anonymous": false,
88
"https://authgear.com/claims/user/can_reauthenticate": true,
99
"https://authgear.com/claims/user/roles": ["role_a"],
10+
"https://authgear.com/claims/user/authenticators": [
11+
{
12+
"created_at": "2023-01-01T00:00:00Z",
13+
"updated_at": "2023-01-01T00:00:00Z",
14+
"type": "password",
15+
"kind": "primary"
16+
},
17+
{
18+
"created_at": "2023-01-01T00:00:00Z",
19+
"updated_at": "2023-01-01T00:00:00Z",
20+
"type": "oob_otp_sms",
21+
"kind": "primary"
22+
},
23+
{
24+
"created_at": "2023-01-01T00:00:00Z",
25+
"updated_at": "2023-01-01T00:00:00Z",
26+
"type": "oob_otp_email",
27+
"kind": "primary"
28+
},
29+
{
30+
"created_at": "2023-01-01T00:00:00Z",
31+
"updated_at": "2023-01-01T00:00:00Z",
32+
"type": "totp",
33+
"kind": "secondary"
34+
}
35+
],
36+
"https://authgear.com/claims/user/recovery_code_enabled": true,
1037
1138
"email": "user@example.com",
1239
"email_verified": true,
@@ -55,6 +82,33 @@ describe("_decodeUserInfo", () => {
5582
birthdate: "1970-01-01",
5683
canReauthenticate: true,
5784
roles: ["role_a"],
85+
authenticators: [
86+
{
87+
createdAt: new Date("2023-01-01T00:00:00Z"),
88+
updatedAt: new Date("2023-01-01T00:00:00Z"),
89+
type: AuthenticatorType.Password,
90+
kind: AuthenticatorKind.Primary,
91+
},
92+
{
93+
createdAt: new Date("2023-01-01T00:00:00Z"),
94+
updatedAt: new Date("2023-01-01T00:00:00Z"),
95+
type: AuthenticatorType.OOBOTPSMS,
96+
kind: AuthenticatorKind.Primary,
97+
},
98+
{
99+
createdAt: new Date("2023-01-01T00:00:00Z"),
100+
updatedAt: new Date("2023-01-01T00:00:00Z"),
101+
type: AuthenticatorType.OOBOTPEmail,
102+
kind: AuthenticatorKind.Primary,
103+
},
104+
{
105+
createdAt: new Date("2023-01-01T00:00:00Z"),
106+
updatedAt: new Date("2023-01-01T00:00:00Z"),
107+
type: AuthenticatorType.TOTP,
108+
kind: AuthenticatorKind.Secondary,
109+
},
110+
],
111+
recoveryCodeEnabled: true,
58112
customAttributes: {
59113
foobar: 42,
60114
},
@@ -84,6 +138,33 @@ describe("_decodeUserInfo", () => {
84138
street_address: "10 Somewhere Street",
85139
},
86140
birthdate: "1970-01-01",
141+
"https://authgear.com/claims/user/authenticators": [
142+
{
143+
created_at: "2023-01-01T00:00:00Z",
144+
updated_at: "2023-01-01T00:00:00Z",
145+
type: "password",
146+
kind: "primary",
147+
},
148+
{
149+
created_at: "2023-01-01T00:00:00Z",
150+
updated_at: "2023-01-01T00:00:00Z",
151+
type: "oob_otp_sms",
152+
kind: "primary",
153+
},
154+
{
155+
created_at: "2023-01-01T00:00:00Z",
156+
updated_at: "2023-01-01T00:00:00Z",
157+
type: "oob_otp_email",
158+
kind: "primary",
159+
},
160+
{
161+
created_at: "2023-01-01T00:00:00Z",
162+
updated_at: "2023-01-01T00:00:00Z",
163+
type: "totp",
164+
kind: "secondary",
165+
},
166+
],
167+
"https://authgear.com/claims/user/recovery_code_enabled": true,
87168
custom_attributes: {
88169
foobar: 42,
89170
},

packages/authgear-core/src/types.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
/**
2+
* @public
3+
*/
4+
export enum AuthenticatorType {
5+
Password = "password",
6+
OOBOTPEmail = "oob_otp_email",
7+
OOBOTPSMS = "oob_otp_sms",
8+
TOTP = "totp",
9+
}
10+
11+
/**
12+
* @public
13+
*/
14+
export enum AuthenticatorKind {
15+
Primary = "primary",
16+
Secondary = "secondary",
17+
}
18+
19+
/**
20+
* @public
21+
*/
22+
export interface Authenticator {
23+
createdAt: Date;
24+
updatedAt: Date;
25+
type: AuthenticatorType;
26+
kind: AuthenticatorKind;
27+
}
28+
129
/**
230
* UserInfo is the result of fetchUserInfo.
331
* It contains `sub` which is the User ID,
@@ -14,6 +42,7 @@ export interface UserInfo {
1442
isVerified: boolean;
1543
isAnonymous: boolean;
1644
canReauthenticate: boolean;
45+
recoveryCodeEnabled?: boolean;
1746
roles?: string[];
1847

1948
raw: Record<string, unknown>;
@@ -44,6 +73,7 @@ export interface UserInfo {
4473
postalCode?: string;
4574
country?: string;
4675
};
76+
authenticators?: Authenticator[];
4777
}
4878

4979
/**
@@ -173,6 +203,23 @@ export interface _APIClientDelegate {
173203
refreshAccessTokenIfNeeded(): Promise<void>;
174204
}
175205

206+
/**
207+
* @internal
208+
*/
209+
export function _decodeAuthenticators(r: any): Authenticator[] | undefined {
210+
if (!Array.isArray(r)) {
211+
return undefined;
212+
}
213+
return r.map((a) => {
214+
return {
215+
createdAt: new Date(a["created_at"]),
216+
updatedAt: new Date(a["updated_at"]),
217+
type: a["type"],
218+
kind: a["kind"],
219+
};
220+
});
221+
}
222+
176223
/**
177224
* @internal
178225
*/
@@ -186,7 +233,12 @@ export function _decodeUserInfo(r: any): UserInfo {
186233
isAnonymous: r["https://authgear.com/claims/user/is_anonymous"] ?? false,
187234
canReauthenticate:
188235
r["https://authgear.com/claims/user/can_reauthenticate"] ?? false,
236+
recoveryCodeEnabled:
237+
r["https://authgear.com/claims/user/recovery_code_enabled"] ?? false,
189238
roles: r["https://authgear.com/claims/user/roles"],
239+
authenticators: _decodeAuthenticators(
240+
r["https://authgear.com/claims/user/authenticators"]
241+
),
190242

191243
raw,
192244
customAttributes,

0 commit comments

Comments
 (0)