|
|
|
|
import * as cookie from 'https://deno.land/std@0.188.0/http/cookie.ts'
|
|
|
|
|
|
|
|
|
|
export class Auth {
|
|
|
|
|
constructor({
|
|
|
|
|
baseUrl,
|
|
|
|
|
remoteBaseUrl,
|
|
|
|
|
giteaAppBaseUrl,
|
|
|
|
|
giteaClientId,
|
|
|
|
|
giteaClientSecret
|
|
|
|
|
}) {
|
|
|
|
|
this.baseUrl = baseUrl
|
|
|
|
|
this.remoteBaseUrl = remoteBaseUrl
|
|
|
|
|
this.giteaAppBaseUrl = giteaAppBaseUrl
|
|
|
|
|
this.giteaClientId = giteaClientId
|
|
|
|
|
this.giteaClientSecret = giteaClientSecret
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async redirect(event) {
|
|
|
|
|
const url = new URL(
|
|
|
|
|
this.giteaAppBaseUrl + '/login/oauth/authorize'
|
|
|
|
|
)
|
|
|
|
|
const search = new URLSearchParams()
|
|
|
|
|
search.set('response_type', 'code')
|
|
|
|
|
search.set('client_id', this.giteaClientId)
|
|
|
|
|
search.set(
|
|
|
|
|
'redirect_uri',
|
|
|
|
|
this.remoteBaseUrl + '/auth/callback'
|
|
|
|
|
)
|
|
|
|
|
const timestamp = new Date().valueOf()
|
|
|
|
|
const randomInt = Math.floor(Math.random() * 10000)
|
|
|
|
|
// TODO: sign
|
|
|
|
|
const state = `${randomInt}-${timestamp}`
|
|
|
|
|
search.set('state', state)
|
|
|
|
|
url.search = search.toString()
|
|
|
|
|
const headers = new Headers({
|
|
|
|
|
Location: url.toString()
|
|
|
|
|
})
|
|
|
|
|
cookie.setCookie(headers, {
|
|
|
|
|
name: 'oauth.gitea.state',
|
|
|
|
|
value: state,
|
|
|
|
|
})
|
|
|
|
|
return new Response('', {
|
|
|
|
|
headers,
|
|
|
|
|
status: 302,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|