mirror of
https://github.com/EnixCoda/Gitako.git
synced 2026-03-11 08:54:44 +00:00
WIP: OAuth
This commit is contained in:
parent
33d270a671
commit
546e53b32e
6 changed files with 60 additions and 11 deletions
36
server/api/gitea.ts
Normal file
36
server/api/gitea.ts
Normal file
|
|
@ -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)
|
||||
|
|
@ -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" }
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -155,8 +155,6 @@ export async function OAuth(code: string): Promise<string | null> {
|
|||
const accessToken = body?.accessToken
|
||||
if (typeof accessToken === 'string') return accessToken
|
||||
}
|
||||
return null
|
||||
} catch (err) {
|
||||
return null
|
||||
}
|
||||
} catch (err) {}
|
||||
return null
|
||||
}
|
||||
|
|
|
|||
|
|
@ -93,15 +93,16 @@ export async function getBlobData(
|
|||
}
|
||||
|
||||
export async function OAuth(code: string): Promise<string | null> {
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue