repository

package
v1.0.0-alpha1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: Apache-2.0 Imports: 13 Imported by: 1 module, 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 repository reads and writes users, roles, groups and group memberships.

Index

Constants

View Source
const DefaultPermissionTTL = time.Minute

DefaultPermissionTTL is how long a role's permissions are remembered. It bounds how stale a grant can be on a server that did not itself make the change.

Variables

This section is empty.

Functions

This section is empty.

Types

type CILogonUserProvisioner

type CILogonUserProvisioner struct {
	DB *gorm.DB
}

CILogonUserProvisioner resolves a CILogon principal to its users row, creating the row the first time the principal authenticates. A new row is SUSPENDED, with the profile filled from the CILogon claims, until an administrator activates it. An existing row is never changed here.

It runs on every CILogon request, so a status change takes effect on the user's next request.

func (*CILogonUserProvisioner) ProvisionUser

func (p *CILogonUserProvisioner) ProvisionUser(ctx context.Context, principal *auth.Principal) (string, error)

ProvisionUser implements auth.UserProvisioner.

type DBRoleLookup

type DBRoleLookup struct {
	DB *gorm.DB
}

DBRoleLookup resolves a caller's authorities from the user_roles table.

It is the intended replacement for auth.MockRoleLookup: the schema already carries the rows, they just are not administered through any endpoint yet.

func (DBRoleLookup) Roles

func (l DBRoleLookup) Roles(ctx context.Context, username string) []string

Roles implements auth.RoleLookup.

A caller with no rows — including one who was never registered — gets USER. Failing closed to the least privilege matters more here than reporting the error, since the alternative on a transient database fault would be to deny every request; USER is the same answer the mock gives an unknown caller.

type GroupMemberRepository

type GroupMemberRepository struct {
	// contains filtered or unexported fields
}

GroupMemberRepository reads and writes group memberships.

func NewGroupMemberRepository

func NewGroupMemberRepository(db *gorm.DB) *GroupMemberRepository

NewGroupMemberRepository returns a repository backed by db.

func (*GroupMemberRepository) Delete

Delete removes a membership.

The where clause is spelled out because a composite primary key carried on the struct is the only thing that scopes the delete, and an empty field would otherwise widen it to every row that matches the rest.

func (*GroupMemberRepository) FindByGroupID

func (r *GroupMemberRepository) FindByGroupID(ctx context.Context, groupID string) ([]model.GroupMember, error)

FindByGroupID returns every membership of one group.

func (*GroupMemberRepository) FindByGroupIDAndUserID

func (r *GroupMemberRepository) FindByGroupIDAndUserID(ctx context.Context, groupID, userID string) (*model.GroupMember, error)

FindByGroupIDAndUserID returns one membership, or gorm.ErrRecordNotFound.

func (*GroupMemberRepository) FindByUserID

func (r *GroupMemberRepository) FindByUserID(ctx context.Context, userID string) ([]model.GroupMember, error)

FindByUserID returns every membership held by one user.

func (*GroupMemberRepository) Save

Save inserts or updates a membership.

This is an explicit upsert rather than a plain Save, which decides between an insert and an update by running the update first and inserting when it reports no rows affected. That indirection is worth avoiding for a composite key: the whole key is always known here, so stating the conflict is both cheaper and exact — one statement that cannot be tripped up by a dialect's idea of how many rows an unchanged update affected.

func (*GroupMemberRepository) WithTx

WithTx returns a repository bound to tx.

type GroupRepository

type GroupRepository struct {
	// contains filtered or unexported fields
}

GroupRepository reads and writes groups.

func NewGroupRepository

func NewGroupRepository(db *gorm.DB) *GroupRepository

NewGroupRepository returns a repository backed by db.

func (*GroupRepository) Delete

func (r *GroupRepository) Delete(ctx context.Context, g *model.Group) error

Delete removes a group; its membership rows go with it via the cascading constraint.

func (*GroupRepository) FindAll

func (r *GroupRepository) FindAll(ctx context.Context) ([]model.Group, error)

FindAll returns every group.

func (*GroupRepository) FindByID

func (r *GroupRepository) FindByID(ctx context.Context, id string) (*model.Group, error)

FindByID returns one group, or gorm.ErrRecordNotFound.

func (*GroupRepository) FindVisibleTo

func (r *GroupRepository) FindVisibleTo(ctx context.Context, userID string) ([]model.Group, error)

FindVisibleTo returns the groups userID owns or holds a membership in.

Inactive memberships count here: a suspended member can still see that the group exists, which is what makes "why can I no longer read anything shared with it?" answerable. What an inactive membership does not grant is reading through the group — that check lives in the service.

func (*GroupRepository) Save

func (r *GroupRepository) Save(ctx context.Context, g *model.Group) error

Save inserts or updates a group.

func (*GroupRepository) WithTx

func (r *GroupRepository) WithTx(tx *gorm.DB) *GroupRepository

WithTx returns a repository bound to tx.

type PermissionResolver

type PermissionResolver struct {
	// contains filtered or unexported fields
}

PermissionResolver resolves a caller to the permissions they hold.

A person holds what their roles grant, read from the role_permissions table. The cache for that is keyed by role rather than by user: there are few roles and many users, so it stays small however many people are signed in, and a change to a role is one Invalidate rather than a hunt for every user holding it.

A service account holds what has been granted to that account in service_account_permissions, and nothing from any role. Those are cached per account, which is bounded by the number of service accounts.

A change made through this process calls Invalidate or InvalidateUser and takes effect at once. A change made by another server instance, or directly in the database, is picked up when the cached entry expires.

func NewPermissionResolver

func NewPermissionResolver(db *gorm.DB, ttl time.Duration) *PermissionResolver

NewPermissionResolver returns a resolver over db. A ttl of zero or less means DefaultPermissionTTL.

func (*PermissionResolver) Invalidate

func (r *PermissionResolver) Invalidate()

Invalidate forgets everything cached, so the next request reads the tables again. Call it after changing role_permissions.

func (*PermissionResolver) InvalidateUser

func (r *PermissionResolver) InvalidateUser(id string)

InvalidateUser forgets one service account's permissions. Call it after changing or deleting that account's grants.

func (*PermissionResolver) Permissions

func (r *PermissionResolver) Permissions(ctx context.Context, name string, authorities []string) permission.Set

Permissions implements auth.PermissionResolver.

For a service account (told apart by its reserved id prefix, which no identity provider can produce) it is the account's own grants. For anyone else it is the union of what every authority grants; an authority that is not a recognised role grants nothing.

SUPER_ADMIN holds every permission without a lookup, so the bootstrap account keeps working whatever state the table is in.

type ServiceAccountRepository

type ServiceAccountRepository struct {
	// contains filtered or unexported fields
}

ServiceAccountRepository reads and writes service account token rows.

func NewServiceAccountRepository

func NewServiceAccountRepository(db *gorm.DB) *ServiceAccountRepository

NewServiceAccountRepository returns a repository backed by db.

func (*ServiceAccountRepository) FindAll

func (r *ServiceAccountRepository) FindAll(ctx context.Context) ([]model.User, error)

FindAll returns every service account with its user row.

func (*ServiceAccountRepository) FindByID

func (r *ServiceAccountRepository) FindByID(ctx context.Context, id string) (*model.User, error)

FindByID returns one service account with its user row, or gorm.ErrRecordNotFound when id names no service account — including when it names a person.

func (*ServiceAccountRepository) ReplacePermissions

func (r *ServiceAccountRepository) ReplacePermissions(ctx context.Context, id string, want []permission.Permission) error

ReplacePermissions makes want the account's complete set of grants: rows not in want are deleted and missing ones inserted, and rows already present are left alone, so an unchanged permission leaves no trace in the audit log.

func (*ServiceAccountRepository) Save

Save inserts or updates a service account row. Its permissions are written only through ReplacePermissions, never as a side effect of saving the account.

func (*ServiceAccountRepository) WithTx

WithTx returns a repository bound to tx.

type ServiceAccountTokenLookup

type ServiceAccountTokenLookup struct {
	DB *gorm.DB

	// Now is the clock, replaceable in tests. Nil means time.Now.
	Now func() time.Time
}

ServiceAccountTokenLookup resolves service account tokens against the database. It implements auth.ServiceAccountResolver.

func (ServiceAccountTokenLookup) ResolveServiceAccount

func (l ServiceAccountTokenLookup) ResolveServiceAccount(ctx context.Context, token string) (*auth.Principal, error)

ResolveServiceAccount implements auth.ServiceAccountResolver.

A token is accepted only if its digest matches an account's current token, the token has not expired, and the account's user row is ACTIVE. Every one of those failures is the same ErrInvalidToken, so a caller cannot tell a revoked token from one that never existed.

The principal's authorities are fixed to USER by auth.ServiceAccountPrincipal; user_roles is deliberately not consulted.

type UserRepository

type UserRepository struct {
	// contains filtered or unexported fields
}

UserRepository reads and writes user records.

func NewUserRepository

func NewUserRepository(db *gorm.DB) *UserRepository

NewUserRepository returns a repository backed by db.

func (*UserRepository) AddRole

func (r *UserRepository) AddRole(ctx context.Context, userID string, role model.Role) error

AddRole grants role to the user. Granting a role already held is a no-op.

func (*UserRepository) FindAll

func (r *UserRepository) FindAll(ctx context.Context) ([]model.User, error)

FindAll returns every user with their roles, newest first.

func (*UserRepository) FindByID

func (r *UserRepository) FindByID(ctx context.Context, id string) (*model.User, error)

FindByID returns one user, or gorm.ErrRecordNotFound.

func (*UserRepository) FindByIDWithRoles

func (r *UserRepository) FindByIDWithRoles(ctx context.Context, id string) (*model.User, error)

FindByIDWithRoles returns one user with their roles, or gorm.ErrRecordNotFound.

func (*UserRepository) RemoveRole

func (r *UserRepository) RemoveRole(ctx context.Context, userID string, role model.Role) error

RemoveRole withdraws role from the user. Withdrawing a role not held is a no-op.

func (*UserRepository) Save

func (r *UserRepository) Save(ctx context.Context, u *model.User) error

Save inserts or updates a user.

func (*UserRepository) WithTx

func (r *UserRepository) WithTx(tx *gorm.DB) *UserRepository

WithTx returns a repository bound to tx.

Jump to

Keyboard shortcuts

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