Skip to content

Commit

Permalink
Fix types so that BearerAuth implements AuthCredentials interface. (#117
Browse files Browse the repository at this point in the history
)

* Fix types so that BearerAuth implements AuthCredentials interface.

* Fix test

* Update version so our user agent is more accurate.

* Add type assertions so we don't make this mistake again.
  • Loading branch information
ggreer authored Mar 28, 2024
1 parent c4e163e commit 63ba54c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/sdk/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sdk

// Version is the current version of the baton SDK.
const Version = "0.0.26"
const Version = "0.1.30"
16 changes: 13 additions & 3 deletions pkg/uhttp/authcredentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,23 @@ import (
)

type AuthCredentials interface {
GetClient(ctx context.Context) (*http.Client, error)
GetClient(ctx context.Context, options ...Option) (*http.Client, error)
}

type NoAuth struct{}

func (n *NoAuth) GetClient(ctx context.Context) (*http.Client, error) {
return http.DefaultClient, nil
var _ AuthCredentials = (*NoAuth)(nil)

func (n *NoAuth) GetClient(ctx context.Context, options ...Option) (*http.Client, error) {
return getHttpClient(ctx, options...)
}

type BearerAuth struct {
Token string
}

var _ AuthCredentials = (*BearerAuth)(nil)

func NewBearerAuth(token string) *BearerAuth {
return &BearerAuth{
Token: token,
Expand All @@ -51,6 +55,8 @@ type BasicAuth struct {
Password string
}

var _ AuthCredentials = (*BasicAuth)(nil)

func NewBasicAuth(username, password string) *BasicAuth {
return &BasicAuth{
Username: username,
Expand Down Expand Up @@ -78,6 +84,8 @@ type OAuth2ClientCredentials struct {
cfg *clientcredentials.Config
}

var _ AuthCredentials = (*OAuth2ClientCredentials)(nil)

func NewOAuth2ClientCredentials(clientId, clientSecret string, tokenURL *url.URL, scopes []string) *OAuth2ClientCredentials {
return &OAuth2ClientCredentials{
cfg: &clientcredentials.Config{
Expand Down Expand Up @@ -109,6 +117,8 @@ type OAuth2JWT struct {
CreateJWTConfig CreateJWTConfig
}

var _ AuthCredentials = (*OAuth2JWT)(nil)

func NewOAuth2JWT(credentials []byte, scopes []string, createfn CreateJWTConfig) *OAuth2JWT {
return &OAuth2JWT{
Credentials: credentials,
Expand Down
9 changes: 6 additions & 3 deletions pkg/uhttp/authcredentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/base64"
"net/http"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -15,10 +14,14 @@ import (

func TestHelpers_NoAuth_GetClient(t *testing.T) {
n := &NoAuth{}
client, err := n.GetClient(context.Background())
ctx := context.Background()
client, err := n.GetClient(ctx)
require.NoError(t, err)
require.NotNil(t, client)
require.Equal(t, http.DefaultClient, client)

expectedClient, err := getHttpClient(ctx)
require.NoError(t, err)
require.EqualExportedValues(t, *expectedClient, *client)
}

func TestHelpers_BearerAuth_GetClient(t *testing.T) {
Expand Down

0 comments on commit 63ba54c

Please sign in to comment.