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

Send version header #1564

Merged
merged 5 commits into from
May 17, 2019
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
4 changes: 4 additions & 0 deletions packages/@uppy/companion/src/server/controllers/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@ module.exports = function connect (req, res) {
state = oAuthState.addToState(state, { uppyInstance: req.uppy.buildURL('', true) }, secret)
}

if (req.uppy.clientVersion) {
state = oAuthState.addToState(state, { clientVersion: req.uppy.clientVersion }, secret)
}

res.redirect(req.uppy.buildURL(`/connect/${req.uppy.provider.authProvider}?state=${state}`, true))
}
28 changes: 27 additions & 1 deletion packages/@uppy/companion/src/server/controllers/send-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ module.exports = function sendToken (req, res, next) {
const state = (req.session.grant || {}).state
if (state) {
const origin = oAuthState.getFromState(state, 'origin', req.uppy.options.secret)
const clientVersion = oAuthState.getFromState(
state,
'clientVersion',
req.uppy.options.secret
)
const allowedClients = req.uppy.options.clients
// if no preset clients then allow any client
if (!allowedClients || hasMatch(origin, allowedClients) || hasMatch(parseUrl(origin).host, allowedClients)) {
return res.send(htmlContent(uppyAuthToken, origin))
// @todo do a more secure client version check, see https://www.npmjs.com/package/semver
return res.send(clientVersion ? htmlContent(uppyAuthToken, origin) : oldHtmlContent(uppyAuthToken, origin))
Copy link
Contributor

Choose a reason for hiding this comment

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

This works but we should add a todo to the changelog to use https://www.npmjs.com/package/semver for these checks, especially if we're going to have more breaking changes.

Copy link
Contributor

@goto-bus-stop goto-bus-stop May 17, 2019

Choose a reason for hiding this comment

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

adding on asana so you can ignore the above

e; nvm you already did it, both is fine :P

}
}
next()
Expand All @@ -49,3 +55,23 @@ const htmlContent = (token, origin) => {
<body></body>
</html>`
}

/**
* @todo remove this function in next major release
* @param {string} token uppy auth token
* @param {string} origin url string
*/
const oldHtmlContent = (token, origin) => {
return `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script>
window.opener.postMessage({token: "${token}"}, "${sanitizeHtml(origin)}")
window.close()
</script>
</head>
<body></body>
</html>`
}
3 changes: 2 additions & 1 deletion packages/@uppy/companion/src/uppy.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module.exports.app = (options = {}) => {
app.use((req, res, next) => {
res.header(
'Access-Control-Allow-Headers',
[res.get('Access-Control-Allow-Headers'), 'uppy-auth-token'].join(', ')
[res.get('Access-Control-Allow-Headers'), 'uppy-auth-token', 'uppy-client'].join(', ')
)
Copy link
Contributor

Choose a reason for hiding this comment

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

uppy-versions?

Copy link
Member

Choose a reason for hiding this comment

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

Uppy-Versions

Copy link
Member

Choose a reason for hiding this comment

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

Node.js will lowercase it for us, but i think so far we've tried to use uppercasing like that in tus/uppy headers

next()
})
Expand Down Expand Up @@ -218,6 +218,7 @@ const getOptionsMiddleware = (options) => {
options,
s3Client,
authToken: req.header('uppy-auth-token') || req.query.uppyAuthToken,
clientVersion: req.header('uppy-versions') || req.query.uppyVersions,
buildURL: getURLBuilder(options)
}
next()
Expand Down
4 changes: 3 additions & 1 deletion packages/@uppy/provider-views/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,9 @@ module.exports = class ProviderView {

handleAuth () {
const authState = btoa(JSON.stringify({ origin: location.origin }))
const link = `${this.provider.authUrl()}?state=${authState}`
// @todo remove this hardcoded version
const clientVersion = 'companion-client:1.0.2'
const link = `${this.provider.authUrl()}?state=${authState}&uppyVersions=${clientVersion}`

const authWindow = window.open(link, '_blank')
const handleToken = (e) => {
Expand Down