feat(auth): add argon2id passwords, sessions, rate limiter, authorization and audit builders
- feat(auth): add argon2id passwords, sessions, rate limiter, authorization and audit builders - feat(cli): add check and audit commands - feat(api): add api framework, login, page routes and account, user, delegation and audit handlers - test(auth): cover passwords, sessions, rate limiter, authorization and audit builders - test(api): cover the api framework, sessions, client origin, headers and cross-origin refusal - test(api): cover account, user, delegation and audit handlers and the authorization matrix - test(ui): cover login, logout, page routes and the page authorization matrix
This commit is contained in:
1 parent
23bec143e3
commit
99fa5de747
28 files changed
+6591
-243
No files matched your search
@@ -0,0 +1,81 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
// Copyright (C) 2026 Bryan Joshua Pedini
|
||||
|
||||
package main
|
||||
|
||||
import "slices"
|
||||
|
||||
const (
|
||||
accessNone = "none"
|
||||
accessSession = "session"
|
||||
accessZone = "zone"
|
||||
accessAdmin = "admin"
|
||||
)
|
||||
|
||||
type accessDecision int
|
||||
|
||||
const (
|
||||
accessAllowed accessDecision = iota
|
||||
accessUnauthenticated
|
||||
accessForbidden
|
||||
accessNotFound
|
||||
)
|
||||
|
||||
func authorize(access string, p *principal, delegated bool) accessDecision {
|
||||
if access == accessNone {
|
||||
return accessAllowed
|
||||
}
|
||||
if p == nil {
|
||||
return accessUnauthenticated
|
||||
}
|
||||
switch access {
|
||||
case accessAdmin:
|
||||
if !p.isAdmin() {
|
||||
return accessForbidden
|
||||
}
|
||||
case accessZone:
|
||||
if !p.isAdmin() && !delegated {
|
||||
return accessNotFound
|
||||
}
|
||||
}
|
||||
return accessAllowed
|
||||
}
|
||||
|
||||
func needsDelegation(access string, p principal) bool {
|
||||
return access == accessZone && !p.isAdmin()
|
||||
}
|
||||
|
||||
func canAccessZone(p principal, zone string, delegated map[string]bool) bool {
|
||||
return p.isAdmin() || (p.User.Role == roleUser && delegated[zone])
|
||||
}
|
||||
|
||||
func visibleZones(p principal, zones []string, delegated map[string]bool) []string {
|
||||
out := make([]string, 0, len(zones))
|
||||
for _, z := range zones {
|
||||
if canAccessZone(p, z, delegated) {
|
||||
out = append(out, z)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func isOwnAccount(p principal, username string) bool {
|
||||
return p.User.Username == username
|
||||
}
|
||||
|
||||
func delegationTargetError(target userRow) *apiError {
|
||||
if target.DeletedAt.Valid {
|
||||
return errUserNotFound()
|
||||
}
|
||||
if target.Role != roleUser {
|
||||
return &apiError{Status: 422, Message: "zones cannot be delegated to admins", Fields: map[string]string{"username": "user is an admin and needs no delegation"}}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func leavesNoActiveAdmin(target userRow, activeAdmins []userRow) bool {
|
||||
if target.Role != roleAdmin || target.DeletedAt.Valid {
|
||||
return false
|
||||
}
|
||||
return !slices.ContainsFunc(activeAdmins, func(u userRow) bool { return u.ID != target.ID })
|
||||
}
|
||||
Reference in new issue
Block a user