-
Notifications
You must be signed in to change notification settings - Fork 28
/
basic.go
54 lines (43 loc) · 1.07 KB
/
basic.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
package basic
import (
"errors"
"net/http"
"strings"
)
// Token implementation for Basic authentication
type Token struct {
T string
}
// Method is set to "Basic"
func (tk Token) Method() string {
return "Basic"
}
// String form of Basic token
func (tk Token) String() string {
return tk.T
}
// ExpiresIn is set to 0 for Basic authentication
func (tk Token) ExpiresIn() int {
return 0
}
func getTokenFromHeader(header string) string {
fields := strings.Split(header, " ")
return fields[1]
}
// RequestToken performs Basic authentication and extracts token from response header
func RequestToken(url, username, password string) (*Token, error) {
hc := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.SetBasicAuth(username, password)
resp, err := hc.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 && resp.StatusCode != 403 {
return nil, errors.New("[AUTH::BASIC] Bad response status: " + resp.Status + " >> " + url)
}
return &Token{T: getTokenFromHeader(req.Header["Authorization"][0])}, nil
}