Show HN: Veri – minimal authentication framework for Rails
4 months ago
6
Veri is a cookie-based authentication library for Ruby on Rails that provides essential authentication building blocks without imposing business logic. Unlike full-featured solutions, Veri gives you complete control over your authentication flow while handling the complex underlying mechanics of secure password storage and session management.
Key Features:
Cookie-based authentication with database-stored sessions
Generate the migration for your user model (replace users with your user table name if different):
# For standard integer IDs
rails generate veri:authentication users
# For UUID primary keys
rails generate veri:authentication users --uuid
Run the migration:
If customization is required, configure Veri in an initializer:
# These are the default values; you can change them as neededVeri.configuredo |config|
config.hashing_algorithm=:argon2# Password hashing algorithm (:argon2, :bcrypt, or :scrypt)config.inactive_session_lifetime=nil# Session inactivity timeout (nil means sessions never expire due to inactivity)config.total_session_lifetime=14.days# Maximum session duration regardless of activityconfig.user_model_name="User"# Your user model nameend
Your user model is automatically extended with password management methods:
# Set or update a passworduser.update_password("new_password")# Verify a passworduser.verify_password("submitted_password")
Include the authentication module and configure protection:
classApplicationController < ActionController::BaseincludeVeri::Authenticationwith_authentication# Require authentication by defaultendclassPicturesController < ApplicationControllerskip_authenticationonly: [:index,:show]# Allow public access to index and show actionsend
This is a simplified example of how to use Veri's authentication methods:
logged_in? - Returns true if user is authenticated
log_in(user) - Authenticates user and creates session
log_out - Terminates current session
return_path - Returns path user was accessing before authentication
current_session - Returns current authentication session
User Impersonation (Shapeshifting)
Veri provides user impersonation functionality that allows, for example, administrators to temporarily assume another user's identity:
moduleAdminclassImpersonationController < ApplicationControllerdefcreateuser=User.find(params[:user_id])current_session.shapeshift(user)redirect_toroot_path,notice: "Now viewing as #{user.name}"enddefdestroyoriginal_user=current_session.true_identitycurrent_session.revert_to_true_identityredirect_toadmin_dashboard_path,notice: "Returned to #{original_user.name}"endendend
Available session methods:
shapeshift(user) - Assume another user's identity (maintains original identity)
revert_to_true_identity - Return to original identity
shapeshifted? - Returns true if currently shapeshifted
true_identity - Returns original user when shapeshifted, otherwise current user
Controller helper:
shapeshifter? - Returns true if the current session is shapeshifted
Override this private method to customize authentication behavior:
classApplicationController < ActionController::BaseincludeVeri::Authenticationwith_authentication# ...private# Customize unauthenticated user handlingdefwhen_unauthenticated# By default redirects back with a fallback to the root path if the request format is HTML,# otherwise responds with 401 Unauthorizedredirect_tologin_pathendend
Veri stores authentication sessions in the database, providing session management capabilities:
# Get all sessions for a useruser.veri_sessions# Get current session in controllercurrent_session
session.active?# Session is active (neither expired nor inactive)session.inactive?# Session exceeded inactivity timeoutsession.expired?# Session exceeded maximum lifetime
# Terminate a specific sessionsession.terminate# Terminate all sessions for a userVeri::Session.terminate_all(user)# Clean up expired/inactive sessionsVeri::Session.prune# All sessionsVeri::Session.prune(user)# Specific user's sessions
Access authentication state in your views:
<% if logged_in? %><p>Welcome, <%=current_user.name%>!</p><%ifshapeshifter?%><p><em>Currently viewing as <%=current_user.name%> (Original: <%=current_session.true_identity.name%>)</em></p><%=link_to"Return to Original Identity",revert_path,method: :patch%><%end%><%=link_to"Logout",logout_path,method: :delete%><%else%><%=link_to"Login",login_path%><%end%>
Veri doesn't provide test helpers, but you can easily create your own:
Request Specs (Recommended)
moduleAuthenticationHelpersdeflog_in(user)password="test_password"user.update_password(password)postlogin_path,params: {email: user.email,password: }enddeflog_outdeletelogout_pathendend# In your spec_helper.rbRSpec.configuredo |config|
config.includeAuthenticationHelpers,type: :requestend
Controller Specs (Legacy)
moduleAuthenticationHelpersdeflog_in(user)controller.log_in(user)enddeflog_outcontroller.log_outendend# In your spec_helper.rbRSpec.configuredo |config|
config.includeAuthenticationHelpers,type: :controllerend
Getting Help and Contributing
Have a question or need assistance? Open a discussion in our discussions section for: