1
1
// @ts -ignore
2
- import Debug from 'debug' ;
3
- import { Application } from '@feathersjs/feathers' ;
4
- import { AuthenticationResult } from '@feathersjs/authentication' ;
2
+ import Debug from 'debug'
3
+ import { Application } from '@feathersjs/feathers'
4
+ import { AuthenticationResult } from '@feathersjs/authentication'
5
5
import {
6
6
Application as ExpressApplication ,
7
7
original as express
8
- } from '@feathersjs/express' ;
9
- import { SamlSetupSettings } from './utils' ;
10
- import { SamlStrategy } from './strategy' ;
11
- import { BadRequest } from '@feathersjs/errors' ;
12
-
13
- const debug = Debug ( 'feathers-saml/express' ) ;
8
+ } from '@feathersjs/express'
9
+ import { SamlSetupSettings } from './utils'
10
+ import { SamlStrategy } from './strategy'
11
+ import { BadRequest } from '@feathersjs/errors'
12
+
13
+ const debug = Debug ( 'feathers-saml/express' )
14
14
15
15
export default ( options : SamlSetupSettings ) => {
16
16
return ( feathersApp : Application ) => {
17
- const { authService } = options ;
18
- const app = feathersApp as ExpressApplication ;
19
- const config = app . get ( authService + 'Saml' ) ;
17
+ const { authService } = options
18
+ const app = feathersApp as ExpressApplication
19
+ const config = app . get ( authService + 'Saml' )
20
20
21
21
if ( ! config ) {
22
- debug ( 'No SAML configuration found, skipping Express SAML setup' ) ;
23
- return ;
22
+ debug ( 'No SAML configuration found, skipping Express SAML setup' )
23
+ return
24
24
}
25
25
26
26
if ( ! config . sp ) {
27
- debug ( 'No SAML SP found, skipping Express SAML setup' ) ;
28
- return ;
27
+ debug ( 'No SAML SP found, skipping Express SAML setup' )
28
+ return
29
29
}
30
30
31
31
if ( ! config . idp ) {
32
- debug ( 'No SAML IdP found, skipping Express SAML setup' ) ;
33
- return ;
32
+ debug ( 'No SAML IdP found, skipping Express SAML setup' )
33
+ return
34
34
}
35
35
36
- const { sp, idp, path } = config ;
36
+ const { sp, idp, path } = config
37
+
38
+ const authApp = express ( )
37
39
38
- const authApp = express ( ) ;
39
-
40
- authApp . get ( '/' , async ( req , res ) => {
41
- sp . create_login_request_url ( idp , config . loginRequestOptions ? config . loginRequestOptions : { } , async ( err : Error , login_url : string , request_id : string ) => {
42
- if ( err != null ) {
43
- return res . send ( 500 ) ;
44
- }
40
+ authApp . get ( '/' , async ( req : any , res : any ) => {
41
+ sp . create_login_request_url (
42
+ idp ,
43
+ config . loginRequestOptions ? config . loginRequestOptions : { } ,
44
+ async ( err : Error , login_url : string , request_id : string ) => {
45
+ if ( err != null ) {
46
+ return res . send ( 500 )
47
+ }
45
48
46
- res . redirect ( login_url ) ;
47
- } ) ;
48
- } ) ;
49
+ res . redirect ( login_url )
50
+ }
51
+ )
52
+ } )
49
53
50
- authApp . get ( '/metadata.xml' , async ( req , res ) => {
51
- res . type ( 'application/xml' ) ;
52
- res . send ( sp . create_metadata ( ) ) ;
53
- } ) ;
54
+ authApp . get ( '/metadata.xml' , async ( req : any , res : any ) => {
55
+ res . type ( 'application/xml' )
56
+ res . send ( sp . create_metadata ( ) )
57
+ } )
54
58
55
- authApp . post ( '/assert' , async ( req , res , next ) => {
56
- const service = app . defaultAuthentication ( authService ) ;
57
- const [ strategy ] = service . getStrategies ( 'saml' ) as SamlStrategy [ ] ;
59
+ authApp . post ( '/assert' , async ( req : any , res : any , next : any ) => {
60
+ const service = app . defaultAuthentication ( authService )
61
+ const [ strategy ] = service . getStrategies ( 'saml' ) as SamlStrategy [ ]
58
62
const params : any = {
59
- authStrategies : [ strategy . name ]
60
- } ;
61
- const sendResponse = async ( data : AuthenticationResult | Error ) => {
63
+ authStrategies : [ strategy . name ]
64
+ }
65
+ const sendResponse = async ( data : AuthenticationResult | Error ) => {
62
66
try {
63
- const redirect = await strategy . getRedirect ( data , params ) ;
67
+ const redirect = await strategy . getRedirect ( data , params )
64
68
65
69
if ( redirect !== null ) {
66
- res . redirect ( redirect ) ;
70
+ res . redirect ( redirect )
67
71
} else if ( data instanceof Error ) {
68
- throw data ;
72
+ throw data
69
73
} else {
70
- res . json ( data ) ;
74
+ res . json ( data )
71
75
}
72
76
} catch ( error ) {
73
- debug ( 'SAML error' , error ) ;
74
- next ( error ) ;
77
+ debug ( 'SAML error' , error )
78
+ next ( error )
75
79
}
76
- } ;
80
+ }
77
81
78
82
try {
79
83
const samlResponse : any = await new Promise ( ( resolve , reject ) => {
80
- let loginResponseOptions : any = { } ;
84
+ let loginResponseOptions : any = { }
81
85
82
86
if ( config . loginResponseOptions ) {
83
- loginResponseOptions = config . loginResponseOptions ;
87
+ loginResponseOptions = config . loginResponseOptions
84
88
}
85
-
86
- loginResponseOptions . request_body = req . body ;
87
-
88
- sp . post_assert ( idp , loginResponseOptions , async ( err : Error , saml_response : any ) => {
89
- if ( err != null ) {
90
- reject ( err ) ;
91
- return ;
89
+
90
+ loginResponseOptions . request_body = req . body
91
+
92
+ sp . post_assert (
93
+ idp ,
94
+ loginResponseOptions ,
95
+ async ( err : Error , saml_response : any ) => {
96
+ if ( err != null ) {
97
+ reject ( err )
98
+ return
99
+ }
100
+
101
+ resolve ( saml_response )
92
102
}
93
-
94
- resolve ( saml_response ) ;
95
- } ) ;
96
- } ) ;
103
+ )
104
+ } )
97
105
98
106
const authentication = {
99
107
strategy : strategy . name ,
100
108
...samlResponse
101
- } ;
109
+ }
102
110
103
111
params . payload = {
104
- nameId : samlResponse && samlResponse . user && samlResponse . user . name_id ? samlResponse . user . name_id : null ,
105
- sessionIndex : samlResponse && samlResponse . user && samlResponse . user . session_index ? samlResponse . user . session_index : null ,
112
+ nameId :
113
+ samlResponse && samlResponse . user && samlResponse . user . name_id
114
+ ? samlResponse . user . name_id
115
+ : null ,
116
+ sessionIndex :
117
+ samlResponse && samlResponse . user && samlResponse . user . session_index
118
+ ? samlResponse . user . session_index
119
+ : null ,
106
120
samlToken : true
107
- } ;
108
-
109
- debug ( `Calling ${ authService } .create authentication with SAML strategy` ) ;
121
+ }
122
+
123
+ debug ( `Calling ${ authService } .create authentication with SAML strategy` )
110
124
111
125
if ( config . samlTokenExpiry ) {
112
126
params . jwtOptions = {
113
127
expiresIn : config . samlTokenExpiry
114
- } ;
128
+ }
115
129
}
116
130
117
- const authResult = await service . create ( authentication , params ) ;
131
+ const authResult = await service . create ( authentication , params )
118
132
119
- debug ( 'Successful SAML authentication, sending response' ) ;
133
+ debug ( 'Successful SAML authentication, sending response' )
120
134
121
- await sendResponse ( authResult ) ;
135
+ await sendResponse ( authResult )
122
136
} catch ( error ) {
123
- debug ( 'Received SAML authentication error' , error . stack ) ;
124
- await sendResponse ( error ) ;
137
+ if ( error instanceof Error ) {
138
+ debug ( 'Received SAML authentication error' , error . stack )
139
+ await sendResponse ( error )
140
+ }
125
141
}
126
- } ) ;
142
+ } )
127
143
128
- authApp . get ( '/logout' , async ( req , res , next ) => {
129
- const { nameId, sessionIndex } = req . query ;
144
+ authApp . get ( '/logout' , async ( req : any , res : any , next : any ) => {
145
+ const { nameId, sessionIndex } = req . query
130
146
131
147
if ( ! nameId || ! sessionIndex ) {
132
- return next ( new BadRequest ( '`nameId` and `sessionIndex` must be set in query params' ) ) ;
148
+ return next (
149
+ new BadRequest (
150
+ '`nameId` and `sessionIndex` must be set in query params'
151
+ )
152
+ )
133
153
}
134
-
135
- let logoutRequestOptions : any = { } ;
154
+
155
+ let logoutRequestOptions : any = { }
136
156
137
157
if ( config . logoutRequestOptions ) {
138
- logoutRequestOptions = config . logoutRequestOptions ;
158
+ logoutRequestOptions = config . logoutRequestOptions
139
159
}
140
160
141
- logoutRequestOptions . name_id = nameId ;
142
- logoutRequestOptions . session_ndex = sessionIndex ;
143
-
144
- sp . create_logout_request_url ( idp , logoutRequestOptions , async ( err : Error , logout_url : string ) => {
145
- if ( err != null ) {
146
- next ( err ) ;
147
- return ;
148
- }
161
+ logoutRequestOptions . name_id = nameId
162
+ logoutRequestOptions . session_ndex = sessionIndex
149
163
150
- res . redirect ( logout_url ) ;
151
- } ) ;
152
- } ) ;
164
+ sp . create_logout_request_url (
165
+ idp ,
166
+ logoutRequestOptions ,
167
+ async ( err : Error , logout_url : string ) => {
168
+ if ( err != null ) {
169
+ next ( err )
170
+ return
171
+ }
153
172
154
- authApp . get ( '/slo' , async ( req , res , next ) => {
155
- sp . create_logout_response_url ( idp , config . logoutResponseOptions ? config . logoutResponseOptions : { } , async ( err : Error , request_url : string ) => {
156
- if ( err != null ) {
157
- next ( err ) ;
158
- return ;
173
+ res . redirect ( logout_url )
159
174
}
175
+ )
176
+ } )
177
+
178
+ authApp . get ( '/slo' , async ( req : any , res : any , next : any ) => {
179
+ sp . create_logout_response_url (
180
+ idp ,
181
+ config . logoutResponseOptions ? config . logoutResponseOptions : { } ,
182
+ async ( err : Error , request_url : string ) => {
183
+ if ( err != null ) {
184
+ next ( err )
185
+ return
186
+ }
160
187
161
- res . redirect ( request_url ) ;
162
- } ) ;
163
- } ) ;
164
-
188
+ res . redirect ( request_url )
189
+ }
190
+ )
191
+ } )
165
192
166
- app . use ( path , authApp ) ;
167
- } ;
168
- } ;
193
+ app . use ( path , authApp )
194
+ }
195
+ }
0 commit comments