rodauth-tools 0.3.1 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "version": "1.0.0",
4
4
  "private": true,
5
5
  "devDependencies": {
6
- "markdownlint-cli2": "^0.15.0"
6
+ "markdownlint-cli2": "^0.23.2"
7
7
  },
8
8
  "scripts": {
9
9
  "lint:md": "markdownlint-cli2 '**/*.md' '#node_modules'"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rodauth-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - delano
@@ -65,8 +65,8 @@ files:
65
65
  - ".pre-commit-config.yaml"
66
66
  - ".rubocop.yml"
67
67
  - ".rubocop_todo.yml"
68
+ - AGENTS.md
68
69
  - CHANGELOG.md
69
- - CLAUDE.md
70
70
  - CODE_OF_CONDUCT.md
71
71
  - CONTRIBUTING.md
72
72
  - Gemfile
@@ -74,14 +74,17 @@ files:
74
74
  - LICENSE.txt
75
75
  - README.md
76
76
  - Rakefile
77
+ - lib/rodauth/features/account_id_obfuscation.rb
77
78
  - lib/rodauth/features/external_identity.rb
78
79
  - lib/rodauth/features/hmac_secret_guard.rb
79
80
  - lib/rodauth/features/jwt_secret_guard.rb
80
81
  - lib/rodauth/features/table_guard.rb
82
+ - lib/rodauth/secret_guard.rb
81
83
  - lib/rodauth/sequel_generator.rb
82
84
  - lib/rodauth/table_inspector.rb
83
85
  - lib/rodauth/template_inspector.rb
84
86
  - lib/rodauth/tools.rb
87
+ - lib/rodauth/tools/account_id_cipher.rb
85
88
  - lib/rodauth/tools/console_helpers.rb
86
89
  - lib/rodauth/tools/migration.rb
87
90
  - lib/rodauth/tools/migration/sequel/account_expiration.erb
@@ -130,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
133
  - !ruby/object:Gem::Version
131
134
  version: '0'
132
135
  requirements: []
133
- rubygems_version: 3.7.2
136
+ rubygems_version: 3.6.9
134
137
  specification_version: 4
135
138
  summary: Framework-agnostic Rodauth tools
136
139
  test_files: []
data/CLAUDE.md DELETED
@@ -1,262 +0,0 @@
1
- # CLAUDE.md
2
-
3
- This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4
-
5
- ## Project Purpose
6
-
7
- Framework-agnostic utilities for Rodauth authentication:
8
-
9
- 1. **External Rodauth Features** - `table_guard` for database validation, `external_identity` for external service IDs, `hmac_secret_guard` for HMAC secret validation, `jwt_secret_guard` for JWT secret validation
10
- 2. **Sequel Migration Generator** - Generate migrations for 19 Rodauth features
11
-
12
- **Not a framework adapter.** For Rails integration, use rodauth-rails. This project demonstrates Rodauth's extensibility and provides reference implementations.
13
-
14
- **Status:** Experimental learning project. Not published to RubyGems.
15
-
16
- **Recent Refactoring (2025-10):** Namespace changed from `Rodauth::Rack::Generators::Migration` to `Rodauth::Tools::Migration`. This reflects the project's evolution away from being a Rack adapter toward being a collection of framework-agnostic utilities. The migration generator is now deprecated in favor of the `table_guard` feature with `sequel_mode`.
17
-
18
- ## Development Commands
19
-
20
- ```bash
21
- # Run all tests
22
- bundle exec rspec
23
-
24
- # Interactive console with helpers
25
- bin/console
26
- ```
27
-
28
- ## Architecture Overview
29
-
30
- ### Core Components
31
-
32
- **lib/rodauth/features/table_guard.rb** - External Rodauth feature
33
-
34
- - Uses `Rodauth::Feature.define(:table_guard, :TableGuard)` pattern
35
- - Validates database tables exist for enabled features at `post_configure` time
36
- - Provides introspection methods: `missing_tables`, `table_status`, `list_all_required_tables`
37
- - Configurable modes: `:warn`, `:error`, `:silent`, or custom block handler
38
- - Demonstrates proper feature lifecycle hooks and configuration DSL
39
-
40
- **lib/rodauth/features/hmac_secret_guard.rb** - External Rodauth feature
41
-
42
- - Uses `Rodauth::Feature.define(:hmac_secret_guard, :HmacSecretGuard)` pattern
43
- - Automatically loads HMAC secret from environment variable (defaults to `HMAC_SECRET`)
44
- - Validates secret is configured at application startup via `post_configure` hook
45
- - Production mode: Raises `ConfigurationError` if secret missing
46
- - Development mode: Logs warning and uses configurable fallback secret
47
- - Deletes secret from ENV after loading for security
48
- - Provides `production?` and `validate_secrets!` public methods
49
-
50
- **lib/rodauth/features/jwt_secret_guard.rb** - External Rodauth feature
51
-
52
- - Uses `Rodauth::Feature.define(:jwt_secret_guard, :JwtSecretGuard)` pattern
53
- - Automatically loads JWT secret from environment variable (defaults to `JWT_SECRET`)
54
- - Validates secret is configured at application startup via `post_configure` hook
55
- - Production mode: Raises `ConfigurationError` if secret missing
56
- - Development mode: Logs warning and uses configurable fallback secret
57
- - Deletes secret from ENV after loading for security
58
- - Provides `production?` and `validate_secrets!` public methods
59
- - Defines `jwt_secret` configuration method for standalone use
60
-
61
- **lib/rodauth/tools/migration.rb** - Sequel migration generator
62
-
63
- - Generates database migrations for 19 Rodauth features
64
- - Uses ERB templates in `lib/rodauth/tools/migration/sequel/`
65
- - Provides `generate()` for migration content and `migration_name()` for filename
66
- - Uses dry-inflector gem for robust table name pluralization
67
- - Mock database adapter pattern when no real DB connection provided
68
- - Deprecated in favor of table_guard feature with sequel_mode
69
-
70
- ### How Rodauth Features Work
71
-
72
- Rodauth features are modules that mix into `Rodauth::Auth` instances:
73
-
74
- ```ruby
75
- Feature.define(:feature_name, :FeatureName) do
76
- # Configuration methods (overridable by users)
77
- auth_value_method :setting_name, 'default_value'
78
-
79
- # Public methods (overridable by users)
80
- auth_methods :public_method
81
-
82
- # Private methods (not overridable)
83
- auth_private_methods :internal_helper
84
-
85
- # Lifecycle hook - runs after configuration
86
- def post_configure
87
- super if defined?(super)
88
- # Initialization code
89
- end
90
- end
91
- ```
92
-
93
- **Key Pattern:** Methods defined in features become part of the Rodauth instance. Users override them in configuration blocks:
94
-
95
- ```ruby
96
- plugin :rodauth do
97
- enable :feature_name
98
-
99
- setting_name 'custom_value' # Override auth_value_method
100
-
101
- public_method do # Override auth_methods
102
- # Custom implementation
103
- end
104
- end
105
- ```
106
-
107
- ### Table Guard Implementation Details
108
-
109
- **Logger Suppression:** The `table_exists?` method temporarily suppresses Sequel's logger during table existence checks. This prevents confusing ERROR logs from Sequel when checking non-existent tables (Sequel's `table_exists?` attempts a SELECT and logs the exception before catching it).
110
-
111
- **Configuration Storage:** Uses instance variables set by `auth_value_method`:
112
-
113
- - Block configs stored as Procs in `@table_guard_mode`
114
- - Symbol configs stored directly as `:warn`, `:error`, `:silent`
115
-
116
- **Check Strategy:**
117
-
118
- 1. `should_check_tables?` examines `@table_guard_mode` to decide if checking is needed
119
- 2. Returns `true` if mode is a Proc (block), enabling custom handlers
120
- 3. Returns `true` if mode is `:warn` or `:error`, `false` for `:silent`
121
-
122
- **Execution Flow:**
123
-
124
- 1. `post_configure` hook calls `check_required_tables!` if `should_check_tables?` returns true
125
- 2. `check_required_tables!` gets missing tables via `missing_tables`
126
- 3. For symbol modes (`:warn`, `:error`), handles directly
127
- 4. For block modes, calls block with missing tables, handles return value (`:error`, `:continue`, String)
128
-
129
- **Introspection Methods:**
130
-
131
- - `all_table_methods` - Finds all methods ending in `_table` using Ruby reflection
132
- - `missing_tables` - Checks each table method against `db.table_exists?`
133
- - `table_status` - Returns array of hashes with method, table name, and existence status
134
-
135
- ### Migration Generator Architecture
136
-
137
- **Note:** The Migration class is deprecated. For new code, use the `table_guard` feature with `sequel_mode` instead.
138
-
139
- **Template System:**
140
-
141
- - Each feature has ERB template in `lib/rodauth/tools/migration/sequel/`
142
- - Templates use binding from Migration instance for variables like `table_prefix`
143
- - `generate()` loads, evaluates, and concatenates all feature templates
144
-
145
- **Pluralization:**
146
-
147
- - Uses `dry-inflector` gem for intelligent pluralization (e.g., "status" → "statuses")
148
- - Helper method `pluralize(str)` available in templates via ERB binding
149
- - Removed Rails/ActiveRecord dependencies (68 lines of cruft eliminated)
150
-
151
- **Database Adapter Pattern:**
152
-
153
- - `MockSequelDatabase` simulates database when no real connection provided
154
- - Allows template generation without active database
155
- - Real `Sequel::Database` object can be passed for actual migrations
156
- - Supports PostgreSQL, MySQL, and SQLite database types
157
-
158
- ### Hidden Tables Architecture
159
-
160
- **Problem:** Some tables are created in ERB templates without corresponding `*_table` methods in Rodauth features.
161
-
162
- **Example from base.erb:**
163
-
164
- ```ruby
165
- # base.erb creates THREE tables:
166
- create_table(:account_statuses) # NO METHOD - Hidden!
167
- create_table(:account_password_hashes) # NO METHOD - Hidden!
168
- create_table(:accounts) # Has accounts_table method ✓
169
- ```
170
-
171
- **Why This Happens:**
172
-
173
- - `account_statuses` - Lookup table for status values (Unverified=1, Verified=2, Closed=3). No method because users configure status IDs directly via `account_open_status_value`, etc.
174
- - `account_password_hashes` - Separate table for security. Method is `account_password_hash_table` (singular), but ERB uses pluralized form based on `table_prefix`.
175
-
176
- #### Solution: TemplateInspector Module
177
-
178
- `lib/rodauth/template_inspector.rb` extracts table names directly from ERB templates by:
179
-
180
- 1. Creating minimal binding context with `table_prefix`, `pluralize`, and mock `db`
181
- 2. Evaluating ERB templates to render actual Ruby code
182
- 3. Parsing rendered code for `create_table()` calls using regex
183
- 4. Returning complete list of tables, including hidden ones
184
-
185
- **Usage:**
186
-
187
- ```ruby
188
- # Extract all tables for a feature
189
- tables = TemplateInspector.extract_tables_from_template(
190
- :base,
191
- table_prefix: 'account'
192
- )
193
- # => [:account_statuses, :account_password_hashes, :accounts]
194
-
195
- # Get tables for multiple features
196
- all_tables = TemplateInspector.all_tables_for_features(
197
- [:base, :verify_account, :lockout],
198
- table_prefix: 'account'
199
- )
200
- ```
201
-
202
- **Impact on DROP Operations:**
203
-
204
- Before TemplateInspector, `generate_drop_statements` only dropped dynamically discovered tables, missing hidden ones. Now it extracts the complete table list from ERB templates, ensuring all tables are properly dropped in correct dependency order.
205
-
206
- **Key Insight:** ERB templates are the single source of truth for table schemas. By extracting information FROM templates instead of duplicating it in Ruby constants, we maintain consistency and eliminate hardcoded mappings.
207
-
208
- ## Testing Patterns
209
-
210
- **RSpec Structure:**
211
-
212
- - `spec/spec_helper.rb` - Minimal configuration, loads `rodauth/rack`
213
- - Feature specs test both behavior and configuration
214
- - Migration generator specs verify template output and configuration
215
-
216
- **Console Helpers:**
217
-
218
- - `setup_test_db` - Creates in-memory SQLite database with tables
219
- - `create_app(db, features: [...])` - Creates Roda app with Rodauth configured
220
- - Useful for interactive testing of table_guard and migration generator
221
-
222
- ## Documentation Reference
223
-
224
- **docs/rodauth-features-api.md** - Complete reference for feature development DSL methods
225
-
226
- **docs/rodauth-internals.rdoc** - Object model explanation:
227
-
228
- - `Rodauth::Auth` - Authentication class (where features mix in)
229
- - `Rodauth::Configuration` - Configuration DSL class
230
- - `Rodauth::Feature` - Module subclass for feature definitions
231
- - `Rodauth::FeatureConfiguration` - Configuration module for features
232
-
233
- **docs/rodauth-mail.md** - Email/SMTP configuration patterns
234
-
235
- **DEVELOPMENT.md** - Architectural decisions, standard Rack integration pattern
236
-
237
- ## Integration Pattern
238
-
239
- Rodauth integrates with any Rack app via Roda middleware (NOT via this library):
240
-
241
- ```ruby
242
- # Create Roda app with Rodauth
243
- class RodauthApp < Roda
244
- plugin :middleware
245
- plugin :rodauth do
246
- enable :login, :logout
247
- enable :table_guard # ← Feature from this library
248
- db DB
249
- end
250
-
251
- route do |r|
252
- r.rodauth
253
- env['rodauth'] = rodauth
254
- end
255
- end
256
-
257
- # Mount as Rack middleware
258
- use RodauthApp
259
- run MyApp
260
- ```
261
-
262
- Access in your app: `request.env['rodauth']` provides all authentication methods.