This repository has been archived by the owner on Jul 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
auth.go
87 lines (76 loc) · 2.36 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package hero
import (
"encoding/base64"
"errors"
"net/http"
"strings"
)
//basicAuth stores information for basic authentication of oauth clients. Basic authentication
// involves username and password.
type basicAuth struct {
UserName string
Password string
}
//bererAuth stores the code for authenciating clients using bearer token.
type bearerAuth struct {
Code string
}
//getClientAuth returns the basic authentication details from the given request. if the allowParams is
// set to true then the basic auth information will be extracted from the request query parameters.
// Make sure you call r.Parse() before calling this, so as to make the query params available in r.Form
//
// Default the details are extracted from the request header.
func getCLientAuth(r *http.Request, allowQueryParams bool) (*basicAuth, error) {
auth := &basicAuth{
UserName: r.Form.Get("client_id"),
Password: r.Form.Get("client_secret"),
}
if allowQueryParams && auth.Password != "" && auth.UserName != "" {
return auth, nil
}
return checkBasicAuth(r)
}
// checkBasiAuth returns basic client athentication details from the given request. The information is
// extracted from the request header.
func checkBasicAuth(r *http.Request) (*basicAuth, error) {
var (
basic = "Basic"
authorize = "Authorization"
errInvalidUthorizeHader = errors.New("Invalid authorization header")
)
authHeader := r.Header.Get(authorize)
components := strings.SplitN(authHeader, " ", 2)
if len(components) != 2 || components[0] != basic {
return nil, errInvalidUthorizeHader
}
base, err := base64.StdEncoding.DecodeString(components[1])
if err != nil {
return nil, err
}
keyPairs := strings.SplitN(string(base), ":", 2)
if len(keyPairs) != 2 {
return nil, errInvalidUthorizeHader
}
return &basicAuth{keyPairs[0], keyPairs[1]}, nil
}
//checkBearerAuth checks for bearer token in the request header.
func checkBearerAuth(r *http.Request) *bearerAuth {
var (
auth = "Authorization"
code = "code"
bearer = "Bearer"
)
authHeader := r.Header.Get(auth)
authCode := r.Form.Get(code)
if authHeader == "" && authCode == "" {
return nil
}
if authHeader != "" {
components := strings.SplitN(authHeader, " ", 2)
if (len(components) != 2 || components[0] != bearer) && authCode == "" {
return nil
}
authCode = components[1]
}
return &bearerAuth{authCode}
}