Skip to content

Commit

Permalink
feat: add token name option for token auth
Browse files Browse the repository at this point in the history
  • Loading branch information
gabor-boros committed Oct 26, 2021
1 parent 6658984 commit cff5e53
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
20 changes: 15 additions & 5 deletions internal/pkg/client/client.go
Expand Up @@ -29,6 +29,8 @@ var (
ErrInvalidBasicAuth = errors.New("invalid basic auth params provided")
// ErrInvalidTokenAuth returns if the provided token is empty.
ErrInvalidTokenAuth = errors.New("invalid token auth params provided")
// ErrOauth2Callback returns if the Oauth2 provider returned an error.
ErrOauth2Callback = errors.New("the Oauth2 callback returned with error")
)

// BaseClientOpts specifies the common options the clients are using.
Expand Down Expand Up @@ -85,19 +87,26 @@ func NewBasicAuth(username string, password string) (Authenticator, error) {
}, nil
}

// TokenAuth represents the required parameters for token based authentication.
// TokenAuth represents the required parameters for token based authentication.
type TokenAuth struct {
Header string
Token string
Header string
TokenName string
Token string
}

func (a *TokenAuth) SetAuthHeader(req *http.Request) {
req.Header.Set(a.Header, a.Token)
token := a.Token

if a.TokenName != "" {
token = a.TokenName + " " + token
}

req.Header.Set(a.Header, token)
}

// NewTokenAuth returns a new TokenAuth that implements Authenticator. If the
// header name is not set, the standard "Authorization" header will be used.
func NewTokenAuth(header string, token string) (Authenticator, error) {
func NewTokenAuth(header string, tokenName string, token string) (Authenticator, error) {
if token == "" {
return nil, ErrInvalidTokenAuth
}
Expand All @@ -108,6 +117,7 @@ func NewTokenAuth(header string, token string) (Authenticator, error) {

return &TokenAuth{
Header: header,
TokenName: tokenName,
Token: token,
}, nil
}
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/client/client_test.go
Expand Up @@ -152,7 +152,7 @@ func TestTokenAuth(t *testing.T) {
require.Nil(t, err)
require.Equal(t, req.Header.Get(header), "")

auth, err := client.NewTokenAuth(header, "the-strongest-avenger")
auth, err := client.NewTokenAuth(header, "", "the-strongest-avenger")
require.Nil(t, err)

auth.SetAuthHeader(req)
Expand All @@ -166,7 +166,7 @@ func TestTokenAuth_FallbackHeader(t *testing.T) {
require.Nil(t, err)
require.Equal(t, req.Header.Get(header), "")

auth, err := client.NewTokenAuth("", "the-strongest-avenger")
auth, err := client.NewTokenAuth("", "", "the-strongest-avenger")
require.Nil(t, err)

auth.SetAuthHeader(req)
Expand All @@ -176,7 +176,7 @@ func TestTokenAuth_FallbackHeader(t *testing.T) {
func TestTokenAuth_Invalid(t *testing.T) {
var err error

_, err = client.NewTokenAuth("", "")
_, err = client.NewTokenAuth("", "", "")
require.ErrorIs(t, err, client.ErrInvalidTokenAuth)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/client/clockify/clockify.go
Expand Up @@ -188,7 +188,7 @@ func NewFetcher(opts *ClientOpts) (client.Fetcher, error) {
return nil, err
}

authenticator, err := client.NewTokenAuth(opts.Header, opts.Token)
authenticator, err := client.NewTokenAuth(opts.Header, "", opts.Token)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit cff5e53

Please sign in to comment.