Documentation ¶
Overview ¶
Package auth contains functions for managing the authentication flows via the command-line.
Index ¶
- Variables
- func DeleteOAuthToken() error
- func GetOAuthConfig() *oauth2.Config
- func GetOAuthTokenForCredentials(ctx context.Context, conf *oauth2.Config, credentials *Credentials) (*oauth2.Token, error)
- func GetOAuthURLAndState(conf *oauth2.Config) (string, string)
- func LoadOAuthToken() (*oauth2.Token, error)
- func SaveOAuthToken(token *oauth2.Token) error
- func TokenFromContext(ctx context.Context) (*oauth2.Token, bool)
- func TokenToContext(ctx context.Context, token *oauth2.Token) context.Context
- func WaitForOAuthToken(ctx context.Context, conf *oauth2.Config, state string) (*oauth2.Token, error)
- type Credentials
Constants ¶
This section is empty.
Variables ¶
var ErrNoCredentials = errors.New("no credentials")
ErrNoCredentials is the error returned from LoadCredentials when a credentials file cannot be found at the specified location.
var ErrNoToken = errors.New("no oauth token")
ErrNoToken is the error given when attempting to load an oauth token from disk that cannot be found.
Functions ¶
func DeleteOAuthToken ¶
func DeleteOAuthToken() error
DeleteOAuthToken attempts to remove an oauth token from the configuration directory. The location of the token file changes based on the host operating system. See the documentation for os.UserConfigDir for specifics on where the token file will be located. Returns ErrNoToken if a token file cannot be found.
func GetOAuthConfig ¶
GetOAuthConfig returns the oauth2 configuration used to authenticate a user.
func GetOAuthTokenForCredentials ¶
func GetOAuthTokenForCredentials(ctx context.Context, conf *oauth2.Config, credentials *Credentials) (*oauth2.Token, error)
GetOAuthTokenForCredentials attempts to exchange the given credentials for an oauth2.Token. The implementation here cannot use the oauth2.Config.PasswordCredentialsToken function as an audience parameter has to be specified which cannot be done using the oauth2 package. This function manually builds and performs the request then uses the response data to build the token.
func GetOAuthURLAndState ¶
GetOAuthURLAndState returns the URL the user should navigate to in order to perform the oauth2 authentication flow and the expected state to validate when the token is provided. At this URL they will be prompted for their credentials.
func LoadOAuthToken ¶
LoadOAuthToken attempts to load an oauth token from the configuration directory. The location of the token file changes based on the host operating system. See the documentation for os.UserConfigDir for specifics on where the token file will be loaded from. Returns ErrNoToken if a token file cannot be found.
func SaveOAuthToken ¶
SaveOAuthToken writes the provided token to a JSON file in the user's config directory. This location changes based on the host operating system. See the documentation for os.UserConfigDir for specifics on where the token file will be placed.
func TokenFromContext ¶
TokenFromContext checks the given context.Context for the presence of an oauth2.Token. The second return value indicates if a token was found within the context.
func TokenToContext ¶
TokenToContext returns a new context.Context that contains the provided oauth2.Token.
func WaitForOAuthToken ¶
func WaitForOAuthToken(ctx context.Context, conf *oauth2.Config, state string) (*oauth2.Token, error)
WaitForOAuthToken starts an HTTP server that listens for an inbound request providing the oauth2 token. This function blocks until a valid token is obtained or the provided context is cancelled. The provided state value must match on the inbound request.
Types ¶
type Credentials ¶
The Credentials type represents service account credentials that are used to obtain authentication tokens rather than using the oauth flow.
func LoadCredentials ¶
func LoadCredentials(location string) (*Credentials, error)
LoadCredentials attempts to load a credentials file from disk at a specified location. Returns ErrNoCredentials if the credentials file does not exist.