Skip to content

Commit 5e54253

Browse files
committed
feat: add requestProcess helper for extracting data from requests
1 parent cfc0b62 commit 5e54253

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

Diff for: index.d.ts

+15
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,16 @@ declare namespace OAuth2Server {
203203
authorizationCodeLifetime?: number;
204204
}
205205

206+
interface TokenRequest {
207+
grant_type: string;
208+
client_assertion?: string;
209+
client_assertion_type?: string;
210+
client_id?: string;
211+
client_secret?: string;
212+
code_verifier?: string;
213+
scope?: string;
214+
}
215+
206216
interface TokenOptions {
207217
/**
208218
* Lifetime of generated access tokens in seconds (default = 1 hour)
@@ -233,6 +243,11 @@ declare namespace OAuth2Server {
233243
* Additional supported grant types.
234244
*/
235245
extendedGrantTypes?: Record<string, typeof AbstractGrantType>;
246+
247+
/**
248+
* Request processor
249+
*/
250+
requestProcessor?: ((request: Request) => TokenRequest)
236251
}
237252

238253
interface AssertionCredential {

Diff for: lib/handlers/token-handler.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class TokenHandler {
6161
this.allowExtendedTokenAttributes = options.allowExtendedTokenAttributes;
6262
this.requireClientAuthentication = options.requireClientAuthentication || {};
6363
this.alwaysIssueNewRefreshToken = options.alwaysIssueNewRefreshToken !== false;
64+
this.requestProcessor = options.requestProcessor ?? ((req) => req.body);
6465
}
6566

6667
/**
@@ -85,8 +86,15 @@ class TokenHandler {
8586
}
8687

8788
try {
88-
const client = await this.getClient(request, response);
89-
const data = await this.handleGrantType(request, client);
89+
const body = this.requestProcessor(request) ?? request.body;
90+
const client = await this.getClient({
91+
...request,
92+
body,
93+
}, response);
94+
const data = await this.handleGrantType({
95+
...request,
96+
body,
97+
}, client);
9098
const model = new TokenModel(data, { allowExtendedTokenAttributes: this.allowExtendedTokenAttributes });
9199
const tokenType = this.getTokenType(model);
92100

@@ -247,7 +255,7 @@ class TokenHandler {
247255
accessTokenLifetime: accessTokenLifetime,
248256
model: this.model,
249257
refreshTokenLifetime: refreshTokenLifetime,
250-
alwaysIssueNewRefreshToken: this.alwaysIssueNewRefreshToken
258+
alwaysIssueNewRefreshToken: this.alwaysIssueNewRefreshToken,
251259
};
252260

253261
return new Type(options).handle(request, client);

0 commit comments

Comments
 (0)