awstokens

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2020 License: MIT Imports: 11 Imported by: 0

README

awstokens

A simple utility to refresh AWS tokens

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAWSError added in v0.0.2

func GetAWSError(err error) (awserr.Error, bool)

GetAWSError recursively checks if the error is an awserr.Error. It unwraps until it gets to the end of the error chain. A common error to look out for is cognitoidentityprovider.NotAuthorizedError. This is what you will be getting back if the refresh token has expired. For example:

var expired bool
if aerr, ok := awstokens.GetAWSError(err); ok {
	if aerr.Code() == cognitoidentityprovider.ErrCodeNotAuthorizedException {
		expired = true
	}
}

func IsNetworkError added in v0.0.3

func IsNetworkError(err error) bool

IsNetworkError checks if a network error has occurred. The reason we can't just use `neterror.GetNetError` is because AWS errors keep the original error in a field instead of wrapping errors, so we have to the original error field too.

Types

type Auth

type Auth struct {
	// contains filtered or unexported fields
}

Auth contains the AWS tokens and some extra info for refreshing them.

func NewAuth

func NewAuth(config Config) (*Auth, error)

NewAuth returns a pointer to an Auth using the provided Config.

func NewAuthWithAuthInitiator

func NewAuthWithAuthInitiator(authInitiator AuthInitiator, config Config) *Auth

NewAuthWithAuthInitiator returns a pointer to an Auth using the provided config and AuthInitiator.

func (*Auth) GetAuthToken

func (t *Auth) GetAuthToken() (string, error)

GetAuthToken returns the Access token by default, but if ShouldUseIDToken has been set to true it returns the ID token. If the token it is going to return has expired then it attempts to refresh the token before returning it.

type AuthInitiator

type AuthInitiator interface {
	InitiateAuth(input *cognitoidentityprovider.InitiateAuthInput) (*cognitoidentityprovider.InitiateAuthOutput, error)
}

AuthInitiator is an interface that represents the cognitoidentityprovider library client, which allows you to refresh tokens.

type Config

type Config struct {
	// Actual tokens
	AccessToken, IDToken, RefreshToken string
	// Info required to refresh the tokens
	ClientID, Region string
	// By default use the access token for auth, but if this is true then use ID
	// token instead
	ShouldUseIDToken bool
	// ExpiryMargin is the margin in which a token is considered to be expired.
	// If it is left empty (i.e. 0) then we will use the default value of 5
	// seconds.
	ExpiryMargin time.Duration
}

Config contains the initial settings for the Auth.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL