Authboss

repository·master·Indexed 26 days ago

https://github.com/aarondl/authboss

A 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.

Tokens
21.2K
Snippets
37
Records
143
Agent score
86%

What's inside Authboss

  1. Overview of Authboss

    master
    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.
  2. Get the current user

    master
    To retrieve the currently authenticated user via Authboss.CurrentUser, you must first load the client state into the request context. This is typically done by using the Authboss.LoadClientStateMiddleware. Alternatively, you can call Authboss.LoadClientState manually.
  3. Reset a user's password

    master

    Use Authboss.UpdatePassword to update a user's password. This method automatically handles bcrypt cost requirements and invalidates existing 'remember me' tokens.

    Note: UpdatePassword does not log the user out. If you want to force a logout after a password reset, you should use authboss.DelKnownCookie to erase known cookies. (Note: authboss.DelKnownSession is deprecated for security reasons).

  4. Implement 'Remember Me' functionality

    master

    The remember module uses cookie storage to log users in without an active session.

    Middleware Setup

    You must include both LoadClientStateMiddleware and remember.Middleware in your middleware stack. To ensure client state is available to Authboss mechanisms, remember.Middleware must be placed after LoadClientStateMiddleware. 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 standard User struct.

    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 forceFullAuth boolean flag on the authboss.Middleware to protect those routes.

  5. Implement User Registration

    master

    The register module allows users to self-register. To prevent data loss when validation fails (e.g., a password doesn't meet requirements), use RegisterPreserveFields to 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 optionally ArbitraryUser for extra fields)
    • Values: UserValuer (and optionally ArbitraryValuer)

    Preserving Fields in Templates: When registration fails, whitelisted values are stored in the data key authboss.DataPreserve as a map[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}}
  6. Implement JSON views for APIs

    master
    If 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 the defaults package that you can use to satisfy the Renderer interface.
  7. Implement Registration Confirmation via Email

    master

    The confirm module enables email-based registration confirmation. This can be used standalone or triggered by a hook in the register module.

    Requirements:

    • Module: confirm
    • Routes: /confirm
    • Emails: confirm_html, confirm_txt
    • Middleware: LoadClientStateMiddleware and confirm.Middleware
    • Client Storage: Session
    • Server Storer: ConfirmingServerStorer
    • User Interface: ConfirmableUser and ConfirmValuer
    • Mailer: Required

    Security Note: Confirmations use both a selector and a verifier in the database to prevent timing attacks. Always ensure your ConfirmingServerStorer searches by the selector and not the verifier.

  8. Implement the Renderer interface

    master
    Authboss uses a single interface, Renderer, to handle template loading and execution. To integrate Authboss with your own rendering engine (such as html/template), you must implement the Renderer interface by defining how to load templates in a Load() method and how to execute them with provided data in a Render() method.
  9. Using One Time Passwords with the otp module

    master

    The otp module 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).
  10. Inject view data using the default Responder

    master
    The default Responder in Authboss collects data from the Request context. You can inject custom data (such as layout information or CSRF tokens) into the Request context, and the Responder will automatically collect it for rendering.