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
// WithPermissions is the list of permissions the user should have to pass the middleware
WithPermissions[]string
}
// // Authenticator is the spec of this library
// type Authenticator interface {
// // Login checks user credentials and adds a session cookie to the user if successfull
// Login(w http.ResponseWriter, r *http.Request, userID, password string)
// // Logout clears the user session cookies (by setting the session cookie timeout to 0)
// Logout(w http.ResponseWriter)
// // Middleware is a configurable middleware to authenticate http routes based on logged status and permissions
// // RequestUser returns the userID for this cookie session token
// RequestUser(r *http.Request) (string, error)
// }
// 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{
// Authenticator handles cookies, authentication and authorization of http routes by providing middlewares, logint/logout methods, user sessions and retriving the userID of an authenticated request.
typeAuthenticatorinterface{
// CheckUserPassword is called to login a user and create a corresponding session, see also "SessionTokenFromUser"
// MiddlewareConfig configures the middleware to only accept logged users (if "RequireLogged" is true) and with certain permissions (user must have all permissions inside "WithPermissions")
typeMiddlewareConfigstruct{
// RequireLogged rejects not logged users if true
RequireLoggedbool
// NeedPermissions is the list of permissions the user should have to pass the middleware
NeedPermissions[]string
}
// AuthService is the spec of this library
typeAuthSessionServicestruct{
SessionCookieNamestring
Authenticator
}
// NewAuthSessionService creates a new *AuthSessionService with a default session cookie name