Documentation
¶
Overview ¶
Package callback holds the state of a browser login while it is happening.
Moodle's browser handoff ends with the site redirecting to "<scheme>://token=<payload>". Once a URL scheme is registered, that address can be produced by anything on the machine, and by any web page the user visits. So the arrival of a well-formed callback proves nothing on its own; what proves something is that it answers a login this process started and has not already finished.
Moodle's own source says passports "are valid only one time", but there is no server-side store enforcing it — the site only computes a hash. Making that true is the client's job, and it is this file's whole purpose.
Index ¶
- Constants
- Variables
- func BusNameFor(scheme string) string
- func CheckScheme(scheme string) error
- func Deliver(runtimeDir, uri string) error
- func Listen(dir string) (net.Listener, error)
- func ObjectPathFor(scheme string) string
- func OpenInBrowser(ctx context.Context, address string) (viaPortal bool, err error)
- func PeerIsSelf(conn net.Conn) (bool, error)
- func Receive(ctx context.Context, listener net.Listener, wait time.Duration) (string, error)
- func RuntimeDir() string
- func ServeCallback(ctx context.Context, scheme, runtimeDir string, wait time.Duration) error
- func Unregister(scheme string) error
- type Broker
- type Registration
- type Store
- type Transaction
Constants ¶
const DefaultLifetime = 5 * time.Minute
DefaultLifetime is how long a login may take. It is generous because the user may have to find a password manager, an SSO prompt and a second factor; it is finite because a transaction left open is a callback someone else can still answer.
const MobileScheme = "moodlemobile"
MobileScheme is the one this must never claim, named so the check below can say why rather than refusing anonymously.
const Scheme = "moodle-cli-auth"
Scheme is the URL scheme this tool asks the desktop to send it.
Not "moodle", "moodlecli" or "login". A registered scheme is a name in a namespace nobody owns — any application may claim the same one — so a generic name is a collision waiting to happen, and claiming one somebody else is using would take their links.
Never "moodlemobile" in particular. A site can set forcedurlscheme to that, and claiming it would be taking the official Moodle app's callbacks.
Variables ¶
ErrPeerCheckUnavailable means this platform cannot say who is at the other end of the channel. It is not a refusal: the caller decides what to do without that answer, and the runtime directory's mode is the boundary either way.
Functions ¶
func BusNameFor ¶
BusNameFor and ObjectPathFor expose what a handler claims, so a test can call it the way the desktop would rather than duplicating the derivation.
func CheckScheme ¶
CheckScheme reports whether a scheme may be registered.
func Deliver ¶
Deliver uses the private socket shared by the Linux D-Bus and macOS Apple Event handlers. A callback without a waiting sign-in is refused.
func Listen ¶
Listen opens the channel the handler hands a callback back through.
The directory is the one XDG reserves for runtime objects: owned by this user and mode 0700 by specification, which is what keeps another user out. That is checked rather than assumed — a runtime directory that is not those things is not a place to put a credential channel, and the honest response is to refuse rather than to continue on a worse guarantee.
func ObjectPathFor ¶
func OpenInBrowser ¶
OpenInBrowser asks the desktop to open an address.
The portal is tried first, and not for politeness: it takes the address as a bus message parameter, whereas running a command puts it on that command's argument list, where /proc exposes it to other users. The address carries the passport — the secret that ties a callback to this login — so that is worth avoiding where it can be.
Where it cannot, the command is used and the caller is told, because the alternative is refusing to sign in on a desktop that works perfectly well.
func PeerIsSelf ¶
PeerIsSelf reports whether the process at the other end runs as this user.
It is defence in depth, not the boundary: the directory's mode is what keeps another user out. This catches the case where that has been weakened without anyone noticing.
func Receive ¶
Receive waits for the desktop handler to deliver one callback.
Socket reception is a Unix concern, not a D-Bus concern: macOS will use a different desktop handler but the same private local channel. Keeping this beside Listen also guarantees every Unix build has the complete channel.
func RuntimeDir ¶ added in v0.3.0
func RuntimeDir() string
RuntimeDir is shared by the waiting CLI and the desktop-activated handler. Launch Services does not inherit a terminal's XDG_RUNTIME_DIR on macOS.
func ServeCallback ¶
ServeCallback runs as the handler the desktop activated.
It waits for one Open, passes the URI to whatever is waiting on the local channel, and returns. A handler that lingers would be a process holding a credential for no reason.
Types ¶
type Broker ¶
type Broker struct {
// contains filtered or unexported fields
}
Broker owns the operating-system part of one browser handoff. It opens the private channel before launching the browser, so a fast redirect cannot arrive before anything is listening.
func DefaultBroker ¶
func DefaultBroker() *Broker
DefaultBroker uses this tool's scheme and the current desktop session.
type Registration ¶
type Registration struct {
Scheme string
DesktopFile string
ServiceFile string
Executable string
MIMEDefault string
}
Registration uses the same status contract for a Linux desktop entry or a macOS application bundle. MIMEDefault identifies the actual OS association.
func Register ¶
func Register(scheme, executable string) (Registration, error)
Register installs the per-user handler for a scheme.
Per-user, never system-wide: this needs no elevation and touches nothing belonging to anyone else.
func Status ¶
func Status(scheme string) Registration
Status reports what is installed for a scheme, and what the desktop actually associates with it — which may be another application entirely.
func (Registration) Installed ¶
func (r Registration) Installed() bool
Installed reports whether both files this writes are present.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store holds the logins in flight.
There is usually one. There can be more: a user may be signing in to two sites at once, and answering one must not finish the other.
func (*Store) Begin ¶
func (s *Store) Begin(t Transaction) error
Begin records a login that is about to start.
func (*Store) Claim ¶
func (s *Store) Claim(siteHash string) (Transaction, error)
Claim answers a callback, at most once.
The site hash is compared in constant time. It is a public value, so this is not strictly required — but a comparison that decides whether to accept a credential is not the place to keep a careless habit.
type Transaction ¶
type Transaction struct {
// SiteHash is md5(canonical wwwroot + passport), which is what comes back
// in the payload. It is the only correlation Moodle offers: the callback
// format is fixed, so nothing else can be sent along and returned.
SiteHash string
// WWWRoot is the site's own idea of its address, as its public config
// reported it — not the address the user typed. The hash is computed from
// this, so a site reached by an alias still matches.
WWWRoot string
Passport string
Expires time.Time
}
Transaction is one login waiting for its answer.