auth

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package auth holds the relay's own credentials: the teacher's admin token, a class's join code, and the session tokens the relay signs for students.

A session token is

"v1." + base64url(claims) + "." + base64url(Ed25519 signature over "v1." + base64url(claims))

where claims is a fixed binary layout (no JSON, no algorithm field: there is exactly one algorithm, so there is nothing to confuse):

u8   key id
i64  expiry, unix seconds, big endian
u8   len, class id
u8   len, session id

The token is checked against the relay's clock only.

Index

Constants

View Source
const CodeLen = 10

CodeLen is a join code's length: 10 symbols of 5 bits, 50 bits.

View Source
const MaxTokenLen = 512

MaxTokenLen bounds what ParseToken will look at.

Variables

View Source
var (
	ErrMalformed = errors.New("auth: malformed token")
	ErrSignature = errors.New("auth: bad signature")
	ErrExpired   = errors.New("auth: token expired")
	ErrKey       = errors.New("auth: unknown key id")
)

Errors from Verify. A client is told only "unauthenticated"; these are for the relay's log.

Functions

func CleanDisplayName

func CleanDisplayName(s string) string

CleanDisplayName bounds and strips a display name: at most 40 runes, no control or format characters, trimmed. It is data, escaped wherever shown.

func Equal

func Equal(a, b string) bool

Equal compares two secrets in constant time.

func Label

func Label(display string) string

Label derives a DNS label from a display name: lower-case ASCII letters and digits, hyphens between words, at most 20 bytes, never empty (it falls back to "student"). Collisions are the caller's to resolve.

func NewJoinCode

func NewJoinCode() (string, error)

NewJoinCode draws a join code.

func NormalizeJoinCode

func NormalizeJoinCode(s string) (string, error)

NormalizeJoinCode reads a typed code: case-insensitive, spaces and dashes ignored, and the look-alikes Crockford maps (O->0, I/L->1).

func RandomToken

func RandomToken() (string, error)

RandomToken is 32 random bytes, base64url: the admin token.

Types

type Claims

type Claims struct {
	KeyID   uint8
	Expiry  time.Time
	Class   string
	Session string
}

Claims are what a session token says.

func ParseToken

func ParseToken(tok string) (c Claims, head string, sig []byte, err error)

ParseToken splits and decodes a token without checking its signature. head is the signed part.

type Keys

type Keys struct {
	Current uint8
	Private map[uint8]ed25519.PrivateKey
}

Keys signs and verifies session tokens. Keys are identified by a one-byte id so they can be rotated (the replicated store, later, distributes them).

func NewKeys

func NewKeys() (*Keys, error)

NewKeys makes a key set with one fresh key, id 1.

func (*Keys) Sign

func (k *Keys) Sign(c Claims) (string, error)

Sign issues a token for c (its KeyID is set to the current key).

func (*Keys) Verify

func (k *Keys) Verify(tok string, now time.Time) (Claims, error)

Verify checks a token's form, signature and expiry at now.

Jump to

Keyboard shortcuts

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