auth

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: AGPL-3.0 Imports: 22 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 is kunveno's optional sign-in: who a person is (Identity, from a Provider), whether they may use the server (Rule), the signed session cookie, guest invitations and the /auth/ routes. It knows no provider; each lives in a package of its own (auth/yandex).

Index

Constants

View Source
const ExpiredKeep = 24 * time.Hour

ExpiredKeep is how long an expired invitation is kept for its guests' reconnects to the meeting they are in: the bound on how long a meeting can outlast its link.

View Source
const LoginCookie = "__Host-kunveno-login"

LoginCookie carries the state and the return path between start and callback.

View Source
const MaxNameLen = 32

MaxNameLen matches the server's limit on participant names.

View Source
const SessionCookie = "__Host-kunveno"

SessionCookie holds a signed-in person. The __Host- prefix makes browsers insist on Secure, Path=/ and no Domain.

Variables

View Source
var ErrBadInvite = errors.New("bad invite")

ErrBadInvite: the token is unknown, belongs to another room, was revoked or expired more than ExpiredKeep ago.

View Source
var ErrExpiredInvite = errors.New("expired invite")

ErrExpiredInvite: the invitation expired (less than ExpiredKeep ago) and was not revoked. It admits no new guest; the conference still lets back the guests it admitted to a meeting that is still running. Check returns the invitation with it.

Functions

func DisplayName

func DisplayName(id Identity) string

DisplayName is the name an employee has in rooms: the account's name, trimmed and cut to MaxNameLen runes, or the login's part before "@" when the account has none.

func LoadKey

func LoadKey(path string) ([]byte, error)

LoadKey reads the signing key at path, creating it (32 random bytes, mode 0600) when missing, so that restarts keep sessions. Deleting the file signs everyone out.

func WithIdentity

func WithIdentity(ctx context.Context, id Identity) context.Context

WithIdentity returns ctx carrying id, as Require passes it to the handler it guards.

Types

type Gate

type Gate struct {
	Providers []Provider
	Rules     map[string]Rule // by provider type
	Sessions  *Sessions
	Invites   *Invites
	PublicURL string // https://meet.example.com, no trailing slash
	Log       *slog.Logger
	OnSignIn  func(provider, result string) // metrics; may be nil
}

Gate is the sign-in of one server: its providers, their rules, sessions and invitations.

func (*Gate) CheckInvite

func (g *Gate) CheckInvite(room, token string) (Invite, error)

CheckInvite finds the usable invitation of room with token; with Session it is signal.Access.

func (*Gate) ExpiredSession

func (g *Gate) ExpiredSession(r *http.Request) (Identity, bool)

ExpiredSession is the person of an intact session that expired at most one lifetime ago and whose login the rules still allow: such a person may only come back to a running meeting.

func (*Gate) ProviderInfos

func (g *Gate) ProviderInfos() []ProviderInfo

func (*Gate) Require

func (g *Gate) Require(h http.Handler) http.Handler

Require lets through only signed-in requests; state-changing ones must also come from our site. The handler finds the person with IdentityFrom(r.Context()).

func (*Gate) Routes

func (g *Gate) Routes(mux *http.ServeMux)

func (*Gate) SameOrigin

func (g *Gate) SameOrigin(r *http.Request) bool

SameOrigin reports whether the request comes from our own pages.

func (*Gate) Session

func (g *Gate) Session(r *http.Request) (Identity, bool)

Session is the signed-in person of r. The allow rules are checked again on every call, so a provider removed from the config or a narrowed allow list takes effect at once.

type Identity

type Identity struct {
	Provider string // the provider's type, e.g. "yandex"
	ID       string // the account's permanent id at the provider
	Login    string // what Rule matches: for Yandex 360 organization accounts name@domain
	Name     string // display name from the account; may be empty
}

Identity is a person as a provider verified them.

func IdentityFrom

func IdentityFrom(ctx context.Context) (Identity, bool)

IdentityFrom is the signed-in person of a request that passed Require.

type Invite

type Invite struct {
	ID        string    `json:"id"`   // public: the dialog revokes by it
	Hash      string    `json:"hash"` // hex SHA-256 of the token
	Room      string    `json:"room"`
	CreatedBy string    `json:"created_by"`
	Created   time.Time `json:"created"`
	Expires   time.Time `json:"expires"`
	Revoked   bool      `json:"revoked"`
}

Invite is one guest link. Hash is the SHA-256 of the token; the token itself is never kept.

type Invites

type Invites struct {
	Now func() time.Time
	// contains filtered or unexported fields
}

Invites keeps the invitations in a JSON file (mode 0600), rewritten whole on every change.

func OpenInvites

func OpenInvites(path string) (*Invites, error)

func (*Invites) Active

func (s *Invites) Active() int

Active counts the usable invitations of every room.

func (*Invites) Check

func (s *Invites) Check(room, token string) (Invite, error)

Check finds the usable invitation of room with this token. An invitation that has expired but is still kept comes back with ErrExpiredInvite; anything else is ErrBadInvite.

func (*Invites) Create

func (s *Invites) Create(room, createdBy string, ttl time.Duration) (string, Invite, error)

Create makes an invitation to room valid for ttl and returns its token (shown once).

func (*Invites) List

func (s *Invites) List(room string) []Invite

List returns room's usable invitations, newest first, without their hashes.

func (*Invites) RemoveRoom

func (s *Invites) RemoveRoom(room string) ([]string, error)

RemoveRoom forgets every invitation of room (it was deleted, and a new room may take its slug) and returns the IDs of those that were kept, for disconnecting their guests. They are forgotten even when the file cannot be written: the error says a restart would bring them back.

func (*Invites) Revoke

func (s *Invites) Revoke(room, id string) (Invite, error)

Revoke ends room's invitation id at once. An expired one that is still kept can be revoked too, which stops its guests from coming back to the running meeting.

func (*Invites) Sweep

func (s *Invites) Sweep() error

Sweep drops revoked invitations and those expired more than ExpiredKeep ago from the file.

type Provider

type Provider interface {
	Type() string  // "yandex"; the path segment of /auth/<type>/
	Label() string // the sign-in button's text
	// AuthURL is where the browser goes to sign in; state and callback come back with the code.
	AuthURL(state, callback string) string
	// Exchange turns the code into a verified identity. It talks to the provider server to server.
	Exchange(ctx context.Context, code, callback string) (Identity, error)
}

Provider signs people in through an OAuth 2.0 authorization code flow.

type ProviderInfo

type ProviderInfo struct {
	Type  string `json:"type"`
	Label string `json:"label"`
}

type Rule

type Rule []string

Rule lists the logins a provider's accounts must match: glob patterns such as "*@example.com".

func (Rule) Allows

func (r Rule) Allows(login string) bool

Allows reports whether login matches a pattern, ignoring case. An empty rule allows nobody.

type Sessions

type Sessions struct {
	Now func() time.Time // time.Now; tests set it
	// contains filtered or unexported fields
}

Sessions issues and reads the session cookie: base64url(JSON) "." base64url(HMAC-SHA256).

func NewSessions

func NewSessions(key []byte, ttl time.Duration) *Sessions

func (*Sessions) Clear

func (s *Sessions) Clear(w http.ResponseWriter)

Clear removes the session cookie.

func (*Sessions) Issue

func (s *Sessions) Issue(w http.ResponseWriter, id Identity)

Issue sets the session cookie for id, valid for the session lifetime. The browser keeps the cookie for one more lifetime after that, so ReadExpired can still find who it was.

func (*Sessions) Read

func (s *Sessions) Read(r *http.Request) (Identity, bool)

Read returns the signed-in identity, if the cookie is present, intact and not expired.

func (*Sessions) ReadExpired

func (s *Sessions) ReadExpired(r *http.Request) (Identity, bool)

ReadExpired returns the identity of an intact session that has expired, at most one session lifetime ago. It signs nobody in: the conference lets such a person back only into the meeting they were admitted to.

Directories

Path Synopsis
Package yandex signs people in with Yandex ID (OAuth 2.0, authorization code).
Package yandex signs people in with Yandex ID (OAuth 2.0, authorization code).

Jump to

Keyboard shortcuts

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