|
1 | | -import defaultsDeep from 'lodash/defaultsDeep' |
2 | | -import each from 'lodash/each' |
3 | | -import omit from 'lodash/omit' |
4 | | -import { createDebug } from '@feathersjs/commons' |
5 | 1 | import { Application } from '@feathersjs/feathers' |
| 2 | +import { createDebug } from '@feathersjs/commons' |
| 3 | +import { resolveDispatch } from '@feathersjs/schema' |
| 4 | + |
6 | 5 | import { OAuthStrategy, OAuthProfile } from './strategy' |
7 | | -import { default as setupExpress } from './express' |
8 | | -import { OauthSetupSettings, getDefaultSettings } from './utils' |
| 6 | +import { redirectHook, OAuthService } from './service' |
| 7 | +import { getServiceOptions, OauthSetupSettings } from './utils' |
9 | 8 |
|
10 | 9 | const debug = createDebug('@feathersjs/authentication-oauth') |
11 | 10 |
|
12 | 11 | export { OauthSetupSettings, OAuthStrategy, OAuthProfile } |
13 | 12 |
|
14 | | -export const setup = (options: OauthSetupSettings) => (app: Application) => { |
15 | | - const service = app.defaultAuthentication ? app.defaultAuthentication(options.authService) : null |
16 | | - |
17 | | - if (!service) { |
18 | | - throw new Error( |
19 | | - 'An authentication service must exist before registering @feathersjs/authentication-oauth' |
20 | | - ) |
21 | | - } |
22 | | - |
23 | | - const { oauth } = service.configuration |
24 | | - |
25 | | - if (!oauth) { |
26 | | - debug('No oauth configuration found in authentication configuration. Skipping oAuth setup.') |
27 | | - return |
28 | | - } |
29 | | - |
30 | | - const { strategyNames } = service |
31 | | - |
32 | | - // Set up all the defaults |
33 | | - const port = app.get('port') |
34 | | - let host = app.get('host') |
35 | | - let protocol = 'https' |
| 13 | +export const oauth = |
| 14 | + (settings: Partial<OauthSetupSettings> = {}) => |
| 15 | + (app: Application) => { |
| 16 | + const authService = app.defaultAuthentication ? app.defaultAuthentication(settings.authService) : null |
36 | 17 |
|
37 | | - // Development environments commonly run on HTTP with an extended port |
38 | | - if (app.get('env') === 'development') { |
39 | | - protocol = 'http' |
40 | | - if (String(port) !== '80') { |
41 | | - host += `:${port}` |
| 18 | + if (!authService) { |
| 19 | + throw new Error( |
| 20 | + 'An authentication service must exist before registering @feathersjs/authentication-oauth' |
| 21 | + ) |
42 | 22 | } |
43 | | - } |
44 | 23 |
|
45 | | - const grant = defaultsDeep({}, omit(oauth, ['redirect', 'origins']), { |
46 | | - defaults: { |
47 | | - prefix: '/oauth', |
48 | | - origin: `${protocol}://${host}`, |
49 | | - transport: 'session', |
50 | | - response: ['tokens', 'raw', 'profile'] |
| 24 | + if (!authService.configuration.oauth) { |
| 25 | + debug('No oauth configuration found in authentication configuration. Skipping oAuth setup.') |
| 26 | + return |
51 | 27 | } |
52 | | - }) |
53 | | - |
54 | | - const getUrl = (url: string) => { |
55 | | - const { defaults } = grant |
56 | | - return `${defaults.origin}${defaults.prefix}/${url}` |
57 | | - } |
58 | | - |
59 | | - each(grant, (value, name) => { |
60 | | - if (name !== 'defaults') { |
61 | | - value.callback = value.callback || getUrl(`${name}/authenticate`) |
62 | | - value.redirect_uri = value.redirect_uri || getUrl(`${name}/callback`) |
63 | 28 |
|
64 | | - if (!strategyNames.includes(name)) { |
65 | | - debug(`Registering oAuth default strategy for '${name}'`) |
66 | | - service.register(name, new OAuthStrategy()) |
67 | | - } |
| 29 | + const oauthOptions = { |
| 30 | + linkStrategy: 'jwt', |
| 31 | + ...settings |
68 | 32 | } |
69 | | - }) |
| 33 | + const serviceOptions = getServiceOptions(authService, oauthOptions) |
70 | 34 |
|
71 | | - app.set('grant', grant) |
72 | | -} |
| 35 | + app.use('oauth/:provider', new OAuthService(authService, oauthOptions), serviceOptions) |
73 | 36 |
|
74 | | -export const express = |
75 | | - (settings: Partial<OauthSetupSettings> = {}) => |
76 | | - (app: Application) => { |
77 | | - const options = getDefaultSettings(app, settings) |
| 37 | + const oauthService = app.service('oauth/:provider') |
78 | 38 |
|
79 | | - app.configure(setup(options)) |
80 | | - app.configure(setupExpress(options)) |
81 | | - } |
| 39 | + oauthService.hooks({ |
| 40 | + around: { all: [resolveDispatch(), redirectHook()] } |
| 41 | + }) |
82 | 42 |
|
83 | | -export const expressOauth = express |
| 43 | + if (typeof oauthService.publish === 'function') { |
| 44 | + app.service('oauth/:provider').publish(() => null) |
| 45 | + } |
| 46 | + } |
0 commit comments