auth

package
v0.1.21 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2023 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package auth contains functions for managing the authentication flows via the command-line.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoCredentials = errors.New("no credentials")

ErrNoCredentials is the error returned from LoadCredentials when a credentials file cannot be found at the specified location.

View Source
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(ctx context.Context) 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 DetermineTokenFilePath added in v0.1.3

func DetermineTokenFilePath(ctx context.Context) (string, error)

DetermineTokenFilePath attempts to determine the path to the oauth token file.

func GetOAuthConfig

func GetOAuthConfig() *oauth2.Config

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

func GetOAuthURLAndState(conf *oauth2.Config) (string, string)

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

func LoadOAuthToken(ctx context.Context) (*oauth2.Token, error)

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

func SaveOAuthToken(ctx context.Context, token *oauth2.Token) error

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

func TokenFromContext(ctx context.Context) (*oauth2.Token, bool)

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

func TokenToContext(ctx context.Context, token *oauth2.Token) context.Context

TokenToContext returns a new context.Context that contains the provided oauth2.Token.

func WaitForOAuthTokenCallback added in v0.1.15

func WaitForOAuthTokenCallback(ctx context.Context, conf *oauth2.Config, state string) (*oauth2.Token, error)

WaitForOAuthTokenCallback 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.

func WaitForOAuthTokenCommandLine added in v0.1.15

func WaitForOAuthTokenCommandLine(ctx context.Context, conf *oauth2.Config, state string) (*oauth2.Token, error)

WaitForOAuthTokenCommandLine waits for a user to enter a redirect URL, then extracts the code and state and requests a token

Types

type Credentials

type Credentials struct {
	UserID string `json:"user_id"`
	Secret string `json:"user_secret"`
}

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.

Jump to

Keyboard shortcuts

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