Added x-api-key auth support

main
Jef Roosens 2022-01-11 21:18:33 +01:00
parent a43ebfeaf0
commit 2663cf3dae
Signed by untrusted user: Jef Roosens
GPG Key ID: 955C0660072F691F
2 changed files with 15 additions and 0 deletions

11
vieter/auth.v 100644
View File

@ -0,0 +1,11 @@
module main
import net.http
fn (mut app App) is_authorized() bool {
x_header := app.req.header.get_custom('X-Api-Key', http.HeaderQueryConfig{ exact: true }) or {
return false
}
return x_header.trim_space() == app.api_key
}

View File

@ -38,6 +38,10 @@ fn (mut app App) get_root(filename string) web.Result {
['/pkgs/:pkg'; put] ['/pkgs/:pkg'; put]
fn (mut app App) put_package(pkg string) web.Result { fn (mut app App) put_package(pkg string) web.Result {
if !app.is_authorized() {
return app.text('Unauthorized.')
}
if !is_pkg_name(pkg) { if !is_pkg_name(pkg) {
app.lwarn("Invalid package name '$pkg'.") app.lwarn("Invalid package name '$pkg'.")