Added reauthentication (token freshness) and protected username change

This commit is contained in:
2025-02-15 20:12:30 +11:00
parent 6bee6edd16
commit 42ea74fd63
10 changed files with 315 additions and 14 deletions

View File

@@ -5,14 +5,19 @@ import (
"projectreshoot/db"
)
type AuthenticatedUser struct {
*db.User
Fresh int64
}
// Return a new context with the user added in
func SetUser(ctx context.Context, u *db.User) context.Context {
func SetUser(ctx context.Context, u *AuthenticatedUser) context.Context {
return context.WithValue(ctx, contextKeyAuthorizedUser, u)
}
// Retrieve a user from the given context. Returns nil if not set
func GetUser(ctx context.Context) *db.User {
user, ok := ctx.Value(contextKeyAuthorizedUser).(*db.User)
func GetUser(ctx context.Context) *AuthenticatedUser {
user, ok := ctx.Value(contextKeyAuthorizedUser).(*AuthenticatedUser)
if !ok {
return nil
}