Skip to content

Commit 9231525

Browse files
authored
feat(authentication-oauth): Koa and transport independent oAuth authentication (#2737)
1 parent 0b2def6 commit 9231525

26 files changed

Lines changed: 2280 additions & 1826 deletions

File tree

package-lock.json

Lines changed: 1471 additions & 1423 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/authentication-local/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export { LocalStrategy }
1616
export const passwordHash =
1717
(options: { service?: string; strategy: string }) =>
1818
async <H extends HookContext<any, any>>(value: string | undefined, _data: any, context: H) => {
19+
if (value === undefined) {
20+
return value
21+
}
22+
1923
const { app, params } = context
2024
const authService = app.defaultAuthentication(options.service)
2125
const localStrategy = authService.getStrategy(options.strategy) as LocalStrategy

packages/authentication-oauth/package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,27 @@
5959
"@feathersjs/errors": "^5.0.0-pre.28",
6060
"@feathersjs/express": "^5.0.0-pre.28",
6161
"@feathersjs/feathers": "^5.0.0-pre.28",
62-
"express-session": "^1.17.3",
62+
"@feathersjs/koa": "^5.0.0-pre.28",
63+
"@feathersjs/schema": "^5.0.0-pre.28",
64+
"cookie-session": "^2.0.0",
6365
"grant": "^5.4.21",
64-
"lodash": "^4.17.21"
66+
"koa-session": "^6.2.0",
67+
"lodash": "^4.17.21",
68+
"qs": "^6.11.0"
6569
},
6670
"devDependencies": {
6771
"@feathersjs/memory": "^5.0.0-pre.28",
72+
"@types/cookie-session": "^2.0.44",
6873
"@types/express": "^4.17.13",
69-
"@types/express-session": "^1.17.5",
74+
"@types/koa-session": "^5.10.6",
7075
"@types/lodash": "^4.14.184",
7176
"@types/mocha": "^9.1.1",
7277
"@types/node": "^18.7.14",
78+
"@types/tough-cookie": "^4.0.2",
7379
"axios": "^0.27.2",
7480
"mocha": "^10.0.0",
7581
"shx": "^0.3.4",
82+
"tough-cookie": "^4.1.2",
7683
"ts-node": "^10.9.1",
7784
"typescript": "^4.8.2"
7885
},

packages/authentication-oauth/src/express.ts

Lines changed: 0 additions & 140 deletions
This file was deleted.
Lines changed: 29 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,46 @@
1-
import defaultsDeep from 'lodash/defaultsDeep'
2-
import each from 'lodash/each'
3-
import omit from 'lodash/omit'
4-
import { createDebug } from '@feathersjs/commons'
51
import { Application } from '@feathersjs/feathers'
2+
import { createDebug } from '@feathersjs/commons'
3+
import { resolveDispatch } from '@feathersjs/schema'
4+
65
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'
98

109
const debug = createDebug('@feathersjs/authentication-oauth')
1110

1211
export { OauthSetupSettings, OAuthStrategy, OAuthProfile }
1312

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
3617

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+
)
4222
}
43-
}
4423

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
5127
}
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`)
6328

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
6832
}
69-
})
33+
const serviceOptions = getServiceOptions(authService, oauthOptions)
7034

71-
app.set('grant', grant)
72-
}
35+
app.use('oauth/:provider', new OAuthService(authService, oauthOptions), serviceOptions)
7336

74-
export const express =
75-
(settings: Partial<OauthSetupSettings> = {}) =>
76-
(app: Application) => {
77-
const options = getDefaultSettings(app, settings)
37+
const oauthService = app.service('oauth/:provider')
7838

79-
app.configure(setup(options))
80-
app.configure(setupExpress(options))
81-
}
39+
oauthService.hooks({
40+
around: { all: [resolveDispatch(), redirectHook()] }
41+
})
8242

83-
export const expressOauth = express
43+
if (typeof oauthService.publish === 'function') {
44+
app.service('oauth/:provider').publish(() => null)
45+
}
46+
}

0 commit comments

Comments
 (0)