|
| 1 | +import { Request, Response, NextFunction } from 'express'; |
| 2 | +import { ConfidentialClientApplication, Configuration, ICachePlugin, CryptoProvider } from '@azure/msal-node'; |
| 3 | +import { TokenValidator } from './TokenValidator'; |
| 4 | +import { UrlUtils } from './UrlUtils'; |
| 5 | +import { AppSettings } from './Types'; |
| 6 | +/** |
| 7 | + * A simple wrapper around MSAL Node ConfidentialClientApplication object. |
| 8 | + * It offers a collection of middleware and utility methods that automate |
| 9 | + * basic authentication and authorization tasks in Express MVC web apps. |
| 10 | + * |
| 11 | + * You must have express and express-sessions package installed. Middleware |
| 12 | + * here can be used with express sessions in route controllers. |
| 13 | + * |
| 14 | + * Session variables accessible are as follows: |
| 15 | + * req.session.isAuthenticated: boolean |
| 16 | + * req.session.isAuthorized: boolean |
| 17 | + * req.session.account: AccountInfo |
| 18 | + * req.session.<resourceName>.accessToken: string |
| 19 | + */ |
| 20 | +export declare class AuthProvider { |
| 21 | + appSettings: AppSettings; |
| 22 | + msalConfig: Configuration; |
| 23 | + urlUtils: UrlUtils; |
| 24 | + cryptoProvider: CryptoProvider; |
| 25 | + tokenValidator: TokenValidator; |
| 26 | + msalClient: ConfidentialClientApplication; |
| 27 | + /** |
| 28 | + * @param {JSON} appSettings |
| 29 | + * @param {ICachePlugin} cache: cachePlugin |
| 30 | + */ |
| 31 | + constructor(appSettings: AppSettings, cache?: ICachePlugin); |
| 32 | + /** |
| 33 | + * Initiate sign in flow |
| 34 | + * @param {Request} req: express request object |
| 35 | + * @param {Response} res: express response object |
| 36 | + * @param {NextFunction} next: express next function |
| 37 | + */ |
| 38 | + signIn: (req: Request, res: Response, next: NextFunction) => void; |
| 39 | + /** |
| 40 | + * Initiate sign out and clean the session |
| 41 | + * @param {Request} req: express request object |
| 42 | + * @param {Response} res: express response object |
| 43 | + * @param {NextFunction} next: express next function |
| 44 | + */ |
| 45 | + signOut: (req: Request, res: Response, next: NextFunction) => void; |
| 46 | + /** |
| 47 | + * Middleware that handles redirect depending on request state |
| 48 | + * There are basically 2 stages: sign-in and acquire token |
| 49 | + * @param {Request} req: express request object |
| 50 | + * @param {Response} res: express response object |
| 51 | + * @param {NextFunction} next: express next function |
| 52 | + */ |
| 53 | + handleRedirect: (req: Request, res: Response, next: NextFunction) => Promise<void>; |
| 54 | + /** |
| 55 | + * Middleware that gets tokens and calls web APIs |
| 56 | + * @param {Object} req: express request object |
| 57 | + * @param {Object} res: express response object |
| 58 | + * @param {Function} next: express next |
| 59 | + */ |
| 60 | + getToken: (req: Request, res: Response, next: NextFunction) => Promise<void>; |
| 61 | + /** |
| 62 | + * Check if authenticated in session |
| 63 | + * @param {Object} req: express request object |
| 64 | + * @param {Object} res: express response object |
| 65 | + * @param {Function} next: express next |
| 66 | + */ |
| 67 | + isAuthenticated: (req: Request, res: Response, next: NextFunction) => void | Response; |
| 68 | + /** |
| 69 | + * Receives access token in req authorization header |
| 70 | + * and validates it using the jwt.verify |
| 71 | + * @param {Object} req: express request object |
| 72 | + * @param {Object} res: express response object |
| 73 | + * @param {Function} next: express next |
| 74 | + */ |
| 75 | + isAuthorized: (req: Request, res: Response, next: NextFunction) => Promise<void | Response>; |
| 76 | + /** |
| 77 | + * This method is used to generate an auth code request |
| 78 | + * @param {Object} req: express request object |
| 79 | + * @param {Object} res: express response object |
| 80 | + * @param {NextFunction} next: express next function |
| 81 | + * @param {AuthCodeParams} params: modifies auth code request url |
| 82 | + */ |
| 83 | + private getAuthCode; |
| 84 | + /** |
| 85 | + * Util method to get the resource name for a given callingPageRoute (appSettings.json) |
| 86 | + * @param {string} path: /path string that the resource is associated with |
| 87 | + */ |
| 88 | + private getResourceName; |
| 89 | +} |
0 commit comments