refactor: use server for oauth

This commit is contained in:
EnixCoda 2020-04-09 15:03:12 +08:00
parent 3bbac6a6a8
commit 6355ef677d
No known key found for this signature in database
GPG key ID: FE06F5DFC1C9B8E4
5 changed files with 60 additions and 67 deletions

View file

@ -1,4 +1,3 @@
import { raiseError } from 'analytics'
import { AccessDeniedDescription } from 'components/AccessDeniedDescription'
import { FileExplorer } from 'components/FileExplorer'
import { MetaBar } from 'components/MetaBar'
@ -32,7 +31,7 @@ const RawGitako: React.FC<Props & ConnectorState> = function RawGitako(props) {
React.useEffect(() => {
const { init } = props
;(async function() {
;(async function () {
if (!accessToken) {
const accessToken = await trySetUpAccessTokenWithCode()
configContext.set({ access_token: accessToken || undefined })
@ -146,18 +145,15 @@ function AccessDeniedError({ hasToken }: { hasToken: boolean }) {
}
async function trySetUpAccessTokenWithCode() {
try {
const search = parseURLSearch()
if ('code' in search) {
const accessToken = await platform.setOAuth(search.code)
window.history.replaceState(
{},
'removed search param',
window.location.pathname.replace(window.location.search, ''),
)
return accessToken
}
} catch (err) {
raiseError(err)
const search = parseURLSearch()
if ('code' in search) {
const accessToken = await platform.setOAuth(search.code)
if (!accessToken) alert(`Gitako: The OAuth token has expired, please try again.`)
window.history.replaceState(
{},
'removed search param',
window.location.pathname.replace(window.location.search, ''),
)
return accessToken
}
}

View file

@ -1,3 +1,4 @@
import { raiseError } from 'analytics'
import { GITHUB_OAUTH } from 'env'
import { errors } from 'platforms'
import { JSONRequest } from 'utils/general'
@ -82,10 +83,42 @@ export async function getBlobData(
return await request(url, { accessToken })
}
export async function OAuth(code: string): Promise<GitHubAPI.OAuth> {
return await JSONRequest('https://github.com/login/oauth/access_token', {
export async function OAuth(code: string): Promise<string | null> {
try {
// TODO: deprecate legacy OAuth
if (+Date.now() < +new Date(2020, 5, 1)) return legacyOAuth(code)
else return safeOAuth(code)
} catch (err) {
return null
}
}
async function safeOAuth(code: string) {
const endpoint = `https://gitako.now.sh/oauth/github?`
const res = await fetch(endpoint + new URLSearchParams({ code }).toString(), {
method: 'post',
})
if (res.ok) {
const body = await res.json()
const accessToken = body?.accessToken
if (typeof accessToken === 'string') return accessToken
}
return null
}
async function legacyOAuth(code: string) {
const res = await JSONRequest('https://github.com/login/oauth/access_token', {
code,
client_id: GITHUB_OAUTH.clientId,
client_secret: GITHUB_OAUTH.clientSecret,
})
const { access_token: accessToken, scope, error_description: errorDescription } = res
if (errorDescription) {
raiseError(new Error(errorDescription))
} else if (scope !== 'repo' || !accessToken) {
raiseError(new Error(`Cannot resolve token response: '${JSON.stringify(res)}'`))
} else {
return accessToken
}
}

View file

@ -138,20 +138,8 @@ export const GitHub: Platform = {
getCurrentPath(branchName) {
return URLHelper.getCurrentPath(branchName)
},
async setOAuth(code) {
const res = await API.OAuth(code)
const { access_token: accessToken, scope, error_description: errorDescription } = res
if (errorDescription) {
if (errorDescription === `The code passed is incorrect or expired.`) {
alert(`Gitako: The OAuth token has expired, please try again.`)
return null
} else {
throw new Error(errorDescription)
}
} else if (scope !== 'repo' || !accessToken) {
throw new Error(`Cannot resolve token response: '${JSON.stringify(res)}'`)
}
return accessToken
setOAuth(code) {
return API.OAuth(code)
},
getOAuthLink() {
const params = new URLSearchParams({

View file

@ -1,5 +1,4 @@
import { raiseError } from 'analytics'
import { GITEE_OAUTH } from 'env'
import { errors } from 'platforms'
function isEmptyProject(content: any /* safe any */) {
@ -85,27 +84,16 @@ export async function getBlobData(
return await request(url, { accessToken })
}
export async function OAuth(code: string): Promise<GiteeAPI.OAuth> {
if (!GITEE_OAUTH.clientId || !GITEE_OAUTH.clientSecret)
throw new Error(`No Gitee OAuth credientials`)
const params = new URLSearchParams({
grant_type: 'authorization_code',
code: code,
client_id: GITEE_OAUTH.clientId,
client_secret: GITEE_OAUTH.clientSecret,
})
const res = await fetch('https://gitee.com/oauth/token?' + params.toString(), {
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
redirect: 'follow',
referrerPolicy: 'no-referrer',
export async function OAuth(code: string): Promise<string | null> {
const endpoint = 'https://gitako.now.sh/oauth/gitee?'
const res = await fetch(endpoint + new URLSearchParams({ code }).toString(), {
method: 'post',
})
return res.json()
if (res.ok) {
const body = await res.json()
const accessToken = body?.accessToken
if (typeof accessToken === 'string') return accessToken
}
return null
}

View file

@ -149,20 +149,8 @@ export const Gitee: Platform = {
})
return `https://gitee.com/oauth/authorize?` + params.toString()
},
async setOAuth(code) {
const res = await API.OAuth(code)
const { access_token: accessToken, scope, error_description: errorDescription } = res
if (errorDescription) {
if (errorDescription === `The code passed is incorrect or expired.`) {
alert(`Gitako: The OAuth token has expired, please try again.`)
return null
} else {
throw new Error(errorDescription)
}
} else if (scope !== 'repo' || !accessToken) {
throw new Error(`Cannot resolve token response: '${JSON.stringify(res)}'`)
}
return accessToken
setOAuth(code) {
return API.OAuth(code)
},
}