securial 1.0.1 → 1.0.2
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.
- checksums.yaml +4 -4
- data/.yardopts +4 -0
- data/README.md +14 -9
- data/app/controllers/concerns/securial/identity.rb +91 -2
- data/app/controllers/securial/accounts_controller.rb +68 -5
- data/app/controllers/securial/application_controller.rb +34 -2
- data/app/controllers/securial/passwords_controller.rb +44 -4
- data/app/controllers/securial/role_assignments_controller.rb +55 -4
- data/app/controllers/securial/roles_controller.rb +54 -0
- data/app/controllers/securial/sessions_controller.rb +77 -3
- data/app/controllers/securial/status_controller.rb +24 -0
- data/app/controllers/securial/users_controller.rb +54 -0
- data/app/jobs/securial/application_job.rb +9 -0
- data/app/mailers/securial/application_mailer.rb +12 -0
- data/app/mailers/securial/securial_mailer.rb +30 -0
- data/app/models/concerns/securial/password_resettable.rb +70 -0
- data/app/models/securial/application_record.rb +19 -0
- data/app/models/securial/current.rb +13 -0
- data/app/models/securial/role.rb +17 -0
- data/app/models/securial/role_assignment.rb +16 -0
- data/app/models/securial/session.rb +79 -1
- data/app/models/securial/user.rb +34 -0
- data/lib/generators/factory_bot/model/model_generator.rb +1 -0
- data/lib/securial/auth.rb +44 -6
- data/lib/securial/cli.rb +124 -0
- data/lib/securial/config.rb +49 -2
- data/lib/securial/engine.rb +41 -0
- data/lib/securial/error/auth.rb +52 -0
- data/lib/securial/error/base_securial_error.rb +51 -0
- data/lib/securial/error/config.rb +33 -0
- data/lib/securial/error.rb +33 -3
- data/lib/securial/helpers.rb +48 -4
- data/lib/securial/logger/broadcaster.rb +89 -1
- data/lib/securial/logger/builder.rb +54 -1
- data/lib/securial/logger/formatter.rb +73 -0
- data/lib/securial/logger.rb +42 -1
- data/lib/securial/middleware.rb +40 -9
- data/lib/securial/security/request_rate_limiter.rb +47 -1
- data/lib/securial/security.rb +37 -6
- data/lib/securial/version.rb +8 -1
- data/lib/securial.rb +36 -0
- metadata +4 -3
data/app/models/securial/user.rb
CHANGED
@@ -1,4 +1,34 @@
|
|
1
1
|
module Securial
|
2
|
+
#
|
3
|
+
# User
|
4
|
+
#
|
5
|
+
# This class represents a user in the Securial authentication and authorization system.
|
6
|
+
#
|
7
|
+
# Users can have multiple roles assigned to them, which define their permissions
|
8
|
+
# and access levels within the application.
|
9
|
+
#
|
10
|
+
# The User model includes functionality for password reset, email normalization,
|
11
|
+
# and various validations to ensure data integrity.
|
12
|
+
#
|
13
|
+
# ## Attributes
|
14
|
+
# - `email_address`: The user's email address, which is normalized
|
15
|
+
# - `username`: A unique username for the user, with specific format requirements
|
16
|
+
# - `first_name`: The user's first name, required and limited in length
|
17
|
+
# - `last_name`: The user's last name, required and limited in length
|
18
|
+
# - `phone`: An optional phone number, limited in length
|
19
|
+
# - `bio`: An optional biography, limited in length
|
20
|
+
#
|
21
|
+
# ## Validations
|
22
|
+
# - Email address must be present, unique, and formatted correctly
|
23
|
+
# - Username must be present, unique (case insensitive), and formatted correctly
|
24
|
+
# - First and last names must be present and limited in length
|
25
|
+
# - Phone and bio fields are optional but have length restrictions
|
26
|
+
#
|
27
|
+
# ## Associations
|
28
|
+
# - Has many role assignments, allowing users to have multiple roles
|
29
|
+
# - Has many roles through role assignments, enabling flexible permission management
|
30
|
+
# - Has many sessions, allowing for session management and tracking
|
31
|
+
#
|
2
32
|
class User < ApplicationRecord
|
3
33
|
include Securial::PasswordResettable
|
4
34
|
|
@@ -45,6 +75,10 @@ module Securial
|
|
45
75
|
has_many :sessions, dependent: :destroy
|
46
76
|
|
47
77
|
|
78
|
+
# Checks if the user has the specified role.
|
79
|
+
#
|
80
|
+
# @param [String] role_name The name of the role to check
|
81
|
+
# @return [Boolean] Returns true if the user has the specified role, false otherwise.
|
48
82
|
def is_admin?
|
49
83
|
roles.exists?(role_name: Securial.titleized_admin_role)
|
50
84
|
end
|
data/lib/securial/auth.rb
CHANGED
@@ -1,12 +1,50 @@
|
|
1
|
+
# @title Securial Authentication System
|
2
|
+
#
|
3
|
+
# Core authentication components for the Securial framework.
|
4
|
+
#
|
5
|
+
# This file serves as the entry point for authentication-related functionality in Securial,
|
6
|
+
# loading specialized modules that handle token generation, encoding/decoding, and session management.
|
7
|
+
# These components work together to provide a secure, flexible authentication system supporting
|
8
|
+
# token-based and session-based authentication patterns.
|
9
|
+
#
|
10
|
+
# @example Encoding a session token
|
11
|
+
# # Create an encoded JWT representing a user session
|
12
|
+
# token = Securial::Auth::AuthEncoder.encode(user_session)
|
13
|
+
# # => "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIxMjM0NTY3ODkwIiwic3ViIjoiM..."
|
14
|
+
#
|
15
|
+
# @example Creating a new user session
|
16
|
+
# # Authenticate user and create a new session with tokens
|
17
|
+
# Securial::Auth::SessionCreator.create_session!(user, request)
|
18
|
+
#
|
19
|
+
# # Access the newly created session
|
20
|
+
# current_session = Current.session
|
21
|
+
#
|
22
|
+
# @example Generating secure tokens
|
23
|
+
# # Generate a password reset token
|
24
|
+
# reset_token = Securial::Auth::TokenGenerator.friendly_token
|
25
|
+
# # => "aBc123DeF456gHi789"
|
26
|
+
#
|
1
27
|
require "securial/auth/auth_encoder"
|
2
28
|
require "securial/auth/session_creator"
|
3
29
|
require "securial/auth/token_generator"
|
4
30
|
|
5
31
|
module Securial
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
32
|
+
# Namespace for authentication-related functionality in the Securial framework.
|
33
|
+
#
|
34
|
+
# The Auth module contains components that work together to provide a complete
|
35
|
+
# authentication system for Securial-powered applications:
|
36
|
+
#
|
37
|
+
# - {AuthEncoder} - Handles JWT token encoding and decoding with security measures
|
38
|
+
# - {SessionCreator} - Creates and manages authenticated user sessions
|
39
|
+
# - {TokenGenerator} - Generates secure random tokens for various authentication needs
|
40
|
+
#
|
41
|
+
# These components handle the cryptographic and security aspects of user authentication,
|
42
|
+
# ensuring that best practices are followed for token generation, validation, and session
|
43
|
+
# management throughout the application lifecycle.
|
44
|
+
#
|
45
|
+
# @see Securial::Auth::AuthEncoder
|
46
|
+
# @see Securial::Auth::SessionCreator
|
47
|
+
# @see Securial::Auth::TokenGenerator
|
48
|
+
#
|
49
|
+
module Auth; end
|
12
50
|
end
|
data/lib/securial/cli.rb
CHANGED
@@ -1,13 +1,51 @@
|
|
1
|
+
# @title Securial Command Line Interface
|
2
|
+
#
|
3
|
+
# Command line interface for the Securial framework.
|
4
|
+
#
|
5
|
+
# This file implements the CLI tool for Securial, providing command-line utilities
|
6
|
+
# for creating new Rails applications with Securial pre-installed. It handles
|
7
|
+
# command parsing, option flags, and orchestrates the application setup process.
|
8
|
+
#
|
9
|
+
# @example Creating a new Rails application with Securial
|
10
|
+
# # Create a basic Securial application
|
11
|
+
# $ securial new myapp
|
12
|
+
#
|
13
|
+
# # Create an API-only Securial application with PostgreSQL
|
14
|
+
# $ securial new myapi --api --database=postgresql
|
15
|
+
#
|
1
16
|
require "optparse"
|
2
17
|
|
3
18
|
# rubocop:disable Rails/Exit, Rails/Output
|
4
19
|
|
5
20
|
module Securial
|
21
|
+
# Command-line interface for the Securial gem.
|
22
|
+
#
|
23
|
+
# This class provides the command-line functionality for Securial, enabling users
|
24
|
+
# to create new Rails applications with Securial pre-installed and configured.
|
25
|
+
# It handles command parsing, flag processing, and orchestrates the setup of
|
26
|
+
# new applications.
|
27
|
+
#
|
6
28
|
class CLI
|
29
|
+
# Entry point for the CLI application.
|
30
|
+
#
|
31
|
+
# Creates a new CLI instance and delegates to its start method.
|
32
|
+
#
|
33
|
+
# @param argv [Array<String>] command line arguments
|
34
|
+
# @return [Integer] exit status code (0 for success, non-zero for errors)
|
35
|
+
#
|
7
36
|
def self.start(argv)
|
8
37
|
new.start(argv)
|
9
38
|
end
|
10
39
|
|
40
|
+
# Processes command line arguments and executes the appropriate action.
|
41
|
+
#
|
42
|
+
# This method handles both option flags (like --version) and commands
|
43
|
+
# (like 'new'), delegating to specialized handlers and returning
|
44
|
+
# the appropriate exit code.
|
45
|
+
#
|
46
|
+
# @param argv [Array<String>] command line arguments
|
47
|
+
# @return [Integer] exit status code (0 for success, non-zero for errors)
|
48
|
+
#
|
11
49
|
def start(argv)
|
12
50
|
# Process options and exit if a flag was handled
|
13
51
|
result = handle_flags(argv)
|
@@ -19,6 +57,14 @@ module Securial
|
|
19
57
|
|
20
58
|
private
|
21
59
|
|
60
|
+
# Processes option flags from the command line arguments.
|
61
|
+
#
|
62
|
+
# Parses options like --version and --help, executing their actions
|
63
|
+
# if present and removing them from the argument list.
|
64
|
+
#
|
65
|
+
# @param argv [Array<String>] command line arguments
|
66
|
+
# @return [nil, Integer] nil to continue processing, or exit code
|
67
|
+
#
|
22
68
|
def handle_flags(argv)
|
23
69
|
parser = create_option_parser
|
24
70
|
|
@@ -32,6 +78,14 @@ module Securial
|
|
32
78
|
end
|
33
79
|
end
|
34
80
|
|
81
|
+
# Processes commands from the command line arguments.
|
82
|
+
#
|
83
|
+
# Identifies the command (e.g., 'new') and delegates to the appropriate
|
84
|
+
# handler method for that command.
|
85
|
+
#
|
86
|
+
# @param argv [Array<String>] command line arguments
|
87
|
+
# @return [Integer] exit status code (0 for success, non-zero for errors)
|
88
|
+
#
|
35
89
|
def handle_commands(argv)
|
36
90
|
cmd = argv.shift
|
37
91
|
|
@@ -44,6 +98,14 @@ module Securial
|
|
44
98
|
end
|
45
99
|
end
|
46
100
|
|
101
|
+
# Handles the 'new' command for creating Rails applications.
|
102
|
+
#
|
103
|
+
# Validates that an application name is provided, then delegates to
|
104
|
+
# securial_new to create the application with Securial.
|
105
|
+
#
|
106
|
+
# @param argv [Array<String>] remaining command line arguments
|
107
|
+
# @return [Integer] exit status code (0 for success, non-zero for errors)
|
108
|
+
#
|
47
109
|
def handle_new_command(argv)
|
48
110
|
app_name = argv.shift
|
49
111
|
|
@@ -57,6 +119,12 @@ module Securial
|
|
57
119
|
0
|
58
120
|
end
|
59
121
|
|
122
|
+
# Creates and configures the option parser.
|
123
|
+
#
|
124
|
+
# Sets up the command-line options and their handlers.
|
125
|
+
#
|
126
|
+
# @return [OptionParser] configured option parser instance
|
127
|
+
#
|
60
128
|
def create_option_parser
|
61
129
|
OptionParser.new do |opts|
|
62
130
|
opts.banner = "Usage: securial [options] <command> [command options]\n\n"
|
@@ -79,6 +147,10 @@ module Securial
|
|
79
147
|
end
|
80
148
|
end
|
81
149
|
|
150
|
+
# Displays the current Securial version.
|
151
|
+
#
|
152
|
+
# @return [void]
|
153
|
+
#
|
82
154
|
def show_version
|
83
155
|
require "securial/version"
|
84
156
|
puts "Securial v#{Securial::VERSION}"
|
@@ -86,6 +158,15 @@ module Securial
|
|
86
158
|
puts "Securial version information not available."
|
87
159
|
end
|
88
160
|
|
161
|
+
# Creates a new Rails application with Securial pre-installed.
|
162
|
+
#
|
163
|
+
# Orchestrates the process of creating a Rails application, adding the
|
164
|
+
# Securial gem, installing dependencies, and configuring the application.
|
165
|
+
#
|
166
|
+
# @param app_name [String] name of the Rails application to create
|
167
|
+
# @param rails_options [Array<String>] options to pass to 'rails new'
|
168
|
+
# @return [void]
|
169
|
+
#
|
89
170
|
def securial_new(app_name, rails_options)
|
90
171
|
puts "🏗️ Creating new Rails app: #{app_name}"
|
91
172
|
|
@@ -97,27 +178,53 @@ module Securial
|
|
97
178
|
print_final_instructions(app_name)
|
98
179
|
end
|
99
180
|
|
181
|
+
# Creates a new Rails application.
|
182
|
+
#
|
183
|
+
# @param app_name [String] name of the Rails application to create
|
184
|
+
# @param rails_options [Array<String>] options to pass to 'rails new'
|
185
|
+
# @return [Integer] command exit status
|
186
|
+
#
|
100
187
|
def create_rails_app(app_name, rails_options)
|
101
188
|
rails_command = ["rails", "new", app_name, *rails_options]
|
102
189
|
run(rails_command)
|
103
190
|
end
|
104
191
|
|
192
|
+
# Adds the Securial gem to the application's Gemfile.
|
193
|
+
#
|
194
|
+
# @param app_name [String] name of the Rails application
|
195
|
+
# @return [void]
|
196
|
+
#
|
105
197
|
def add_securial_gem(app_name)
|
106
198
|
puts "📦 Adding Securial gem to Gemfile"
|
107
199
|
gemfile_path = File.join(app_name, "Gemfile")
|
108
200
|
File.open(gemfile_path, "a") { |f| f.puts "\ngem 'securial'" }
|
109
201
|
end
|
110
202
|
|
203
|
+
# Installs gems for the application using Bundler.
|
204
|
+
#
|
205
|
+
# @param app_name [String] name of the Rails application
|
206
|
+
# @return [Integer] command exit status
|
207
|
+
#
|
111
208
|
def install_gems(app_name)
|
112
209
|
run("bundle install", chdir: app_name)
|
113
210
|
end
|
114
211
|
|
212
|
+
# Installs and configures Securial in the application.
|
213
|
+
#
|
214
|
+
# @param app_name [String] name of the Rails application
|
215
|
+
# @return [Integer] command exit status
|
216
|
+
#
|
115
217
|
def install_securial(app_name)
|
116
218
|
puts "🔧 Installing Securial"
|
117
219
|
run("bin/rails generate securial:install", chdir: app_name)
|
118
220
|
run("bin/rails db:migrate", chdir: app_name)
|
119
221
|
end
|
120
222
|
|
223
|
+
# Mounts the Securial engine in the application's routes.
|
224
|
+
#
|
225
|
+
# @param app_name [String] name of the Rails application
|
226
|
+
# @return [void]
|
227
|
+
#
|
121
228
|
def mount_securial_engine(app_name)
|
122
229
|
puts "🔗 Mounting Securial engine in routes"
|
123
230
|
routes_path = File.join(app_name, "config/routes.rb")
|
@@ -128,6 +235,11 @@ module Securial
|
|
128
235
|
File.write(routes_path, updated)
|
129
236
|
end
|
130
237
|
|
238
|
+
# Prints final setup instructions after successful installation.
|
239
|
+
#
|
240
|
+
# @param app_name [String] name of the Rails application
|
241
|
+
# @return [void]
|
242
|
+
#
|
131
243
|
def print_final_instructions(app_name)
|
132
244
|
puts <<~INSTRUCTIONS
|
133
245
|
🎉 Securial has been successfully installed in your Rails app!
|
@@ -140,6 +252,16 @@ module Securial
|
|
140
252
|
INSTRUCTIONS
|
141
253
|
end
|
142
254
|
|
255
|
+
# Runs a system command with optional directory change.
|
256
|
+
#
|
257
|
+
# Executes the provided command, optionally in a different directory,
|
258
|
+
# and handles success/failure conditions.
|
259
|
+
#
|
260
|
+
# @param command [String, Array<String>] command to run
|
261
|
+
# @param chdir [String, nil] directory to change to before running command
|
262
|
+
# @return [Integer] command exit status code (0 for success)
|
263
|
+
# @raise [SystemExit] if the command fails
|
264
|
+
#
|
143
265
|
def run(command, chdir: nil)
|
144
266
|
puts "→ #{command.inspect}"
|
145
267
|
result =
|
@@ -156,3 +278,5 @@ module Securial
|
|
156
278
|
end
|
157
279
|
end
|
158
280
|
end
|
281
|
+
|
282
|
+
# rubocop:enable Rails/Exit, Rails/Output
|
data/lib/securial/config.rb
CHANGED
@@ -1,16 +1,52 @@
|
|
1
|
+
# @title Securial Configuration System
|
2
|
+
#
|
3
|
+
# Configuration management for the Securial framework.
|
4
|
+
#
|
5
|
+
# This file defines the configuration system for Securial, providing mechanisms
|
6
|
+
# to set, retrieve, and validate configuration options. It uses a conventional
|
7
|
+
# Ruby configuration block pattern to allow applications to customize Securial's
|
8
|
+
# behavior through an easy-to-use interface.
|
9
|
+
#
|
10
|
+
# @example Basic configuration
|
11
|
+
# # In config/initializers/securial.rb
|
12
|
+
# Securial.configure do |config|
|
13
|
+
# config.jwt_secret = ENV["JWT_SECRET"]
|
14
|
+
# config.token_expiry = 2.hours
|
15
|
+
# config.refresh_token_expiry = 7.days
|
16
|
+
# config.session_timeout = 1.hour
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# @example Accessing configuration values
|
20
|
+
# # Get the configured token expiry
|
21
|
+
# expiry = Securial.configuration.token_expiry
|
22
|
+
#
|
1
23
|
require "securial/config/configuration"
|
2
24
|
require "securial/config/signature"
|
3
25
|
require "securial/config/validation"
|
4
26
|
|
5
|
-
|
6
27
|
module Securial
|
7
28
|
extend self
|
8
|
-
attr_accessor :configuration
|
9
29
|
|
30
|
+
# Gets the current configuration instance.
|
31
|
+
#
|
32
|
+
# Returns the existing configuration or creates a new default configuration
|
33
|
+
# if one hasn't been set yet.
|
34
|
+
#
|
35
|
+
# @return [Securial::Config::Configuration] the current configuration instance
|
10
36
|
def configuration
|
11
37
|
@configuration ||= Config::Configuration.new
|
12
38
|
end
|
13
39
|
|
40
|
+
# Sets the configuration instance.
|
41
|
+
#
|
42
|
+
# Validates the provided configuration to ensure all required settings
|
43
|
+
# are present and have valid values.
|
44
|
+
#
|
45
|
+
# @param config [Securial::Config::Configuration] the configuration instance to use
|
46
|
+
# @raise [ArgumentError] if the provided object is not a Configuration instance
|
47
|
+
# @raise [Securial::Error::Config::ValidationError] if the configuration is invalid
|
48
|
+
# @return [Securial::Config::Configuration] the newly set configuration
|
49
|
+
#
|
14
50
|
def configuration=(config)
|
15
51
|
if config.is_a?(Config::Configuration)
|
16
52
|
@configuration = config
|
@@ -20,8 +56,19 @@ module Securial
|
|
20
56
|
end
|
21
57
|
end
|
22
58
|
|
59
|
+
# Configures the Securial framework using a block.
|
60
|
+
#
|
61
|
+
# This method provides the conventional Ruby configuration block pattern,
|
62
|
+
# yielding the current configuration instance to the provided block for
|
63
|
+
# modification. After the block executes, the configuration is validated.
|
64
|
+
#
|
65
|
+
# @yield [config] Yields the configuration instance to the block
|
66
|
+
# @yieldparam config [Securial::Config::Configuration] the configuration instance
|
67
|
+
# @raise [Securial::Error::Config::ValidationError] if the configuration is invalid
|
68
|
+
# @return [Securial::Config::Configuration] the updated configuration
|
23
69
|
def configure
|
24
70
|
yield(configuration) if block_given?
|
25
71
|
Securial::Config::Validation.validate_all!(configuration)
|
72
|
+
configuration
|
26
73
|
end
|
27
74
|
end
|
data/lib/securial/engine.rb
CHANGED
@@ -1,3 +1,25 @@
|
|
1
|
+
# @title Securial Rails Engine
|
2
|
+
#
|
3
|
+
# Rails engine configuration for the Securial framework.
|
4
|
+
#
|
5
|
+
# This file defines the core Rails engine that powers Securial, setting up
|
6
|
+
# namespace isolation, generator defaults, and loading all required components.
|
7
|
+
# The engine configuration establishes how Securial integrates with host Rails
|
8
|
+
# applications and defines the conventions used throughout the codebase.
|
9
|
+
#
|
10
|
+
# @example Mounting the engine in a Rails application
|
11
|
+
# # In config/routes.rb
|
12
|
+
# Rails.application.routes.draw do
|
13
|
+
# mount Securial::Engine => '/securial'
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# @example Accessing engine routes in views or controllers
|
17
|
+
# # Generate path to Securial login
|
18
|
+
# securial.login_path
|
19
|
+
#
|
20
|
+
# # Generate URL to Securial user profile
|
21
|
+
# securial.user_url(@user)
|
22
|
+
#
|
1
23
|
require "securial/auth"
|
2
24
|
require "securial/config"
|
3
25
|
require "securial/error"
|
@@ -7,11 +29,29 @@ require "securial/middleware"
|
|
7
29
|
require "securial/security"
|
8
30
|
|
9
31
|
module Securial
|
32
|
+
# Rails engine implementation for the Securial framework.
|
33
|
+
#
|
34
|
+
# This class defines the core engine that powers Securial's integration with
|
35
|
+
# Rails applications. It isolates all Securial components within their own
|
36
|
+
# namespace to prevent conflicts with host application code and configures
|
37
|
+
# generator defaults to ensure consistent code generation.
|
38
|
+
#
|
39
|
+
# The engine handles:
|
40
|
+
# - Namespace isolation to prevent conflicts
|
41
|
+
# - Configuration of generators for consistent code generation
|
42
|
+
# - Setting up Securial's API-only mode for JSON responses
|
43
|
+
# - Loading initializers for various framework components
|
44
|
+
#
|
45
|
+
# @see https://guides.rubyonrails.org/engines.html Rails Engines Guide
|
46
|
+
#
|
10
47
|
class Engine < ::Rails::Engine
|
48
|
+
# Isolates the Securial namespace to prevent conflicts with host applications
|
11
49
|
isolate_namespace Securial
|
12
50
|
|
51
|
+
# Configures the engine for API-only mode by default
|
13
52
|
config.generators.api_only = true
|
14
53
|
|
54
|
+
# Sets up standard generator configurations for consistent code generation
|
15
55
|
config.generators do |g|
|
16
56
|
g.orm :active_record, primary_key_type: :string
|
17
57
|
g.test_framework :rspec,
|
@@ -28,4 +68,5 @@ module Securial
|
|
28
68
|
end
|
29
69
|
end
|
30
70
|
|
71
|
+
# Load engine initializers that configure various aspects of the framework
|
31
72
|
require_relative "engine_initializers"
|
data/lib/securial/error/auth.rb
CHANGED
@@ -1,18 +1,70 @@
|
|
1
|
+
# @title Securial Authentication Errors
|
2
|
+
#
|
3
|
+
# Authentication-related errors for the Securial framework.
|
4
|
+
#
|
5
|
+
# This file defines error classes for authentication-related issues in the
|
6
|
+
# Securial framework, such as token encoding/decoding failures, expired tokens,
|
7
|
+
# and revoked sessions. These errors help identify and troubleshoot problems
|
8
|
+
# with the authentication system during runtime.
|
9
|
+
#
|
10
|
+
# @example Handling authentication errors
|
11
|
+
# begin
|
12
|
+
# decoded_token = Securial::Auth::AuthEncoder.decode(token)
|
13
|
+
# rescue Securial::Error::Auth::TokenDecodeError => e
|
14
|
+
# # Handle invalid token format
|
15
|
+
# logger.warn("Invalid token format: #{e.message}")
|
16
|
+
# rescue Securial::Error::Auth::TokenExpiredError => e
|
17
|
+
# # Handle expired token
|
18
|
+
# render json: { error: "Your session has expired. Please log in again." }, status: :unauthorized
|
19
|
+
# rescue Securial::Error::Auth::TokenRevokedError => e
|
20
|
+
# # Handle revoked token
|
21
|
+
# render json: { error: "Your session has been revoked." }, status: :unauthorized
|
22
|
+
# end
|
23
|
+
#
|
1
24
|
module Securial
|
2
25
|
module Error
|
26
|
+
# Namespace for authentication-related errors in the Securial framework.
|
27
|
+
#
|
28
|
+
# These errors are raised during authentication operations such as
|
29
|
+
# token encoding/decoding, session validation, and access control checks.
|
30
|
+
#
|
3
31
|
module Auth
|
32
|
+
# Error raised when a JWT token cannot be encoded properly.
|
33
|
+
#
|
34
|
+
# This may occur due to invalid payload data, missing required claims,
|
35
|
+
# or issues with the signing key.
|
36
|
+
#
|
37
|
+
# @see Securial::Auth::AuthEncoder#encode
|
38
|
+
#
|
4
39
|
class TokenEncodeError < BaseError
|
5
40
|
default_message "Error while encoding session token"
|
6
41
|
end
|
7
42
|
|
43
|
+
# Error raised when a JWT token cannot be decoded or validated.
|
44
|
+
#
|
45
|
+
# This may occur due to malformed tokens, invalid signatures,
|
46
|
+
# or tokens that don't match the expected format.
|
47
|
+
#
|
48
|
+
# @see Securial::Auth::AuthEncoder#decode
|
49
|
+
#
|
8
50
|
class TokenDecodeError < BaseError
|
9
51
|
default_message "Error while decoding session token"
|
10
52
|
end
|
11
53
|
|
54
|
+
# Error raised when attempting to use a token from a revoked session.
|
55
|
+
#
|
56
|
+
# This occurs when a token references a session that has been
|
57
|
+
# explicitly revoked, such as after a user logout or account suspension.
|
58
|
+
#
|
12
59
|
class TokenRevokedError < BaseError
|
13
60
|
default_message "Session token is revoked"
|
14
61
|
end
|
15
62
|
|
63
|
+
# Error raised when attempting to use an expired token.
|
64
|
+
#
|
65
|
+
# This occurs when a token's expiration time (exp claim) has passed,
|
66
|
+
# indicating that the token is no longer valid for authentication.
|
67
|
+
#
|
16
68
|
class TokenExpiredError < BaseError
|
17
69
|
default_message "Session token is expired"
|
18
70
|
end
|
@@ -1,17 +1,68 @@
|
|
1
|
+
# @title Securial Base Error
|
2
|
+
#
|
3
|
+
# Base error class for the Securial framework.
|
4
|
+
#
|
5
|
+
# This file defines the base error class that all other specialized Securial
|
6
|
+
# errors inherit from. It provides functionality for default error messages
|
7
|
+
# and customized backtrace handling.
|
8
|
+
#
|
1
9
|
module Securial
|
2
10
|
module Error
|
11
|
+
# Base error class for all Securial-specific exceptions.
|
12
|
+
#
|
13
|
+
# This class serves as the foundation for Securial's error hierarchy, providing
|
14
|
+
# common functionality like default messages and backtrace customization.
|
15
|
+
# All specialized error types in Securial should inherit from this class.
|
16
|
+
#
|
17
|
+
# @example Defining a custom error class
|
18
|
+
# module Securial
|
19
|
+
# module Error
|
20
|
+
# class AuthError < BaseError
|
21
|
+
# default_message "Authentication failed"
|
22
|
+
# end
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# @example Raising a custom error
|
27
|
+
# raise Securial::Error::AuthError
|
28
|
+
# # => Authentication failed (Securial::Error::AuthError)
|
29
|
+
#
|
30
|
+
# raise Securial::Error::AuthError, "Invalid token provided"
|
31
|
+
# # => Invalid token provided (Securial::Error::AuthError)
|
32
|
+
#
|
3
33
|
class BaseError < StandardError
|
4
34
|
class_attribute :_default_message, instance_writer: false
|
5
35
|
|
36
|
+
# Sets or gets the default message for this error class.
|
37
|
+
#
|
38
|
+
# This class method allows error classes to define a standard message
|
39
|
+
# that will be used when no explicit message is provided at instantiation.
|
40
|
+
#
|
41
|
+
# @param message [String, nil] The default message to set for this error class
|
42
|
+
# @return [String, nil] The current default message for this error class
|
43
|
+
#
|
6
44
|
def self.default_message(message = nil)
|
7
45
|
self._default_message = message if message
|
8
46
|
self._default_message
|
9
47
|
end
|
10
48
|
|
49
|
+
# Initializes a new error instance with the provided message or default.
|
50
|
+
#
|
51
|
+
# @param message [String, nil] The error message or nil to use default
|
52
|
+
# @return [BaseError] New error instance
|
53
|
+
#
|
11
54
|
def initialize(message = nil)
|
12
55
|
super(message || self.class._default_message || "An error occurred in Securial")
|
13
56
|
end
|
14
57
|
|
58
|
+
# Returns an empty backtrace.
|
59
|
+
#
|
60
|
+
# This method overrides the standard backtrace behavior to return an empty array,
|
61
|
+
# which helps keep error responses clean without showing internal implementation
|
62
|
+
# details in production environments.
|
63
|
+
#
|
64
|
+
# @return [Array] An empty array
|
65
|
+
#
|
15
66
|
def backtrace; []; end
|
16
67
|
end
|
17
68
|
end
|
@@ -1,6 +1,39 @@
|
|
1
|
+
# @title Securial Configuration Errors
|
2
|
+
#
|
3
|
+
# Configuration-related errors for the Securial framework.
|
4
|
+
#
|
5
|
+
# This file defines error classes for configuration-related issues in the
|
6
|
+
# Securial framework, such as missing or invalid configuration options.
|
7
|
+
# These errors help identify and troubleshoot problems with application setup.
|
8
|
+
#
|
1
9
|
module Securial
|
2
10
|
module Error
|
11
|
+
# Namespace for configuration-related errors in the Securial framework.
|
12
|
+
#
|
13
|
+
# These errors are raised when the Securial configuration is invalid,
|
14
|
+
# missing required values, or contains values that don't meet validation
|
15
|
+
# requirements.
|
16
|
+
#
|
17
|
+
# @example Handling configuration errors
|
18
|
+
# begin
|
19
|
+
# Securial.configure do |config|
|
20
|
+
# # Configure Securial settings
|
21
|
+
# end
|
22
|
+
# rescue Securial::Error::Config::InvalidConfigurationError => e
|
23
|
+
# Rails.logger.error("Securial configuration error: #{e.message}")
|
24
|
+
# # Handle configuration error
|
25
|
+
# end
|
26
|
+
#
|
3
27
|
module Config
|
28
|
+
# Error raised when Securial configuration is invalid.
|
29
|
+
#
|
30
|
+
# This error is typically raised during application initialization when
|
31
|
+
# the provided configuration doesn't meet requirements, such as missing
|
32
|
+
# required settings or having values that fail validation.
|
33
|
+
#
|
34
|
+
# @example Raising a configuration error
|
35
|
+
# raise InvalidConfigurationError, "JWT secret is missing"
|
36
|
+
#
|
4
37
|
class InvalidConfigurationError < Securial::Error::BaseError
|
5
38
|
default_message "Invalid configuration for Securial"
|
6
39
|
end
|
data/lib/securial/error.rb
CHANGED
@@ -1,9 +1,39 @@
|
|
1
|
+
# Requires all error classes used in the Securial framework.
|
2
|
+
#
|
3
|
+
# This file serves as the central error management system for Securial,
|
4
|
+
# loading all specialized error types and establishing the Error namespace.
|
5
|
+
# Each error type extends from BaseSecurialError and provides specific
|
6
|
+
# error handling for different parts of the framework.
|
7
|
+
#
|
8
|
+
# @example Accessing a specific error type
|
9
|
+
# begin
|
10
|
+
# # Some operation that might fail
|
11
|
+
# rescue Securial::Error::Auth::TokenDecodeError => e
|
12
|
+
# # Handle token decoding errors
|
13
|
+
# rescue Securial::Error::Config::ValidationError => e
|
14
|
+
# # Handle configuration validation errors
|
15
|
+
# rescue Securial::Error::BaseError => e
|
16
|
+
# # Handle any other Securial errors
|
17
|
+
# end
|
18
|
+
#
|
1
19
|
require "securial/error/base_securial_error"
|
2
20
|
require "securial/error/config"
|
3
21
|
require "securial/error/auth"
|
4
22
|
|
5
23
|
module Securial
|
6
|
-
|
7
|
-
|
8
|
-
|
24
|
+
# Namespace for all Securial-specific errors and exceptions.
|
25
|
+
#
|
26
|
+
# The Error module contains specialized error classes for different
|
27
|
+
# components of the Securial framework, organized into submodules
|
28
|
+
# for better categorization:
|
29
|
+
#
|
30
|
+
# - {BaseError} - Base class for all Securial errors
|
31
|
+
# - {Config} - Configuration-related errors
|
32
|
+
# - {Auth} - Authentication and authorization errors
|
33
|
+
#
|
34
|
+
# @see Securial::Error::BaseError
|
35
|
+
# @see Securial::Error::Config
|
36
|
+
# @see Securial::Error::Auth
|
37
|
+
#
|
38
|
+
module Error; end
|
9
39
|
end
|