Phase 12: signing in with the Core's Authelia

Admins make the client's secret under Settings → Sign-in, which shows the
entry for the Core's idproviders.yml (the secret's PBKDF2-SHA512 hash, the
redirect address, PKCE). People sign in with their Core account: invited
ones are matched by verified email, then by subject; optionally anyone at
the Core's domain joins. Passwords can be turned off but for the owner's,
and signing out ends the Core's session where it can. On multi-tenant, the
team is found by its proved domain.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 13:00:36 -07:00
co-authored by Claude Opus 5.5
parent 637ef1cc25
commit 962dbe3286
19 changed files with 1458 additions and 15 deletions
+9 -2
View File
@@ -36,6 +36,7 @@ type Session struct {
// A platform admin viewing a team: until when, and why.
ViewUntil time.Time
ViewReason string
Via string // password or core
}
// ErrNoSession is a cookie that leads nowhere: unknown, ended or expired,
@@ -88,12 +89,12 @@ func expiry(created, seen time.Time) time.Time {
return max
}
const sessionCols = `id, user_id, created_at, seen_at, expires_at, ip, user_agent, team_id, view_until, view_reason`
const sessionCols = `id, user_id, created_at, seen_at, expires_at, ip, user_agent, team_id, view_until, view_reason, via`
func scanSession(row interface{ Scan(...any) error }) (*Session, error) {
var s Session
var created, seen, expires, until string
if err := row.Scan(&s.ID, &s.User, &created, &seen, &expires, &s.IP, &s.UserAgent, &s.Team, &until, &s.ViewReason); err != nil {
if err := row.Scan(&s.ID, &s.User, &created, &seen, &expires, &s.IP, &s.UserAgent, &s.Team, &until, &s.ViewReason, &s.Via); err != nil {
return nil, err
}
s.Created, s.Seen, s.Expires, s.ViewUntil = parseTS(created), parseTS(seen), parseTS(expires), parseTS(until)
@@ -185,3 +186,9 @@ func PruneSessions(d *db.DB, now time.Time) (int64, error) {
}
return res.RowsAffected()
}
// SetVia records how a session signed in.
func SetVia(d *db.DB, id, via string) error {
_, err := d.Exec(`UPDATE sessions SET via = ? WHERE id = ?`, via, id)
return err
}
+17 -2
View File
@@ -88,6 +88,7 @@ type User struct {
LockedAt time.Time // zero unless locked after too many failures
TwoFactor bool // signs in with an authenticator app's code too
RecoveryLeft int // unused recovery codes
OIDCSubject string // <issuer>|<subject> at the Core, once they've signed in with it
passwordHash string
totpStep int64
@@ -138,14 +139,14 @@ func NormalEmail(s string) (string, error) {
return strings.ToLower(s), nil
}
const userCols = `id, email, name, kind, status, password_hash, title, description, time_zone, photo, emails, created_at, signed_in_at, failed_signins, locked_at, two_factor, totp_step, recovery_codes`
const userCols = `id, email, name, kind, status, password_hash, title, description, time_zone, photo, emails, created_at, signed_in_at, failed_signins, locked_at, two_factor, totp_step, recovery_codes, oidc_subject`
func scanUser(row interface{ Scan(...any) error }) (*User, error) {
var u User
var kind, status, emails, created, signedIn, locked, codes string
var twoFactor int
err := row.Scan(&u.ID, &u.Email, &u.Name, &kind, &status, &u.passwordHash, &u.Title, &u.Description, &u.TimeZone, &u.Photo, &emails, &created, &signedIn,
&u.FailedSignIns, &locked, &twoFactor, &u.totpStep, &codes)
&u.FailedSignIns, &locked, &twoFactor, &u.totpStep, &codes, &u.OIDCSubject)
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrNotFound
}
@@ -454,3 +455,17 @@ type rowWith struct {
}
func (r rowWith) Scan(dest ...any) error { return r.row.Scan(append(dest, r.more...)...) }
// ByOIDCSubject finds the user the Core knows by a subject.
func ByOIDCSubject(d *db.DB, subject string) (*User, error) {
if subject == "" {
return nil, ErrNotFound
}
return scanUser(d.QueryRow(`SELECT `+userCols+` FROM users WHERE oidc_subject = ?`, subject))
}
// SetOIDCSubject remembers who a user is at the Core.
func SetOIDCSubject(d *db.DB, id int64, subject string) error {
_, err := d.Exec(`UPDATE users SET oidc_subject = ? WHERE id = ?`, subject, id)
return err
}