auth

package
v0.0.0-...-ce8ff45 Latest Latest
Warning

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

Go to latest
Published: May 21, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidLength = errors.New("invalid length")

Functions

func GenerateSelfSignedCert

func GenerateSelfSignedCert() (tls.Certificate, error)

func NewState

func NewState(length ...int) (string, error)

NewState generates a new random state string.

func WaitForCode

func WaitForCode(addr string, state string) (string, error)

func WithCacheFile

func WithCacheFile(file string) func(*Auth) error

WithCacheFile sets the file to cache the token.

func WithOAuth2ClientID

func WithOAuth2ClientID(id string) func(*Auth) error

WithOAuth2ClientID sets the client ID.

func WithOAuth2ClientSecret

func WithOAuth2ClientSecret(secret string) func(*Auth) error

WithOAuth2ClientSecret sets the client secret.

func WithOAuth2Config

func WithOAuth2Config(config *oauth2.Config) func(*Auth) error

WithOAuth2Config sets the oauth2.Config.

func WithOAuth2Endpoint

func WithOAuth2Endpoint(endpoint oauth2.Endpoint) func(*Auth) error

WithOAuth2Endpoint sets the endpoint.

func WithOAuth2EndpointAuthURL

func WithOAuth2EndpointAuthURL(url string) func(*Auth) error

WithOAuth2EndpointAuthURL sets the auth URL.

func WithOAuth2EndpointTokenURL

func WithOAuth2EndpointTokenURL(url string) func(*Auth) error

WithOAuth2EndpointTokenURL sets the token URL.

func WithOAuth2RedirectURL

func WithOAuth2RedirectURL(url string) func(*Auth) error

WithOAuth2RedirectURL sets the redirect URL.

func WithOutput

func WithOutput(output io.Writer) func(*Auth) error

WithOutput sets the output writer.

func WithTokenCacheFile

func WithTokenCacheFile(file string) func(*TokenSource) error

Types

type Auth

type Auth struct {
	Config *oauth2.Config
	File   string
	Output io.Writer
}

Auth wraps the oauth2.Config and provides a method to get the and persist the token / refresh token.

func New

func New(opts ...func(*Auth) error) (*Auth, error)

New creates a new Auth instance. Use `With*` functions to set the options. By default it will use the `CLIENT_ID` and `CLIENT_SECRET` (optionally prefixed with `COTTON_SCHWAB_`, `COTTON_`, or `SCHWAB_`) environment variables.

func (*Auth) TokenSource

func (auth *Auth) TokenSource(ctx context.Context) (*TokenSource, error)

TokenSource returns the token source.

type CacheToken

type CacheToken struct {
	// AccessToken is the current access token.
	AccessToken string `json:"access_token,omitempty"`

	// RefreshToken is used to get a new access token.
	RefreshToken string `json:"refresh_token,omitempty"`

	// Expiry is the expiry time of the access token.
	Expiry time.Time `json:"expiry,omitempty"`
}

CacheToken represents a token that is stored in the cache.

func LoadToken

func LoadToken(r io.Reader) (*CacheToken, error)

LoadToken loads a token from a the reader.

func NewCacheToken

func NewCacheToken(t *oauth2.Token) *CacheToken

NewCacheToken creates a new token from an oauth2.Token.

func (*CacheToken) Compare

func (token *CacheToken) Compare(other *CacheToken) bool

Compare returns true if the tokens are equal.

func (*CacheToken) IsOauth2Token

func (token *CacheToken) IsOauth2Token(other *oauth2.Token) bool

IsOauth2Token returns true if the token is the same as the oauth2.Token.

func (*CacheToken) MarshalJSON

func (token *CacheToken) MarshalJSON() ([]byte, error)

MarshalJSON marshals the token to JSON. Saving the expiry time as a UTC string.

func (*CacheToken) OAuth2

func (token *CacheToken) OAuth2() *oauth2.Token

OAuth2 converts a CacheToken to an oauth2.Token.

func (*CacheToken) SaveToken

func (token *CacheToken) SaveToken(writer io.Writer) error

SaveToken saves a token to a writer.

func (*CacheToken) UnmarshalJSON

func (token *CacheToken) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the token from JSON. Loading the expiry time as a UTC string.

type TokenSource

type TokenSource struct {
	Source oauth2.TokenSource
	Last   *oauth2.Token
	File   string
	// contains filtered or unexported fields
}

TokenSource wraps an oauth2.TokenSource and optionally caches the token to a file.

func NewTokenSource

func NewTokenSource(
	source oauth2.TokenSource,
	options ...func(*TokenSource) error,
) (*TokenSource, error)

NewTokenSource creates a new TokenSource.

func (*TokenSource) Save

func (source *TokenSource) Save() error

Save forces a save of the last token to the file.

func (*TokenSource) Token

func (source *TokenSource) Token() (*oauth2.Token, error)

Token returns a token from the source.

Jump to

Keyboard shortcuts

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