1- import * as request from 'request' ;
1+ import { RequestAPI , RequiredUriUrl , Options , Request , RequestResponse } from 'request' ;
22import { my } from 'my-express' ;
33import { Log } from '../../core/log' ;
44
5- const log = new Log ( 'api:middleware.authenticate' ) ;
5+ // const log = new Log('api:middleware.authenticate');
66
77/**
88 * authenticate middleware
@@ -13,38 +13,39 @@ const log = new Log('api:middleware.authenticate');
1313 * @param res
1414 * @param next
1515 */
16- export const authenticate = ( req : my . Request , res : my . Response , next : my . NextFunction ) => {
17- const token = getToken ( req ) ;
16+ export const authenticate = ( request : RequestAPI < Request , Options , RequiredUriUrl > , log : Log ) =>
17+ ( req : my . Request , res : my . Response , next : my . NextFunction ) => {
18+ const token = getToken ( req ) ;
1819
19- if ( token === null ) {
20- log . warn ( 'No token given' ) ;
21- return res . failed ( 403 , 'You are not allowed to request this resource!' ) ;
22- }
23- log . debug ( 'Token is provided' ) ;
24-
25- // Request user info at auth0 with the provided token
26- request . post ( {
27- url : `${ process . env . AUTH0_HOST } /tokeninfo` ,
28- form : {
29- id_token : token
20+ if ( token === null ) {
21+ log . warn ( 'No token given' ) ;
22+ return res . failed ( 403 , 'You are not allowed to request this resource!' ) ;
3023 }
31- } , ( error : any , response : request . RequestResponse , body : any ) => {
32-
33- // Verify if the requests was successful and append user
34- // information to our extended express request object
35- if ( ! error && response . statusCode === 200 ) {
36- req . tokeninfo = JSON . parse ( body ) ;
37- log . info ( `Retrieved user ${ req . tokeninfo . email } ` ) ;
38- return next ( ) ;
39- }
40-
41- // Catch auth0 exception and return it as it is
42- log . warn ( `Could not retrieve the user, because of` , body ) ;
43- res . failed ( response . statusCode || 401 , body ) ;
44-
45- } ) ;
46-
47- } ;
24+ log . debug ( 'Token is provided' ) ;
25+
26+ // Request user info at auth0 with the provided token
27+ request . post ( {
28+ url : `${ process . env . AUTH0_HOST } /tokeninfo` ,
29+ form : {
30+ id_token : token
31+ }
32+ } , ( error : any , response : RequestResponse , body : any ) => {
33+
34+ // Verify if the requests was successful and append user
35+ // information to our extended express request object
36+ if ( ! error && response . statusCode === 200 ) {
37+ req . tokeninfo = JSON . parse ( body ) ;
38+ log . info ( `Retrieved user ${ req . tokeninfo . email } ` ) ;
39+ return next ( ) ;
40+ }
41+
42+ // Catch auth0 exception and return it as it is
43+ log . warn ( `Could not retrieve the user, because of` , body ) ;
44+ res . failed ( response . statusCode || 401 , body ) ;
45+
46+ } ) ;
47+
48+ } ;
4849
4950/**
5051 * Returns the access token of the given request header
0 commit comments