Skip to content

Commit d215117

Browse files
committed
feat: add requestProcess helper for extracting data from requests
1 parent d578fb9 commit d215117

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

index.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,16 @@ declare namespace OAuth2Server {
210210
authorizationCodeLifetime?: number;
211211
}
212212

213+
interface TokenRequest {
214+
grant_type: string;
215+
client_assertion?: string;
216+
client_assertion_type?: string;
217+
client_id?: string;
218+
client_secret?: string;
219+
code_verifier?: string;
220+
scope?: string;
221+
}
222+
213223
interface TokenOptions {
214224
/**
215225
* Lifetime of generated access tokens in seconds (default = 1 hour)
@@ -240,6 +250,11 @@ declare namespace OAuth2Server {
240250
* Additional supported grant types.
241251
*/
242252
extendedGrantTypes?: Record<string, typeof AbstractGrantType>;
253+
254+
/**
255+
* Request processor
256+
*/
257+
requestProcessor?: ((request: Request) => TokenRequest)
243258
}
244259

245260
interface AssertionCredential {

lib/handlers/token-handler.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class TokenHandler {
6262
this.requireClientAuthentication = options.requireClientAuthentication || {};
6363
this.alwaysIssueNewRefreshToken = options.alwaysIssueNewRefreshToken !== false;
6464
this.enablePlainPKCE = options.enablePlainPKCE === true;
65+
this.requestProcessor = options.requestProcessor;
6566
}
6667

6768
/**
@@ -86,8 +87,13 @@ class TokenHandler {
8687
}
8788

8889
try {
89-
const client = await this.getClient(request, response);
90-
const data = await this.handleGrantType(request, client);
90+
const body = this.requestProcessor?.(request) ?? request.body;
91+
const req = new Request({
92+
...request,
93+
body,
94+
});
95+
const client = await this.getClient(req, response);
96+
const data = await this.handleGrantType(req, client);
9197
const model = new TokenModel(data, { allowExtendedTokenAttributes: this.allowExtendedTokenAttributes });
9298
const tokenType = this.getTokenType(model);
9399

0 commit comments

Comments
 (0)