Documentation
¶
Index ¶
- Variables
- func GenerateSelfSignedCert() (tls.Certificate, error)
- func NewState(length ...int) (string, error)
- func WaitForCode(addr string, state string) (string, error)
- func WithCacheFile(file string) func(*Auth) error
- func WithOAuth2ClientID(id string) func(*Auth) error
- func WithOAuth2ClientSecret(secret string) func(*Auth) error
- func WithOAuth2Config(config *oauth2.Config) func(*Auth) error
- func WithOAuth2Endpoint(endpoint oauth2.Endpoint) func(*Auth) error
- func WithOAuth2EndpointAuthURL(url string) func(*Auth) error
- func WithOAuth2EndpointTokenURL(url string) func(*Auth) error
- func WithOAuth2RedirectURL(url string) func(*Auth) error
- func WithOutput(output io.Writer) func(*Auth) error
- func WithTokenCacheFile(file string) func(*TokenSource) error
- type Auth
- type CacheToken
- func (token *CacheToken) Compare(other *CacheToken) bool
- func (token *CacheToken) IsOauth2Token(other *oauth2.Token) bool
- func (token *CacheToken) MarshalJSON() ([]byte, error)
- func (token *CacheToken) OAuth2() *oauth2.Token
- func (token *CacheToken) SaveToken(writer io.Writer) error
- func (token *CacheToken) UnmarshalJSON(data []byte) error
- type TokenSource
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidLength = errors.New("invalid length")
Functions ¶
func GenerateSelfSignedCert ¶
func GenerateSelfSignedCert() (tls.Certificate, error)
func WithCacheFile ¶
WithCacheFile sets the file to cache the token.
func WithOAuth2ClientID ¶
WithOAuth2ClientID sets the client ID.
func WithOAuth2ClientSecret ¶
WithOAuth2ClientSecret sets the client secret.
func WithOAuth2Config ¶
WithOAuth2Config sets the oauth2.Config.
func WithOAuth2Endpoint ¶
WithOAuth2Endpoint sets the endpoint.
func WithOAuth2EndpointAuthURL ¶
WithOAuth2EndpointAuthURL sets the auth URL.
func WithOAuth2EndpointTokenURL ¶
WithOAuth2EndpointTokenURL sets the token URL.
func WithOAuth2RedirectURL ¶
WithOAuth2RedirectURL sets the redirect URL.
func WithOutput ¶
WithOutput sets the output writer.
func WithTokenCacheFile ¶
func WithTokenCacheFile(file string) func(*TokenSource) error
Types ¶
type Auth ¶
Auth wraps the oauth2.Config and provides a method to get the and persist the token / refresh token.
func New ¶
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.