-
Notifications
You must be signed in to change notification settings - Fork 1
/
basicauth.go
64 lines (56 loc) · 1.4 KB
/
basicauth.go
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package app
import (
"bufio"
"fmt"
"net/http"
"os"
"strings"
)
// BasicAuthFromFile opens a file and sets a global user/password if a line with two words is found
func BasicAuthFromFile(filePath string) error {
f, err := os.Open(filePath)
if err != nil {
return err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
l := scanner.Text()
if l != "" {
words := strings.Fields(l)
if len(words) == 2 {
Config.AuthUser = words[0]
Config.AuthPass = words[1]
}
}
if Config.AuthUser == "" || Config.AuthPass == "" {
return fmt.Errorf("Basic auth user or password not found in %s\nThe format should be <user> <password>", filePath)
}
}
err = scanner.Err()
return err
}
// basicAuthWrapper uses login details
func basicAuthWrapper(handler http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if Config.AuthUser != "" && Config.AuthPass != "" {
user, pass, ok := r.BasicAuth()
if !ok {
basicAuthResponse(w)
return
}
if user != Config.AuthUser || pass != Config.AuthPass {
basicAuthResponse(w)
return
}
}
// pass on request
handler(w, r)
}
}
// BasicAuthResponse returns an basic auth response to the browser
func basicAuthResponse(w http.ResponseWriter) {
w.Header().Set("WWW-Authenticate", `Basic realm="Login"`)
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Unauthorised.\n"))
}