auth

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: MIT Imports: 11 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 turns a configured account into a usable session.

It is the seam between the command layer and the transport: commands ask for a session, this package finds the credential and builds the client. Without it the CLI would be calling Moodle directly, which the import rules forbid precisely because that is how presentation and protocol get welded together.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NotAvailable

func NotAvailable(method, reason string) error

NotAvailable builds the error for a method that cannot run.

Types

type Availability

type Availability string

Availability is what a probe could establish about a login method.

The three-way answer matters: "I could not tell" is not "no". Collapsing them into a bool would make an offline site look like one that forbids every login method.

const (
	Available   Availability = "available"
	Unavailable Availability = "unavailable"
	Unknown     Availability = "unknown"
)

type Candidate

type Candidate struct {
	Method Method
	Probe  ProbeResult
}

Candidate is a method paired with what a probe said about it.

type Capabilitier

type Capabilitier interface {
	Capabilities(ctx context.Context) (*site.Capabilities, error)
}

Capabilitier reports what the signed-in account may do.

type ClientFactory

type ClientFactory func(target site.Site) *moodle.Client

ClientFactory builds a transport client for a site. The composition root supplies it, so tests can point a session at a fake Moodle.

type Coordinator

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

Coordinator picks a login method and runs it.

The order lives here rather than in the methods themselves. A method can only see its own situation; deciding that a token beats a password, and that a password beats pasting a callback by hand, needs the whole picture.

func NewCoordinator

func NewCoordinator(manager *Manager, methods ...Method) *Coordinator

NewCoordinator builds a Coordinator. The slice order is the preference order: most reliable and least disruptive first.

func (*Coordinator) Authenticate

func (c *Coordinator) Authenticate(ctx context.Context, req Request, methodName string) (Credential, error)

Authenticate runs one named method, or picks the best available one.

Choosing automatically never includes a method that needs the user to paste something: silently demanding manual work is worse than saying which methods exist and letting them choose.

func (*Coordinator) Candidates

func (c *Coordinator) Candidates(ctx context.Context, target site.Site, config *PublicConfig) []Candidate

Candidates probes every method against a site.

A nil config means the site could not be asked. Every method then reports Unknown rather than Unavailable: an unreachable site must not look like one that forbids signing in.

func (*Coordinator) Find

func (c *Coordinator) Find(name string) (Method, error)

Find returns a method by name.

func (*Coordinator) Methods

func (c *Coordinator) Methods() []Method

Methods returns the registered methods in preference order.

type Credential

type Credential struct {
	Token        string
	PrivateToken string
	// Method names how it was obtained, for the configuration record.
	Method string
}

Credential is what a successful login produced.

type Manager

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

Manager opens sessions and stores credentials.

func NewManager

func NewManager(secrets secret.Store, newClient ClientFactory) *Manager

NewManager builds a Manager.

func (*Manager) Forget

func (m *Manager) Forget(siteID, accountID site.ID) error

Forget deletes every credential held for an account.

It never asks Moodle to revoke the token: the same token is often the one the user's phone app holds, and signing out here must not sign them out there.

func (*Manager) KeepCredentialFresh

func (m *Manager) KeepCredentialFresh(s *Session, siteID, accountID site.ID)

KeepCredentialFresh lets a session recover from a credential replaced while it is running.

Only a long-lived process needs it, and only a long-lived process should ask for it: re-reading the keychain on a command that lasts half a second buys nothing, and every keychain read is a prompt on some platforms.

It reads through the manager, so what reaches the transport is a way to ask rather than a copy of the answer.

func (*Manager) Open

func (m *Manager) Open(target site.Site, siteID, accountID site.ID) (*Session, error)

Open builds a session from a stored credential.

The token is read at the moment it is needed rather than kept in the configuration, so it only ever lives in the keychain and in memory.

func (*Manager) OpenWithSession

func (m *Manager) OpenWithSession(target site.Site, accountID site.ID, cookie string) *Session

OpenWithSession builds a session that authenticates with a browser session rather than a token, for sites that issue none. The cookie is given as text because the command layer may not name the transport's types; parsing it is this seam's job.

func (*Manager) OpenWithToken

func (m *Manager) OpenWithToken(target site.Site, accountID site.ID, token string) *Session

OpenWithToken builds a session from a token the caller already holds, such as one supplied through the environment for a single run.

func (*Manager) Probe

func (m *Manager) Probe(target site.Site) Prober

Probe returns a Prober for a site, usable before signing in.

func (*Manager) Session

func (m *Manager) Session(siteID, accountID site.ID) (string, error)

Session returns a stored browser session for an account.

func (*Manager) StoreCredential

func (m *Manager) StoreCredential(siteID, accountID site.ID, credential Credential) error

StoreCredential persists every secret returned by a login method.

The private token is optional, but when Moodle returns one it is part of the credential rather than disposable metadata. If storing it fails after the web-service token was replaced, the old token is restored so a failed login does not silently change the account underneath its configuration.

func (*Manager) StoreSession

func (m *Manager) StoreSession(siteID, accountID site.ID, cookie string) error

StoreSession saves a browser session that has been shown to work.

A session is stored rather than exchanged for a token because the exchange is usually not available: Moodle only offers it to a session that has just signed in, and a session taken out of a browser somebody has been using is past that by definition — measured against Moodle 5.2, with a cookie read from a real Firefox profile.

func (*Manager) StoreToken

func (m *Manager) StoreToken(siteID, accountID site.ID, token string) error

StoreToken saves a verified token.

func (*Manager) Token

func (m *Manager) Token(siteID, accountID site.ID) (string, error)

Token returns the stored web service token for an account.

type Method

type Method interface {
	Name() string
	Describe() string
	Probe(ctx context.Context, target site.Site, config *PublicConfig) ProbeResult
	Authenticate(ctx context.Context, req Request) (Credential, error)
}

Method is one way of signing in.

A method reports whether it could work and performs the login. It does not decide whether it should be preferred: ordering is the coordinator's job, because only the coordinator can see the alternatives.

type ProbeResult

type ProbeResult struct {
	Availability Availability
	// Reason explains an Unavailable or Unknown verdict, in words a user can
	// act on.
	Reason string
}

ProbeResult is a method's verdict on a site.

type Prober

type Prober interface {
	PublicConfig(ctx context.Context) (*PublicConfig, error)
}

Prober reads a site's pre-login configuration.

type PublicConfig

type PublicConfig struct {
	SiteName string
	WWWRoot  string
	// TypeOfLogin is 1 in-app, 2 browser, 3 embedded browser.
	TypeOfLogin int
	// QRCodeType is 0 disabled, 1 site URL only, 2 login.
	QRCodeType             int
	EnableWebServices      int
	EnableMobileWebService int
	ShowLoginForm          int
	MaintenanceEnabled     int
	HasIdentityProviders   bool
}

PublicConfig is a site's pre-login configuration: enough to decide which login methods are possible before any credential exists.

type Request

type Request struct {
	// Interactive enables terminal prompts; unattended callers never pause.
	Interactive bool
	Site        site.Site
	// WWWRoot is the site's canonical root as Moodle reports it. Callback
	// hashes are computed over this, which is not always the URL the user
	// typed.
	WWWRoot string

	// Username and Password are used by the password method only.
	Username string
	Password string

	// Callback is a pasted "<scheme>://token=..." URL.
	Callback string
	// Passport ties a callback to the login that started it.
	Passport string

	// QR is the decoded content of a login QR code.
	QR string

	// SessionCookie is a session a browser already holds, as either
	// "MoodleSession=value" or the bare value.
	SessionCookie string

	// Token is an existing web service token.
	Token string

	// In and Out let a method prompt. Out is the diagnostic stream: a method
	// must never write to stdout, which belongs to the result.
	In  io.Reader
	Out io.Writer
}

Request carries what a method needs from the caller.

type Session

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

Session is an authenticated connection to one site as one account.

func (*Session) Capabilities

func (s *Session) Capabilities(ctx context.Context) (*site.Capabilities, error)

Capabilities reports what this account may do, fetching once per process.

Nothing is cached between runs: the CLI is short-lived, and a stale idea of what a site allows is worse than the one request it would save.

func (*Session) Client

func (s *Session) Client() *moodle.Client

Client and Token expose what the composition root needs to assemble feature backends. They are not for command code: the import rules stop the command layer from naming the transport at all.

func (*Session) Cookie

func (s *Session) Cookie() moodle.SessionCookie

Cookie returns the browser session, empty when there is none.

It reports none while the session is restricted to the web service route, which is how that restriction reaches every feature: a backend built from a cookie is exactly what "ws-only" means to exclude.

func (*Session) ExpireCapabilitiesAfter

func (s *Session) ExpireCapabilitiesAfter(d time.Duration)

ExpireCapabilitiesAfter makes a long-lived session ask the site again from time to time.

Only a process that outlives the answer needs this. A site's administrator can switch a function on while an MCP server is running, and a server that never asks again keeps telling its agent the tool is unavailable — measured, and only a restart cleared it.

func (*Session) HasToken

func (s *Session) HasToken() bool

HasToken reports whether a web service token is available.

func (*Session) RestrictToWebService

func (s *Session) RestrictToWebService()

RestrictToWebService confines this session to the web service route. The fallbacks read pages meant for a person, which a site may have every reason to treat differently from an API call, so a caller is allowed to say no.

func (*Session) Token

func (s *Session) Token() string

Token returns the credential this session authenticates with.

func (*Session) WebServiceOnly

func (s *Session) WebServiceOnly() bool

WebServiceOnly reports whether the fallbacks were ruled out.

Jump to

Keyboard shortcuts

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