auth

package
v0.5.56 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: BSD-3-Clause Imports: 27 Imported by: 3 modules, 2 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 a system for identifying and authenticating users through third party cloud systems in Cogent Core apps.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoToken = errors.New("auth: no saved token")

ErrNoToken is the error of Auth and TokenSource with AuthConfig.NoPrompt when there is no saved token that works.

Functions

func Auth

func Auth(c *AuthConfig) (*oauth2.Token, *oidc.UserInfo, error)

Auth authenticates the user using the given configuration information and returns the resulting oauth token and user info. See AuthConfig for more information on the configuration options. Use TokenSource to keep the access token fresh.

func Button

func Button(par core.Widget, c *ButtonsConfig, provider string, authFunc func(c *AuthConfig) (*oauth2.Token, *oidc.UserInfo, error)) *core.Button

Button makes a new button for signing in with the provider that has the given name and auth func. It should not typically be used by end users; instead, use Buttons or the platform-specific functions (eg: Google). The configuration options can be nil, in which case default values will be used.

func Buttons

func Buttons(par core.Widget, c *ButtonsConfig) *core.Frame

Buttons adds a new vertical layout to the given parent with authentication buttons for major platforms, using the given configuration options. See ButtonsConfig for more information on the configuration options. The configuration options can be nil, in which case default values will be used.

func DeleteToken

func DeleteToken(file string) error

DeleteToken deletes the token saved for the given token file (see AuthConfig.TokenFile), from the system keychain and the file.

func Google

func Google(c *AuthConfig) (*oauth2.Token, *oidc.UserInfo, error)

Google authenticates the user with Google using Auth and the given configuration information and returns the resulting oauth token and user info. It sets the values of AuthConfig.ProviderName, AuthConfig.ProviderURL, AuthConfig.ClientID, and AuthConfig.ClientSecret if they are not already set.

func GoogleButton

func GoogleButton(par core.Widget, c *ButtonsConfig) *core.Button

GoogleButton adds a new button for signing in with Google to the given parent using the given configuration information.

func GoogleTokenSource

func GoogleTokenSource(c *AuthConfig) (oauth2.TokenSource, *oidc.UserInfo, error)

GoogleTokenSource authenticates the user with Google using TokenSource, setting the values of the configuration like Google.

func Microsoft

func Microsoft(c *AuthConfig) (*oauth2.Token, *oidc.UserInfo, error)

Microsoft authenticates the user with Microsoft (a work or school account of Microsoft 365, or a personal one, as of Outlook.com) using Auth, setting the values of the configuration like Google, with the client ID of the environment variable MICROSOFT_OAUTH2_CLIENT_ID (of an app registered in the Microsoft Entra admin center, as a public client, of the platform "Mobile and desktop applications", with the redirect URI http://127.0.0.1:5556/auth/microsoft/callback), and no client secret.

func MicrosoftTokenSource

func MicrosoftTokenSource(c *AuthConfig) (oauth2.TokenSource, *oidc.UserInfo, error)

MicrosoftTokenSource authenticates the user with Microsoft using TokenSource, setting the values of the configuration like Microsoft.

func SaveToken

func SaveToken(token *oauth2.Token, file string) error

SaveToken saves the given token for the given token file (see AuthConfig.TokenFile) in the system keychain (as the macOS Keychain), with the name of the file, removing the file if any. Where there is no keychain, as on a Linux server, it is saved in the file instead, readable only by the user.

func TokenSource

func TokenSource(c *AuthConfig) (oauth2.TokenSource, *oidc.UserInfo, error)

TokenSource authenticates the user like Auth, and returns a token source whose tokens have a valid access token, refreshed as needed (which requires a refresh token, which the provider returns for the offline access requested here), and the user info. Each refreshed token is saved to the token file (see AuthConfig.TokenFile).

Types

type AuthConfig

type AuthConfig struct {
	// Ctx is the context to use. It is [context.TODO] if unspecified.
	// Canceling it stops waiting for the user to sign in in the browser.
	Ctx context.Context

	// ProviderName is the name of the provider to authenticate with (eg: "google")
	ProviderName string

	// ProviderURL is the URL of the provider (eg: "https://accounts.google.com")
	ProviderURL string

	// ClientID is the client ID for the app, which is typically obtained through a developer oauth
	// portal (eg: the Credentials section of https://console.developers.google.com/).
	ClientID string

	// ClientSecret is the client secret for the app, which is typically obtained through a developer oauth
	// portal (eg: the Credentials section of https://console.developers.google.com/).
	ClientSecret string

	// TokenFile is an optional function that returns the filename at which the token for the given user will be stored as JSON,
	// or rather the name of the token in the system keychain, where it is stored if there is one (see [SaveToken]).
	// If it is nil or it returns "", the token is not stored. Also, if it is non-nil, Auth skips the user-facing authentication
	// step if it finds a valid token at the file (ie: remember me). It checks all [AuthConfig.Accounts] until it finds one
	// that works for that step. If [AuthConfig.Accounts] is nil, it checks with a blank ("") email account.
	// The token is saved again whenever its access token is refreshed (see [TokenSource]).
	TokenFile func(email string) string

	// Accounts are optional accounts to check for the remember me feature described in [AuthConfig.TokenFile].
	// If it is nil and TokenFile is not, it defaults to contain one blank ("") element.
	Accounts []string

	// Scopes are additional scopes to request beyond the default "openid", "profile", and "email" scopes
	Scopes []string

	// SelectAccount is whether to always have the user sign in in the
	// browser, choosing the account there, without using a saved token,
	// as for adding another account.
	SelectAccount bool

	// LoginHint, if set, is the email address of the account to suggest
	// when the user signs in in the browser, as for signing in to an
	// account again.
	LoginHint string

	// NoPrompt is whether to only use a saved token, returning
	// [ErrNoToken] if there is none that works, instead of having the
	// user sign in in the browser.
	NoPrompt bool

	// IssuerURL, if set, is the issuer in the discovery document of the
	// provider, when it is not the ProviderURL, as for the endpoint of
	// Microsoft for the accounts of all organizations, whose issuer has
	// "{tenantid}" in it (each organization has its own).
	IssuerURL string

	// UserFromIDToken is whether to get the user info (the email address)
	// from the ID token of the provider, instead of its user info
	// endpoint, as for Microsoft, whose endpoint does not accept the
	// access tokens of other resources (as those for mail).
	UserFromIDToken bool

	// OnePrompt is whether the provider takes only one value of the
	// prompt parameter, as Microsoft: "select_account" for
	// SelectAccount, and none otherwise (it returns a refresh token for
	// the offline_access scope without asking for consent each time).
	OnePrompt bool
}

AuthConfig is the configuration information passed to Auth.

type ButtonsConfig

type ButtonsConfig struct {
	// SuccessFunc, if non-nil, is the function called after the user successfully
	// authenticates. It is passed the user's authentication token and info.
	SuccessFunc func(token *oauth2.Token, userInfo *oidc.UserInfo)

	// TokenFile, if non-nil, is the function used to determine what token file function is
	// used for [AuthConfig.TokenFile]. It is passed the provider being used (eg: "google") and the
	// email address of the user authenticating.
	TokenFile func(provider, email string) string

	// Accounts are optional accounts to check for the remember me feature described in [AuthConfig.TokenFile].
	// See [AuthConfig.Accounts] for more information. If it is nil and TokenFile is not, it defaults to contain
	// one blank ("") element.
	Accounts []string

	// Scopes, if non-nil, is a map of scopes to pass to [Auth], keyed by the
	// provider being used (eg: "google").
	Scopes map[string][]string
}

ButtonsConfig is the configuration information passed to Buttons.

type ScopesError

type ScopesError struct {
	// Missing are the requested scopes that were not allowed.
	Missing []string
}

ScopesError is the error of Auth and TokenSource when the user did not allow all of the access of the requested scopes, as the provider can let the user uncheck some of them when signing in.

func (*ScopesError) Error

func (e *ScopesError) Error() string

Directories

Path Synopsis
Package cicons provides the icons of the sign-in buttons of the auth package, generated from their SVG files.
Package cicons provides the icons of the sign-in buttons of the auth package, generated from their SVG files.
examples
basic command
scopes command

Jump to

Keyboard shortcuts

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