1
0
mirror of https://github.com/rancher/norman.git synced 2025-09-09 02:59:19 +00:00

Feat: Added token suport to clientbase auth

This commit is contained in:
Raul Sanchez
2018-02-05 16:04:41 +01:00
committed by Darren Shepherd
parent 83916ae78f
commit aa9c66a519
2 changed files with 16 additions and 5 deletions

View File

@@ -32,11 +32,23 @@ type ClientOpts struct {
URL string
AccessKey string
SecretKey string
TokenKey string
Timeout time.Duration
HTTPClient *http.Client
CACerts string
}
func (c *ClientOpts) getAuthHeader() string {
if c.TokenKey != "" {
return "Bearer " + c.TokenKey
}
if c.AccessKey != "" && c.SecretKey != "" {
s := c.AccessKey + ":" + c.SecretKey
return "Basic " + base64.StdEncoding.EncodeToString([]byte(s))
}
return ""
}
type APIError struct {
StatusCode int
URL string
@@ -169,7 +181,7 @@ func NewAPIClient(opts *ClientOpts) (APIBaseClient, error) {
return result, err
}
req.SetBasicAuth(opts.AccessKey, opts.SecretKey)
req.Header.Add("Authorization", opts.getAuthHeader())
resp, err := client.Do(req)
if err != nil {
@@ -188,7 +200,7 @@ func NewAPIClient(opts *ClientOpts) (APIBaseClient, error) {
if schemasURLs != opts.URL {
req, err = http.NewRequest("GET", schemasURLs, nil)
req.SetBasicAuth(opts.AccessKey, opts.SecretKey)
req.Header.Add("Authorization", opts.getAuthHeader())
if err != nil {
return result, err
}
@@ -243,8 +255,7 @@ func (a *APIBaseClient) Websocket(url string, headers map[string][]string) (*web
}
if a.Opts != nil {
s := a.Opts.AccessKey + ":" + a.Opts.SecretKey
httpHeaders.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(s)))
httpHeaders.Add("Authorization", a.Opts.getAuthHeader())
}
return dialer.Dial(url, http.Header(httpHeaders))