oauthutil - ActiveState ActiveGo 1.8

Package oauthutil

import "go4.org/oauthutil"

Overview ▾

Package oauthutil contains OAuth 2 related utilities.


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.

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


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

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

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

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.

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

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.