Version: v0.0.0-...-d4a0794 Latest Latest

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

Go to latest
Published: Dec 9, 2020 License: Apache-2.0 Imports: 6 Imported by: 16



Package oauthutil contains OAuth 2 related utilities.



View Source
const TitleBarRedirectURL = "urn:ietf:wg:oauth:2.0:oob"

TitleBarRedirectURL is the OAuth2 redirect URL to use when the authorization code should be returned in the title bar of the browser, with the page text prompting the user to copy the code and paste it in the application.


View Source
var ErrNoAuthCode = errors.New("oauthutil: unspecified TokenSource.AuthCode")

ErrNoAuthCode is returned when Token() has not found any valid cached token and TokenSource does not have an AuthCode for getting a new token.


func NewRefreshTokenSource

func NewRefreshTokenSource(config *oauth2.Config, refreshToken string) oauth2.TokenSource

NewRefreshTokenSource returns a token source that obtains its initial token based on the provided config and the refresh token.


type TokenSource

type TokenSource struct {
	Config *oauth2.Config

	// CacheFile is where the token will be stored JSON-encoded. Any call to Token
	// first tries to read a valid token from CacheFile.
	CacheFile string

	// AuthCode provides the authorization code that Token will exchange for a token.
	// It usually is a way to prompt the user for the code. If CacheFile does not provide
	// a token and AuthCode is nil, Token returns ErrNoAuthCode.
	AuthCode func() string

TokenSource is an implementation of oauth2.TokenSource. It uses CacheFile to store and reuse the the acquired token, and AuthCode to provide the authorization code that will be exchanged for a token otherwise.

func (TokenSource) Token

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

Token first tries to find a valid token in CacheFile, and otherwise uses Config and AuthCode to fetch a new token. This new token is saved in CacheFile (if not blank). If CacheFile did not provide a token and AuthCode is nil, ErrNoAuthCode is returned.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto