auth

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT, MIT Imports: 30 Imported by: 0 modules, 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 is the backend-agnostic authentication conversation (crates/wayle-auth): a backend — PAM for the lock screen, greetd for the greeter — asks one or more questions (password, OTP, "your password expired, enter a new one") and the UI answers each in turn.

A backend implements Conversation and drives the whole exchange on a worker goroutine, calling ask whenever it needs the user. Spawn wires ask to the UI: every prompt goes out through onEvent, and the UI replies through the returned Handle. The package has no GUI dependency, so the lock screen and the greeter drive it identically.

Index

Constants

View Source
const (
	GreetdCreateSession = "create_session"
	GreetdPostResponse  = "post_auth_message_response"
	GreetdStartSession  = "start_session"
	GreetdCancelSession = "cancel_session"
)

The request types.

View Source
const GreetdSockEnv = "GREETD_SOCK"

GreetdSockEnv names the IPC socket greetd hands its greeter.

View Source
const SessionTypeEnv = "XDG_SESSION_TYPE"

SessionTypeEnv is the variable pam_systemd registers a session's type with, and sessions read to tell what they run on.

Variables

View Source
var ErrCancelled = errors.New("cancelled")

ErrCancelled is the failure a backend reports when the user cancelled an input prompt.

Functions

func AwaitLoginKeyring

func AwaitLoginKeyring(ctx context.Context, env xdg.Env, password, previous []byte) error

AwaitLoginKeyring is unlockOrRekey for a session with environment env that is still starting: it activates a keyring on the session's bus and tries until lys-secrets or gnome-keyring answers, and returns what that keyring said, or what it found instead when none answered before ctx ended.

func CheckLibrary

func CheckLibrary() error

func CurrentUsername

func CurrentUsername() string

CurrentUsername is the session user's login name from the environment: USER, then LOGNAME, then "". A locked graphical session always carries one of them.

func WriteGreetdError

func WriteGreetdError(w io.Writer, authFailure bool, description string) error

WriteGreetdError answers with an error: an authentication failure, or any other.

func WriteGreetdFingerprint

func WriteGreetdFingerprint(w io.Writer, r fprint.Result) error

func WriteGreetdPrompt

func WriteGreetdPrompt(w io.Writer, p Prompt) error

WriteGreetdPrompt sends a PAM prompt for the greeter to show and answer.

func WriteGreetdSuccess

func WriteGreetdSuccess(w io.Writer) error

WriteGreetdSuccess answers a request with success.

Types

type Ask

type Ask func(Prompt) (answer string, ok bool)

Ask is how a backend reaches the user. For prompts that want input it blocks until the UI answers and returns the answer, with ok false when the user cancelled; for info and error prompts it returns ("", false) at once.

type AskUntil

type AskUntil func(p Prompt, withdrawn <-chan struct{}) (answer string, ok bool)

type Conversation

type Conversation interface {
	Run(username string, ask Ask) error
}

Conversation is a backend that drives one authentication to completion. Run executes on a worker goroutine (blocking I/O is fine) and must never touch the GUI. username may be empty to let the backend ask for it. The error is the failure reason.

type Event

type Event struct {
	Kind EventKind
	// Prompt is set for EventPrompt.
	Prompt Prompt
	// Reason is set for EventFailure: the detail for the log, not for the
	// person logging in.
	Reason string
	// Refusal is set for EventFailure when PAM refused the login.
	Refusal Refusal
}

Event is surfaced to the UI as a conversation progresses.

type EventKind

type EventKind uint8

EventKind is what an Event reports.

const (
	// EventPrompt: the backend asks something (see Prompt.WantsInput).
	EventPrompt EventKind = iota
	// EventSuccess: authentication succeeded; the conversation is over.
	EventSuccess
	// EventFailure: authentication failed or was aborted; the
	// conversation is over, a retry starts a fresh one.
	EventFailure
)

Event kinds.

type FingerprintServices

type FingerprintServices struct {
	Typed, Fingerprint string
}

type Greetd

type Greetd struct {
	// contains filtered or unexported fields
}

Greetd is the pre-login backend (greetd_backend.rs): an unprivileged greeter does not talk to PAM, greetd does, over its IPC socket — a native-endian u32 length prefix before each JSON body. One Run is the whole login: create the session, answer every auth message, and on success start the session; greetd then replaces the greeter.

func NewGreetd

func NewGreetd(conn io.ReadWriter, start Start) *Greetd

NewGreetd runs the protocol over an established connection.

func NewGreetdFromEnv

func NewGreetdFromEnv(start Start) (*Greetd, error)

NewGreetdFromEnv connects to the socket in $GREETD_SOCK.

func (*Greetd) Close

func (g *Greetd) Close() error

Close closes the connection when it is closable.

func (*Greetd) Run

func (g *Greetd) Run(username string, ask Ask) error

Run implements Conversation: create_session, one post_auth_message_response per auth message, then start_session. A greetd error or a cancelled input prompt cancels the session.

type GreetdRequest

type GreetdRequest struct {
	Type     string   `json:"type"`
	Username string   `json:"username"`
	Response *string  `json:"response"`
	Cmd      []string `json:"cmd"`
	Env      []string `json:"env"`
}

GreetdRequest is one request a greeter sends over the greetd protocol, as the server side reads it.

func ReadGreetdRequest

func ReadGreetdRequest(r io.Reader) (GreetdRequest, error)

ReadGreetdRequest reads one request from a greeter; an unknown type is an error.

type Handle

type Handle struct {
	// contains filtered or unexported fields
}

Handle is the UI's side of a running conversation: it answers the pending prompt. The zero value is not usable; Spawn returns one.

func Spawn

func Spawn(conv Conversation, username string, onEvent func(Event)) *Handle

Spawn runs conv on a worker goroutine. Prompts reach onEvent (called from the worker: marshal it onto the UI loop), and the UI answers through the returned Handle. Exactly one terminal EventSuccess or EventFailure is emitted before the worker exits.

func (*Handle) Answer

func (h *Handle) Answer(text string)

Answer replies to the prompt on screen. A reply nothing waits for — the conversation already ended — is dropped, never blocking the UI.

func (*Handle) Cancel

func (h *Handle) Cancel()

Cancel aborts the conversation: a backend blocked in ask sees a cancellation, and every later ask does too. Idempotent.

type Login

type Login struct {
	// contains filtered or unexported fields
}

Login is a display manager's PAM login: one transaction from the authentication through the session it opens, ended by Close. PAM keeps per-thread state, and pam_loginuid writes /proc/self/loginuid, which the kernel lets only the main thread write, so every call must come from the process's main thread; a session worker stays on it for its whole life.

func Authenticate

func Authenticate(service, user string, ask Ask) (*Login, error)

Authenticate starts service's transaction for user, authenticates it through ask and checks the account. A failure ends the transaction.

func AuthenticateOrFingerprint

func AuthenticateOrFingerprint(services FingerprintServices, user string, ask AskUntil, scan Scanner) (*Login, error)

func Greeter

func Greeter(service, user string) (*Login, error)

Greeter starts service's transaction for the greeter's user, who signs nothing in: only its session opens.

func Passwordless

func Passwordless(service, user string) (*Login, error)

func (*Login) Close

func (l *Login) Close() error

Close closes an opened session, deletes the credentials it established, and ends the transaction.

func (*Login) HasSecret

func (l *Login) HasSecret() bool

func (*Login) OpenSession

func (l *Login) OpenSession(env []string) ([]string, error)

OpenSession hands env (KEY=value: the session's type, class, seat and VT, which pam_systemd registers it with) to the transaction, establishes the credentials, opens the session, and returns the environment the session's process starts with.

func (*Login) TakePrevious

func (l *Login) TakePrevious() []byte

func (*Login) TakeSecret

func (l *Login) TakeSecret() []byte

TakeSecret hands over the last secret the user authenticated with (the password, for a password login), nil without one, and forgets it: the caller clears it after use.

func (*Login) Typed

func (l *Login) Typed() bool

func (*Login) User

func (l *Login) User() string

User is the account the login is for.

type PAM

type PAM struct {
	// Service is the PAM service name (/etc/pam.d/<service>).
	Service string
	// contains filtered or unexported fields
}

PAM authenticates against a PAM service (e.g. system-auth): the in-session backend of the lock screen. The conversation runs on the worker goroutine Spawn creates, locked to its OS thread for the transaction's lifetime, so the UI loop never blocks on PAM. The password is never logged.

func NewPAM

func NewPAM(service string, verified *Verified) *PAM

NewPAM returns the backend for service over the system libpam, whose verified passwords go to verified.

func (*PAM) Run

func (p *PAM) Run(username string, ask Ask) error

Run implements Conversation. The transaction opens for username (the session user when empty), authenticates, then checks the account (pam_authenticate + pam_acct_mgmt), and always ends with pam_end carrying the last call's status, as nonstick's transaction does; it sets no credentials, an unlock needs none. On success the last secret answered goes to Verified, which does not hold up the result: it unlocks the session keyring's login collection (lys-secrets, else gnome-keyring), best effort, and reports its own failures.

type Prompt

type Prompt struct {
	Kind        PromptKind
	Text        string
	Fingerprint fprint.Result `json:",omitempty"`
}

Prompt is one thing a backend wants from the user.

func (Prompt) WantsInput

func (p Prompt) WantsInput() bool

WantsInput reports whether the prompt expects a typed answer.

type PromptKind

type PromptKind uint8

PromptKind is what a prompt wants from the user.

const (
	// PromptSecret is echo-off input (password, PIN).
	PromptSecret PromptKind = iota
	// PromptVisible is echo-on input (username, OTP).
	PromptVisible
	// PromptInfo is informational text; no answer.
	PromptInfo
	// PromptError is error text; no answer.
	PromptError
	PromptFingerprint
)

Prompt kinds.

func (PromptKind) String

func (k PromptKind) String() string

String names the kind for logs.

type Refusal

type Refusal uint8

Refusal is why a login was turned down, for a login screen to word. The zero value claims nothing, so a failure nobody classified never reads as a wrong password.

const (
	RefusalUnknown Refusal = iota
	// RefusalCredentials: a wrong password or an unknown user, one
	// refusal so a login screen does not reveal which accounts exist.
	RefusalCredentials
	// RefusalExpired: the account or its password expired.
	RefusalExpired
	// RefusalTooManyAttempts: a module gave up after repeated failures.
	RefusalTooManyAttempts
	// RefusalDenied: the account may not log in, whatever its password.
	RefusalDenied
)

Refusals.

type RefusedError

type RefusedError struct {
	Call, Code string
}

RefusedError is PAM turning a login down: the call that refused it and the code it returned. Its text is what greetd's protocol carries.

func (*RefusedError) Error

func (e *RefusedError) Error() string

func (*RefusedError) Refusal

func (e *RefusedError) Refusal() Refusal

Refusal classifies the code; a code outside pamCodes is unknown.

type Scanner

type Scanner func(user string) (matched <-chan struct{}, halt func())

type Session

type Session struct {
	Cmd, Env []string
}

Session is the command and the environment a login starts.

type Start

type Start struct {
	Session func() Session
	Before  func()
}

Start is what a successful login does: Session, read at start_session, says what it starts, and Before runs between the authentication and start_session.

type Verified

type Verified struct {
	// contains filtered or unexported fields
}

Verified is what follows a verified password: the keyring, the vault. It runs behind the success rather than before it, since a keyring deriving its key must not keep the screen locked, and whoever ends the process waits for it, or the keyring would never hear of the password.

func NewVerified

func NewVerified(hooks ...func(password []byte)) *Verified

NewVerified is what follows a verified password: each of hooks in turn, then unlocking the session keyring. Each borrows the password until it returns.

func (*Verified) Wait

func (v *Verified) Wait()

Wait returns once every verified password has been dealt with.

Jump to

Keyboard shortcuts

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