fix path of cookies

shared-server
bat 3 years ago
parent 57eea2e79f
commit 3f09fab2ef

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

@ -32,7 +32,7 @@ export class Server {
'GITEA_CLIENT_SECRET', 'GITEA_CLIENT_SECRET',
]) ])
this.port = env.PORT ?? 3000 this.port = env.PORT ?? 3000
this.baseUrl = env.BASE_URL ?? '/macchiato' this.basePath = env.BASE_PATH ?? '/macchiato'
this.remoteBaseUrl = env.REMOTE_BASE_URL this.remoteBaseUrl = env.REMOTE_BASE_URL
this.giteaAppBaseUrl = ( this.giteaAppBaseUrl = (
env.GITEA_APP_BASE_URL ?? 'http://gitea:3000' env.GITEA_APP_BASE_URL ?? 'http://gitea:3000'
@ -50,7 +50,7 @@ export class Server {
await this.configure() await this.configure()
} }
this.auth = new Auth({ this.auth = new Auth({
baseUrl: this.baseUrl, basePath: this.basePath,
remoteBaseUrl: this.remoteBaseUrl, remoteBaseUrl: this.remoteBaseUrl,
giteaAppBaseUrl: this.giteaAppBaseUrl, giteaAppBaseUrl: this.giteaAppBaseUrl,
giteaApiBaseUrl: this.giteaApiBaseUrl, giteaApiBaseUrl: this.giteaApiBaseUrl,
@ -127,14 +127,14 @@ export class Server {
async serveRequest(event) { async serveRequest(event) {
const {pathname: p} = new URL(event.request.url) const {pathname: p} = new URL(event.request.url)
const base = this.baseUrl const base = this.basePath
if (p.startsWith(`${base}/api/auth`)) { if (p.startsWith(`${base}/api/auth`)) {
await this.auth.serve(event) await this.auth.serve(event)
} else if (p.startsWith(`${base}/api/storage`)) { } else if (p.startsWith(`${base}/api/storage`)) {
const {allow, headers} = ( const {allow, headers} = (
await this.auth.requireAuth(event) await this.auth.requireAuth(event)
) )
if (allowed) { if (allow) {
await this.storage.serve(event, headers) await this.storage.serve(event, headers)
} }
} else { } else {

Loading…
Cancel
Save