auth

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package auth implements Google's documented OAuth flow for desktop applications: a loopback redirect on 127.0.0.1 with a random port and PKCE, then a refresh-token-backed token source for API calls.

Index

Constants

View Source
const (
	ScopeDocuments         = "https://www.googleapis.com/auth/documents"
	ScopeDocumentsReadonly = "https://www.googleapis.com/auth/documents.readonly"
	ScopeDrive             = "https://www.googleapis.com/auth/drive"
	ScopeDriveReadonly     = "https://www.googleapis.com/auth/drive.readonly"
)

OAuth scopes. documents is "sensitive", drive is "restricted"; both are fine for a per-user app that is never published.

View Source
const (
	GoogleAuthURL  = "https://accounts.google.com/o/oauth2/auth"
	GoogleTokenURL = "https://oauth2.googleapis.com/token"
)

Google's OAuth endpoints, used when the client JSON omits them.

View Source
const DefaultHTTPTimeout = 60 * time.Second

DefaultHTTPTimeout bounds an OAuth call when no timeout is given.

Variables

View Source
var (
	RevokeURL    = "https://oauth2.googleapis.com/revoke"
	TokenInfoURL = "https://oauth2.googleapis.com/tokeninfo"
)

Endpoints used for revocation and token inspection. Vars so tests can point them at a local server.

View Source
var ErrNoBrowser = errors.New("--no-browser")

ErrNoBrowser is what an OpenBrowser hook returns when the caller asked for the URL instead of a browser. Exported so the caller does not have to encode that choice in an error string the printer then matches on.

View Source
var ErrNotDesktopClient = errors.New("auth: client secret JSON is not a Desktop app client (expected an \"installed\" section)")

ErrNotDesktopClient means the JSON is not a "Desktop app" OAuth client.

Functions

func HasScopes

func HasScopes(granted, wanted []string) (missing []string)

HasScopes reports whether every wanted scope was granted.

func LoadClientSecret

func LoadClientSecret(path string, scopes []string) (*oauth2.Config, error)

LoadClientSecret reads a Desktop-app client JSON downloaded from the Google Cloud console and returns an oauth2.Config for the scopes.

func Login

func Login(ctx context.Context, cfg *oauth2.Config, opts LoginOptions) (*oauth2.Token, error)

Login runs the loopback authorization-code flow and returns a token that includes a refresh token.

func OpenBrowser

func OpenBrowser(u string) error

OpenBrowser opens url with the platform's default handler.

func ParseClientSecret

func ParseClientSecret(data []byte, scopes []string) (*oauth2.Config, error)

ParseClientSecret is LoadClientSecret on bytes.

func Revoke

func Revoke(ctx context.Context, client *http.Client, token string) error

Revoke invalidates a refresh (or access) token at Google.

func Scopes

func Scopes(readOnly bool) []string

Scopes returns the scope set for the requested access level.

func TokenSource

func TokenSource(ctx context.Context, cfg *oauth2.Config, refreshToken string, timeout time.Duration) oauth2.TokenSource

TokenSource returns a caching token source backed by the refresh token. timeout bounds each refresh; zero means DefaultHTTPTimeout.

Types

type LoginOptions

type LoginOptions struct {
	// OpenBrowser is called with the authorization URL. nil uses the OS
	// default browser; a function that returns an error is not fatal, the
	// URL is always printed to Out as well.
	OpenBrowser func(url string) error
	// Out receives the URL and progress messages. nil discards them.
	Out io.Writer
	// Timeout bounds the wait for the browser. Default 5 minutes.
	Timeout time.Duration
	// Listener overrides the loopback listener (tests).
	Listener net.Listener
	// HTTPTimeout bounds the code exchange. Zero means
	// DefaultHTTPTimeout; it is not the same as Timeout, which bounds
	// the person's trip through the browser.
	HTTPTimeout time.Duration
}

LoginOptions tune the interactive flow. Zero values are sensible.

type TokenInfo

type TokenInfo struct {
	Scopes    []string
	Email     string
	ExpiresIn time.Duration
	Audience  string
}

TokenInfo describes an access token as Google sees it.

func Inspect

func Inspect(ctx context.Context, client *http.Client, accessToken string) (*TokenInfo, error)

Inspect calls the tokeninfo endpoint for an access token.

Jump to

Keyboard shortcuts

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