1
0
mirror of https://github.com/rancher/norman.git synced 2025-09-09 11:09:59 +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))

View File

@@ -14,7 +14,7 @@ import (
)
func (a *APIOperations) setupRequest(req *http.Request) {
req.SetBasicAuth(a.Opts.AccessKey, a.Opts.SecretKey)
req.Header.Add("Authorization", a.Opts.getAuthHeader())
}
func (a *APIOperations) DoDelete(url string) error {