-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.js
195 lines (177 loc) · 6.36 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* @module provider
*/
// @ts-ignore
const purestConfig = require('@purest/providers')
const dropbox = require('./dropbox')
const box = require('./box')
const drive = require('./drive')
const instagram = require('./instagram/graph')
const facebook = require('./facebook')
const onedrive = require('./onedrive')
const unsplash = require('./unsplash')
const zoom = require('./zoom')
const { getURLBuilder } = require('../helpers/utils')
const logger = require('../logger')
const { getCredentialsResolver } = require('./credentials')
// eslint-disable-next-line
const Provider = require('./Provider')
// eslint-disable-next-line
const SearchProvider = require('./SearchProvider')
const { wrapLegacyProvider } = require('./ProviderCompat')
// leave here for now until Purest Providers gets updated with Zoom provider
purestConfig.zoom = {
'https://zoom.us/': {
__domain: {
auth: {
auth: { bearer: '[0]' },
},
},
'[version]/{endpoint}': {
__path: {
alias: '__default',
version: 'v2',
},
},
'oauth/revoke': {
__path: {
alias: 'logout',
auth: {
auth: { basic: '[0]' },
},
},
},
},
}
/**
*
* @param {{server: object}} options
*/
const validOptions = (options) => {
return options.server.host && options.server.protocol
}
/**
*
* @param {string} name of the provider
* @param {{server: object, providerOptions: object}} options
* @returns {string} the authProvider for this provider
*/
const providerNameToAuthName = (name, options) => { // eslint-disable-line no-unused-vars
const providers = exports.getDefaultProviders()
return (providers[name] || {}).authProvider
}
/**
* adds the desired provider module to the request object,
* based on the providerName parameter specified
*
* @param {Record<string, (typeof Provider) | typeof SearchProvider>} providers
* @param {boolean} [needsProviderCredentials]
*/
module.exports.getProviderMiddleware = (providers, needsProviderCredentials) => {
/**
*
* @param {object} req
* @param {object} res
* @param {Function} next
* @param {string} providerName
*/
const middleware = (req, res, next, providerName) => {
let ProviderClass = providers[providerName]
if (ProviderClass && validOptions(req.companion.options)) {
// TODO remove this legacy provider compatibility when we release a new major
// @ts-ignore
if (ProviderClass.version !== 2) ProviderClass = wrapLegacyProvider(ProviderClass)
req.companion.provider = new ProviderClass({ providerName, config: purestConfig })
if (needsProviderCredentials) {
req.companion.getProviderCredentials = getCredentialsResolver(providerName, req.companion.options, req)
}
} else {
logger.warn('invalid provider options detected. Provider will not be loaded', 'provider.middleware.invalid', req.id)
}
next()
}
return middleware
}
/**
* @returns {Record<string, typeof Provider>}
*/
module.exports.getDefaultProviders = () => {
const providers = { dropbox, box, drive, facebook, onedrive, zoom, instagram }
return providers
}
/**
* @returns {Record<string, typeof SearchProvider>}
*/
module.exports.getSearchProviders = () => {
return { unsplash }
}
/**
*
* @typedef {{'module': typeof Provider, config: Record<string,unknown>}} CustomProvider
*
* @param {Record<string, CustomProvider>} customProviders
* @param {Record<string, typeof Provider>} providers
* @param {object} grantConfig
*/
module.exports.addCustomProviders = (customProviders, providers, grantConfig) => {
Object.keys(customProviders).forEach((providerName) => {
const customProvider = customProviders[providerName]
providers[providerName] = customProvider.module
grantConfig[providerName] = {
...customProvider.config,
// todo: consider setting these options from a universal point also used
// by official providers. It'll prevent these from getting left out if the
// requirement changes.
callback: `/${providerName}/callback`,
transport: 'session',
}
})
}
/**
*
* @param {{server: object, providerOptions: object}} companionOptions
* @param {object} grantConfig
*/
module.exports.addProviderOptions = (companionOptions, grantConfig) => {
const { server, providerOptions } = companionOptions
if (!validOptions({ server })) {
logger.warn('invalid provider options detected. Providers will not be loaded', 'provider.options.invalid')
return
}
grantConfig.defaults = {
host: server.host,
protocol: server.protocol,
path: server.path,
}
const { oauthDomain } = server
const keys = Object.keys(providerOptions).filter((key) => key !== 'server')
keys.forEach((providerName) => {
const authProvider = providerNameToAuthName(providerName, companionOptions)
if (authProvider && grantConfig[authProvider]) {
// explicitly add providerOptions so users don't override other providerOptions.
grantConfig[authProvider].key = providerOptions[providerName].key
grantConfig[authProvider].secret = providerOptions[providerName].secret
if (providerOptions[providerName].credentialsURL) {
grantConfig[authProvider].dynamic = ['key', 'secret', 'redirect_uri']
}
const provider = exports.getDefaultProviders()[providerName]
Object.assign(grantConfig[authProvider], provider.getExtraConfig())
// override grant.js redirect uri with companion's custom redirect url
const isExternal = !!server.implicitPath
const redirectPath = `/${providerName}/redirect`
grantConfig[authProvider].redirect_uri = getURLBuilder(companionOptions)(redirectPath, isExternal)
if (oauthDomain) {
const fullRedirectPath = getURLBuilder(companionOptions)(redirectPath, isExternal, true)
grantConfig[authProvider].redirect_uri = `${server.protocol}://${oauthDomain}${fullRedirectPath}`
}
if (server.implicitPath) {
// no url builder is used for this because grant internally adds the path
grantConfig[authProvider].callback = `${server.implicitPath}${grantConfig[authProvider].callback}`
} else if (server.path) {
grantConfig[authProvider].callback = `${server.path}${grantConfig[authProvider].callback}`
}
} else if (!['s3', 'searchProviders'].includes(providerName)) {
logger.warn(`skipping one found unsupported provider "${providerName}".`, 'provider.options.skip')
}
})
}