Documentation
¶
Index ¶
- type PGStore
- func (s *PGStore) AddPermissionToGroup(ctx context.Context, groupID string, permissionKey string) error
- func (s *PGStore) AddUsersToGroup(ctx context.Context, groupID string, userIDs []string) error
- func (s *PGStore) AssignPermission(ctx context.Context, userID string, permissionKey string) error
- func (s *PGStore) AssignPermissionsToOrganization(ctx context.Context, orgID string, permissionKeys []string) error
- func (s *PGStore) AssignUserToGroup(ctx context.Context, userID string, groupID string) error
- func (s *PGStore) Bootstrap(ctx context.Context, superAdminEmail string, superAdminPassword string, ...) error
- func (s *PGStore) ChangePassword(ctx context.Context, userID string, currentPassword string, newPassword string) error
- func (s *PGStore) CreateGroup(ctx context.Context, name string) (*auth.Group, error)
- func (s *PGStore) CreateOTP(ctx context.Context, email string) (*auth.OTP, error)
- func (s *PGStore) CreateOrganizationWithPermissions(ctx context.Context, name string, permissionKeys []string) (*auth.Organization, error)
- func (s *PGStore) CreatePasswordReset(ctx context.Context, email string) (rawToken string, expiresAt time.Time, err error)
- func (s *PGStore) CreatePermission(ctx context.Context, key string, description string) (*auth.Permission, error)
- func (s *PGStore) CreateSchema(ctx context.Context) error
- func (s *PGStore) CreateUser(ctx context.Context, email string) (*auth.User, error)
- func (s *PGStore) CreateUserWithOrganization(ctx context.Context, email string, organization string) (*auth.User, error)
- func (s *PGStore) DeleteGroup(ctx context.Context, id string) error
- func (s *PGStore) DeletePermission(ctx context.Context, id string) error
- func (s *PGStore) DropSchema(ctx context.Context) error
- func (s *PGStore) GetAllUserPermissions(ctx context.Context, userID string) ([]auth.Permission, error)
- func (s *PGStore) GetGroup(ctx context.Context, id string) (*auth.Group, error)
- func (s *PGStore) GetGroupMembers(ctx context.Context, groupID string) ([]auth.User, error)
- func (s *PGStore) GetOrganization(ctx context.Context, id string) (*auth.Organization, error)
- func (s *PGStore) GetOrganizationByName(ctx context.Context, name string) (*auth.Organization, error)
- func (s *PGStore) GetOrganizationPermissions(ctx context.Context, orgID string) ([]auth.Permission, error)
- func (s *PGStore) GetPermission(ctx context.Context, key string) (*auth.Permission, error)
- func (s *PGStore) GetResolvedPermissions(ctx context.Context, userID string) ([]auth.Permission, error)
- func (s *PGStore) GetUserByEmail(ctx context.Context, email string) (*auth.User, error)
- func (s *PGStore) GetUserByID(ctx context.Context, id string) (*auth.User, error)
- func (s *PGStore) GetUserGroups(ctx context.Context, userID string) ([]auth.Group, error)
- func (s *PGStore) GetUserOrganization(ctx context.Context, userID string) (string, error)
- func (s *PGStore) GetUserPermissions(ctx context.Context, userID string) ([]auth.Permission, error)
- func (s *PGStore) HasAnyPermission(ctx context.Context, userID string, permissionKeys []string) (bool, error)
- func (s *PGStore) HasPassword(ctx context.Context, userID string) (bool, error)
- func (s *PGStore) HasPermission(ctx context.Context, userID string, permissionKey string) (bool, error)
- func (s *PGStore) HasResolvedPermission(ctx context.Context, userID string, permissionKey string) (bool, error)
- func (s *PGStore) ListGroups(ctx context.Context) ([]auth.Group, error)
- func (s *PGStore) ListOrganizations(ctx context.Context) ([]auth.Organization, error)
- func (s *PGStore) ListPermissions(ctx context.Context) ([]auth.Permission, error)
- func (s *PGStore) ListUsers(ctx context.Context) ([]auth.User, error)
- func (s *PGStore) LoginWithPassword(ctx context.Context, email string, plainPassword string) (*auth.User, error)
- func (s *PGStore) Migrate(ctx context.Context) error
- func (s *PGStore) MigrationStatus(ctx context.Context) ([]auth.MigrationRecord, error)
- func (s *PGStore) RegisterWithPassword(ctx context.Context, email string, plainPassword string) (*auth.User, error)
- func (s *PGStore) RemovePermissionFromGroup(ctx context.Context, groupID string, permissionID string) error
- func (s *PGStore) RemovePermissionsFromOrganization(ctx context.Context, orgID string, permissionKeys []string) error
- func (s *PGStore) RemoveUserFromGroup(ctx context.Context, userID string, groupID string) error
- func (s *PGStore) RemoveUsersFromGroup(ctx context.Context, groupID string, userIDs []string) error
- func (s *PGStore) ResetPassword(ctx context.Context, rawToken string, newPassword string) error
- func (s *PGStore) RevokePermission(ctx context.Context, userID string, permissionKey string) error
- func (s *PGStore) Rollback(ctx context.Context) error
- func (s *PGStore) SetPassword(ctx context.Context, userID string, plainPassword string) error
- func (s *PGStore) VerifyOTP(ctx context.Context, email string, code string) (*auth.User, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type PGStore ¶
type PGStore struct {
// contains filtered or unexported fields
}
PGStore implements auth.Store using PostgreSQL via pgx.
func (*PGStore) AddPermissionToGroup ¶
func (s *PGStore) AddPermissionToGroup(ctx context.Context, groupID string, permissionKey string) error
AddPermissionToGroup adds a permission to a group.
func (*PGStore) AddUsersToGroup ¶ added in v2.1.0
AddUsersToGroup adds multiple users to a group in bulk.
func (*PGStore) AssignPermission ¶
AssignPermission assigns a permission directly to a user.
func (*PGStore) AssignPermissionsToOrganization ¶ added in v2.1.0
func (s *PGStore) AssignPermissionsToOrganization(ctx context.Context, orgID string, permissionKeys []string) error
AssignPermissionsToOrganization assigns permissions to an organization.
func (*PGStore) AssignUserToGroup ¶
AssignUserToGroup adds a user to a group.
func (*PGStore) Bootstrap ¶
func (s *PGStore) Bootstrap(ctx context.Context, superAdminEmail string, superAdminPassword string, organizations ...map[string][]string) error
Bootstrap creates the schema, seeds default permissions, and ensures the super admin user exists with all permissions and a password. Safe to call on every server start (idempotent). organizations is optional variadic map of organization name -> permission keys. superAdminPassword is optional — if provided, sets the super admin's password; if empty, no password is set.
func (*PGStore) ChangePassword ¶
func (s *PGStore) ChangePassword(ctx context.Context, userID string, currentPassword string, newPassword string) error
ChangePassword verifies the current password, then updates to the new one. Returns ErrPasswordNotSet if the user has no password (OTP-only account). Returns ErrPasswordInvalid if the current password is wrong. Returns ErrPasswordTooWeak if the new password doesn't meet strength requirements.
func (*PGStore) CreateGroup ¶
CreateGroup creates a new permission group.
func (*PGStore) CreateOTP ¶
CreateOTP generates a random OTP code for the given email and stores it.
func (*PGStore) CreateOrganizationWithPermissions ¶ added in v2.1.0
func (s *PGStore) CreateOrganizationWithPermissions(ctx context.Context, name string, permissionKeys []string) (*auth.Organization, error)
CreateOrganizationWithPermissions creates a new organization with assigned permissions. Idempotent — returns existing organization if already exists.
func (*PGStore) CreatePasswordReset ¶
func (s *PGStore) CreatePasswordReset(ctx context.Context, email string) (rawToken string, expiresAt time.Time, err error)
CreatePasswordReset generates a password reset token for the given email. Invalidates any existing unused tokens for that user. Returns the raw token (for the reset link) and expiry time.
func (*PGStore) CreatePermission ¶
func (s *PGStore) CreatePermission(ctx context.Context, key string, description string) (*auth.Permission, error)
CreatePermission creates a new permission with the given key and description.
func (*PGStore) CreateSchema ¶
CreateSchema applies all pending migrations. Delegates to Migrate for migration-based schema management.
func (*PGStore) CreateUser ¶
CreateUser creates a new user with the given email.
func (*PGStore) CreateUserWithOrganization ¶ added in v2.1.0
func (s *PGStore) CreateUserWithOrganization(ctx context.Context, email string, organization string) (*auth.User, error)
CreateUserWithOrganization creates a new user assigned to an organization.
func (*PGStore) DeleteGroup ¶
DeleteGroup deletes a group by ID. Cascades to group_permissions and user_groups.
func (*PGStore) DeletePermission ¶
DeletePermission deletes a permission by its ID. Cascades to user_permissions and group_permissions.
func (*PGStore) DropSchema ¶
DropSchema drops all auth tables and the migrations tracking table.
func (*PGStore) GetAllUserPermissions ¶ added in v2.1.0
func (s *PGStore) GetAllUserPermissions(ctx context.Context, userID string) ([]auth.Permission, error)
GetAllUserPermissions returns all permissions for a user (direct + from groups + from organizations).
func (*PGStore) GetGroupMembers ¶ added in v2.1.0
GetGroupMembers returns all users in a group.
func (*PGStore) GetOrganization ¶ added in v2.1.0
GetOrganization fetches an organization by ID, including its permissions.
func (*PGStore) GetOrganizationByName ¶ added in v2.1.0
func (s *PGStore) GetOrganizationByName(ctx context.Context, name string) (*auth.Organization, error)
GetOrganizationByName fetches an organization by name.
func (*PGStore) GetOrganizationPermissions ¶ added in v2.1.0
func (s *PGStore) GetOrganizationPermissions(ctx context.Context, orgID string) ([]auth.Permission, error)
GetOrganizationPermissions returns all permissions for an organization.
func (*PGStore) GetPermission ¶
GetPermission fetches a permission by its key.
func (*PGStore) GetResolvedPermissions ¶
func (s *PGStore) GetResolvedPermissions(ctx context.Context, userID string) ([]auth.Permission, error)
GetResolvedPermissions returns all permissions for a user (direct + from groups + from organizations), deduplicated.
func (*PGStore) GetUserByEmail ¶
GetUserByEmail fetches a user by their email. Returns nil, nil if not found.
func (*PGStore) GetUserByID ¶
GetUserByID fetches a user by their ID. Returns nil, nil if not found.
func (*PGStore) GetUserGroups ¶
GetUserGroups returns all groups a user belongs to.
func (*PGStore) GetUserOrganization ¶ added in v2.1.0
GetUserOrganization returns the organization assigned to a user.
func (*PGStore) GetUserPermissions ¶
GetUserPermissions returns all direct permissions for a user.
func (*PGStore) HasAnyPermission ¶ added in v2.1.0
func (s *PGStore) HasAnyPermission(ctx context.Context, userID string, permissionKeys []string) (bool, error)
HasAnyPermission checks if a user has any of the given permissions.
func (*PGStore) HasPassword ¶
HasPassword checks if a user has a password set.
func (*PGStore) HasPermission ¶
func (s *PGStore) HasPermission(ctx context.Context, userID string, permissionKey string) (bool, error)
HasPermission checks if a user has a specific direct permission.
func (*PGStore) HasResolvedPermission ¶
func (s *PGStore) HasResolvedPermission(ctx context.Context, userID string, permissionKey string) (bool, error)
HasResolvedPermission checks if a user has a permission (direct, via group, or via organization).
func (*PGStore) ListGroups ¶
ListGroups returns all groups (without permissions).
func (*PGStore) ListOrganizations ¶ added in v2.1.0
ListOrganizations returns all organizations (without permissions).
func (*PGStore) ListPermissions ¶
ListPermissions returns all permissions.
func (*PGStore) LoginWithPassword ¶
func (s *PGStore) LoginWithPassword(ctx context.Context, email string, plainPassword string) (*auth.User, error)
LoginWithPassword validates the email and password, returning the user if valid. Returns ErrPasswordNotSet if the user exists but has no password (OTP-only account). Returns ErrPasswordInvalid if the password is incorrect.
func (*PGStore) MigrationStatus ¶
MigrationStatus returns all migrations with their applied status.
func (*PGStore) RegisterWithPassword ¶
func (s *PGStore) RegisterWithPassword(ctx context.Context, email string, plainPassword string) (*auth.User, error)
RegisterWithPassword creates a new user with an email and password. Returns ErrEmailAlreadyRegistered if the email is already taken.
func (*PGStore) RemovePermissionFromGroup ¶
func (s *PGStore) RemovePermissionFromGroup(ctx context.Context, groupID string, permissionID string) error
RemovePermissionFromGroup removes a permission from a group.
func (*PGStore) RemovePermissionsFromOrganization ¶ added in v2.1.0
func (s *PGStore) RemovePermissionsFromOrganization(ctx context.Context, orgID string, permissionKeys []string) error
RemovePermissionsFromOrganization removes permissions from an organization.
func (*PGStore) RemoveUserFromGroup ¶
RemoveUserFromGroup removes a user from a group.
func (*PGStore) RemoveUsersFromGroup ¶ added in v2.1.0
RemoveUsersFromGroup removes multiple users from a group in bulk.
func (*PGStore) ResetPassword ¶
ResetPassword validates the reset token and updates the user's password. Marks the token as used atomically. Returns ErrResetTokenInvalid if the token is not found or has expired. Returns ErrResetTokenUsed if the token has already been used. Returns ErrPasswordTooWeak if the new password doesn't meet strength requirements.
func (*PGStore) RevokePermission ¶
RevokePermission removes a direct permission from a user.
func (*PGStore) SetPassword ¶
SetPassword updates a user's password (admin override). Returns ErrUserNotFound if the user does not exist.