callback

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: 19 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 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

View Source
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.

View Source
const MobileScheme = "moodlemobile"

MobileScheme is the one this must never claim, named so the check below can say why rather than refusing anonymously.

View Source
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

View Source
var ErrPeerCheckUnavailable = errors.New("the peer's user cannot be checked here")

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

func BusNameFor(scheme string) string

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

func CheckScheme(scheme string) error

CheckScheme reports whether a scheme may be registered.

func Deliver

func Deliver(runtimeDir, uri string) error

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

func Listen(dir string) (net.Listener, error)

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 ObjectPathFor(scheme string) string

func OpenInBrowser

func OpenInBrowser(ctx context.Context, address string) (viaPortal bool, err error)

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

func PeerIsSelf(conn net.Conn) (bool, error)

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

func Receive(ctx context.Context, listener net.Listener, wait time.Duration) (string, error)

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

func ServeCallback(ctx context.Context, scheme, runtimeDir string, wait time.Duration) error

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.

func Unregister

func Unregister(scheme string) error

Unregister removes only what Register wrote.

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.

func NewBroker

func NewBroker(scheme, runtimeDir string) *Broker

NewBroker builds a broker for one registered scheme and desktop session.

func (*Broker) Installed

func (b *Broker) Installed() bool

Installed reports whether this broker has a registered desktop handler.

func (*Broker) OpenAndReceive

func (b *Broker) OpenAndReceive(ctx context.Context, address string, wait time.Duration, opened func(bool)) (string, error)

OpenAndReceive performs the platform handoff and waits for its reply.

func (*Broker) Scheme

func (b *Broker) Scheme() string

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 NewStore

func NewStore() *Store

NewStore builds an empty store.

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.

func (*Store) Pending

func (s *Store) Pending() int

Pending reports how many logins are waiting, for a status line.

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.

Jump to

Keyboard shortcuts

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