|
|
|
|
@ -2,7 +2,7 @@ import * as cookie from 'https://deno.land/std@0.188.0/http/cookie.ts'
|
|
|
|
|
|
|
|
|
|
export class Auth {
|
|
|
|
|
constructor({
|
|
|
|
|
baseUrl,
|
|
|
|
|
basePath,
|
|
|
|
|
remoteBaseUrl,
|
|
|
|
|
giteaAppBaseUrl,
|
|
|
|
|
giteaApiBaseUrl,
|
|
|
|
|
@ -10,7 +10,7 @@ export class Auth {
|
|
|
|
|
giteaClientId,
|
|
|
|
|
giteaClientSecret
|
|
|
|
|
}) {
|
|
|
|
|
this.baseUrl = baseUrl
|
|
|
|
|
this.basePath = basePath
|
|
|
|
|
this.remoteBaseUrl = remoteBaseUrl
|
|
|
|
|
this.giteaAppBaseUrl = giteaAppBaseUrl
|
|
|
|
|
this.giteaApiBaseUrl = giteaApiBaseUrl
|
|
|
|
|
@ -50,8 +50,10 @@ export class Auth {
|
|
|
|
|
Location: url
|
|
|
|
|
})
|
|
|
|
|
cookie.setCookie(headers, {
|
|
|
|
|
...this.cookieSettings,
|
|
|
|
|
name: 'oauth.gitea.state',
|
|
|
|
|
value: state,
|
|
|
|
|
maxAge: 600,
|
|
|
|
|
})
|
|
|
|
|
event.respondWith(new Response('', {
|
|
|
|
|
headers,
|
|
|
|
|
@ -73,6 +75,12 @@ export class Auth {
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get cookieSettings() {
|
|
|
|
|
return {
|
|
|
|
|
path: `${this.basePath}/api`,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getToken(code) {
|
|
|
|
|
const resp = await fetch(this.tokenEndpoint, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
@ -93,14 +101,17 @@ export class Auth {
|
|
|
|
|
|
|
|
|
|
saveTokens(headers, data) {
|
|
|
|
|
cookie.setCookie(headers, {
|
|
|
|
|
...this.cookieSettings,
|
|
|
|
|
name: 'oauth.gitea.accessToken',
|
|
|
|
|
value: data.access_token,
|
|
|
|
|
})
|
|
|
|
|
cookie.setCookie(headers, {
|
|
|
|
|
...this.cookieSettings,
|
|
|
|
|
name: 'oauth.gitea.refreshToken',
|
|
|
|
|
value: data.refresh_token,
|
|
|
|
|
})
|
|
|
|
|
cookie.setCookie(headers, {
|
|
|
|
|
...this.cookieSettings,
|
|
|
|
|
name: 'oauth.gitea.expires',
|
|
|
|
|
value: String(
|
|
|
|
|
Math.floor(new Date().valueOf() / 1000) +
|
|
|
|
|
@ -165,12 +176,12 @@ export class Auth {
|
|
|
|
|
|
|
|
|
|
async serve(event) {
|
|
|
|
|
const {pathname} = new URL(event.request.url)
|
|
|
|
|
const u = this.baseUrl
|
|
|
|
|
if (pathname === `${u}/api/auth`) {
|
|
|
|
|
const b = this.basePath
|
|
|
|
|
if (pathname === `${b}/api/auth`) {
|
|
|
|
|
await this.redirect(event)
|
|
|
|
|
} else if (pathname === `${u}/api/auth/callback`) {
|
|
|
|
|
} else if (pathname === `${b}/api/auth/callback`) {
|
|
|
|
|
await this.callback(event)
|
|
|
|
|
} else if (pathname === `${u}/api/auth/refresh`) {
|
|
|
|
|
} else if (pathname === `${b}/api/auth/refresh`) {
|
|
|
|
|
await this.refresh(event)
|
|
|
|
|
} else {
|
|
|
|
|
event.respondWith(new Response(
|
|
|
|
|
|