Authboss
repository·master·Indexed 26 days ago
https://github.com/aarondl/authbossA modular authentication system for Go web applications. Authboss provides pluggable modules for common authentication and authorization features—including password auth, registration, email confirmation, password recovery, and two-factor authentication (OTP, TOTP, SMS)—allowing developers to enable only the specific functionality their application requires. It is designed to be framework-agnostic and requires implementations for specific storage and core interfaces.
What's inside Authboss
- Authboss is a modular authentication system for the web designed to provide common authentication and authorization features. It uses a modular architecture that allows you to enable only the specific features your application requires, reducing integration effort and minimizing the risk of security mistakes. It is designed to be framework-agnostic and can integrate with or without a specific web framework.
Get the current user
masterTo retrieve the currently authenticated user viaAuthboss.CurrentUser, you must first load the client state into the request context. This is typically done by using theAuthboss.LoadClientStateMiddleware. Alternatively, you can callAuthboss.LoadClientStatemanually.Reset a user's password
masterUse
Authboss.UpdatePasswordto update a user's password. This method automatically handles bcrypt cost requirements and invalidates existing 'remember me' tokens.Note:
UpdatePassworddoes not log the user out. If you want to force a logout after a password reset, you should useauthboss.DelKnownCookieto erase known cookies. (Note:authboss.DelKnownSessionis deprecated for security reasons).Implement 'Remember Me' functionality
masterThe
remembermodule uses cookie storage to log users in without an active session.Middleware Setup
You must include both
LoadClientStateMiddlewareandremember.Middlewarein your middleware stack. To ensure client state is available to Authboss mechanisms,remember.Middlewaremust be placed afterLoadClientStateMiddleware. Because it manages session-less logins, it should be placed high up in the stack.Storage Requirements
- Client Storage: Uses Session and Cookies.
- Server Storage: Requires a
RememberingServerStorer. Note that this storer typically requires a separate database table to save tokens to a PID, as it does not use the standardUserstruct.
Handling 'Half-Authed' Users
Users logged in via Remember tokens are considered "half-authed". You can check for this state using the session key
authboss.SessionHalfAuthKey.To prevent half-authed users from accessing sensitive routes (e.g., changing user details), use the
forceFullAuthboolean flag on theauthboss.Middlewareto protect those routes.Implement User Registration
masterThe
registermodule allows users to self-register. To prevent data loss when validation fails (e.g., a password doesn't meet requirements), useRegisterPreserveFieldsto whitelist fields that should be kept in the session.Requirements:
- Module:
register - Routes:
/register - Middleware:
LoadClientStateMiddleware - Client Storage: Session
- Server Storer:
CreatingServerStorer - User Interface:
AuthableUser(and optionallyArbitraryUserfor extra fields) - Values:
UserValuer(and optionallyArbitraryValuer)
Preserving Fields in Templates: When registration fails, whitelisted values are stored in the data key
authboss.DataPreserveas amap[string]string. In your templates, access these using.preserve.field_name. Always use{{with ...}}to avoid errors if the map is empty or nil.Example template usage:
{{with .preserve.username}} <input type="text" name="username" value="{{ . }}"> {{end}}- Module:
Implement JSON views for APIs
masterIf you are building an API for a JavaScript-based frontend, you should implement a renderer that converts data to JSON. Authboss provides a simple JSON renderer in thedefaultspackage that you can use to satisfy theRendererinterface.Implement Registration Confirmation via Email
masterThe
confirmmodule enables email-based registration confirmation. This can be used standalone or triggered by a hook in theregistermodule.Requirements:
- Module:
confirm - Routes:
/confirm - Emails:
confirm_html,confirm_txt - Middleware:
LoadClientStateMiddlewareandconfirm.Middleware - Client Storage: Session
- Server Storer:
ConfirmingServerStorer - User Interface:
ConfirmableUserandConfirmValuer - Mailer: Required
Security Note: Confirmations use both a
selectorand averifierin the database to prevent timing attacks. Always ensure yourConfirmingServerStorersearches by theselectorand not theverifier.- Module:
Implement the Renderer interface
masterAuthboss uses a single interface,Renderer, to handle template loading and execution. To integrate Authboss with your own rendering engine (such ashtml/template), you must implement theRendererinterface by defining how to load templates in aLoad()method and how to execute them with provided data in aRender()method.Using One Time Passwords with the otp module
masterThe
otpmodule allows users to add, clear, or log in using one-time passwords. This is useful for backup access or logging in from untrusted computers.Key Behaviors:
- Logging in with an OTP is functionally identical to a standard password login, except the OTP is consumed immediately upon use.
- Note: This module is a mechanism for alternative login methods and is distinct from Two-Factor Authentication (2FA).
Inject view data using the default Responder
masterThe defaultResponderin Authboss collects data from the Request context. You can inject custom data (such as layout information or CSRF tokens) into the Request context, and theResponderwill automatically collect it for rendering.Install Authboss v3
masterAuthboss v3 requires Go modules. You can install the latest version using the following command:
go get -u github.com/aarondl/authboss/v3Use JSON Views for API-driven applications
masterIf you are building an API intended for a JavaScript frontend, you should use a renderer that converts data to JSON. A simple JSON renderer is available in thegithub.com/aarondl/authboss/v3/defaultspackage.