Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test endpoint for dynamic oauth creds #4667

Merged
merged 3 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ COMPANION_UNSPLASH_SECRET=***
COMPANION_ONEDRIVE_KEY=***
COMPANION_ONEDRIVE_SECRET=****

# To test dynamic Oauth against local companion (which is pointless but allows us to test it without Transloadit's servers), enable these:
#COMPANION_GOOGLE_KEYS_ENDPOINT=http://localhost:3020/drive/test-dynamic-oauth-credentials?secret=development
#COMPANION_TEST_DYNAMIC_OAUTH_CREDENTIALS=true
#COMPANION_TEST_DYNAMIC_OAUTH_CREDENTIALS_SECRET=development


# Development environment
# =======================

Expand All @@ -73,6 +79,9 @@ VITE_COMPANION_ALLOWED_HOSTS="/\.transloadit\.com\$/"
VITE_TUS_ENDPOINT=https://tusd.tusdemo.net/files/
VITE_XHR_ENDPOINT=https://xhr-server.herokuapp.com/upload

# If you want to test dynamic Oauth
# VITE_COMPANION_GOOGLE_DRIVE_KEYS_PARAMS_CREDENTIALS_NAME=companion-google-drive

VITE_TRANSLOADIT_KEY=***
VITE_TRANSLOADIT_TEMPLATE=***
VITE_TRANSLOADIT_SERVICE_URL=https://api2.transloadit.com
Expand Down
21 changes: 21 additions & 0 deletions packages/@uppy/companion/src/companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,27 @@ module.exports.app = (optionsArg = {}) => {

app.get('/:providerName/thumbnail/:id', middlewares.hasSessionAndProvider, middlewares.hasOAuthProvider, middlewares.cookieAuthToken, middlewares.verifyToken, controllers.thumbnail)

// Used for testing dynamic credentials only, normally this would run on a separate server.
if (options.testDynamicOauthCredentials) {
app.post('/:providerName/test-dynamic-oauth-credentials', (req, res) => {
if (req.query.secret !== options.testDynamicOauthCredentialsSecret) throw new Error('Invalid secret')
logger.info('Returning dynamic OAuth2 credentials')
const { providerName } = req.params
// for simplicity, we just return the normal credentials for the provider, but in a real-world scenario,
// we would query based on parameters
const { key, secret } = options.providerOptions[providerName]
res.send({
credentials: {
key,
secret,
redirect_uri: providerManager.getGrantConfigForProvider({
providerName, companionOptions: options, grantConfig,
})?.redirect_uri,
},
})
})
}

app.param('providerName', providerManager.getProviderMiddleware(providers, grantConfig))

if (app.get('env') !== 'test') {
Expand Down
9 changes: 9 additions & 0 deletions packages/@uppy/companion/src/server/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ const providerNameToAuthName = (name, options) => { // eslint-disable-line no-un
return (providers[name] || {}).authProvider
}

function getGrantConfigForProvider ({ providerName, companionOptions, grantConfig }) {
const authProvider = providerNameToAuthName(providerName, companionOptions)

if (!isOAuthProvider(authProvider)) return undefined
return grantConfig[authProvider]
}

module.exports.getGrantConfigForProvider = getGrantConfigForProvider

/**
* adds the desired provider module to the request object,
* based on the providerName parameter specified
Expand Down
2 changes: 2 additions & 0 deletions packages/@uppy/companion/src/standalone/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ const getConfigFromEnv = () => {
metrics: process.env.COMPANION_HIDE_METRICS !== 'true',
loggerProcessName: process.env.COMPANION_LOGGER_PROCESS_NAME,
corsOrigins: getCorsOrigins(),
testDynamicOauthCredentials: process.env.COMPANION_TEST_DYNAMIC_OAUTH_CREDENTIALS === 'true',
testDynamicOauthCredentialsSecret: process.env.COMPANION_TEST_DYNAMIC_OAUTH_CREDENTIALS_SECRET,
}
}

Expand Down
24 changes: 22 additions & 2 deletions private/dev/Dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import ImageEditor from '@uppy/image-editor'
import DropTarget from '@uppy/drop-target'
import Audio from '@uppy/audio'
import Compressor from '@uppy/compressor'
import GoogleDrive from '@uppy/google-drive'
/* eslint-enable import/no-extraneous-dependencies */

import generateSignatureIfSecret from './generateSignatureIfSecret.js'
Expand Down Expand Up @@ -53,6 +54,25 @@ async function assemblyOptions () {
})
}

function getCompanionKeysParams (name) {
const {
[`VITE_COMPANION_${name}_KEYS_PARAMS_CREDENTIALS_NAME`]: credentialsName,
[`VITE_COMPANION_${name}_KEYS_PARAMS_KEY`]: key,
mifi marked this conversation as resolved.
Show resolved Hide resolved
} = import.meta.env

if (credentialsName && key) {
// https://github.com/transloadit/uppy/pull/2802#issuecomment-1023093616
return {
companionKeysParams: {
key,
credentialsName,
},
}
}

return {}
}

// Rest is implementation! Obviously edit as necessary...

export default () => {
Expand Down Expand Up @@ -80,7 +100,7 @@ export default () => {
proudlyDisplayPoweredByUppy: true,
note: `${JSON.stringify(restrictions)}`,
})
// .use(GoogleDrive, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
.use(GoogleDrive, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts, ...getCompanionKeysParams('GOOGLE_DRIVE') })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arturi should RemoteSources support this too? Otherwise you need both the regular plugin and remote sources.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should do that in a different PR anyways?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sure, just wanted to make Artur aware.

// .use(Instagram, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
// .use(Dropbox, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
// .use(Box, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
Expand All @@ -91,7 +111,7 @@ export default () => {
// .use(Unsplash, { target: Dashboard, companionUrl: COMPANION_URL, companionAllowedHosts })
.use(RemoteSources, {
companionUrl: COMPANION_URL,
sources: ['Box', 'Dropbox', 'Facebook', 'GoogleDrive', 'Instagram', 'OneDrive', 'Unsplash', 'Zoom', 'Url'],
sources: ['Box', 'Dropbox', 'Facebook', 'Instagram', 'OneDrive', 'Unsplash', 'Zoom', 'Url'],
companionAllowedHosts,
})
.use(Webcam, {
Expand Down