You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.4 KiB
JavaScript
58 lines
1.4 KiB
JavaScript
import * as cookie from 'https://deno.land/std@0.188.0/http/cookie.ts'
|
|
|
|
export class Auth {
|
|
constructor({
|
|
baseUrl,
|
|
remoteBaseUrl,
|
|
giteaApiBaseUrl,
|
|
giteaWebBaseUrl,
|
|
giteaClientId,
|
|
giteaClientSecret
|
|
}) {
|
|
this.baseUrl = baseUrl
|
|
this.remoteBaseUrl = remoteBaseUrl
|
|
this.giteaApiBaseUrl = giteaApiBaseUrl
|
|
this.giteaWebBaseUrl = giteaWebBaseUrl
|
|
this.giteaClientId = giteaClientId
|
|
this.giteaClientSecret = giteaClientSecret
|
|
}
|
|
|
|
redirectUrl(state) {
|
|
const url = new URL(
|
|
this.giteaWebBaseUrl + '/login/oauth/authorize'
|
|
)
|
|
const search = new URLSearchParams()
|
|
search.set('response_type', 'code')
|
|
search.set('client_id', this.giteaClientId)
|
|
search.set(
|
|
'redirect_uri',
|
|
this.remoteBaseUrl + '/api/auth/callback'
|
|
)
|
|
search.set('state', state)
|
|
url.search = search.toString()
|
|
return url.toString()
|
|
}
|
|
|
|
buildState() {
|
|
const timestamp = new Date().valueOf()
|
|
const randomInt = Math.floor(Math.random() * 10000)
|
|
// TODO: sign
|
|
return `${randomInt}-${timestamp}`
|
|
}
|
|
|
|
async redirect(event) {
|
|
const state = this.buildState()
|
|
const url = this.redirectUrl(state)
|
|
const headers = new Headers({
|
|
Location: url
|
|
})
|
|
cookie.setCookie(headers, {
|
|
name: 'oauth.gitea.state',
|
|
value: state,
|
|
})
|
|
event.respondWith(new Response('', {
|
|
headers,
|
|
status: 302,
|
|
}))
|
|
}
|
|
} |