Skip to content

Commit

Permalink
@uppy/companion: fix merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Jun 19, 2024
1 parent a42b150 commit 0dd7ac3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { ProviderAuthError } = require('../error')
const got = require('../../got')

// For testing refresh token:
// first run a download with mockAccessTokenExpiredError = true
// first run a download with mockAccessTokenExpiredError = true
// then when you want to test expiry, set to mockAccessTokenExpiredError to the logged access token
// This will trigger companion/nodemon to restart, and it will respond with a simulated invalid token response
const mockAccessTokenExpiredError = undefined
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const got = require('got').default
const got = require('../../../got')

const { logout, refreshToken } = require('../index')
const logger = require('../../../logger')
Expand All @@ -23,15 +23,15 @@ const DRIVE_FILES_FIELDS = `kind,nextPageToken,incompleteSearch,files(${DRIVE_FI
// using wildcard to get all 'drive' fields because specifying fields seems no to work for the /drives endpoint
const SHARED_DRIVE_FIELDS = '*'

const getClient = ({ token }) => got.extend({
const getClient = async ({ token }) => (await got).extend({
prefixUrl: 'https://www.googleapis.com/drive/v3',
headers: {
authorization: `Bearer ${token}`,
},
})

async function getStats ({ id, token }) {
const client = getClient({ token })
const client = await getClient({ token })

const getStatsInner = async (statsOfId) => (
client.get(`files/${encodeURIComponent(statsOfId)}`, { searchParams: { fields: DRIVE_FILE_FIELDS, supportsAllDrives: true }, responseType: 'json' }).json()
Expand Down Expand Up @@ -66,7 +66,7 @@ class Drive extends Provider {
const isRoot = directory === 'root'
const isVirtualSharedDirRoot = directory === VIRTUAL_SHARED_DIR

const client = getClient({ token })
const client = await getClient({ token })

async function fetchSharedDrives (pageToken = null) {
const shouldListSharedDrives = isRoot && !query.cursor
Expand Down Expand Up @@ -136,7 +136,7 @@ class Drive extends Provider {
}

return withGoogleErrorHandling(Drive.authProvider, 'provider.drive.download.error', async () => {
const client = getClient({ token })
const client = await getClient({ token })

const { mimeType, id, exportLinks } = await getStats({ id: idIn, token })

Expand All @@ -152,7 +152,7 @@ class Drive extends Provider {
// Implemented based on the answer from StackOverflow: https://stackoverflow.com/a/59168288
const mimeTypeExportLink = exportLinks?.[mimeType2]
if (mimeTypeExportLink) {
const gSuiteFilesClient = got.extend({
const gSuiteFilesClient = (await got).extend({
headers: {
authorization: `Bearer ${token}`,
},
Expand Down

This file was deleted.

8 changes: 4 additions & 4 deletions packages/@uppy/companion/src/server/provider/google/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const got = require('got').default
const got = require('../../got')


const { withGoogleErrorHandling } = require('../providerErrors')
Expand All @@ -8,20 +8,20 @@ const { withGoogleErrorHandling } = require('../providerErrors')
* Reusable google stuff
*/

const getOauthClient = () => got.extend({
const getOauthClient = async () => (await got).extend({
prefixUrl: 'https://oauth2.googleapis.com',
})

async function refreshToken({ clientId, clientSecret, refreshToken: theRefreshToken }) {
return withGoogleErrorHandling('google', 'provider.google.token.refresh.error', async () => {
const { access_token: accessToken } = await getOauthClient().post('token', { responseType: 'json', form: { refresh_token: theRefreshToken, grant_type: 'refresh_token', client_id: clientId, client_secret: clientSecret } }).json()
const { access_token: accessToken } = await (await getOauthClient()).post('token', { responseType: 'json', form: { refresh_token: theRefreshToken, grant_type: 'refresh_token', client_id: clientId, client_secret: clientSecret } }).json()
return { accessToken }
})
}

async function logout({ token }) {
return withGoogleErrorHandling('google', 'provider.google.logout.error', async () => {
await got.post('https://accounts.google.com/o/oauth2/revoke', {
await (await got).post('https://accounts.google.com/o/oauth2/revoke', {
searchParams: { token },
responseType: 'json',
})
Expand Down
15 changes: 14 additions & 1 deletion packages/@uppy/companion/src/server/provider/providerErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,17 @@ async function withProviderErrorHandling({
}
}

module.exports = { withProviderErrorHandling }
async function withGoogleErrorHandling (providerName, tag, fn) {
return withProviderErrorHandling({
fn,
tag,
providerName,
isAuthError: (response) => (
response.statusCode === 401
|| (response.statusCode === 400 && response.body?.error === 'invalid_grant') // Refresh token has expired or been revoked
),
getJsonErrorMessage: (body) => body?.error?.message,
})
}

module.exports = { withProviderErrorHandling, withGoogleErrorHandling }

0 comments on commit 0dd7ac3

Please sign in to comment.