shieldify 0.1.2.pre.alpha
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +375 -0
- data/Rakefile +3 -0
- data/app/controllers/users/access_controller.rb +16 -0
- data/app/controllers/users/emails/reset_passwords_controller.rb +30 -0
- data/app/controllers/users/emails_controller.rb +17 -0
- data/app/models/jwt_session.rb +5 -0
- data/lib/generators/shieldify/USAGE +8 -0
- data/lib/generators/shieldify/install_generator.rb +52 -0
- data/lib/generators/shieldify/templates/initializer.rb.tt +52 -0
- data/lib/generators/shieldify/templates/locales/en.shieldify.yml.tt +58 -0
- data/lib/generators/shieldify/templates/locales/es.shieldify.yml.tt +48 -0
- data/lib/generators/shieldify/templates/mailer_layouts/mailer.html.erb +10 -0
- data/lib/generators/shieldify/templates/mailer_layouts/mailer.text.erb +3 -0
- data/lib/generators/shieldify/templates/mailer_views/email_changed.html.erb +3 -0
- data/lib/generators/shieldify/templates/mailer_views/email_changed.text.erb +5 -0
- data/lib/generators/shieldify/templates/mailer_views/email_confirmation_instructions.html.erb +7 -0
- data/lib/generators/shieldify/templates/mailer_views/email_confirmation_instructions.text.erb +7 -0
- data/lib/generators/shieldify/templates/mailer_views/password_changed.html.erb +3 -0
- data/lib/generators/shieldify/templates/mailer_views/password_changed.text.erb +5 -0
- data/lib/generators/shieldify/templates/mailer_views/reset_email_password_instructions.html.erb +5 -0
- data/lib/generators/shieldify/templates/mailer_views/reset_email_password_instructions.text.erb +9 -0
- data/lib/generators/shieldify/templates/mailer_views/unlock_access_instructions.html.erb +4 -0
- data/lib/generators/shieldify/templates/mailer_views/unlock_access_instructions.text.erb +7 -0
- data/lib/generators/shieldify/templates/migration.rb.tt +28 -0
- data/lib/generators/shieldify/templates/model.rb.tt +2 -0
- data/lib/shieldify/controllers/helpers.rb +29 -0
- data/lib/shieldify/failure_app.rb +8 -0
- data/lib/shieldify/jwt_service.rb +158 -0
- data/lib/shieldify/mailer.rb +44 -0
- data/lib/shieldify/middleware/authentication.rb +27 -0
- data/lib/shieldify/middleware.rb +36 -0
- data/lib/shieldify/model_extensions.rb +73 -0
- data/lib/shieldify/models/email_authenticatable/confirmable.rb +159 -0
- data/lib/shieldify/models/email_authenticatable/registerable.rb +117 -0
- data/lib/shieldify/models/email_authenticatable.rb +41 -0
- data/lib/shieldify/railtie.rb +52 -0
- data/lib/shieldify/strategies/email.rb +48 -0
- data/lib/shieldify/strategies/jwt.rb +78 -0
- data/lib/shieldify/version.rb +3 -0
- data/lib/shieldify.rb +74 -0
- data/lib/tasks/shieldify_tasks.rake +4 -0
- metadata +163 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6517cc618765bddd3e12fae12071e15d5c1fce7caba98f48468b8997d7269d72
|
4
|
+
data.tar.gz: b62d7d9fd12ffbaa0dd40261925e537bbfe88dbcaafa40f54b2fb666b44c400d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cde0954c079bd2fa807002523e8f0b9d6fa929efef4c8c2154fb737625fa6c8e9a54041426718172e663f46cc664eb158f42fd3b2e783acdf3a7f375a7ec9394
|
7
|
+
data.tar.gz: 05e5de36f1b7344fbdd56610ad32b0169be7e9e57c4cbea03b32483a7f7972a220d7915fa024dbb1f298b4eac00204852df081a4f456cc7ccfd6bc45f2ccf4ac
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright Armando Alejandre
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,375 @@
|
|
1
|
+
# Shieldify
|
2
|
+
|
3
|
+
![Gem Version](https://img.shields.io/gem/v/shieldify.svg)
|
4
|
+
|
5
|
+
Shieldify is a Ruby on Rails user authentication plugin. It handles authentication through email and API authentication via JWT (JSON Web Tokens). The gem includes functionalities for user registration, email confirmation, and user authentication via email/password and JWT.
|
6
|
+
|
7
|
+
## Features
|
8
|
+
|
9
|
+
- **Authentication**:
|
10
|
+
- **Email Authentication**: Allows users to authenticate using their email and password, generating a JWT that is managed in a whitelist.
|
11
|
+
- **API Authentication with JWT**: Manages API authentication using JSON Web Tokens to secure requests.
|
12
|
+
|
13
|
+
- **User Management**:
|
14
|
+
- **Email**:
|
15
|
+
- **Registration**: Facilitates new user registration and sends confirmation emails upon registration.
|
16
|
+
- **Confirmation**: Manages email address confirmation for user accounts through confirmation emails.
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
To install the Shieldify gem, follow these steps:
|
21
|
+
|
22
|
+
Add the gem to your Gemfile:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
gem 'shieldify'
|
26
|
+
```
|
27
|
+
|
28
|
+
Run `bundle install` to install the gem:
|
29
|
+
|
30
|
+
```sh
|
31
|
+
bundle install
|
32
|
+
```
|
33
|
+
|
34
|
+
Run the Shieldify install generator to set up the necessary files and configurations:
|
35
|
+
|
36
|
+
```sh
|
37
|
+
rails generate shieldify:install
|
38
|
+
```
|
39
|
+
|
40
|
+
### Install Generator Steps
|
41
|
+
|
42
|
+
The `rails generate shieldify:install` command performs the following steps:
|
43
|
+
|
44
|
+
1. **Copy Initializer**:
|
45
|
+
- **Description**: Copies the initializer file for Shieldify to the application's initializer directory.
|
46
|
+
- **Generated File**: `config/initializers/shieldify.rb`
|
47
|
+
- **Purpose**: This file is used to configure settings for Shieldify, such as JWT parameters, password complexity requirements, and other authentication-related settings.
|
48
|
+
|
49
|
+
2. **Generate Migration**:
|
50
|
+
- **Description**: Creates a migration file to set up the users table with necessary fields for authentication.
|
51
|
+
- **Generated File**: `db/migrate/[timestamp]_shieldify_create_users.rb`
|
52
|
+
- **Purpose**: This migration creates the users table with fields like `email`, `password_digest`, `email_confirmation_token`, `email_confirmation_token_generated_at`, etc., which are essential for managing user authentication.
|
53
|
+
|
54
|
+
3. **Generate Model**:
|
55
|
+
- **Description**: Generates the User model if it does not already exist.
|
56
|
+
- **Generated File**: `app/models/user.rb`
|
57
|
+
- **Purpose**: Defines the User model and includes necessary modules for email authentication, user registration, and email confirmation.
|
58
|
+
|
59
|
+
4. **Inject Method**:
|
60
|
+
- **Description**: Injects the `shieldify` method call into the User model to include the necessary Shieldify modules.
|
61
|
+
- **Injected Code**: `shieldify email_authenticatable: %i[registerable confirmable]`
|
62
|
+
- **Purpose**: This call specifies which Shieldify modules should be included in the User model, enabling email authentication, user registration, and email confirmation functionalities.
|
63
|
+
|
64
|
+
5. **Copy Mailer Layouts**:
|
65
|
+
- **Description**: Copies mailer layout files to the application's views directory.
|
66
|
+
- **Generated Directory**: `app/views/layouts/shieldify`
|
67
|
+
- **Purpose**: Provides the layout templates used for sending emails related to user authentication, such as confirmation emails and password reset emails.
|
68
|
+
|
69
|
+
6. **Copy Mailer Views**:
|
70
|
+
- **Description**: Copies mailer view templates to the application's views directory.
|
71
|
+
- **Generated Directory**: `app/views/shieldify/mailer`
|
72
|
+
- **Purpose**: Provides the email templates used for various user authentication-related emails, including email confirmation instructions and email changed notification.
|
73
|
+
|
74
|
+
7. **Copy Locale File**:
|
75
|
+
- **Description**: Copies the locale file to the application's locales directory.
|
76
|
+
- **Generated File**: `config/locales/en.shieldify.yml`
|
77
|
+
- **Purpose**: Contains translations for the messages and notifications used by Shieldify, ensuring that they are properly localized.
|
78
|
+
|
79
|
+
### Final Steps
|
80
|
+
|
81
|
+
Run the migrations to update your database schema:
|
82
|
+
|
83
|
+
```sh
|
84
|
+
rails db:migrate
|
85
|
+
```
|
86
|
+
|
87
|
+
Configure your mailer settings to ensure that confirmation emails are sent correctly. Update your environment configuration (e.g., `config/environments/development.rb`) with the appropriate settings.
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
config.action_mailer.delivery_method = :smtp
|
91
|
+
config.action_mailer.smtp_settings = {
|
92
|
+
address: 'smtp.example.com',
|
93
|
+
port: 587,
|
94
|
+
user_name: 'your_email@example.com',
|
95
|
+
password: 'your_password',
|
96
|
+
authentication: 'plain',
|
97
|
+
enable_starttls_auto: true
|
98
|
+
}
|
99
|
+
```
|
100
|
+
|
101
|
+
That's it! You have successfully installed and configured the Shieldify gem in your Rails application.
|
102
|
+
|
103
|
+
## Usage
|
104
|
+
|
105
|
+
### Authentication
|
106
|
+
|
107
|
+
#### Email and Password
|
108
|
+
|
109
|
+
Authentication using email and password in this plugin provides a robust and secure way to verify users and generate JSON Web Tokens (JWT) for session management. This method leverages middleware and Warden strategies to seamlessly handle authentication requests. Below is a detailed overview of how this process works:
|
110
|
+
|
111
|
+
- **Middleware and Warden Strategy**:
|
112
|
+
Authentication is processed through middleware that intercepts requests. If the request ends with `/shfy/login`, the Warden strategy is used to authenticate the user. If authentication is successful, a JWT is generated and sent in the response headers as `Authorization: Bearer <token>`.
|
113
|
+
|
114
|
+
- **Shieldify Middleware**:
|
115
|
+
The middleware intercepts requests to handle authentication based on the URL.
|
116
|
+
|
117
|
+
- **Warden Email Strategy**:
|
118
|
+
The Warden strategy validates the user's credentials (`email` and `password`) that have to come with the request and, if correct, generates a JWT and sends it in the response headers.
|
119
|
+
|
120
|
+
- **Custom Response**:
|
121
|
+
In the controller (not generated by the plugin), you can return whatever you want in the body, customizing the response according to your needs.
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
render json: { message: 'Authenticated successfully', user: current_user }
|
125
|
+
```
|
126
|
+
|
127
|
+
##### Usage Example
|
128
|
+
|
129
|
+
1. **Add the Route**:
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
# config/routes.rb
|
133
|
+
post '/shfy/login', to: 'sessions#create'
|
134
|
+
```
|
135
|
+
|
136
|
+
2. **Create the Controller that Handles the Login**:
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
# app/controllers/sessions_controller.rb
|
140
|
+
class SessionsController < ApplicationController
|
141
|
+
def create
|
142
|
+
# Authentication will be handled by Warden
|
143
|
+
|
144
|
+
# Custom Response
|
145
|
+
render json: { message: 'Authenticated successfully', user: current_user }
|
146
|
+
end
|
147
|
+
end
|
148
|
+
```
|
149
|
+
|
150
|
+
3. **Example Request using curl**:
|
151
|
+
|
152
|
+
```sh
|
153
|
+
curl -X POST http://localhost:3000/shfy/login -d '{
|
154
|
+
"email": "user@example.com",
|
155
|
+
"password": "password"
|
156
|
+
}' -H "Content-Type: application/json"
|
157
|
+
```
|
158
|
+
|
159
|
+
With this setup, you can authenticate users using email and password, generate a JWT, and handle the response logic in your sessions controller or any controller.
|
160
|
+
|
161
|
+
#### JSON Web Token (JWT)
|
162
|
+
|
163
|
+
Authentication using JSON Web Tokens (JWT) in this plugin ensures secure and stateless user sessions. This method leverages middleware and Warden strategies to handle authentication requests seamlessly. Below is a detailed overview of how this process works:
|
164
|
+
|
165
|
+
- **Middleware and Warden Strategy**:
|
166
|
+
Authentication is processed through middleware that intercepts requests containing a JWT in the `Authorization` header. If a valid JWT is present, the Warden strategy is used to authenticate the user.
|
167
|
+
|
168
|
+
- **Shieldify Middleware**:
|
169
|
+
The middleware intercepts requests to handle authentication based on the presence of the `Authorization` header.
|
170
|
+
|
171
|
+
- **Warden JWT Strategy**:
|
172
|
+
The Warden strategy extracts the JWT from the `Authorization` header, validates it, and authenticates the user if the token is valid.
|
173
|
+
|
174
|
+
- **Protected Request**:
|
175
|
+
You can protect specific actions in your controllers by using a `before_action` filter that ensures the user is authenticated.
|
176
|
+
|
177
|
+
```ruby
|
178
|
+
before_action :authenticate_user!
|
179
|
+
```
|
180
|
+
|
181
|
+
##### Usage Example
|
182
|
+
|
183
|
+
1. **Protect Controller Action**:
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
# app/controllers/protected_controller.rb
|
187
|
+
class ProtectedController < ApplicationController
|
188
|
+
before_action :authenticate_user!
|
189
|
+
|
190
|
+
def index
|
191
|
+
render json: { message: 'You have accessed a protected resource' }
|
192
|
+
end
|
193
|
+
end
|
194
|
+
```
|
195
|
+
|
196
|
+
2. **Example Request using curl**:
|
197
|
+
|
198
|
+
```sh
|
199
|
+
curl -X GET http://localhost:3000/protected_resource -H "Authorization: Bearer <your_jwt_token>"
|
200
|
+
```
|
201
|
+
|
202
|
+
With this setup, you can authenticate requests using JWTs, ensuring secure and stateless authentication in your Rails application.
|
203
|
+
|
204
|
+
Tu documentación se ve excelente. Aquí está con algunas pequeñas correcciones para mejorar la claridad y el formato:
|
205
|
+
|
206
|
+
## Controller Helpers
|
207
|
+
|
208
|
+
The `Shieldify::Controllers::Helpers` module, which is already included in `ActionController::API`, provides several helper methods to manage user authentication within your controllers. Below is a detailed explanation of each method and how to use them:
|
209
|
+
|
210
|
+
- **current_user**
|
211
|
+
|
212
|
+
This method returns the currently authenticated user based on the Warden strategy.
|
213
|
+
|
214
|
+
**Usage Example:**
|
215
|
+
```ruby
|
216
|
+
# app/controllers/user_controller.rb
|
217
|
+
class UserController < ApplicationController
|
218
|
+
def index
|
219
|
+
user = current_user
|
220
|
+
render json: { user: user }
|
221
|
+
end
|
222
|
+
end
|
223
|
+
```
|
224
|
+
|
225
|
+
- **user_signed_in?**
|
226
|
+
|
227
|
+
This method checks if a user is currently signed in. It returns `true` if a user is signed in, otherwise `false`.
|
228
|
+
|
229
|
+
**Usage Example:**
|
230
|
+
```ruby
|
231
|
+
# app/controllers/home_controller.rb
|
232
|
+
class HomeController < ApplicationController
|
233
|
+
def index
|
234
|
+
if user_signed_in?
|
235
|
+
render json: { message: 'User is signed in' }
|
236
|
+
else
|
237
|
+
render json: { message: 'User is not signed in' }
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
241
|
+
```
|
242
|
+
|
243
|
+
- **authenticate_user!**
|
244
|
+
|
245
|
+
This method ensures that a user is authenticated before accessing certain actions. If the user is not signed in, it responds with an unauthorized status.
|
246
|
+
|
247
|
+
**Usage Example:**
|
248
|
+
```ruby
|
249
|
+
# app/controllers/protected_controller.rb
|
250
|
+
class ProtectedController < ApplicationController
|
251
|
+
before_action :authenticate_user!
|
252
|
+
|
253
|
+
def index
|
254
|
+
render json: { message: 'You have accessed a protected resource' }
|
255
|
+
end
|
256
|
+
end
|
257
|
+
```
|
258
|
+
|
259
|
+
With these helper methods, you can easily manage user authentication and access control in your Rails application.
|
260
|
+
|
261
|
+
## Model Extensions
|
262
|
+
|
263
|
+
### Email Authenticatable
|
264
|
+
|
265
|
+
The `Shieldify::Models::EmailAuthenticatable` module provides email and password authentication functionality for users. It includes methods to authenticate users by their email and password, and uses `has_secure_password` for secure password handling.
|
266
|
+
|
267
|
+
- **Email and Password Authentication**:
|
268
|
+
- Adds methods to set and authenticate against a BCrypt password.
|
269
|
+
- Requires a `password_digest` attribute.
|
270
|
+
|
271
|
+
**Usage Example**:
|
272
|
+
```ruby
|
273
|
+
# app/models/user.rb
|
274
|
+
class User < ApplicationRecord
|
275
|
+
include Shieldify::Models::EmailAuthenticatable
|
276
|
+
# or
|
277
|
+
shieldify email_authenticatable: %i[]
|
278
|
+
end
|
279
|
+
```
|
280
|
+
|
281
|
+
**Authenticate by Email**:
|
282
|
+
```ruby
|
283
|
+
# Authenticate a user by email and password
|
284
|
+
user = User.authenticate_by_email(email: 'user@example.com', password: 'password')
|
285
|
+
if user.errors.any?
|
286
|
+
# Handle authentication failure
|
287
|
+
else
|
288
|
+
# Handle authentication success
|
289
|
+
end
|
290
|
+
```
|
291
|
+
|
292
|
+
#### Confirmable
|
293
|
+
|
294
|
+
The `Shieldify::Models::EmailAuthenticatable::Confirmable` module implements email confirmation using tokens. It generates confirmation tokens when registering or changing a user's email address, enabling confirmation through a secure process. It includes functionality to automatically send confirmation instructions and manage email confirmations.
|
295
|
+
|
296
|
+
- **Email Confirmation**:
|
297
|
+
- Generates confirmation tokens for email confirmation.
|
298
|
+
- Sends email confirmation instructions.
|
299
|
+
- Manages email confirmations and checks token expiration.
|
300
|
+
|
301
|
+
**Usage Example**:
|
302
|
+
```ruby
|
303
|
+
# app/models/user.rb
|
304
|
+
class User < ApplicationRecord
|
305
|
+
include Shieldify::Models::EmailAuthenticatable::Confirmable
|
306
|
+
# or
|
307
|
+
shieldify email_authenticatable: %i[confirmable]
|
308
|
+
end
|
309
|
+
```
|
310
|
+
|
311
|
+
**Confirm Email by Token**:
|
312
|
+
```ruby
|
313
|
+
# Confirm a user's email by token
|
314
|
+
user = User.confirm_email_by_token('confirmation_token')
|
315
|
+
if user.errors.any?
|
316
|
+
# Handle confirmation failure
|
317
|
+
else
|
318
|
+
# Handle confirmation success
|
319
|
+
end
|
320
|
+
```
|
321
|
+
|
322
|
+
#### Registerable
|
323
|
+
|
324
|
+
The `Shieldify::Models::EmailAuthenticatable::Registerable` module provides registration functionality for users, including email normalization, validation of email and password, and methods for registering a user and updating their email and password.
|
325
|
+
|
326
|
+
- **User Registration**:
|
327
|
+
- Normalizes email before validation.
|
328
|
+
- Validates email format, uniqueness, and presence.
|
329
|
+
- Validates password complexity and minimum length.
|
330
|
+
- Provides methods to register a new user, update password, and update email.
|
331
|
+
|
332
|
+
**Usage Example**:
|
333
|
+
```ruby
|
334
|
+
# app/models/user.rb
|
335
|
+
class User < ApplicationRecord
|
336
|
+
include Shieldify::Models::EmailAuthenticatable::Registerable
|
337
|
+
# or
|
338
|
+
shieldify email_authenticatable: %i[registerable]
|
339
|
+
end
|
340
|
+
```
|
341
|
+
|
342
|
+
**Register a New User**:
|
343
|
+
```ruby
|
344
|
+
# Register a new user
|
345
|
+
user = User.register(email: 'newuser@example.com', password: 'password', password_confirmation: 'password')
|
346
|
+
if user.errors.any?
|
347
|
+
# Handle registration failure
|
348
|
+
else
|
349
|
+
# Handle registration success
|
350
|
+
end
|
351
|
+
```
|
352
|
+
|
353
|
+
**Update Password**:
|
354
|
+
```ruby
|
355
|
+
# Update user's password
|
356
|
+
user.update_password(current_password: 'current_password', new_password: 'new_password', password_confirmation: 'new_password')
|
357
|
+
if user.errors.any?
|
358
|
+
# Handle password update failure
|
359
|
+
else
|
360
|
+
# Handle password update success
|
361
|
+
end
|
362
|
+
```
|
363
|
+
|
364
|
+
**Update Email**:
|
365
|
+
```ruby
|
366
|
+
# Update user's email
|
367
|
+
user.update_email(current_password: 'current_password', new_email: 'newemail@example.com')
|
368
|
+
if user.errors.any?
|
369
|
+
# Handle email update failure
|
370
|
+
else
|
371
|
+
# Handle email update success
|
372
|
+
end
|
373
|
+
```
|
374
|
+
|
375
|
+
With these modules, you can easily manage user authentication, email confirmation, and registration within your Rails application.
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
module Users
|
2
|
+
class AccessController < ActionController::Base
|
3
|
+
# GET /users/access/:token/unlock
|
4
|
+
def show
|
5
|
+
user = User.find_by(unlock_token: params[:token])
|
6
|
+
|
7
|
+
if user.present? && user.unlock_access
|
8
|
+
# Asume que `unlock_access` es un método en tu modelo User que realiza la lógica necesaria
|
9
|
+
# para desbloquear el acceso del usuario y limpiar el token de desbloqueo.
|
10
|
+
render json: { message: 'Tu cuenta ha sido desbloqueada exitosamente. Ahora puedes iniciar sesión.' }, status: :ok
|
11
|
+
else
|
12
|
+
render json: { error: 'El token proporcionado no es válido o ya ha sido utilizado.' }, status: :not_found
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Users
|
2
|
+
module Emails
|
3
|
+
class ResetPasswordsController < ActionController::Base
|
4
|
+
# POST /users/email/reset_password
|
5
|
+
def create
|
6
|
+
user = User.find_by(email: params[:email])
|
7
|
+
|
8
|
+
if user.present?
|
9
|
+
# Aquí se asume la existencia de un método que genera un token de restablecimiento
|
10
|
+
# de contraseña y envía un correo electrónico al usuario con instrucciones.
|
11
|
+
user.send_reset_email_password_instructions
|
12
|
+
render json: { message: 'Se ha enviado un correo con instrucciones para restablecer tu contraseña.' }, status: :ok
|
13
|
+
else
|
14
|
+
render json: { error: 'No se encontró un usuario con ese correo electrónico.' }, status: :not_found
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# PUT /users/email/:token/reset_password
|
19
|
+
def update
|
20
|
+
user = User.find_by(reset_email_password_token: params[:token])
|
21
|
+
|
22
|
+
if user.present? && user.reset_password(params[:password], params[:password_confirmation])
|
23
|
+
render json: { message: 'Tu contraseña ha sido restablecida exitosamente.' }, status: :ok
|
24
|
+
else
|
25
|
+
render json: { error: 'El token no es válido o las contraseñas no coinciden.' }, status: :unprocessable_entity
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Users
|
2
|
+
class EmailsController < ActionController::Base
|
3
|
+
def show
|
4
|
+
token = params[:token]
|
5
|
+
user = User.confirm_email_by_token(token)
|
6
|
+
|
7
|
+
if user.errors.blank?
|
8
|
+
render(
|
9
|
+
json: { message: I18n.t("shieldify.controllers.emails.confirmation.success_messages") },
|
10
|
+
status: :ok
|
11
|
+
)
|
12
|
+
else
|
13
|
+
render json: { errors: user.errors.full_messages }, status: :unprocessable_entity
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Shieldify
|
2
|
+
class InstallGenerator < Rails::Generators::Base
|
3
|
+
include Rails::Generators::Migration
|
4
|
+
|
5
|
+
source_root File.expand_path("templates", __dir__)
|
6
|
+
|
7
|
+
def copy_initializer
|
8
|
+
template 'initializer.rb', File.join("config", "initializers", "shieldify.rb")
|
9
|
+
end
|
10
|
+
|
11
|
+
def generate_migration
|
12
|
+
migration_template 'migration.rb', File.join("db", "migrate", "shieldify_create_users.rb"), migration_version: migration_version
|
13
|
+
end
|
14
|
+
|
15
|
+
def generate_model
|
16
|
+
template 'model.rb', File.join("app", "models", "user.rb")
|
17
|
+
end
|
18
|
+
|
19
|
+
def inject_method
|
20
|
+
inject_into_class File.join("app", "models", "user.rb"), :User, model_contents
|
21
|
+
end
|
22
|
+
|
23
|
+
def copy_mailer_layouts
|
24
|
+
directory File.join("mailer_layouts"), File.join("app", "views", "layouts", "shieldify")
|
25
|
+
end
|
26
|
+
|
27
|
+
def copy_mailer_views
|
28
|
+
directory File.join("mailer_views"), File.join("app", "views", "shieldify", "mailer")
|
29
|
+
end
|
30
|
+
|
31
|
+
def copy_locale_file
|
32
|
+
template "locales/en.shieldify.yml", File.join("config", "locales", "en.shieldify.yml")
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def model_contents
|
38
|
+
<<-CONTENT
|
39
|
+
shieldify email_authenticatable: %i[registerable confirmable]
|
40
|
+
CONTENT
|
41
|
+
end
|
42
|
+
|
43
|
+
def migration_version
|
44
|
+
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.next_migration_number(dirname)
|
48
|
+
next_migration_number = current_migration_number(dirname) + 1
|
49
|
+
ActiveRecord::Migration.next_migration_number(next_migration_number)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
Shieldify.setup do |conf|
|
2
|
+
## Mailer
|
3
|
+
|
4
|
+
# The parent mailer for internal mailers.
|
5
|
+
# The default value is "ActionMailer::Base", which sets the base class for all internal mailers.
|
6
|
+
# You can change this to another mailer class if your application requires a different base mailer.
|
7
|
+
# conf.parent_mailer = "ActionMailer::Base"
|
8
|
+
|
9
|
+
# The default email address used as the sender for outgoing emails.
|
10
|
+
# The default value is "shieldify@example.com", and it is necessary to change this to a valid email address for your application.
|
11
|
+
# conf.mailer_sender = "shieldify@example.com"
|
12
|
+
|
13
|
+
# The default email address used for the "Reply-To" field in outgoing emails.
|
14
|
+
# The default value is "shieldify@example.com", and it is necessary to change this to a valid email address for your application.
|
15
|
+
# conf.reply_to = "shieldify@example.com"
|
16
|
+
|
17
|
+
# Used to send notification to the original user email when their email is changed.
|
18
|
+
# The default value is true, and it is recommended to keep this enabled for security purposes.
|
19
|
+
# conf.send_email_changed_notification = false
|
20
|
+
|
21
|
+
# Used to enable sending notification to user when their password is changed.
|
22
|
+
# The default value is true, and it is recommended to keep this enabled for security purposes.
|
23
|
+
# conf.send_password_changed_notification = false
|
24
|
+
|
25
|
+
## Password
|
26
|
+
|
27
|
+
# Password complexity regex.
|
28
|
+
# Explanation of the regular expression:
|
29
|
+
# - (?=.*\d) ensures there is at least one digit
|
30
|
+
# - (?=.*[a-z]) ensures there is at least one lowercase letter
|
31
|
+
# - (?=.*[A-Z]) ensures there is at least one uppercase letter
|
32
|
+
# - (?=.*[@$#!%*?&]) ensures there is at least one of these special characters
|
33
|
+
# - \S{8,} ensures the password is at least 8 characters in length and contains no spaces
|
34
|
+
# The default value enforces the above rules, ensuring a strong password policy.
|
35
|
+
# It is possible to change this regex to suit your application's specific requirements.
|
36
|
+
# conf.password_complexity = /[expression-here]/
|
37
|
+
|
38
|
+
# JWT Related Configuration
|
39
|
+
|
40
|
+
# The secret key used to encode and decode JWT tokens.
|
41
|
+
# The default value is "whatever", but it is highly recommended to change this to a strong,
|
42
|
+
# unique secret for your application.
|
43
|
+
# conf.jwt_secret = Rails.application.credentials.jwt_secret
|
44
|
+
|
45
|
+
# The issuer claim for the JWT tokens.
|
46
|
+
# The default value is "Shieldify". You can change this to match your application's name or domain.
|
47
|
+
# conf.jwt_issuer = "Shieldify"
|
48
|
+
|
49
|
+
# The expiration time for the JWT tokens in hours.
|
50
|
+
# The default value is 24 hours. You can adjust this to set the desired token validity period.
|
51
|
+
# conf.jwt_exp = 24.hours.from_now.to_i
|
52
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
en:
|
2
|
+
shieldify:
|
3
|
+
controllers:
|
4
|
+
emails:
|
5
|
+
confirmation:
|
6
|
+
success_messages: "Email successfully confirmed"
|
7
|
+
models:
|
8
|
+
email_authenticatable:
|
9
|
+
registerable:
|
10
|
+
password:
|
11
|
+
errors:
|
12
|
+
invalid: "invalid"
|
13
|
+
password_complexity:
|
14
|
+
format: It must include at least one uppercase letter, one lowercase letter, one number, and one special character (@$!%*?&)
|
15
|
+
confirmable:
|
16
|
+
email_confirmation_token:
|
17
|
+
errors:
|
18
|
+
invalid: "invalid"
|
19
|
+
expired: "has expired"
|
20
|
+
unconfirmed_email:
|
21
|
+
errors:
|
22
|
+
not_found: "not found"
|
23
|
+
email_confirmation_token_generated_at:
|
24
|
+
mailer:
|
25
|
+
email_confirmation_instructions:
|
26
|
+
subject: "Email Confirmation Instructions"
|
27
|
+
title: "Email Confirmation Instructions"
|
28
|
+
greeting: "Hello %{email},"
|
29
|
+
thanks: "Please confirm your email by clicking on the following link:"
|
30
|
+
confirm_account: "Confirm email"
|
31
|
+
ignore: "If you did not request this confirmation, please ignore this email."
|
32
|
+
reset_password_instructions:
|
33
|
+
subject: "Password Reset Instructions"
|
34
|
+
title: "Password Reset Instructions"
|
35
|
+
greeting: "Hello %{email},"
|
36
|
+
instructions: "Someone has requested a link to change your password. You can do this through the link below:"
|
37
|
+
change_password: "Change my password"
|
38
|
+
ignore: "If you didn't request this, please ignore this email. Your password will not change."
|
39
|
+
link_expiration: "This link will expire in %{expiration_hours} hours."
|
40
|
+
unlock_instructions:
|
41
|
+
subject: "Account Unlock Instructions"
|
42
|
+
title: "Account Unlock Instructions"
|
43
|
+
greeting: "Hello %{email},"
|
44
|
+
instructions: "Your account has been locked due to an excessive number of failed login attempts. Please unlock your account by clicking on the following link:"
|
45
|
+
unlock_account: "Unlock Account"
|
46
|
+
ignore: "If you did not request this, please ignore this email. Your account will remain secure."
|
47
|
+
email_changed:
|
48
|
+
subject: "Email Updated"
|
49
|
+
title: "Email Updated"
|
50
|
+
greeting: "Hello %{email},"
|
51
|
+
message: "We received a request to change the email address associated with your account to %{unconfirmed_email}."
|
52
|
+
ignore: "If you did not make this change, please contact support immediately."
|
53
|
+
password_changed:
|
54
|
+
subject: "Password Updated"
|
55
|
+
title: "Password Updated"
|
56
|
+
greeting: "Hello %{email},"
|
57
|
+
message: "This is a notification that your password has been successfully changed."
|
58
|
+
advice: "If you did not make this change, please contact our support team immediately."
|