aboutsummaryrefslogtreecommitdiff
path: root/auth_basic.go
diff options
context:
space:
mode:
authorQuentin <quentin@deuxfleurs.fr>2021-08-23 20:40:03 +0200
committerQuentin <quentin@deuxfleurs.fr>2021-08-23 20:40:03 +0200
commit15e4d10fd4b35a2e70cc4fa6ad4117cd5c402cbc (patch)
tree0fe31ac79d5dbe480b1b386845b3dd8fcaa55bd4 /auth_basic.go
parent5d64be33e64eb75160b6fe37b01997de7e96237a (diff)
downloadbagage-15e4d10fd4b35a2e70cc4fa6ad4117cd5c402cbc.tar.gz
bagage-15e4d10fd4b35a2e70cc4fa6ad4117cd5c402cbc.zip
Refactor the codebase
Diffstat (limited to 'auth_basic.go')
-rw-r--r--auth_basic.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/auth_basic.go b/auth_basic.go
new file mode 100644
index 0000000..f0cfaae
--- /dev/null
+++ b/auth_basic.go
@@ -0,0 +1,28 @@
+package main
+
+import (
+ "errors"
+ "net/http"
+)
+
+/*
+ * We extract the credentials from the Basic Auth headers
+ * (We may think to other ways to pass credentials such as a JWT)
+ */
+type BasicAuthExtract struct {
+ OnNotFound ErrorHandler
+ OnCreds CredsHandler
+}
+
+func (b BasicAuthExtract) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ username, password, ok := r.BasicAuth()
+ if !ok {
+ b.OnNotFound.WithError(errors.New("LDAP. Missing Authentication Header")).ServeHTTP(w, r)
+ return
+ }
+ if username == "" || password == "" {
+ b.OnNotFound.WithError(errors.New("LDAP. Username or password cannot be empty")).ServeHTTP(w, r)
+ return
+ }
+ b.OnCreds.WithCreds(username, password).ServeHTTP(w, r)
+}