-
-
Notifications
You must be signed in to change notification settings - Fork 741
/
index.ts
78 lines (61 loc) · 2.25 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import Debug from 'debug';
import { merge, each, omit } from 'lodash';
import { Application } from '@feathersjs/feathers';
import { OAuthStrategy, OAuthProfile } from './strategy';
import { default as setupExpress } from './express';
import { OauthSetupSettings, getDefaultSettings } from './utils';
const debug = Debug('@feathersjs/authentication-oauth');
export { OauthSetupSettings, OAuthStrategy, OAuthProfile };
export const setup = (options: OauthSetupSettings) => (app: Application) => {
const service = app.defaultAuthentication ? app.defaultAuthentication(options.authService) : null;
if (!service) {
throw new Error('An authentication service must exist before registering @feathersjs/authentication-oauth');
}
const { oauth } = service.configuration;
if (!oauth) {
debug(`No oauth configuration found in authentication configuration. Skipping oAuth setup.`);
return;
}
const { strategyNames } = service;
// Set up all the defaults
const { path = '/oauth' } = oauth.defaults || {};
const port = app.get('port');
let host = app.get('host');
let protocol = 'https';
// Development environments commonly run on HTTP with an extended port
if (app.get('env') === 'development') {
protocol = 'http';
if (String(port) !== '80') {
host += ':' + port;
}
}
const grant = merge({
defaults: {
path,
host,
protocol,
transport: 'session'
}
}, omit(oauth, 'redirect'));
const getUrl = (url: string) => {
const { defaults } = grant;
return `${defaults.protocol}://${defaults.host}${path}/${url}`;
};
each(grant, (value, name) => {
if (name !== 'defaults') {
value.callback = value.callback || getUrl(`${name}/authenticate`);
value.redirect_uri = value.redirect_uri || getUrl(`${name}/callback`);
if (!strategyNames.includes(name)) {
debug(`Registering oAuth default strategy for '${name}'`);
service.register(name, new OAuthStrategy());
}
}
});
app.set('grant', grant);
};
export const express = (settings: Partial<OauthSetupSettings> = {}) => (app: Application) => {
const options = getDefaultSettings(app, settings);
app.configure(setup(options));
app.configure(setupExpress(options));
};
export const expressOauth = express;