@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/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
- }