|
|
|
|
@ -66,6 +66,13 @@ export class Auth {
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get userinfoEndpoint() {
|
|
|
|
|
return (
|
|
|
|
|
this.giteaAppBaseUrl +
|
|
|
|
|
'/login/oauth/userinfo'
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getToken(code) {
|
|
|
|
|
const resp = await fetch(this.tokenEndpoint, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
@ -140,7 +147,7 @@ export class Auth {
|
|
|
|
|
grant_type: 'refresh_token',
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
return await resp.json()
|
|
|
|
|
return resp.ok
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async refresh(event) {
|
|
|
|
|
@ -148,7 +155,8 @@ export class Auth {
|
|
|
|
|
const cookies = cookie.getCookies(
|
|
|
|
|
event.request.headers
|
|
|
|
|
)
|
|
|
|
|
const data = await this.refreshToken(body)
|
|
|
|
|
const token = cookies['oauth.gitea.refreshToken']
|
|
|
|
|
const data = await this.refreshToken(token)
|
|
|
|
|
this.saveTokens(headers, data)
|
|
|
|
|
event.respondWith(
|
|
|
|
|
new Response(JSON.stringify({}), {headers})
|
|
|
|
|
@ -170,4 +178,53 @@ export class Auth {
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async userInfo(token) {
|
|
|
|
|
const resp = await fetch(this.userinfoEndpoint, {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
headers: {
|
|
|
|
|
Accept: 'application/json',
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
Authorization: `Bearer ${token}`,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
return {
|
|
|
|
|
resp,
|
|
|
|
|
json: await resp.json()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async requireAuth(event) {
|
|
|
|
|
const headers = new Headers()
|
|
|
|
|
const cookies = cookie.getCookies(
|
|
|
|
|
event.request.headers
|
|
|
|
|
)
|
|
|
|
|
const token = cookies['oauth.gitea.accessToken']
|
|
|
|
|
if (!token) {
|
|
|
|
|
event.respondWith(Response.json(
|
|
|
|
|
{error: 'Token missing'}, {status: 401}
|
|
|
|
|
))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const {resp, json} = await this.userInfo(token)
|
|
|
|
|
if (!resp.ok) {
|
|
|
|
|
const refresh = cookies['oauth.gitea.refreshToken']
|
|
|
|
|
if (refresh) {
|
|
|
|
|
const data = await this.refreshToken(token)
|
|
|
|
|
this.saveTokens(headers, data)
|
|
|
|
|
const token = data.access_token
|
|
|
|
|
if (token) {
|
|
|
|
|
const {resp, json} = await this.userInfo(token)
|
|
|
|
|
return {
|
|
|
|
|
allow: resp.ok,
|
|
|
|
|
headers,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
allow: resp.ok,
|
|
|
|
|
headers,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|