Documentation
¶
Index ¶
- Constants
- func CountUsers(d *sql.DB) (int, error)
- func CreateTokensTable(d *sql.DB) error
- func CreateUsersTable(d *sql.DB) error
- func DeleteToken(d *sql.DB, token string) error
- func DeleteTokensByUser(d *sql.DB, userID string) error
- func DeleteUser(d *sql.DB, id string) (bool, error)
- func GenerateID() string
- func GenerateToken() (string, error)
- func HashPassword(password string) (string, error)
- func InsertToken(d *sql.DB, token, userID string) error
- func InsertUser(d *sql.DB, u *User, passwordHash string) error
- func Middleware(d *sql.DB) func(http.Handler) http.Handler
- func RegisterRoutes(mux *http.ServeMux, d *sql.DB)
- func UpdateUser(d *sql.DB, id string, fields map[string]string, updateTime string) error
- func WithContext(ctx context.Context, u *User) context.Context
- type User
- func FromContext(ctx context.Context) *User
- func GetUserByEmail(d *sql.DB, email string) (*User, string, error)
- func GetUserByID(d *sql.DB, id string) (*User, string, error)
- func GetUserByPath(d *sql.DB, path string) (*User, string, error)
- func GetUserByToken(d *sql.DB, token string) (*User, error)
- func ListUsers(d *sql.DB, pageSize int, pageToken string) ([]User, string, error)
Constants ¶
const ( TypeSuperuser = "superuser" TypeRegular = "regular" )
const ( // UserResourceSingular is the singular name for the built-in user resource. UserResourceSingular = "user" // UserResourcePlural is the plural name for the built-in user resource. UserResourcePlural = "users" )
Variables ¶
This section is empty.
Functions ¶
func CreateTokensTable ¶
CreateTokensTable creates the _tokens table if it does not exist.
func CreateUsersTable ¶
CreateUsersTable creates the _users table if it does not exist.
func DeleteToken ¶
DeleteToken removes a specific token.
func DeleteTokensByUser ¶
DeleteTokensByUser removes all tokens for a user.
func DeleteUser ¶
DeleteUser deletes a user by ID. Returns true if a row was deleted.
func GenerateID ¶
func GenerateID() string
GenerateID creates a hex-encoded nanosecond timestamp ID (matches aepbase pattern).
func GenerateToken ¶
GenerateToken creates a cryptographically random token string.
func HashPassword ¶
HashPassword returns a bcrypt hash of the given password.
func InsertToken ¶
InsertToken stores a new auth token.
func InsertUser ¶
InsertUser inserts a new user into the database.
func Middleware ¶
Middleware returns an http.Handler that validates the Authorization header, looks up the token in the database, and injects the User into the request context. Requests without a valid token receive a 401 response.
The login endpoint (POST /users/:login) and the OpenAPI spec endpoint (GET /openapi.json) are exempt from auth.
func RegisterRoutes ¶
RegisterRoutes registers user CRUD and auth endpoints on the mux.
func UpdateUser ¶
UpdateUser updates mutable user fields.
Types ¶
type User ¶
type User struct {
ID string `json:"id"`
Path string `json:"path"`
Email string `json:"email"`
DisplayName string `json:"display_name,omitempty"`
Type string `json:"type"`
CreateTime string `json:"create_time"`
UpdateTime string `json:"update_time"`
}
User represents an authenticated user of the API.
func FromContext ¶
FromContext extracts the authenticated User from the request context. Returns nil if no user is present.
func GetUserByEmail ¶
GetUserByEmail retrieves a user by email.
func GetUserByID ¶
GetUserByID retrieves a user by ID.
func GetUserByPath ¶
GetUserByPath retrieves a user by path.
func GetUserByToken ¶
GetUserByToken looks up the user associated with a token.