From a5667bb828d55063ab67967d65e8a70dd8a7cb83 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Wed, 2 Aug 2023 16:08:38 +0200 Subject: [PATCH] Add bearer token authentication In this commit, we've added the capability to authenticate via bearer token to Meteologix's HTTP client. A new method "WithBearerToken" has been implemented, allowing a bearer token to be set in 'meteologix.go'. Additionally, in 'httpclient.go', the token is attached to the "Authorization" header for API requests. Tests asserting token setting functionality have been added in 'meteologix_test.go'. These changes open up an alternative authentication option for our API users. The bearer auth is not public yet, so there is no way for us to test this auth --- httpclient.go | 4 ++++ meteologix.go | 13 +++++++++++++ meteologix_test.go | 22 ++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/httpclient.go b/httpclient.go index 8065a6b..27e86bd 100644 --- a/httpclient.go +++ b/httpclient.go @@ -133,6 +133,10 @@ func (hc *HTTPClient) setAuthentication(hr *http.Request) { hr.Header.Set("X-API-Key", hc.Config.apiKey) return } + if hc.bearerToken != "" { + hr.Header.Set("Authorization", "Bearer"+hc.bearerToken) + return + } if hc.authUser != "" && hc.authPass != "" { hr.SetBasicAuth(url.QueryEscape(hc.authUser), url.QueryEscape(hc.authPass)) } diff --git a/meteologix.go b/meteologix.go index 03daa27..e8698f7 100644 --- a/meteologix.go +++ b/meteologix.go @@ -46,6 +46,8 @@ type Config struct { authPass string // authUser holds the (optional) username for the API user authentication authUser string + // bearerToken holds the (optional) bearer token for the API authentication + bearerToken string // userAgent represents an alternative User-Agent HTTP header string userAgent string } @@ -97,6 +99,17 @@ func WithAPIKey(k string) Option { } } +// WithBearerToken uses a bearer token for the client authentication of the +// HTTP client +func WithBearerToken(t string) Option { + if t == "" { + return nil + } + return func(co *Config) { + co.bearerToken = t + } +} + // WithPassword sets the HTTP Basic auth authPass for the HTTP client func WithPassword(p string) Option { if p == "" { diff --git a/meteologix_test.go b/meteologix_test.go index bcfbb5d..33d1b6c 100644 --- a/meteologix_test.go +++ b/meteologix_test.go @@ -66,6 +66,28 @@ func TestNew_WithAPIKey(t *testing.T) { } } +func TestNew_WithBearerToken(t *testing.T) { + e := "BEARER-TOKEN" + c := New(WithBearerToken(e)) + if c == nil { + t.Errorf("NewWithBearerToken failed, expected Client, got nil") + return + } + if c.config.bearerToken != e { + t.Errorf("NewWithBearerToken failed, expected token value: %s, got: %s", e, + c.config.bearerToken) + } + c = New(WithBearerToken("")) + if c == nil { + t.Errorf("NewWithBearerToken failed, expected Client, got nil") + return + } + if c.config.bearerToken != "" { + t.Errorf("NewWithBearerToken failed, expected empty token, got: %s", + c.config.bearerToken) + } +} + func TestNew_WithUsername(t *testing.T) { e := "username" c := New(WithUsername(e))