1
0
mirror of https://github.com/rancher/norman.git synced 2025-09-10 19:49:01 +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 URL string
AccessKey string AccessKey string
SecretKey string SecretKey string
TokenKey string
Timeout time.Duration Timeout time.Duration
HTTPClient *http.Client HTTPClient *http.Client
CACerts string 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 { type APIError struct {
StatusCode int StatusCode int
URL string URL string
@@ -169,7 +181,7 @@ func NewAPIClient(opts *ClientOpts) (APIBaseClient, error) {
return result, err return result, err
} }
req.SetBasicAuth(opts.AccessKey, opts.SecretKey) req.Header.Add("Authorization", opts.getAuthHeader())
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
@@ -188,7 +200,7 @@ func NewAPIClient(opts *ClientOpts) (APIBaseClient, error) {
if schemasURLs != opts.URL { if schemasURLs != opts.URL {
req, err = http.NewRequest("GET", schemasURLs, nil) req, err = http.NewRequest("GET", schemasURLs, nil)
req.SetBasicAuth(opts.AccessKey, opts.SecretKey) req.Header.Add("Authorization", opts.getAuthHeader())
if err != nil { if err != nil {
return result, err return result, err
} }
@@ -243,8 +255,7 @@ func (a *APIBaseClient) Websocket(url string, headers map[string][]string) (*web
} }
if a.Opts != nil { if a.Opts != nil {
s := a.Opts.AccessKey + ":" + a.Opts.SecretKey httpHeaders.Add("Authorization", a.Opts.getAuthHeader())
httpHeaders.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(s)))
} }
return dialer.Dial(url, http.Header(httpHeaders)) return dialer.Dial(url, http.Header(httpHeaders))

View File

@@ -14,7 +14,7 @@ import (
) )
func (a *APIOperations) setupRequest(req *http.Request) { 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 { func (a *APIOperations) DoDelete(url string) error {