Skip to content

Commit efca12c

Browse files
authored
feat(auth): add string array support for AMR claims (#1967)
1 parent 281070a commit efca12c

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

packages/core/auth-js/src/lib/types.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,16 +291,21 @@ const AMRMethods = [
291291
'sso/saml',
292292
'magiclink',
293293
'web3',
294+
'oauth_provider/authorization_code',
294295
] as const
295296

296297
export type AMRMethod = (typeof AMRMethods)[number] | (string & {})
297298

298299
/**
299-
* An authentication methord reference (AMR) entry.
300+
* An authentication method reference (AMR) entry.
300301
*
301302
* An entry designates what method was used by the user to verify their
302303
* identity and at what time.
303304
*
305+
* Note: Custom access token hooks can return AMR claims as either:
306+
* - An array of AMREntry objects (detailed format with timestamps)
307+
* - An array of strings (RFC-8176 compliant format)
308+
*
304309
* @see {@link GoTrueMFAApi#getAuthenticatorAssuranceLevel}.
305310
*/
306311
export interface AMREntry {
@@ -1181,8 +1186,12 @@ export type AuthMFAGetAuthenticatorAssuranceLevelResponse = RequestResult<{
11811186
* A list of all authentication methods attached to this session. Use
11821187
* the information here to detect the last time a user verified a
11831188
* factor, for example if implementing a step-up scenario.
1189+
*
1190+
* Supports both RFC-8176 compliant format (string[]) and detailed format (AMREntry[]).
1191+
* - String format: ['password', 'otp'] - RFC-8176 compliant
1192+
* - Object format: [{ method: 'password', timestamp: 1234567890 }] - includes timestamps
11841193
*/
1185-
currentAuthenticationMethods: AMREntry[]
1194+
currentAuthenticationMethods: AMREntry[] | string[]
11861195
}>
11871196

11881197
/**
@@ -1501,7 +1510,13 @@ export interface JwtPayload extends RequiredClaims {
15011510
nbf?: number
15021511
app_metadata?: UserAppMetadata
15031512
user_metadata?: UserMetadata
1504-
amr?: AMREntry[]
1513+
/**
1514+
* Authentication Method References.
1515+
* Supports both RFC-8176 compliant format (string[]) and detailed format (AMREntry[]).
1516+
* - String format: ['password', 'otp'] - RFC-8176 compliant
1517+
* - Object format: [{ method: 'password', timestamp: 1234567890 }] - includes timestamps
1518+
*/
1519+
amr?: AMREntry[] | string[]
15051520

15061521
// Special claims (only in anon/service role tokens)
15071522
ref?: string

packages/core/auth-js/test/GoTrueClient.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,11 +1728,19 @@ describe('getClaims', () => {
17281728
}
17291729

17301730
// Verify amr array structure if present
1731+
// AMR can be either string[] (RFC-8176 compliant) or AMREntry[] (detailed format)
17311732
if (claims?.amr) {
17321733
expect(Array.isArray(claims.amr)).toBe(true)
17331734
if (claims.amr.length > 0) {
1734-
expect(typeof claims.amr[0].method).toBe('string')
1735-
expect(typeof claims.amr[0].timestamp).toBe('number')
1735+
const firstEntry = claims.amr[0]
1736+
if (typeof firstEntry === 'string') {
1737+
// RFC-8176 compliant format: array of strings
1738+
expect(typeof firstEntry).toBe('string')
1739+
} else {
1740+
// Detailed format: array of objects with method and timestamp
1741+
expect(typeof firstEntry.method).toBe('string')
1742+
expect(typeof firstEntry.timestamp).toBe('number')
1743+
}
17361744
}
17371745
}
17381746

0 commit comments

Comments
 (0)