src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

bearer_token_adapter.go (1080B)


      1 package smithy
      2 
      3 import (
      4 	"context"
      5 	"fmt"
      6 	"time"
      7 
      8 	"github.com/aws/smithy-go"
      9 	"github.com/aws/smithy-go/auth"
     10 	"github.com/aws/smithy-go/auth/bearer"
     11 )
     12 
     13 // BearerTokenAdapter adapts smithy bearer.Token to smithy auth.Identity.
     14 type BearerTokenAdapter struct {
     15 	Token bearer.Token
     16 }
     17 
     18 var _ auth.Identity = (*BearerTokenAdapter)(nil)
     19 
     20 // Expiration returns the time of expiration for the token.
     21 func (v *BearerTokenAdapter) Expiration() time.Time {
     22 	return v.Token.Expires
     23 }
     24 
     25 // BearerTokenProviderAdapter adapts smithy bearer.TokenProvider to smithy
     26 // auth.IdentityResolver.
     27 type BearerTokenProviderAdapter struct {
     28 	Provider bearer.TokenProvider
     29 }
     30 
     31 var _ (auth.IdentityResolver) = (*BearerTokenProviderAdapter)(nil)
     32 
     33 // GetIdentity retrieves a bearer token using the underlying provider.
     34 func (v *BearerTokenProviderAdapter) GetIdentity(ctx context.Context, _ smithy.Properties) (
     35 	auth.Identity, error,
     36 ) {
     37 	token, err := v.Provider.RetrieveBearerToken(ctx)
     38 	if err != nil {
     39 		return nil, fmt.Errorf("get token: %w", err)
     40 	}
     41 
     42 	return &BearerTokenAdapter{Token: token}, nil
     43 }