Files
pages/internal/store/auth_mysql.go
T
Anya Tokugawa 6a67fd9afe feat(storage): add native MySQL namespace and identity pilots
Add resumable checked migrations and explicit schema administration commands. Port ML budgets, namespaces, identity and group persistence with three-backend contracts. Keep MySQL unavailable as the full application backend.
2026-11-30 23:03:24 +09:00

175 lines
6.9 KiB
Go

package store
import (
"context"
"database/sql"
"pageswiki/internal/db"
)
// This is a persistence pilot, not a server/authentication endpoint. The caller
// MUST authenticate and authorize every administrator mutation. It intentionally
// exposes neither *Store nor grant-only checks as a complete authorization API.
// MFA/provider persistence is internally tested but not exposed as a login API.
// Audit/locality policy, CLI handoff and Domain deletion remain unported.
type MySQLIdentityRepository struct{ store *Store }
func NewMySQLIdentityRepository(database *sql.DB) *MySQLIdentityRepository {
return &MySQLIdentityRepository{store: &Store{DB: database, sqlBackend: storeMySQL}}
}
func (r *MySQLIdentityRepository) PrepareSchema(ctx context.Context) error {
return r.store.ensureMySQLAuthFoundation(ctx)
}
func (r *MySQLIdentityRepository) InspectSchema(ctx context.Context) (db.MySQLMigrationStatus, error) {
base, err := NewMySQLNamespaceRepository(r.store.DB).InspectSchema(ctx)
if err != nil {
return base, err
}
auth, err := db.InspectMySQLMigrations(ctx, r.store.DB, "store-auth-core", mysqlAuthMigrations())
if err != nil {
return auth, err
}
result := db.MySQLMigrationStatus{Applied: base.Applied + auth.Applied, Pending: base.Pending + auth.Pending, Dirty: base.Dirty || auth.Dirty}
if base.Dirty {
result.DirtyComponent, result.NextOperation = base.DirtyComponent, base.NextOperation
} else if auth.Dirty {
result.DirtyComponent, result.NextOperation = auth.DirtyComponent, auth.NextOperation
}
return result, nil
}
func (r *MySQLIdentityRepository) ResumeSchema(ctx context.Context) error {
base, err := NewMySQLNamespaceRepository(r.store.DB).InspectSchema(ctx)
if err != nil {
return err
}
if base.Dirty || base.Pending != 0 {
return db.ErrMySQLMigrationMismatch
}
return db.ResumeMySQLMigrations(ctx, r.store.DB, "store-auth-core", mysqlAuthMigrations())
}
func mysqlIdentityIDs(ids ...string) error {
for _, id := range ids {
if err := mysqlNamespaceUUID(id); err != nil {
return err
}
}
return nil
}
func (r *MySQLIdentityRepository) BootstrapOwner(ctx context.Context, domain, username, password, displayName, email string) (*Principal, error) {
if err := mysqlIdentityIDs(domain); err != nil {
return nil, err
}
return r.store.BootstrapOwnerContext(ctx, domain, username, password, displayName, email)
}
func (r *MySQLIdentityRepository) CreateAccount(ctx context.Context, domain, username, password, displayName, email string) (*Principal, error) {
if err := mysqlIdentityIDs(domain); err != nil {
return nil, err
}
return r.store.CreatePasswordAccountContext(ctx, domain, username, password, displayName, email)
}
func (r *MySQLIdentityRepository) Principals(ctx context.Context, domain string) ([]Principal, error) {
if err := mysqlIdentityIDs(domain); err != nil {
return nil, err
}
return r.store.ListDomainPrincipalsContext(ctx, domain)
}
func (r *MySQLIdentityRepository) Sessions(ctx context.Context, domain string) ([]AuthSessionInfo, error) {
if err := mysqlIdentityIDs(domain); err != nil {
return nil, err
}
return r.store.ListAuthSessionsContext(ctx, domain)
}
func (r *MySQLIdentityRepository) RevokeSession(ctx context.Context, domain, session string) error {
if err := mysqlIdentityIDs(domain, session); err != nil {
return err
}
return r.store.RevokeAuthSessionByIDContext(ctx, domain, session)
}
func (r *MySQLIdentityRepository) SetPrincipalState(ctx context.Context, domain, principal, state string) error {
if err := mysqlIdentityIDs(domain, principal); err != nil {
return err
}
return r.store.SetPrincipalStateContext(ctx, domain, principal, state)
}
func (r *MySQLIdentityRepository) CreateGroup(ctx context.Context, domain, owner, name, title, description, visibility string) (*Group, error) {
if err := mysqlIdentityIDs(domain, owner); err != nil {
return nil, err
}
return r.store.CreateGroupContext(ctx, domain, owner, name, title, description, visibility)
}
func (r *MySQLIdentityRepository) Groups(ctx context.Context, domain, principal string) ([]Group, error) {
if err := mysqlIdentityIDs(domain); err != nil {
return nil, err
}
if principal != "" {
if err := mysqlIdentityIDs(principal); err != nil {
return nil, err
}
}
return r.store.ListGroupsContext(ctx, domain, principal)
}
func (r *MySQLIdentityRepository) Group(ctx context.Context, domain, name string) (*Group, error) {
if err := mysqlIdentityIDs(domain); err != nil {
return nil, err
}
return r.store.GroupByNameContext(ctx, domain, name)
}
func (r *MySQLIdentityRepository) Members(ctx context.Context, domain, group string) ([]GroupMember, error) {
if err := r.groupScope(ctx, domain, group); err != nil {
return nil, err
}
return r.store.ListGroupMembersContext(ctx, group)
}
func (r *MySQLIdentityRepository) groupScope(ctx context.Context, domain, group string) error {
if err := mysqlIdentityIDs(domain, group); err != nil {
return err
}
_, err := r.store.GroupByUUIDContext(ctx, domain, group)
return err
}
func (r *MySQLIdentityRepository) AddMember(ctx context.Context, domain, group, username, role string) error {
if err := mysqlIdentityIDs(domain, group); err != nil {
return err
}
return r.store.AddGroupMemberByUsernameContext(ctx, domain, group, username, role)
}
func (r *MySQLIdentityRepository) RemoveMember(ctx context.Context, domain, group, membership string, ownerAuthority bool) error {
if err := mysqlIdentityIDs(membership); err != nil {
return err
}
if err := r.groupScope(ctx, domain, group); err != nil {
return err
}
return r.store.RemoveGroupMemberWithAuthorityContext(ctx, group, membership, ownerAuthority)
}
func (r *MySQLIdentityRepository) SetPermission(ctx context.Context, domain, group, path, permission string) error {
if err := r.groupScope(ctx, domain, group); err != nil {
return err
}
return r.store.SetGroupPermissionContext(ctx, group, path, permission)
}
func (r *MySQLIdentityRepository) SetInheritance(ctx context.Context, domain, group string, enabled bool) error {
if err := r.groupScope(ctx, domain, group); err != nil {
return err
}
return r.store.SetCampusAdminInheritanceContext(ctx, group, enabled)
}
func (r *MySQLIdentityRepository) IsBuiltinAdmin(ctx context.Context, domain, principal, key string) (bool, error) {
if err := mysqlIdentityIDs(domain, principal); err != nil {
return false, err
}
return r.store.IsBuiltinGroupAdminContext(ctx, domain, principal, key)
}
func (r *MySQLIdentityRepository) CreateLocalDomain(ctx context.Context, campus, name, title, host string, enabled bool) error {
if err := mysqlIdentityIDs(campus); err != nil {
return err
}
return r.store.writeDomain(ctx, 0, false, campus, name, title, host, "local", "", "", enabled)
}
func (r *MySQLIdentityRepository) UpdateLocalDomain(ctx context.Context, id int64, campus, name, title, host string, enabled bool) error {
if err := mysqlIdentityIDs(campus); err != nil {
return err
}
return r.store.writeDomain(ctx, id, true, campus, name, title, host, "local", "", "", enabled)
}