-
Notifications
You must be signed in to change notification settings - Fork 3
/
header.go
50 lines (38 loc) · 1015 Bytes
/
header.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
package header
import (
"errors"
"strings"
)
const (
// BasicAuthorization scheme.
BasicAuthorization = "Basic"
// BearerAuthorization scheme.
BearerAuthorization = "Bearer"
)
var (
// AllAuthorizations supported by the header.
AllAuthorizations = []string{BasicAuthorization, BearerAuthorization}
// ErrInvalidAuthorization header.
ErrInvalidAuthorization = errors.New("authorization is invalid")
// ErrNotSupportedScheme for http.
ErrNotSupportedAuthorization = errors.New("authorization is not supported")
)
// ParseAuthorization header into type and credentials or error.
func ParseAuthorization(header string) (string, string, error) {
s := strings.Split(header, " ")
if len(s) != 2 {
return "", "", ErrInvalidAuthorization
}
if !containsAuthorization(s[0]) {
return "", "", ErrNotSupportedAuthorization
}
return s[0], s[1], nil
}
func containsAuthorization(scheme string) bool {
for _, v := range AllAuthorizations {
if v == scheme {
return true
}
}
return false
}