-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcredentials_basicauth.go
45 lines (40 loc) · 1.43 KB
/
credentials_basicauth.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
package goauth
import (
"net/http"
"net/url"
"strings"
"github.com/grokify/goauth/authutil"
"github.com/grokify/mogo/net/http/httpsimple"
"github.com/grokify/mogo/net/http/httputilmore"
)
type CredentialsBasicAuth struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
Encoded string `json:"encoded,omitempty"`
ServerURL string `json:"serverURL,omitempty"`
AllowInsecure bool `json:"allowInsecure,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
}
func (c *CredentialsBasicAuth) NewClient() (*http.Client, error) {
if len(strings.TrimSpace(c.Encoded)) > 0 {
if strings.Index(strings.ToLower(strings.TrimSpace(c.Encoded)), "basic ") == 0 {
return authutil.NewClientHeaderQuery(
http.Header{httputilmore.HeaderAuthorization: []string{c.Encoded}},
url.Values{},
c.AllowInsecure), nil
}
return authutil.NewClientToken(authutil.TokenBasic, c.Encoded, c.AllowInsecure), nil
} else if len(c.Username) > 0 || len(c.Password) > 0 {
return authutil.NewClientBasicAuth(c.Username, c.Password, c.AllowInsecure)
}
return &http.Client{}, nil
}
func (c *CredentialsBasicAuth) NewSimpleClient() (httpsimple.Client, error) {
hclient, err := c.NewClient()
if err != nil {
return httpsimple.Client{}, err
}
return httpsimple.Client{
HTTPClient: hclient,
BaseURL: c.ServerURL}, nil
}