Skip to content

Commit b67ba7f

Browse files
fix: ensure authorization_details can be passed in as array instead of only string
1 parent ccd61c8 commit b67ba7f

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

src/auth/oauth.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ import { BaseAuthAPI, AuthenticationClientOptions, grant } from './base-auth-api
88
import { IDTokenValidateOptions, IDTokenValidator } from './id-token-validator.js';
99
import { mtlsPrefix } from '../utils.js';
1010

11-
export interface TokenSet {
11+
interface AuthorizationDetails {
12+
readonly type: string;
13+
readonly [parameter: string]: unknown;
14+
}
15+
16+
export interface TokenSet<
17+
TAuthorizationDetails extends AuthorizationDetails = AuthorizationDetails
18+
> {
1219
/**
1320
* The access token.
1421
*/
@@ -29,6 +36,11 @@ export interface TokenSet {
2936
* The duration in secs that the access token is valid.
3037
*/
3138
expires_in: number;
39+
/**
40+
* The authorization details when using Rich Authorization Requests (RAR).
41+
* @see https://auth0.com/docs/get-started/apis/configure-rich-authorization-requests
42+
*/
43+
authorization_details?: TAuthorizationDetails[];
3244
}
3345

3446
export interface GrantOptions {
@@ -99,7 +111,9 @@ export interface ClientCredentialsGrantRequest extends ClientCredentials {
99111
organization?: string;
100112
}
101113

102-
export interface PushedAuthorizationRequest extends ClientCredentials {
114+
export interface PushedAuthorizationRequest<
115+
TAuthorizationDetails extends AuthorizationDetails = AuthorizationDetails
116+
> extends ClientCredentials {
103117
/**
104118
* URI to redirect to.
105119
*/
@@ -162,7 +176,7 @@ export interface PushedAuthorizationRequest extends ClientCredentials {
162176
/**
163177
* A JSON stringified array of objects. It can carry fine-grained authorization data in OAuth messages as part of Rich Authorization Requests (RAR) {@link https://auth0.com/docs/get-started/authentication-and-authorization-flow/authorization-code-flow/authorization-code-flow-with-rar | Reference}
164178
*/
165-
authorization_details?: string;
179+
authorization_details?: string | TAuthorizationDetails[];
166180

167181
/**
168182
* Allow for any custom property to be sent to Auth0
@@ -449,6 +463,15 @@ export class OAuth extends BaseAuthAPI {
449463
options: { initOverrides?: InitOverride } = {}
450464
): Promise<JSONApiResponse<PushedAuthorizationResponse>> {
451465
validateRequiredRequestParams(bodyParameters, ['client_id', 'response_type', 'redirect_uri']);
466+
const { authorization_details } = bodyParameters;
467+
468+
if (authorization_details) {
469+
// Convert to string if not already
470+
bodyParameters.authorization_details =
471+
typeof authorization_details !== 'string'
472+
? JSON.stringify(authorization_details)
473+
: authorization_details;
474+
}
452475

453476
const bodyParametersWithClientAuthentication = await this.addClientAuthentication(
454477
bodyParameters

test/auth/fixtures/oauth.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,17 @@
168168
"expires_in": 86400
169169
}
170170
},
171+
{
172+
"scope": "https://test-domain.auth0.com",
173+
"method": "POST",
174+
"path": "/oauth/par",
175+
"body": "client_id=test-client-id&response_type=code&redirect_uri=https%3A%2F%2Fexample-as-string.com&authorization_details=%5B%7B%22type%22%3A%22payment_initiation%22%2C%22actions%22%3A%5B%22write%22%5D%7D%5D&client_secret=test-client-secret",
176+
"status": 200,
177+
"response": {
178+
"request_uri": "https://www.request.uri",
179+
"expires_in": 86400
180+
}
181+
},
171182
{
172183
"scope": "https://test-domain.auth0.com",
173184
"method": "POST",

test/auth/oauth.test.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,13 @@ describe('OAuth', () => {
332332
});
333333
});
334334

335-
it('should send authorization_details when provided', async () => {
335+
it('should send authorization_details when provided as string', async () => {
336336
const oauth = new OAuth(opts);
337337
await expect(
338338
oauth.pushedAuthorization({
339339
client_id: 'test-client-id',
340340
response_type: 'code',
341-
redirect_uri: 'https://example.com',
341+
redirect_uri: 'https://example-as-string.com',
342342
authorization_details: JSON.stringify([
343343
{ type: 'payment_initiation', actions: ['write'] },
344344
]),
@@ -351,6 +351,23 @@ describe('OAuth', () => {
351351
});
352352
});
353353

354+
it('should send authorization_details when provided as array', async () => {
355+
const oauth = new OAuth(opts);
356+
await expect(
357+
oauth.pushedAuthorization({
358+
client_id: 'test-client-id',
359+
response_type: 'code',
360+
redirect_uri: 'https://example.com',
361+
authorization_details: [{ type: 'payment_initiation', actions: ['write'] }],
362+
})
363+
).resolves.toMatchObject({
364+
data: {
365+
request_uri: 'https://www.request.uri',
366+
expires_in: 86400,
367+
},
368+
});
369+
});
370+
354371
it('should send request param when provided', async () => {
355372
const oauth = new OAuth(opts);
356373
await expect(

0 commit comments

Comments
 (0)