login

package
v0.0.0-...-f5c2ca4 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2023 License: GPL-3.0 Imports: 23 Imported by: 0

Documentation

Overview

Package login implements the authentication workflow, protecting another application by means of its Wrap() middleware.

Internally, it runs a simple state machine meant to match the interactions with the underlying auth-server. State transitions happen on POST requests. Request handling is split into two stages: the processing stage (processing request parameters and eventually modifying the current state), and the rendering stage (which renders content to the user).

- we start from the BEGIN state, where the user is asked for username and password.

- we make the first AuthRequest, which has two possible non-error return values ("ok" and "need 2fa"), resulting in the OK or 2FA states.

- in the 2FA state, we present the user with a request for the second authentication factor. We make a second AuthRequest that includes second factor information, resulting in the OK state if successful.

States are tied to specific URLs because we want to make states visible to the browser, and possibly give users the option of hitting the back button -- though doing so will likely result in being reset to the BEGIN state (but it makes it easier to have multiple endpoints for the 2FA state).

The login state machine is stored in a short-lived session cookie, which is global browser state, but the original_url parameter must instead be tracked per-window, so it must be brought along the request flow as a form parameter.

Index

Constants

View Source
const (
	StateBEGIN = iota
	State2FA_OTP
	State2FA_U2F
	StateOK
)

Variables

This section is empty.

Functions

func GetSessionID

func GetSessionID(ctx context.Context) (string, bool)

GetSessionID retrieves the session ID which is available during the AuthClient.Authenticate call.

func Wrap

func Wrap(wrap http.Handler, authClient AuthClient, config *Config, urls common.URLMap) (http.Handler, error)

Wrap a http.Handler with an authentication web UI backed by authClient. The wrapped code can then call GetAuth() to obtain the authenticated user's details.

Types

type Auth

type Auth struct {
	// User name and other information (like group membership).
	Username string         `json:"u"`
	UserInfo *auth.UserInfo `json:"ui"`

	// Sticky session ID.
	SessionID string `json:"sid"`

	// Deadline until authentication will need to be renewed. The
	// securecookie also provides a similar expiration mechanism,
	// but we do not use it here because we want to be able to
	// detect the expiration for UX purposes.
	Deadline time.Time `json:"d"`
}

func GetAuth

func GetAuth(ctx context.Context) (*Auth, bool)

GetAuth returns the current user information, if any. Presence of an Auth object implies that the authentication succeeded in all contexts *except* the logout handler.

type AuthClient

type AuthClient interface {
	Authenticate(context.Context, *auth.Request) (*auth.Response, error)
	Logout(context.Context, string, *auth.UserInfo) error
}

AuthClient is a wrapper interface for an id/auth.Client that adds support for a Logout event. This allows injection of state-aware components that can trigger on both successful authentication and logout to maintain external session-scoped state.

type AuthServiceList

type AuthServiceList struct {
	// Services the user has logged in to from this session.
	Services []string `json:"s"`
	// contains filtered or unexported fields
}

func GetServiceList

func GetServiceList(ctx context.Context) (*AuthServiceList, bool)

GetServiceList returns the AuthServiceList object associated with the current session.

func (*AuthServiceList) AddService

func (s *AuthServiceList) AddService(service string)

AddService adds a service to the current session (if it's not already there).

func (*AuthServiceList) Delete

func (*AuthServiceList) Save

type Config

type Config struct {
	ui.Config `yaml:",inline"`

	AuthService                string `yaml:"auth_service"`
	AuthSessionLifetimeSeconds int    `yaml:"auth_session_lifetime"`

	DeviceManager *device.Config `yaml:"device_manager"`

	SessionAuthKey          common.SessionAuthenticationKey `yaml:"session_auth_key"`
	SessionEncKey           common.SessionEncryptionKey     `yaml:"session_enc_key"`
	CSRFSecret              common.YAMLBytes                `yaml:"csrf_secret"`
	TrustedOrigins          []string                        `yaml:"trusted_origins"`
	DefaultSignedInRedirect string                          `yaml:"default_signed_in_redirect"`
	CookieSameSiteMode      string                          `yaml:"cookie_same_site_mode"`
}

type State

type State int

State enum for the login state machine.

func (State) String

func (s State) String() string

Jump to

Keyboard shortcuts

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