Role System
BookMe has two user roles defined ininternal/service/reservation.go:20:
Role Assignment
Roles are automatically assigned during OAuth registration based on the 42 Intranet API response:Roles are determined by the
staff? boolean field from the 42 API. Hive staff members automatically receive the STAFF role, while students receive STUDENT.Role Storage
Roles are:- Stored in database:
users.rolecolumn - Embedded in JWT: Part of the token claims
- Propagated via context: Available to all handlers
Permission Model
BookMe uses a permission-based authorization model where each operation checks permissions inline rather than using a centralized permission system.Reservation Permissions
Creating Reservations
All authenticated users can create reservations, but students have duration limits:- Students: Maximum 4-hour reservation duration
- Staff: No duration limit
The 4-hour limit for students is hardcoded in the service layer. Staff members can book rooms for any duration to accommodate longer meetings or events.
Viewing Reservations
All authenticated users can view reservations, but who made the reservation is conditionally visible:- Everyone: Can see all reservations (times and rooms)
- Staff: Can see who made every reservation
- Students: Can only see their own name on their reservations
- Privacy: Other students’ names are hidden from non-staff users
Why hide booking names?
Why hide booking names?
This privacy feature prevents students from seeing who booked meeting rooms. Only staff members (who manage the facility) and the person who made the reservation can see the name.This helps prevent:
- Unwanted interruptions of private meetings
- Social pressure around room usage
- Potential misuse of booking information
Cancelling Reservations
Reservation cancellation has the most complex authorization logic:- Owners: Can cancel their own reservations
- Staff: Can cancel any reservation (useful for facility management)
- Other students: Cannot cancel someone else’s reservation
Context-Based Authorization
User information flows through the request using Go’scontext.Context:
Setting User Context
Authenticate middleware populates this context after verifying the JWT token.
Retrieving User Context
User Structure
The context contains a minimal user representation:Authorization in Handlers
Handlers extract user information and pass it to service methods:Middleware Chain
Authorization works through a chain of middleware:- Rate Limiter: Prevents abuse
- Authenticate: Validates JWT, adds user to context
- RequireAuth: Rejects requests without valid user
- Handler: Accesses user from context
Role Checking Patterns
Pattern 1: Role-Based Logic
Simple boolean checks based on role:Pattern 2: Ownership Check
Combining ownership with role:Pattern 3: Conditional Visibility
Showing/hiding data based on permissions:*string) allows the API to omit sensitive data without breaking the response structure.
Campus Validation
Before even creating a user account, BookMe validates campus affiliation:Campus ID 13 is hardcoded as Hive Helsinki. Users must have this as their primary campus to register. This is a coarse-grained authorization check at the registration level.
Authorization Error Handling
Authorization failures return specific error types: Service-Level Errors:ErrUnauthorizedCancellation: Attempting to cancel someone else’s reservationErrInvalidCampus: User from wrong campus trying to registerErrExceedsMaxDuration: Student trying to book > 4 hours
401 Unauthorized: Not authenticated (no valid JWT)403 Forbidden: Authenticated but not authorized (e.g., cancelling another user’s reservation)400 Bad Request: Business rule violation (e.g., duration limit)
Future Authorization Enhancements
Potential authorization features not yet implemented:Possible Future Features
Possible Future Features
- Admin Role: Separate role for system administrators with full permissions
- Room Permissions: Certain rooms restricted to staff only
- Time-Based Restrictions: Different rules for peak vs. off-peak hours
- Quota System: Limit number of concurrent reservations per user
- Blackout Periods: Block certain dates/times for maintenance
- Delegation: Allow users to make reservations on behalf of others
Best Practices
When working with authorization in BookMe:- Always check permissions in the service layer, not just in handlers. Handlers can be bypassed; service methods are the real gatekeepers.
- Use the user from context, not from the request body. Clients can lie about their ID/role; the JWT is the source of truth.
-
Log authorization failures for security monitoring:
- Return specific errors to help with debugging, but avoid leaking sensitive information in error messages.
- Test authorization paths separately from happy paths. Ensure unauthorized actions are properly rejected.