-
Notifications
You must be signed in to change notification settings - Fork 38
/
basic_auth.go
52 lines (37 loc) · 1.24 KB
/
basic_auth.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
package mango
import (
"encoding/base64"
"errors"
"strings"
)
func defaultFailure() (Status, Headers, Body) {
return 401, Headers{"WWW-Authenticate": []string{"Basic realm=\"Basic\""}, "Content-Type": []string{"text/html"}}, Body("Access Denied.") // default failure page
}
func BasicAuth(auth func(string, string, Request, error) bool, failure func(Env) (Status, Headers, Body)) Middleware {
return func(env Env, app App) (Status, Headers, Body) {
if auth == nil { // fail auth by default if you use this middleware
return defaultFailure()
}
username, password, err := getAuth(env.Request())
if auth(username, password, *env.Request(), err) { // check users auth function
return app(env)
}
if failure == nil { // if no special failure function
return defaultFailure()
}
return failure(env)
}
}
// get username and password from header
func getAuth(req *Request) (string, string, error) {
auth64 := req.Header.Get("Authorization")
if auth64 == "" {
return "", "", errors.New("No Authorization Header")
}
auth, err := base64.StdEncoding.DecodeString(strings.Replace(auth64, "Basic ", "", 1))
if err != nil {
return "", "", err
}
result := strings.Split(string(auth), ":")
return result[0], result[1], nil
}