diff --git a/server/api/gitea.ts b/server/api/gitea.ts new file mode 100644 index 0000000..5f9901a --- /dev/null +++ b/server/api/gitea.ts @@ -0,0 +1,36 @@ +import fetch from 'node-fetch' +import { createCodeHandler } from './utils' + +const { GITEA_OAUTH_CLIENT_ID = '', GITEA_OAUTH_CLIENT_SECRET = '' } = process.env + +async function oauth(code: string) { + const res = await fetch('https://gitea.com/login/oauth/access_token', { + headers: { + 'Content-Type': 'application/json', + Accept: 'application/json', + }, + redirect: 'follow', + method: 'post', + body: JSON.stringify({ + code, + client_id: GITEA_OAUTH_CLIENT_ID, + client_secret: GITEA_OAUTH_CLIENT_SECRET, + grant_type: 'authorization_code', + redirect_uri: encodeURIComponent('https://gitako.now.sh'), + }), + }) + + const body = await res.json() + const { + access_token: accessToken, + token_type: tokenType, + expires_in: expiresIn, + refresh_token: refreshToken, + } = body + if (!accessToken || !(typeof accessToken === 'string')) { + throw new Error(`Cannot resolve response: '${JSON.stringify(res)}'`) + } + return accessToken +} + +export default createCodeHandler(oauth) diff --git a/server/vercel.json b/server/vercel.json index a41e547..5920c5a 100644 --- a/server/vercel.json +++ b/server/vercel.json @@ -4,6 +4,7 @@ { "src": "/redirect/?", "dest": "/api/redirect.ts" }, { "src": "/oauth/github/?", "dest": "/api/github.ts" }, { "src": "/oauth/gitee/?", "dest": "/api/gitee.ts" }, + { "src": "/oauth/gitea/?", "dest": "/api/gitea.ts" }, { "src": "/", "dest": "/api/index.ts" } ] } diff --git a/src/env.ts b/src/env.ts index a013dc3..31a37ff 100644 --- a/src/env.ts +++ b/src/env.ts @@ -8,4 +8,8 @@ export const GITEE_OAUTH = { clientId: process.env.GITEE_OAUTH_CLIENT_ID || '', } +export const GITEA_OAUTH = { + clientId: process.env.GITEA_OAUTH_CLIENT_ID || '', +} + export const VERSION = process.env.VERSION diff --git a/src/platforms/GitHub/API.ts b/src/platforms/GitHub/API.ts index 083912e..966c5e2 100644 --- a/src/platforms/GitHub/API.ts +++ b/src/platforms/GitHub/API.ts @@ -155,8 +155,6 @@ export async function OAuth(code: string): Promise { const accessToken = body?.accessToken if (typeof accessToken === 'string') return accessToken } - return null - } catch (err) { - return null - } + } catch (err) {} + return null } diff --git a/src/platforms/Gitea/API.ts b/src/platforms/Gitea/API.ts index 2a7a849..04ee58c 100644 --- a/src/platforms/Gitea/API.ts +++ b/src/platforms/Gitea/API.ts @@ -93,15 +93,16 @@ export async function getBlobData( } export async function OAuth(code: string): Promise { - const endpoint = `https://gitako.now.sh/oauth/gitea?` + try { + const endpoint = `https://gitako.now.sh/oauth/gitea?` 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 + } catch (err) {} + return null } diff --git a/src/platforms/Gitea/index.ts b/src/platforms/Gitea/index.ts index 783b18f..56053d5 100644 --- a/src/platforms/Gitea/index.ts +++ b/src/platforms/Gitea/index.ts @@ -1,4 +1,4 @@ -import { platform } from 'platforms' +import { GITEA_OAUTH } from 'env' import { resolveGitModules } from 'utils/gitSubmodule' import { sortFoldersToFront } from 'utils/treeParser' import * as API from './API' @@ -76,7 +76,7 @@ function getUrlForRedirect( export const Gitea: Platform = { isEnterprise() { - return false; + return false }, resolveMeta() { if (!DOMHelper.isInRepoPage()) { @@ -107,6 +107,9 @@ export const Gitea: Platform = { const { userName, repoName, branchName } = metaData const treeData = await API.getTreeData(userName, repoName, branchName, recursive) + // No APIs for PR files found + // if (pullId) {} + const root = processTree( treeData.tree.map(item => ({ path: item.path || '', @@ -155,6 +158,12 @@ export const Gitea: Platform = { return API.OAuth(code) }, getOAuthLink() { - return `https://${window.location.host}/api/v1/user/applications/oauth2` + const params = new URLSearchParams({ + client_id: GITEA_OAUTH.clientId, + // scope: 'repo', // not supported by Gitea yet + redirect_uri: window.location.href, + response_type: 'code', + }) + return `https://${window.location.host}/login/oauth/authorize?` + params.toString() }, -} \ No newline at end of file +}