Skip to content

Commit

Permalink
Update TS defs for config functions
Browse files Browse the repository at this point in the history
  • Loading branch information
joshcanhelp committed Feb 18, 2020
1 parent e18327d commit 8b147d1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
24 changes: 21 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
import { AuthorizationParameters, TokenSet, UserinfoResponse } from 'openid-client';
import { Request, Response, NextFunction, RequestHandler, ErrorRequestHandler } from 'express';

interface OpenidRequest extends Request {
/**
* Library namespace for methods and data.
* See RequestContext and ResponseContext for how this is used.
*/
openid: object;

/**
* Decoded state for use in config.handleCallback().
*/
openidState: object;

/**
* Tokens for use in config.handleCallback().
*/
openidTokens: TokenSet;
}

/**
* Configuration parameters passed to the auth() middleware.
*/
Expand Down Expand Up @@ -71,17 +89,17 @@ interface ConfigParams {
/**
* Function that returns a URL-safe state value for `res.openid.login()`.
*/
getLoginState?: (req: Request, config: object) => object;
getLoginState?: (req: OpenidRequest, config: object) => object;

/**
* Function that returns the profile for `req.openid.user`.
*/
getUser?: (req: Request, config: ConfigParams) => undefined | UserinfoResponse;
getUser?: (req: OpenidRequest, config: ConfigParams) => undefined | UserinfoResponse;

/**
* Function that runs on the callback route, after callback processing but before redirection.
*/
handleCallback?: RequestHandler;
handleCallback?: (req: OpenidRequest, res: Response, next: NextFunction) => void;

/**
* Default options object used for all HTTP calls made by the library.
Expand Down
6 changes: 3 additions & 3 deletions middleware/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,21 @@ module.exports = function (params) {
const redirectUri = res.openid.getRedirectUri();
const client = req.openid.client;

req.openidState = transient.getOnce('state', req, res, transientOpts);
const returnedState = transient.getOnce('state', req, res, transientOpts);

let tokenSet;
try {
const callbackParams = client.callbackParams(req);
tokenSet = await client.callback(redirectUri, callbackParams, {
nonce: transient.getOnce('nonce', req, res, transientOpts),
state: req.openidState,
state: returnedState,
response_type: authorizeParams.response_type,
});
} catch (err) {
throw createError.BadRequest(err.message);
}

req.openidState = decodeState(req.openidState);
req.openidState = decodeState(returnedState);
req.openidTokens = tokenSet;

if (config.appSessionSecret) {
Expand Down

0 comments on commit 8b147d1

Please sign in to comment.