Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 82 additions & 1 deletion packages/authgear-core/src/types.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { _decodeUserInfo } from "./types";
import { _decodeUserInfo, AuthenticatorType, AuthenticatorKind } from "./types";

const USER_INFO = `
{
Expand All @@ -7,6 +7,33 @@ const USER_INFO = `
"https://authgear.com/claims/user/is_anonymous": false,
"https://authgear.com/claims/user/can_reauthenticate": true,
"https://authgear.com/claims/user/roles": ["role_a"],
"https://authgear.com/claims/user/authenticators": [
{
"created_at": "2023-01-01T00:00:00Z",
"updated_at": "2023-01-01T00:00:00Z",
"type": "password",
"kind": "primary"
},
{
"created_at": "2023-01-01T00:00:00Z",
"updated_at": "2023-01-01T00:00:00Z",
"type": "oob_otp_sms",
"kind": "primary"
},
{
"created_at": "2023-01-01T00:00:00Z",
"updated_at": "2023-01-01T00:00:00Z",
"type": "oob_otp_email",
"kind": "primary"
},
{
"created_at": "2023-01-01T00:00:00Z",
"updated_at": "2023-01-01T00:00:00Z",
"type": "totp",
"kind": "secondary"
}
],
"https://authgear.com/claims/user/recovery_code_enabled": true,

"email": "user@example.com",
"email_verified": true,
Expand Down Expand Up @@ -55,6 +82,33 @@ describe("_decodeUserInfo", () => {
birthdate: "1970-01-01",
canReauthenticate: true,
roles: ["role_a"],
authenticators: [
{
createdAt: new Date("2023-01-01T00:00:00Z"),
updatedAt: new Date("2023-01-01T00:00:00Z"),
type: AuthenticatorType.Password,
kind: AuthenticatorKind.Primary,
},
{
createdAt: new Date("2023-01-01T00:00:00Z"),
updatedAt: new Date("2023-01-01T00:00:00Z"),
type: AuthenticatorType.OOBOTPSMS,
kind: AuthenticatorKind.Primary,
},
{
createdAt: new Date("2023-01-01T00:00:00Z"),
updatedAt: new Date("2023-01-01T00:00:00Z"),
type: AuthenticatorType.OOBOTPEmail,
kind: AuthenticatorKind.Primary,
},
{
createdAt: new Date("2023-01-01T00:00:00Z"),
updatedAt: new Date("2023-01-01T00:00:00Z"),
type: AuthenticatorType.TOTP,
kind: AuthenticatorKind.Secondary,
},
],
recoveryCodeEnabled: true,
customAttributes: {
foobar: 42,
},
Expand Down Expand Up @@ -84,6 +138,33 @@ describe("_decodeUserInfo", () => {
street_address: "10 Somewhere Street",
},
birthdate: "1970-01-01",
"https://authgear.com/claims/user/authenticators": [
{
created_at: "2023-01-01T00:00:00Z",
updated_at: "2023-01-01T00:00:00Z",
type: "password",
kind: "primary",
},
{
created_at: "2023-01-01T00:00:00Z",
updated_at: "2023-01-01T00:00:00Z",
type: "oob_otp_sms",
kind: "primary",
},
{
created_at: "2023-01-01T00:00:00Z",
updated_at: "2023-01-01T00:00:00Z",
type: "oob_otp_email",
kind: "primary",
},
{
created_at: "2023-01-01T00:00:00Z",
updated_at: "2023-01-01T00:00:00Z",
type: "totp",
kind: "secondary",
},
],
"https://authgear.com/claims/user/recovery_code_enabled": true,
custom_attributes: {
foobar: 42,
},
Expand Down
52 changes: 52 additions & 0 deletions packages/authgear-core/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
/**
* @public
*/
export enum AuthenticatorType {
Password = "password",
OOBOTPEmail = "oob_otp_email",
OOBOTPSMS = "oob_otp_sms",
TOTP = "totp",
}

/**
* @public
*/
export enum AuthenticatorKind {
Primary = "primary",
Secondary = "secondary",
}

/**
* @public
*/
export interface Authenticator {
createdAt: Date;
updatedAt: Date;
type: AuthenticatorType;
kind: AuthenticatorKind;
}

/**
* UserInfo is the result of fetchUserInfo.
* It contains `sub` which is the User ID,
Expand All @@ -14,6 +42,7 @@ export interface UserInfo {
isVerified: boolean;
isAnonymous: boolean;
canReauthenticate: boolean;
recoveryCodeEnabled?: boolean;
roles?: string[];

raw: Record<string, unknown>;
Expand Down Expand Up @@ -44,6 +73,7 @@ export interface UserInfo {
postalCode?: string;
country?: string;
};
authenticators?: Authenticator[];
}

/**
Expand Down Expand Up @@ -173,6 +203,23 @@ export interface _APIClientDelegate {
refreshAccessTokenIfNeeded(): Promise<void>;
}

/**
* @internal
*/
export function _decodeAuthenticators(r: any): Authenticator[] | undefined {
if (!Array.isArray(r)) {
return undefined;
}
return r.map((a) => {
return {
createdAt: new Date(a["created_at"]),
updatedAt: new Date(a["updated_at"]),
type: a["type"],
kind: a["kind"],
};
});
}

/**
* @internal
*/
Expand All @@ -186,7 +233,12 @@ export function _decodeUserInfo(r: any): UserInfo {
isAnonymous: r["https://authgear.com/claims/user/is_anonymous"] ?? false,
canReauthenticate:
r["https://authgear.com/claims/user/can_reauthenticate"] ?? false,
recoveryCodeEnabled:
r["https://authgear.com/claims/user/recovery_code_enabled"] ?? false,
roles: r["https://authgear.com/claims/user/roles"],
authenticators: _decodeAuthenticators(
r["https://authgear.com/claims/user/authenticators"]
),

raw,
customAttributes,
Expand Down