forked from cookieY/yee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basicAuth.go
65 lines (53 loc) 路 1.6 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
65
package middleware
import (
"encoding/base64"
"errors"
"net/http"
"strings"
"github.com/xuji-cny/yee"
)
// BasicAuthConfig defines the config of basicAuth middleware
type BasicAuthConfig struct {
Validator fnValidator
Realm string
}
type fnValidator func([]byte) (bool, error)
const (
basic = "basic"
)
// BasicAuth is the default implementation BasicAuth middleware
func BasicAuth(fn fnValidator) yee.HandlerFunc {
config := BasicAuthConfig{Validator: fn}
config.Realm = "."
return BasicAuthWithConfig(config)
}
// BasicAuthWithConfig is the custom implementation BasicAuth middleware
func BasicAuthWithConfig(config BasicAuthConfig) yee.HandlerFunc {
if config.Validator == nil {
panic("yee: basic-auth middleware requires a validator function")
}
return func(context yee.Context) (err error) {
decode, _ := parserVerifyData(context)
if verify, err := config.Validator(decode); err == nil && verify {
return err
}
context.Response().Header().Set(yee.HeaderWWWAuthenticate, basic+" realm="+config.Realm)
return context.ServerError(http.StatusUnauthorized, "invalid basic auth token")
}
}
func parserVerifyData(context yee.Context) ([]byte, error) {
var decode []byte
res := context.Request()
if res.Header.Get(yee.HeaderAuthorization) != "" {
auth := strings.Split(res.Header.Get(yee.HeaderAuthorization), " ")
if auth[0] == basic {
decode, err := base64.StdEncoding.DecodeString(auth[1])
if err != nil {
return decode, err
}
return decode, nil
}
return decode, errors.New("cannot get basic keyword")
}
return decode, errors.New("authorization header is empty")
}