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 ¶
- func NotAvailable(method, reason string) error
- type Availability
- type Candidate
- type Capabilitier
- type ClientFactory
- type Coordinator
- func (c *Coordinator) Authenticate(ctx context.Context, req Request, methodName string) (Credential, error)
- func (c *Coordinator) Candidates(ctx context.Context, target site.Site, config *PublicConfig) []Candidate
- func (c *Coordinator) Find(name string) (Method, error)
- func (c *Coordinator) Methods() []Method
- type Credential
- type Manager
- func (m *Manager) Forget(siteID, accountID site.ID) error
- func (m *Manager) KeepCredentialFresh(s *Session, siteID, accountID site.ID)
- func (m *Manager) Open(target site.Site, siteID, accountID site.ID) (*Session, error)
- func (m *Manager) OpenWithSession(target site.Site, accountID site.ID, cookie string) *Session
- func (m *Manager) OpenWithToken(target site.Site, accountID site.ID, token string) *Session
- func (m *Manager) Probe(target site.Site) Prober
- func (m *Manager) Session(siteID, accountID site.ID) (string, error)
- func (m *Manager) StoreCredential(siteID, accountID site.ID, credential Credential) error
- func (m *Manager) StoreSession(siteID, accountID site.ID, cookie string) error
- func (m *Manager) StoreToken(siteID, accountID site.ID, token string) error
- func (m *Manager) Token(siteID, accountID site.ID) (string, error)
- type Method
- type ProbeResult
- type Prober
- type PublicConfig
- type Request
- type Session
- func (s *Session) Capabilities(ctx context.Context) (*site.Capabilities, error)
- func (s *Session) Client() *moodle.Client
- func (s *Session) Cookie() moodle.SessionCookie
- func (s *Session) ExpireCapabilitiesAfter(d time.Duration)
- func (s *Session) HasToken() bool
- func (s *Session) RestrictToWebService()
- func (s *Session) Token() string
- func (s *Session) WebServiceOnly() bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NotAvailable ¶
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" 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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
OpenWithToken builds a session from a token the caller already holds, such as one supplied through the environment for a single run.
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 ¶
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 ¶
StoreToken saves a verified token.
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 ¶
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 ¶
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 ¶
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) 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) WebServiceOnly ¶
WebServiceOnly reports whether the fallbacks were ruled out.