Authentication Zero Documentation

repository·main·Indexed 23 days ago

https://github.com/lazaronixon/authentication-zero

A code-generation tool for Ruby on Rails that injects a secure, customizable authentication system directly into an application. It provides full source code control rather than a closed library. Features include password breach checking, two-factor authentication, WebAuthn, social login via OmniAuth, passwordless sign-in, and multi-tenant support using AccountScoped and AccountMiddleware.

Tokens
1.2K
Snippets
1
Records
9
Agent score
84%

What's inside Authentication Zero

  1. Authentication Zero features

    main

    Authentication Zero supports several essential and advanced authentication features, many of which can be enabled via flags during generation (e.g., --api, --two-factor).

    Essential Features

    • Sign up and Email/Password validations
    • Password breach checking (--pwned)
    • Authentication via Cookie or Token (--api)
    • Two-factor authentication (--two-factor) and Hardware Security Keys (--webauthn)
    • Email verification via token links
    • Sudo mode (--sudoable)
    • Password reset (with verified email restrictions)
    • Email bombing protection (--lockable)
    • Session and device management
    • Activity logging (--trackable)

    Advanced Features

    • Social login via OmniAuth (--omniauthable)
    • Passwordless authentication (--passwordless)
    • Invitation system (--invitable)
    • "Sign-in as" / Masquerading (--masqueradable)
    • Multi-tenant support (--tenantable)
  2. Understand the Authentication Zero philosophy

    main

    Authentication Zero generates code directly into your application instead of providing a closed library.

    Key implications:

    • Freedom: You have full control to modify the authentication logic to fit your app.
    • Maintenance: Because the code is generated into your app, it will not be automatically updated when you update the gem. You are responsible for porting improvements or security fixes from the gem's CHANGELOG.md into your application code.
  3. Generate the authentication system

    main

    Once the gem is installed, run the Rails generator to inject the authentication code directly into your application. This approach provides complete freedom to modify the generated code to suit your specific use case.

    $ rails generate authentication
  4. Implement Sudoable functionality

    main
    To protect sensitive data or actions, you can require a user to re-authenticate (ask for their password) before proceeding. Use the before_action :require_sudo filter in your controllers. By default, this will prompt for a password on the first access or after 30 minutes.
  5. Implement Tenantable (Multi-tenant) applications

    main

    Authentication Zero provides artifacts to support row-level multi-tenancy using Current.account. To implement this, follow these steps:

    1. Prepare Tables: Add an account_id to every table that needs to be scoped. Example: rails g migration add_account_to_projects account:references.
    2. Scope Models: Include AccountScoped in your models to set up the relationship and a default scope based on the current account.
    3. Middleware Setup (Optional): To set Current.account via the URL (e.g., http://myapp.com/:account_id), configure the AccountMiddleware:
      • Add require_relative "../lib/account_middleware" to config/application.rb.
      • Add config.middleware.use AccountMiddleware to your application class.
  6. Scope models to the current account using AccountScoped

    main

    When building multi-tenant applications with Authentication Zero, you can include the AccountScoped concern in your ActiveRecord models to automatically scope all database queries to the currently active account.

    Including this concern performs two actions:

    1. Establishes a belongs_to :account relationship.
    2. Applies a default_scope that filters all queries using where account: Current.account.

    This ensures that data isolation is handled at the model level by leveraging the Current.account global context.

  7. Scaffold authentication with AuthenticationGenerator

    main
    Use the AuthenticationGenerator Rails generator to scaffold a complete authentication system. The generator allows you to customize the installation by including various security and functional features via command-line options. Depending on the options selected, it will add necessary gems, create migrations, models, controllers, views, and routes.
  8. Configure AuthenticationGenerator options

    main

    When running the AuthenticationGenerator, you can use the following flags to customize your authentication setup:

    OptionDescription
    --apiGenerates API-focused authentication (skips HTML views and certain controllers)
    --pwnedAdds pwned password validation (checks against data breaches)
    --sudoableAdds a requirement for a password request before sensitive data changes
    --lockableAdds password reset locking
    --passwordlessAdds passwordless sign-in functionality
    --omniauthableAdds social login support (OmniAuth)
    --trackableAdds activity log support (events)
    --two_factorAdds two-factor authentication (TOTP/Recovery Codes)
    --webauthnAdds hardware security key support (requires --two_factor)
    --invitableAdds user invitation functionality
    --masqueradableAdds 'sign-in as' functionality
    --tenantableAdds artifacts to implement a row-level tenant application