Skip to content

Commit

Permalink
feat(authentication-oauth): Koa and transport independent oAuth authe…
Browse files Browse the repository at this point in the history
…ntication (#2737)
  • Loading branch information
daffl authored Sep 9, 2022
1 parent 0b2def6 commit 9231525
Show file tree
Hide file tree
Showing 26 changed files with 2,280 additions and 1,826 deletions.
2,894 changes: 1,471 additions & 1,423 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions packages/authentication-local/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export { LocalStrategy }
export const passwordHash =
(options: { service?: string; strategy: string }) =>
async <H extends HookContext<any, any>>(value: string | undefined, _data: any, context: H) => {
if (value === undefined) {
return value
}

const { app, params } = context
const authService = app.defaultAuthentication(options.service)
const localStrategy = authService.getStrategy(options.strategy) as LocalStrategy
Expand Down
13 changes: 10 additions & 3 deletions packages/authentication-oauth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,27 @@
"@feathersjs/errors": "^5.0.0-pre.28",
"@feathersjs/express": "^5.0.0-pre.28",
"@feathersjs/feathers": "^5.0.0-pre.28",
"express-session": "^1.17.3",
"@feathersjs/koa": "^5.0.0-pre.28",
"@feathersjs/schema": "^5.0.0-pre.28",
"cookie-session": "^2.0.0",
"grant": "^5.4.21",
"lodash": "^4.17.21"
"koa-session": "^6.2.0",
"lodash": "^4.17.21",
"qs": "^6.11.0"
},
"devDependencies": {
"@feathersjs/memory": "^5.0.0-pre.28",
"@types/cookie-session": "^2.0.44",
"@types/express": "^4.17.13",
"@types/express-session": "^1.17.5",
"@types/koa-session": "^5.10.6",
"@types/lodash": "^4.14.184",
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.14",
"@types/tough-cookie": "^4.0.2",
"axios": "^0.27.2",
"mocha": "^10.0.0",
"shx": "^0.3.4",
"tough-cookie": "^4.1.2",
"ts-node": "^10.9.1",
"typescript": "^4.8.2"
},
Expand Down
140 changes: 0 additions & 140 deletions packages/authentication-oauth/src/express.ts

This file was deleted.

95 changes: 29 additions & 66 deletions packages/authentication-oauth/src/index.ts
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)
}
}
Loading

0 comments on commit 9231525

Please sign in to comment.