-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
53 lines (42 loc) · 966 Bytes
/
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
package apiclient
import (
"fmt"
"net/http"
)
type Authenticator interface {
AuthenticateRequest(req *http.Request) error
}
type QueryArgAuth struct {
arg string
key string
}
func NewQueryArgAuth(arg, key string) *QueryArgAuth {
return &QueryArgAuth{arg: arg, key: key}
}
func (a *QueryArgAuth) AuthenticateRequest(req *http.Request) error {
u := req.URL
v := u.Query()
v.Set(a.arg, a.key)
u.RawQuery = v.Encode()
return nil
}
type BearerAuth string
func NewBearerAuth(token string) *BearerAuth {
a := BearerAuth(token)
return &a
}
func (a *BearerAuth) AuthenticateRequest(req *http.Request) error {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", string(*a)))
return nil
}
type BasicAuth struct {
user string
pwd string
}
func NewBasicAuth(user, pwd string) *BasicAuth {
return &BasicAuth{user: user, pwd: pwd}
}
func (a *BasicAuth) AuthenticateRequest(req *http.Request) error {
req.SetBasicAuth(a.user, a.pwd)
return nil
}