-
-
Notifications
You must be signed in to change notification settings - Fork 751
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(authentication-oauth): Koa and transport independent oAuth authe…
…ntication (#2737)
- Loading branch information
Showing
26 changed files
with
2,280 additions
and
1,826 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,46 @@ | ||
import defaultsDeep from 'lodash/defaultsDeep' | ||
import each from 'lodash/each' | ||
import omit from 'lodash/omit' | ||
import { createDebug } from '@feathersjs/commons' | ||
import { Application } from '@feathersjs/feathers' | ||
import { createDebug } from '@feathersjs/commons' | ||
import { resolveDispatch } from '@feathersjs/schema' | ||
|
||
import { OAuthStrategy, OAuthProfile } from './strategy' | ||
import { default as setupExpress } from './express' | ||
import { OauthSetupSettings, getDefaultSettings } from './utils' | ||
import { redirectHook, OAuthService } from './service' | ||
import { getServiceOptions, OauthSetupSettings } from './utils' | ||
|
||
const debug = createDebug('@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 port = app.get('port') | ||
let host = app.get('host') | ||
let protocol = 'https' | ||
export const oauth = | ||
(settings: Partial<OauthSetupSettings> = {}) => | ||
(app: Application) => { | ||
const authService = app.defaultAuthentication ? app.defaultAuthentication(settings.authService) : null | ||
|
||
// Development environments commonly run on HTTP with an extended port | ||
if (app.get('env') === 'development') { | ||
protocol = 'http' | ||
if (String(port) !== '80') { | ||
host += `:${port}` | ||
if (!authService) { | ||
throw new Error( | ||
'An authentication service must exist before registering @feathersjs/authentication-oauth' | ||
) | ||
} | ||
} | ||
|
||
const grant = defaultsDeep({}, omit(oauth, ['redirect', 'origins']), { | ||
defaults: { | ||
prefix: '/oauth', | ||
origin: `${protocol}://${host}`, | ||
transport: 'session', | ||
response: ['tokens', 'raw', 'profile'] | ||
if (!authService.configuration.oauth) { | ||
debug('No oauth configuration found in authentication configuration. Skipping oAuth setup.') | ||
return | ||
} | ||
}) | ||
|
||
const getUrl = (url: string) => { | ||
const { defaults } = grant | ||
return `${defaults.origin}${defaults.prefix}/${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()) | ||
} | ||
const oauthOptions = { | ||
linkStrategy: 'jwt', | ||
...settings | ||
} | ||
}) | ||
const serviceOptions = getServiceOptions(authService, oauthOptions) | ||
|
||
app.set('grant', grant) | ||
} | ||
app.use('oauth/:provider', new OAuthService(authService, oauthOptions), serviceOptions) | ||
|
||
export const express = | ||
(settings: Partial<OauthSetupSettings> = {}) => | ||
(app: Application) => { | ||
const options = getDefaultSettings(app, settings) | ||
const oauthService = app.service('oauth/:provider') | ||
|
||
app.configure(setup(options)) | ||
app.configure(setupExpress(options)) | ||
} | ||
oauthService.hooks({ | ||
around: { all: [resolveDispatch(), redirectHook()] } | ||
}) | ||
|
||
export const expressOauth = express | ||
if (typeof oauthService.publish === 'function') { | ||
app.service('oauth/:provider').publish(() => null) | ||
} | ||
} |
Oops, something went wrong.