src

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

credentials_adapter.go (1171B)


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