auth

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: BSD-3-Clause Imports: 38 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package auth provides OIDC and OAuth 2.0 authentication.

On a desktop the package implements the Authorization Code flow with PKCE, which is the recommended flow for native/desktop applications. It opens the user's system browser for authentication and receives the callback on a temporary localhost HTTP server.

On phones the same Authenticate call uses the platform's own sign-in:

  • "apple" on iOS shows the Sign in with Apple sheet. The app needs the com.apple.developer.applesignin entitlement in ios/Entitlements.plist.
  • "google" on Android uses Credential Manager. The app ships the androidx.credentials and googleid libraries in android/libs, and ClientID is the Web client ID: the audience of the token Google returns.
  • "google" on iOS runs the PKCE flow in ASWebAuthenticationSession, with ClientID the iOS client ID and the redirect on its reversed-ID scheme.

Other providers return an error on phones.

Supported providers: Google, Microsoft (Entra ID), Apple, GitHub, Okta, Keycloak, and any custom OIDC-compliant provider.

Index

Constants

This section is empty.

Variables

View Source
var ErrCancelled = errors.New("auth: sign-in cancelled")

ErrCancelled is the error Authenticate returns when the user closes the sign-in sheet or browser session without signing in. It is told apart with errors.Is. The desktop flow, which cannot see the browser tab close, never returns it.

Since: 1.3

View Source
var ErrNoAppID = errors.New("auth: storing a sign-in token needs an app ID; create the app with app.NewWithID")

ErrNoAppID is returned by SaveToken, LoadToken and ClearToken, and by SaveTokenFor, LoadTokenFor and ClearTokenFor, when the running app has no unique ID. Tokens are stored per app, so an app that signs users in must be created with app.NewWithID (or have an ID in its manifest.pxf), as Preferences already require.

View Source
var Providers = map[string]ProviderPreset{
	"google": {
		Name:       "Google",
		Issuer:     "https://accounts.google.com",
		Logo:       bellui.NewStaticResource("google.svg", googleLogo),
		BgColor:    color.NRGBA{R: 0xFF, G: 0xFF, B: 0xFF, A: 0xFF},
		TextColor:  color.NRGBA{R: 0x1F, G: 0x1F, B: 0x1F, A: 0xFF},
		ButtonText: "Sign in with Google",
	},
	"microsoft": {
		Name:       "Microsoft",
		Issuer:     "https://login.microsoftonline.com/common/v2.0",
		Logo:       bellui.NewStaticResource("microsoft.svg", microsoftLogo),
		BgColor:    color.NRGBA{R: 0x2F, G: 0x2F, B: 0x2F, A: 0xFF},
		TextColor:  color.NRGBA{R: 0xFF, G: 0xFF, B: 0xFF, A: 0xFF},
		ButtonText: "Sign in with Microsoft",
	},
	"apple": {
		Name:       "Apple",
		Issuer:     "https://appleid.apple.com",
		Logo:       bellui.NewStaticResource("apple.svg", appleLogo),
		BgColor:    color.NRGBA{R: 0x00, G: 0x00, B: 0x00, A: 0xFF},
		TextColor:  color.NRGBA{R: 0xFF, G: 0xFF, B: 0xFF, A: 0xFF},
		ButtonText: "Sign in with Apple",
	},
	"github": {
		Name:       "GitHub",
		Issuer:     "",
		Logo:       bellui.NewStaticResource("github.svg", githubLogo),
		BgColor:    color.NRGBA{R: 0x24, G: 0x29, B: 0x2E, A: 0xFF},
		TextColor:  color.NRGBA{R: 0xFF, G: 0xFF, B: 0xFF, A: 0xFF},
		ButtonText: "Sign in with GitHub",
	},
	"okta": {
		Name:       "Okta",
		Issuer:     "",
		Logo:       bellui.NewStaticResource("okta.svg", oktaLogo),
		BgColor:    color.NRGBA{R: 0x00, G: 0x7D, B: 0xC1, A: 0xFF},
		TextColor:  color.NRGBA{R: 0xFF, G: 0xFF, B: 0xFF, A: 0xFF},
		ButtonText: "Sign in with Okta",
	},
	"keycloak": {
		Name:       "Keycloak",
		Issuer:     "",
		Logo:       nil,
		BgColor:    color.NRGBA{R: 0x4D, G: 0x4D, B: 0x4D, A: 0xFF},
		TextColor:  color.NRGBA{R: 0xFF, G: 0xFF, B: 0xFF, A: 0xFF},
		ButtonText: "Sign in with Keycloak",
	},
}

Providers maps provider keys to their presets.

Functions

func ClearToken

func ClearToken(provider string) error

ClearToken removes the running app's stored token for a provider. It does not remove the tokens SaveTokenFor keeps for the provider's issuers: sign out of one of those with ClearTokenFor.

func ClearTokenFor added in v1.2.0

func ClearTokenFor(provider, issuer string) error

ClearTokenFor removes the token SaveTokenFor stored for one issuer of a provider. An empty issuer, or the provider preset's own, is the same as ClearToken.

Since: 1.2

func LoadToken

func LoadToken(provider string) (refreshToken, email string, err error)

LoadToken reads and decrypts the running app's stored refresh token for a provider. Returns empty strings if no token is stored.

func LoadTokenFor added in v1.2.0

func LoadTokenFor(provider, issuer string) (refreshToken, email string, err error)

LoadTokenFor reads back the token SaveTokenFor stored for one issuer of a provider. An empty issuer, or the provider preset's own, is the same as LoadToken.

Since: 1.2

func SaveToken

func SaveToken(provider, refreshToken, email string) error

SaveToken encrypts and persists a refresh token for a provider. Tokens are stored per app (by the running app's unique ID), so two apps that sign in with the same provider keep separate tokens.

One token is kept per provider name. For a provider whose issuer the app chooses ("okta", "keycloak", "custom"), use SaveTokenFor, so that two issuers under one provider name keep separate tokens.

func SaveTokenFor added in v1.2.0

func SaveTokenFor(provider, issuer, refreshToken, email string) error

SaveTokenFor is SaveToken for one issuer of a provider: tokens for two issuers under the same provider name, such as two Keycloak realms, are kept apart (#356). An empty issuer, or the provider preset's own (see Providers), is the same as SaveToken. widget.AuthButton saves this way, with its Issuer.

Since: 1.2

Types

type OIDCConfig

type OIDCConfig struct {
	// Provider is a known provider key ("google", "microsoft", "apple",
	// "github", "okta", "keycloak") or "custom" for arbitrary OIDC providers.
	Provider string

	// Issuer is the OIDC issuer URL. For known providers this is set
	// automatically from the preset. Required for "okta", "keycloak",
	// and "custom" providers.
	Issuer string

	// ClientID is the OAuth 2.0 client identifier.
	ClientID string

	// Scopes to request. Defaults to ["openid", "email", "profile"], or to
	// ["user:email", "read:user"] for GitHub.
	Scopes []string

	// Nonce, when set, is the nonce sent in the authorization request; the
	// flow then requires the ID token to carry it. A server that issued it
	// can check the token against it, so a captured token cannot be replayed
	// against another request. Empty means a random nonce, checked the same
	// way but known only to this flow.
	//
	// Since: 1.3
	Nonce string

	// Timeout for the entire flow. Defaults to 5 minutes.
	Timeout time.Duration
}

OIDCConfig configures an authentication flow.

type OIDCResult

type OIDCResult struct {
	Email        string `json:"email"`
	Name         string `json:"name"`
	Picture      string `json:"picture"`
	Sub          string `json:"sub"` // unique user identifier
	AccessToken  string `json:"-"`
	RefreshToken string `json:"-"`
	IDToken      string `json:"-"`

	// AuthorizationCode is the one-time code a native sign-in sheet returns
	// beside the ID token. Sign in with Apple on iOS gives one: the app's
	// server redeems it with Apple for the user's refresh token. The sheet
	// gives no access or refresh token itself. Empty for the other flows,
	// which redeem their code themselves.
	//
	// Since: 1.3
	AuthorizationCode string `json:"-"`
}

OIDCResult contains the authenticated user's information.

func Authenticate

func Authenticate(cfg OIDCConfig) (*OIDCResult, error)

Authenticate runs the full OIDC Authorization Code + PKCE flow. It opens the system browser, waits for the callback, exchanges the code for tokens, and returns the user's claims.

On iOS and Android it uses the platform's own sign-in where the package has one (see the package documentation), and returns the same verified claims. A user who closes the sheet gets ErrCancelled.

type ProviderPreset

type ProviderPreset struct {
	// Name is the human-readable provider name.
	Name string

	// Issuer is the OIDC discovery base URL.
	// Empty for providers that need a tenant-specific issuer (Okta, Keycloak).
	Issuer string

	Logo bellui.Resource

	// BgColor is the brand-compliant button background color.
	BgColor color.NRGBA

	// TextColor is the brand-compliant button text color.
	TextColor color.NRGBA

	// ButtonText is the default button label (e.g., "Sign in with Google").
	ButtonText string
}

ProviderPreset defines the visual identity and discovery URL for a known authentication provider.

Jump to

Keyboard shortcuts

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