Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions sdk/access_token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package sdk

import (
"context"
"testing"

"github.com/opentdf/platform/sdk/auth"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAccessToken_ReturnsTokenFromSource(t *testing.T) {
s := &SDK{tokenSource: FakeAccessTokenSource{accessToken: "test-token"}}

tok, err := s.AccessToken(context.Background())
require.NoError(t, err)
assert.Equal(t, auth.AccessToken("test-token"), tok)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

func TestAccessToken_NoTokenSource(t *testing.T) {
s := &SDK{}

tok, err := s.AccessToken(context.Background())
require.ErrorIs(t, err, ErrNoAccessTokenSource)
assert.Empty(t, tok)
}
10 changes: 10 additions & 0 deletions sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
ErrPlatformTokenEndpointNotFound = Error("token_endpoint not found in well-known idp configuration")
ErrPlatformEndpointNotFound = Error("platform_endpoint not found in well-known configuration")
ErrAccessTokenInvalid = Error("access token is invalid")
ErrNoAccessTokenSource = Error("no access token source configured; SDK was created without credentials")
ErrWellKnowConfigEmpty = Error("well-known configuration is empty")
ErrAttributeNotFound = Error("attribute not found")
)
Expand Down Expand Up @@ -305,6 +306,15 @@ func (s SDK) Close() error {
return nil
}

// AccessToken returns a valid access token for the SDK's configured credentials.
// It returns ErrNoAccessTokenSource if the SDK was constructed without credentials.
func (s *SDK) AccessToken(ctx context.Context) (auth.AccessToken, error) {
if s.tokenSource == nil {
Comment thread
alkalescent marked this conversation as resolved.
Outdated
return "", ErrNoAccessTokenSource
}
return s.tokenSource.AccessToken(ctx, s.httpClient)
}

// Logger returns the configured slog.Logger for this SDK instance
func (s SDK) Logger() *slog.Logger {
return s.logger
Expand Down
Loading