-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
35 lines (29 loc) · 809 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
package client
import (
"net/http"
"strings"
)
// Authenticator is the interface all auth methods must satisfy.
type Authenticator interface {
Set(*http.Request)
}
// BasicAuth is an implementation of the Authenticator interface for
// username/password authentication.
type BasicAuth struct {
Username string
Password string
}
// TokenAuth is an implementation of the Authenticator interface for token
// authentication.
type TokenAuth struct {
Token string
}
// Set adds an authentication header to a request.
func (a *BasicAuth) Set(req *http.Request) {
req.SetBasicAuth(a.Username, a.Password)
}
// Set add an authentication header to a request.
func (a *TokenAuth) Set(req *http.Request) {
bearer := strings.Join([]string{"bearer", a.Token}, " ")
req.Header.Set("Authorization", bearer)
}