aboutsummaryrefslogtreecommitdiff
path: root/auth_basic.go
blob: f0cfaae81c574d30d708b690b606f576ecb57b7a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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)
}