Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
Support github.dev redirect URL
Browse files Browse the repository at this point in the history
  • Loading branch information
znck authored Feb 2, 2023
1 parent 67d6bd0 commit 541cfc9
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions redirect/functions/redirect.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
// @ts-check
const schemes = new Set(['vscode', 'vscode-insiders', 'vscodium', 'gitpod-code', 'code-oss'])
const validQueryParams = new Set(['vscode-reqid', 'vscode-scheme', 'vscode-authority', 'vscode-path', 'windowId'])
let schemes = new Set([
'vscode',
'vscode-insiders',
'vscodium',
'gitpod-code',
'code-oss',
])
let validQueryParams = new Set([
'vscode-reqid',
'vscode-scheme',
'vscode-authority',
'vscode-path',
'windowId',
])

/**
* @param {import('@netlify/functions').HandlerEvent} event
Expand All @@ -12,7 +24,12 @@ exports.handler = async function (event, _context) {
if (state == null) throw new Error(`Missing "state" query parameter.`)
const url = new URL(Buffer.from(state, 'base64url').toString())
const scheme = url.protocol.slice(0, -1)
if (scheme === 'http' || scheme === 'https') {
if (
url.host === 'https://github.dev' &&
url.pathname === '/extension-auth-callback'
) {
validQueryParams = new Set(['state'])
} else if (scheme === 'http' || scheme === 'https') {
validate(
url.searchParams.get('vscode-scheme'),
url.searchParams.get('vscode-authority'),
Expand All @@ -22,20 +39,20 @@ exports.handler = async function (event, _context) {
validate(scheme, url.host, url.pathname)
}

url.searchParams.set('code', code)
url.searchParams.forEach((_, key) => {
if (!validQueryParams.has(key)) url.searchParams.delete(key)
})

url.searchParams.set('code', code)
url.searchParams.set('state', '')

return getResponse(url.toString())
}

function validate(scheme, hostname, pathname) {
if (!schemes.has(scheme)) throw new Error(`Invalid scheme: ${scheme}`)
if (hostname !== 'znck.grammarly') throw new Error(`Invalid authority: ${hostname}`)
if (pathname !== '/auth/callback') throw new Error(`Invalid path: ${pathname}`)
if (hostname !== 'znck.grammarly')
throw new Error(`Invalid authority: ${hostname}`)
if (pathname !== '/auth/callback')
throw new Error(`Invalid path: ${pathname}`)
}

/**
Expand Down

0 comments on commit 541cfc9

Please sign in to comment.