// This package provides a utility for managing session cookies.
//
// The main struct is "AuthService" that provides a pluggable system for login and logout of users, creating and storing their session tokens and middlewares for accepting only logged users or ones with some specified permissions.
ErrNoUserForSession=errors.New(`no user for session token`)
)
varSessionCookieName="session"// TODO: Make configurable
// AuthMiddlewareConfig configures the middleware to only accept logged users (if "RequireLogged" is true) and with certain permissions (user must have all permissions inside "WithPermissions")
typeAuthMiddlewareConfigstruct{
// RequireLogged rejects not logged users if true
RequireLoggedbool
@ -21,52 +24,52 @@ type AuthMiddlewareConfig struct {
WithPermissions[]string
}
typeAuthenticatorinterface{
// Login checks user credentials and adds a session cookie to the user if successfull
// RequestUser returns the userID for this cookie session token
RequestUser(r *http.Request) (string, error)
}
// // RequestUser returns the userID for this cookie session token
// RequestUser(r *http.Request) (string, error)
// }
var_Authenticator=&AuthService{}
// var _ Authenticator = &AuthService{}
// AuthService handles cookies, authentication and authorization of http routes by providing middlewares, logint/logout methods, user sessions and retriving the userID of an authenticated request.
typeAuthServicestruct{
// CheckUserPassword
// CheckUserPassword is called to login a user and create a corresponding session, see also "SessionTokenFromUser"
// Room represents a room in the database, a room has an id, a name and a collection of seatIDs of this room.
typeRoomstruct{
IDstring`json:"id"`
SeatIDs[]string`json:"seatIds"`
@ -32,6 +49,7 @@ type Room struct {
// gridRows, gridCols int
}
// Seat represents a seat in the database, it belongs to a single room and can be free or occupied by some user (referenced by userID).
typeSeatstruct{
IDstring`json:"id"`
RoomIDstring`json:"roomId"`
@ -42,8 +60,26 @@ type Seat struct {
// x, y, w, h int
}
// type FrontendSeat struct {
// ID string `json:"id"`
// RoomID string `json:"roomId"`
// // OccupiedBy is an empty list or a singleton of a userID
// OccupiedBy *FrontendUser `json:"occupiedBy"`
// DiagramRect struct {
// X int `json:"x"`
// Y int `json:"y"`
// Width int `json:"width"`
// Height int `json:"height"`
// } `json:"diagramRect"`
// }
//
// Database Interfaces
//
// Store is the main interface for interacting with database implementations, for now the only implementation is "memDB".
typeStoreinterface{
CreateUser(user*User)error
CreateRoom(room*Room)error
@ -70,12 +106,17 @@ type Store interface {
// TODO: Create an SQLite implementation
// memDB is the first Store implementation used for testing.
typememDBstruct{
// FIXME: Giusto per la cronaca fare le modifiche in questo modo alle mappe non è per niente thread safe, servirebbe come minimo usare un mutex per quando si scrive su una di queste variabili
// mutex *sync.Mutex
usersmap[string]*User
roomsmap[string]*Room
seatsmap[string]*Seat
}
// NewInMemoryStore creates an instance of memDB hidden behind the Store interface.
funcNewInMemoryStore()Store{
db:=&memDB{
make(map[string]*User),
@ -115,6 +156,10 @@ func NewInMemoryStore() Store {