@feathersjs/authentication-oauth 5.0.0-pre.27 → 5.0.0-pre.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +28 -0
- package/lib/index.d.ts +1 -3
- package/lib/index.js +19 -54
- package/lib/index.js.map +1 -1
- package/lib/service.d.ts +36 -0
- package/lib/service.js +141 -0
- package/lib/service.js.map +1 -0
- package/lib/strategy.js +7 -14
- package/lib/strategy.js.map +1 -1
- package/lib/utils.d.ts +13 -4
- package/lib/utils.js +89 -7
- package/lib/utils.js.map +1 -1
- package/package.json +24 -16
- package/src/index.ts +29 -66
- package/src/service.ts +177 -0
- package/src/strategy.ts +9 -16
- package/src/utils.ts +114 -8
- package/lib/express.d.ts +0 -19
- package/lib/express.js +0 -120
- package/lib/express.js.map +0 -1
- package/src/express.ts +0 -140
package/src/express.ts
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
import grant from 'grant'
|
|
2
|
-
import session from 'express-session'
|
|
3
|
-
import { Request, Response, NextFunction } from 'express'
|
|
4
|
-
import { createDebug } from '@feathersjs/commons'
|
|
5
|
-
import { Application } from '@feathersjs/feathers'
|
|
6
|
-
import { AuthenticationResult } from '@feathersjs/authentication'
|
|
7
|
-
import { Application as ExpressApplication, original as originalExpress } from '@feathersjs/express'
|
|
8
|
-
import { OauthSetupSettings } from './utils'
|
|
9
|
-
import { OAuthStrategy } from './strategy'
|
|
10
|
-
|
|
11
|
-
const grantInstance = grant.express()
|
|
12
|
-
const debug = createDebug('@feathersjs/authentication-oauth/express')
|
|
13
|
-
|
|
14
|
-
declare module 'express-session' {
|
|
15
|
-
interface SessionData {
|
|
16
|
-
redirect: string
|
|
17
|
-
accessToken: string
|
|
18
|
-
query: { [key: string]: any }
|
|
19
|
-
grant: { [key: string]: any }
|
|
20
|
-
headers: { [key: string]: any }
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export default (options: OauthSetupSettings) => {
|
|
25
|
-
return (feathersApp: Application) => {
|
|
26
|
-
const { authService, linkStrategy } = options
|
|
27
|
-
const app = feathersApp as ExpressApplication
|
|
28
|
-
const config = app.get('grant')
|
|
29
|
-
|
|
30
|
-
if (!config) {
|
|
31
|
-
debug('No grant configuration found, skipping Express oAuth setup')
|
|
32
|
-
return
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const { prefix } = config.defaults
|
|
36
|
-
const expressSession =
|
|
37
|
-
options.expressSession ||
|
|
38
|
-
session({
|
|
39
|
-
secret: Math.random().toString(36).substring(7),
|
|
40
|
-
saveUninitialized: true,
|
|
41
|
-
resave: true
|
|
42
|
-
})
|
|
43
|
-
const grantApp = grantInstance(config)
|
|
44
|
-
const authApp = originalExpress()
|
|
45
|
-
|
|
46
|
-
authApp.use(expressSession)
|
|
47
|
-
|
|
48
|
-
authApp.get('/:name', (req: Request, _res: Response, next: NextFunction) => {
|
|
49
|
-
const { feathers_token, redirect, ...query } = req.query
|
|
50
|
-
|
|
51
|
-
if (feathers_token) {
|
|
52
|
-
debug('Got feathers_token query parameter to link accounts', feathers_token)
|
|
53
|
-
req.session.accessToken = feathers_token as string
|
|
54
|
-
}
|
|
55
|
-
req.session.redirect = redirect as string
|
|
56
|
-
req.session.query = query
|
|
57
|
-
req.session.headers = req.headers
|
|
58
|
-
if (typeof req.session.save === 'function') {
|
|
59
|
-
req.session.save((err: any) => {
|
|
60
|
-
if (err) {
|
|
61
|
-
next(`Error storing session: ${err}`)
|
|
62
|
-
} else {
|
|
63
|
-
next()
|
|
64
|
-
}
|
|
65
|
-
})
|
|
66
|
-
} else {
|
|
67
|
-
next()
|
|
68
|
-
}
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
authApp.get('/:name/authenticate', async (req: Request, res: Response, next: NextFunction) => {
|
|
72
|
-
const { name } = req.params
|
|
73
|
-
const { accessToken, grant, query = {}, redirect, headers } = req.session
|
|
74
|
-
const service = app.defaultAuthentication(authService)
|
|
75
|
-
const [strategy] = service.getStrategies(name) as OAuthStrategy[]
|
|
76
|
-
const params = {
|
|
77
|
-
...req.feathers,
|
|
78
|
-
authStrategies: [name],
|
|
79
|
-
authentication: accessToken
|
|
80
|
-
? {
|
|
81
|
-
strategy: linkStrategy,
|
|
82
|
-
accessToken
|
|
83
|
-
}
|
|
84
|
-
: null,
|
|
85
|
-
query,
|
|
86
|
-
redirect,
|
|
87
|
-
headers
|
|
88
|
-
}
|
|
89
|
-
const sendResponse = async (data: AuthenticationResult | Error) => {
|
|
90
|
-
try {
|
|
91
|
-
const redirect = await strategy.getRedirect(data, params)
|
|
92
|
-
|
|
93
|
-
if (redirect !== null) {
|
|
94
|
-
res.redirect(redirect)
|
|
95
|
-
} else if (data instanceof Error) {
|
|
96
|
-
throw data
|
|
97
|
-
} else {
|
|
98
|
-
res.json(data)
|
|
99
|
-
}
|
|
100
|
-
} catch (error: any) {
|
|
101
|
-
debug('oAuth error', error)
|
|
102
|
-
next(error)
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
try {
|
|
107
|
-
const payload = config.defaults.transport === 'session' ? grant.response : req.query
|
|
108
|
-
const authentication = {
|
|
109
|
-
strategy: name,
|
|
110
|
-
...payload
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
await new Promise<void>((resolve, reject) => {
|
|
114
|
-
if (req.session.destroy) {
|
|
115
|
-
req.session.destroy((err: any) => (err ? reject(err) : resolve()))
|
|
116
|
-
} else {
|
|
117
|
-
req.session = null
|
|
118
|
-
resolve()
|
|
119
|
-
}
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
debug(`Calling ${authService}.create authentication with strategy ${name}`)
|
|
123
|
-
|
|
124
|
-
const authResult = await service.create(authentication, params)
|
|
125
|
-
|
|
126
|
-
debug('Successful oAuth authentication, sending response')
|
|
127
|
-
|
|
128
|
-
await sendResponse(authResult)
|
|
129
|
-
} catch (error: any) {
|
|
130
|
-
debug('Received oAuth authentication error', error.stack)
|
|
131
|
-
await sendResponse(error)
|
|
132
|
-
}
|
|
133
|
-
})
|
|
134
|
-
|
|
135
|
-
authApp.use(grantApp)
|
|
136
|
-
|
|
137
|
-
app.set('grant', grantApp.config)
|
|
138
|
-
app.use(prefix, authApp)
|
|
139
|
-
}
|
|
140
|
-
}
|