Skip to content

Commit 6ca9b01

Browse files
Merge pull request #194 from tobiasdcl/extra-args
feat: provide originatingAuthCodeId as an argument to extraTokenFields
2 parents 3bd60f7 + 43acaa2 commit 6ca9b01

File tree

6 files changed

+48
-4
lines changed

6 files changed

+48
-4
lines changed

src/grants/abstract/abstract.grant.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,9 @@ export abstract class AbstractGrant implements GrantInterface {
318318
req: RequestInterface,
319319
client: OAuthClient,
320320
user?: OAuthUser | null,
321+
originatingAuthCodeId?: string,
321322
): Promise<ExtraAccessTokenFields> {
322-
const extraJwtFields = await this.jwt.extraTokenFields?.({ user, client });
323+
const extraJwtFields = await this.jwt.extraTokenFields?.({ user, client, originatingAuthCodeId });
323324
const aud: string[] | string | undefined =
324325
this.getQueryStringParameter("audience", req) ??
325326
this.getRequestParameter("audience", req) ??

src/grants/auth_code.grant.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export class AuthCodeGrant extends AbstractAuthorizedGrant {
138138

139139
await this.authCodeRepository.revoke(validatedPayload.auth_code_id);
140140

141-
const extraJwtFields = await this.extraJwtFields(req, client, user);
141+
const extraJwtFields = await this.extraJwtFields(req, client, user, accessToken.originatingAuthCodeId);
142142

143143
return await this.makeBearerTokenResponse(client, accessToken, scopes, extraJwtFields);
144144
}

src/grants/refresh_token.grant.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class RefreshTokenGrant extends AbstractGrant {
4040

4141
newToken = await this.issueRefreshToken(newToken, client);
4242

43-
const extraJwtFields = await this.extraJwtFields(req, client, user);
43+
const extraJwtFields = await this.extraJwtFields(req, client, user, newToken.originatingAuthCodeId);
4444

4545
return await this.makeBearerTokenResponse(client, newToken, scopes, extraJwtFields);
4646
}

src/utils/jwt.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export type ExtraAccessTokenFields = Record<string, string | number | boolean |
66
export type ExtraAccessTokenFieldArgs = {
77
user?: OAuthUser | null;
88
client: OAuthClient;
9+
originatingAuthCodeId?: string;
910
};
1011
export interface JwtInterface {
1112
verify(token: string, options?: VerifyOptions): Promise<Record<string, unknown>>;

test/e2e/grants/auth_code.grant.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,25 @@ describe("authorization_code grant", () => {
471471
authorizationCode = String(authorizeResponseQuery.get("code"));
472472
});
473473

474+
it("provides originatingAuthCodeId as argument to extraJwtFields", async () => {
475+
request = new OAuthRequest({
476+
body: {
477+
grant_type: "authorization_code",
478+
code: authorizationCode,
479+
redirect_uri: authorizationRequest.redirectUri,
480+
client_id: client.id,
481+
code_verifier: codeVerifier,
482+
},
483+
});
484+
485+
const extraJwtFieldsSpy = vi.spyOn(grant as any, "extraJwtFields");
486+
487+
const accessTokenResponse = await grant.respondToAccessTokenRequest(request, new DateInterval("1h"));
488+
489+
expectTokenResponse(accessTokenResponse);
490+
expect(extraJwtFieldsSpy).toHaveBeenCalledWith(request, client, user, "my-super-secret-auth-code");
491+
});
492+
474493
it("is successful with pkce S256", async () => {
475494
// act
476495
request = new OAuthRequest({

test/e2e/grants/refresh_token.grant.spec.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,30 @@ describe("refresh_token grant", () => {
9797
expectTokenResponse(tokenResponse);
9898
expect(tokenResponse.body.scope).toBe("scope-1");
9999
expect(tokenResponse.body.refresh_token).toMatch(REGEX_ACCESS_TOKEN);
100-
expect(extraJwtFieldsSpy).toHaveBeenCalledWith(request, client, user);
100+
expect(extraJwtFieldsSpy).toHaveBeenCalledWith(request, client, user, undefined);
101+
});
102+
103+
it("provides originatingAuthCodeId as argument to extraJwtFields", async () => {
104+
accessToken.originatingAuthCodeId = "my-super-secret-auth-code";
105+
106+
// arrange
107+
const bearerResponse = await grant.makeBearerTokenResponse(client, accessToken);
108+
request = new OAuthRequest({
109+
body: {
110+
grant_type: "refresh_token",
111+
client_id: client.id,
112+
client_secret: client.secret,
113+
refresh_token: bearerResponse.body.refresh_token,
114+
scope: "scope-1",
115+
},
116+
});
117+
const accessTokenTTL = new DateInterval("1h");
118+
119+
const extraJwtFieldsSpy = vi.spyOn(grant as any, "extraJwtFields");
120+
121+
await grant.respondToAccessTokenRequest(request, accessTokenTTL);
122+
123+
expect(extraJwtFieldsSpy).toHaveBeenCalledWith(request, client, user, "my-super-secret-auth-code");
101124
});
102125

103126
it("populates originatingAuthCodeId property in OAuthToken object", async () => {

0 commit comments

Comments
 (0)