oauthflow

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Presets = map[string]Preset{
	"github": {
		AuthURL:            "https://github.com/login/oauth/authorize",
		TokenURL:           "https://github.com/login/oauth/access_token",
		DeviceAuthURL:      "https://github.com/login/device/code",
		AuthStyle:          "params",
		SupportsDeviceFlow: true,
	},
	"google": {
		AuthURL:            "https://accounts.google.com/o/oauth2/auth",
		TokenURL:           "https://oauth2.googleapis.com/token",
		DeviceAuthURL:      "https://oauth2.googleapis.com/device/code",
		AuthStyle:          "params",
		SupportsDeviceFlow: true,
		ExtraAuthParams:    map[string]string{"access_type": "offline", "prompt": "consent"},
	},
	"microsoft": {
		AuthURL:            "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
		TokenURL:           "https://login.microsoftonline.com/common/oauth2/v2.0/token",
		DeviceAuthURL:      "https://login.microsoftonline.com/common/oauth2/v2.0/devicecode",
		AuthStyle:          "params",
		SupportsDeviceFlow: true,
	},
	"gitlab": {
		AuthURL:            "https://gitlab.com/oauth/authorize",
		TokenURL:           "https://gitlab.com/oauth/token",
		DeviceAuthURL:      "https://gitlab.com/oauth/authorize_device",
		AuthStyle:          "params",
		SupportsDeviceFlow: true,
	},
	"spotify": {
		AuthURL:   "https://accounts.spotify.com/authorize",
		TokenURL:  "https://accounts.spotify.com/api/token",
		AuthStyle: "header",
	},
}

Functions

func BrowserLogin

func BrowserLogin(ctx context.Context, cfg *LoginConfig) (*oauth2.Token, error)

BrowserLogin runs the authorization_code grant with PKCE: starts a one-shot callback listener on 127.0.0.1, sends the user to the authorization url and exchanges the received code

func DeviceLogin

func DeviceLogin(ctx context.Context, cfg *LoginConfig) (*oauth2.Token, error)

DeviceLogin runs the oauth2 device_code grant: prints the verification url + user code and polls the token endpoint until the user approves

func ExpandPath

func ExpandPath(path string) string

ExpandPath resolves a leading "~/" to the user home directory

func LoadTokenFile

func LoadTokenFile(path string) (*oauth2.Token, error)

LoadTokenFile reads a persisted oauth2 token; returns os.ErrNotExist if the file is absent

func LoginURLHost

func LoginURLHost(authURL string) string

LoginURLHost extracts the host of the authorization url, for user-facing messages

func SaveJSONFile

func SaveJSONFile(path string, value interface{}) error

SaveJSONFile persists any credential-like value as JSON with 0600 permissions via temp file + rename

func SaveTokenFile

func SaveTokenFile(path string, token *oauth2.Token) error

SaveTokenFile persists an oauth2 token with 0600 permissions via temp file + rename

Types

type LoginConfig

type LoginConfig struct {
	ClientID      string
	ClientSecret  string
	Scopes        []string
	AuthURL       string
	TokenURL      string
	DeviceAuthURL string
	// AuthStyle: "" auto detect, "header" or "params"
	AuthStyle string
	// Port for the browser flow local callback, default 8988
	Port int
	// ListenAddr is the browser flow bind address, default 127.0.0.1;
	// inside a container use 0.0.0.0 so the published port is reachable
	ListenAddr string
	// RedirectURL overrides the registered callback url when it differs from
	// the listen address (e.g. docker port mapping); default http://127.0.0.1:<port>/callback
	RedirectURL string
	// ExtraAuthParams are appended to the authorization request (e.g. access_type=offline for Google)
	ExtraAuthParams map[string]string
	// OpenBrowser opens the authorization url; nil = the url is only printed to Out
	OpenBrowser func(url string) error
	// Out receives user-facing instructions, default os.Stderr behaviour is up to the caller
	Out io.Writer
}

type Preset

type Preset struct {
	AuthURL       string
	TokenURL      string
	DeviceAuthURL string
	// AuthStyle matches config.OAuth2Config.AuthStyle: "" auto, "header", "params"
	AuthStyle string
	// SupportsDeviceFlow reports whether the provider offers the device_code grant
	SupportsDeviceFlow bool
	// ExtraAuthParams are appended to the browser-flow authorization request
	ExtraAuthParams map[string]string
}

Preset holds well-known endpoints and quirks of an oauth2 provider

Jump to

Keyboard shortcuts

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