Skip to content

Commit aba8308

Browse files
authored
Fix type issues with Typescript 4
The catch blocks have some "new" syntax we need to adhere to: microsoft/TypeScript#36775 As of Feathers 4, the request and response body are actually of type any, but we have to specify that explicitly. We can probably get rid of this `// @ts-ignore` now.
1 parent ce70b87 commit aba8308

File tree

1 file changed

+128
-101
lines changed

1 file changed

+128
-101
lines changed

src/express.ts

+128-101
Original file line numberDiff line numberDiff line change
@@ -1,168 +1,195 @@
11
// @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'
55
import {
66
Application as ExpressApplication,
77
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')
1414

1515
export default (options: SamlSetupSettings) => {
1616
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')
2020

2121
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
2424
}
2525

2626
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
2929
}
3030

3131
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
3434
}
3535

36-
const { sp, idp, path } = config;
36+
const { sp, idp, path } = config
37+
38+
const authApp = express()
3739

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+
}
4548

46-
res.redirect(login_url);
47-
});
48-
});
49+
res.redirect(login_url)
50+
}
51+
)
52+
})
4953

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+
})
5458

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[]
5862
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) => {
6266
try {
63-
const redirect = await strategy.getRedirect(data, params);
67+
const redirect = await strategy.getRedirect(data, params)
6468

6569
if (redirect !== null) {
66-
res.redirect(redirect);
70+
res.redirect(redirect)
6771
} else if (data instanceof Error) {
68-
throw data;
72+
throw data
6973
} else {
70-
res.json(data);
74+
res.json(data)
7175
}
7276
} catch (error) {
73-
debug('SAML error', error);
74-
next(error);
77+
debug('SAML error', error)
78+
next(error)
7579
}
76-
};
80+
}
7781

7882
try {
7983
const samlResponse: any = await new Promise((resolve, reject) => {
80-
let loginResponseOptions: any = {};
84+
let loginResponseOptions: any = {}
8185

8286
if (config.loginResponseOptions) {
83-
loginResponseOptions = config.loginResponseOptions;
87+
loginResponseOptions = config.loginResponseOptions
8488
}
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)
92102
}
93-
94-
resolve(saml_response);
95-
});
96-
});
103+
)
104+
})
97105

98106
const authentication = {
99107
strategy: strategy.name,
100108
...samlResponse
101-
};
109+
}
102110

103111
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,
106120
samlToken: true
107-
};
108-
109-
debug(`Calling ${authService}.create authentication with SAML strategy`);
121+
}
122+
123+
debug(`Calling ${authService}.create authentication with SAML strategy`)
110124

111125
if (config.samlTokenExpiry) {
112126
params.jwtOptions = {
113127
expiresIn: config.samlTokenExpiry
114-
};
128+
}
115129
}
116130

117-
const authResult = await service.create(authentication, params);
131+
const authResult = await service.create(authentication, params)
118132

119-
debug('Successful SAML authentication, sending response');
133+
debug('Successful SAML authentication, sending response')
120134

121-
await sendResponse(authResult);
135+
await sendResponse(authResult)
122136
} 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+
}
125141
}
126-
});
142+
})
127143

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
130146

131147
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+
)
133153
}
134-
135-
let logoutRequestOptions: any = {};
154+
155+
let logoutRequestOptions: any = {}
136156

137157
if (config.logoutRequestOptions) {
138-
logoutRequestOptions = config.logoutRequestOptions;
158+
logoutRequestOptions = config.logoutRequestOptions
139159
}
140160

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
149163

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+
}
153172

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)
159174
}
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+
}
160187

161-
res.redirect(request_url);
162-
});
163-
});
164-
188+
res.redirect(request_url)
189+
}
190+
)
191+
})
165192

166-
app.use(path, authApp);
167-
};
168-
};
193+
app.use(path, authApp)
194+
}
195+
}

0 commit comments

Comments
 (0)