rails_auth_generator 0.2.1 → 1.0.0
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/README.md +1 -1
- data/lib/generators/auth/auth_generator.rb +37 -65
- data/lib/generators/auth/templates/concerns/authenticatable.rb +77 -0
- data/lib/generators/auth/templates/{auth_controller.rb → controllers/auth_controller.rb} +1 -1
- data/lib/generators/auth/templates/initializers/rails_auth_generator.rb +6 -0
- data/lib/generators/auth/templates/serializers/user_serializer.rb +3 -0
- data/lib/rails_auth_generator/configuration.rb +15 -0
- data/lib/rails_auth_generator/version.rb +1 -1
- data/lib/rails_auth_generator.rb +10 -0
- metadata +52 -7
- data/lib/generators/auth/templates/user_serializer.rb +0 -3
- /data/lib/generators/auth/templates/{password_resets_controller.rb → controllers/password_resets_controller.rb} +0 -0
- /data/lib/generators/auth/templates/{users_controller.rb → controllers/users_controller.rb} +0 -0
- /data/lib/generators/auth/templates/{refresh_token.rb → models/refresh_token.rb} +0 -0
- /data/lib/generators/auth/templates/{user.rb → models/user.rb} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bf69a30ca1854528205157aa7d45cf0138f78b50dc87b86e3db9a803e1368443
|
4
|
+
data.tar.gz: de809c1c82d18fae7af04964caf5d07c26db2178cd4e4cd40407a7debaf78d7f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0203aa88a66a391c60837ecd69dee7a69b813b407e840256cdbd891e10b3f29a354450811dcbeaca3f50588e48b369aba92b41c96cf781ce130aae1630a35717
|
7
|
+
data.tar.gz: c3fbeee8f0b7f1ba781d14872b43e33571826866c868a51df2c5e316ac2471a6b2e564555bc854be9f0691076f5e795125e3f9457541449a87bb63941cd2c086
|
data/README.md
CHANGED
@@ -13,14 +13,14 @@ def modify_gemfile
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
def modify_application_rb
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
16
|
+
def modify_application_rb
|
17
|
+
insert_into_file "config/application.rb", after: "config.api_only = true\n" do
|
18
|
+
<<~RUBY
|
19
|
+
config.middleware.use ActionDispatch::Cookies
|
20
|
+
RUBY
|
21
|
+
end
|
22
22
|
|
23
|
-
end
|
23
|
+
end
|
24
24
|
|
25
25
|
def add_routes
|
26
26
|
route <<~RUBY
|
@@ -41,77 +41,49 @@ end
|
|
41
41
|
end
|
42
42
|
|
43
43
|
def create_auth_files
|
44
|
-
template "auth_controller.rb", "app/controllers/auth_controller.rb"
|
45
|
-
template "user_serializer.rb", "app/serializers/user_serializer.rb"
|
46
|
-
template "users_controller.rb", "app/controllers/users_controller.rb"
|
47
|
-
template "password_resets_controller.rb", "app/controllers/password_resets_controller.rb"
|
48
|
-
template "user.rb", "app/models/user.rb"
|
49
|
-
template "refresh_token.rb", "app/models/refresh_token.rb"
|
44
|
+
template "controllers/auth_controller.rb", "app/controllers/auth_controller.rb"
|
45
|
+
template "serializers/user_serializer.rb", "app/serializers/user_serializer.rb"
|
46
|
+
template "controllers/users_controller.rb", "app/controllers/users_controller.rb"
|
47
|
+
template "controllers/password_resets_controller.rb", "app/controllers/password_resets_controller.rb"
|
48
|
+
template "models/user.rb", "app/models/user.rb"
|
49
|
+
template "models/refresh_token.rb", "app/models/refresh_token.rb"
|
50
50
|
template "mailers/user_mailer.rb", "app/mailers/user_mailer.rb"
|
51
51
|
template "mailers/application_mailer.rb", "app/mailers/application_mailer.rb"
|
52
|
+
template "concerns/authenticatable.rb", "app/controllers/concerns/authenticatable.rb"
|
53
|
+
template "initializers/rails_auth_generator.rb", "config/initializers/rails_auth_generator.rb"
|
52
54
|
end
|
53
55
|
|
54
56
|
def modify_application_controller
|
55
57
|
inject_into_class "app/controllers/application_controller.rb", "ApplicationController" do
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
SECRET_KEY = Rails.application.credentials.dig(:jwt, :secret)
|
60
|
-
|
61
|
-
|
62
|
-
def encode_token(payload, exp = 15.minutes.from_now)
|
63
|
-
# Add admin status to the payload if user is admin
|
64
|
-
payload[:admin] = @user.admin? if @user.is_a?(User)
|
65
|
-
payload[:exp] = exp.to_i
|
66
|
-
JWT.encode(payload, SECRET_KEY)
|
67
|
-
end
|
58
|
+
" include Authenticatable\n"
|
59
|
+
end
|
60
|
+
end
|
68
61
|
|
69
|
-
def decoded_token
|
70
|
-
header = request.headers['Authorization']
|
71
|
-
if header
|
72
|
-
token = header.split(" ")[1]
|
73
|
-
begin
|
74
|
-
JWT.decode(token, SECRET_KEY, true, algorithm: 'HS256')
|
75
|
-
rescue JWT::DecodeError
|
76
|
-
nil
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
62
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
end
|
63
|
+
def self.next_migration_number(dirname)
|
64
|
+
@prev_migration_nr ||= Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
|
65
|
+
@prev_migration_nr += 1
|
66
|
+
@prev_migration_nr.to_s
|
67
|
+
end
|
87
68
|
|
88
|
-
|
89
|
-
|
90
|
-
|
69
|
+
def copy_migration
|
70
|
+
migration_template "migrations/create_user.rb", "db/migrate/create_users.rb"
|
71
|
+
migration_template "migrations/create_refresh_token.rb", "db/migrate/create_refresh_tokens.rb"
|
72
|
+
end
|
91
73
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
74
|
+
def enable_cors
|
75
|
+
insert_into_file "config/application.rb"do
|
76
|
+
<<~RUBY
|
77
|
+
Rails.application.config.middleware.insert_before 0, Rack::Cors do
|
78
|
+
allow do
|
79
|
+
origins "example.com"
|
97
80
|
|
98
|
-
|
99
|
-
|
100
|
-
|
81
|
+
resource "*",
|
82
|
+
headers: :any,
|
83
|
+
methods: [:get, :post, :put, :patch, :delete, :options, :head]
|
84
|
+
end
|
101
85
|
end
|
102
|
-
end
|
103
86
|
RUBY
|
104
87
|
end
|
105
88
|
end
|
106
|
-
|
107
|
-
def self.next_migration_number(dirname)
|
108
|
-
@prev_migration_nr ||= Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
|
109
|
-
@prev_migration_nr += 1
|
110
|
-
@prev_migration_nr.to_s
|
111
|
-
end
|
112
|
-
|
113
|
-
def copy_migration
|
114
|
-
migration_template "migrations/create_user.rb", "db/migrate/create_users.rb"
|
115
|
-
migration_template "migrations/create_refresh_token.rb", "db/migrate/create_refresh_tokens.rb"
|
116
|
-
end
|
117
89
|
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Authenticatable
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
include ActionController::Cookies
|
8
|
+
before_action :authorized
|
9
|
+
|
10
|
+
SECRET_KEY = RailsAuthGenerator.configuration.jwt_secret
|
11
|
+
|
12
|
+
def encode_token(payload, exp = RailsAuthGenerator.configuration.access_token_expiry.from_now)
|
13
|
+
|
14
|
+
|
15
|
+
payload[:exp] = exp.to_i
|
16
|
+
payload[:admin] = @user.admin? if @user.is_a?(User) && RailsAuthGenerator.configuration.enable_roles
|
17
|
+
|
18
|
+
token = JWT.encode(payload, SECRET_KEY)
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
def decoded_token
|
23
|
+
header = request.headers['Authorization']
|
24
|
+
return nil unless header
|
25
|
+
|
26
|
+
token = header.split(" ")[1]
|
27
|
+
puts "🔍 DECODE_TOKEN DEBUG: Token: #{token}"
|
28
|
+
|
29
|
+
begin
|
30
|
+
decoded = JWT.decode(token, SECRET_KEY, true, { algorithm: 'HS256', verify_expiration: true })
|
31
|
+
puts " Token valid, expires at: #{Time.at(decoded[0]['exp'])}" if decoded[0]['exp']
|
32
|
+
decoded
|
33
|
+
rescue JWT::ExpiredSignature => e
|
34
|
+
puts " ❌ Token expired: #{e.message}"
|
35
|
+
@token_expired = true
|
36
|
+
nil
|
37
|
+
rescue JWT::DecodeError => e
|
38
|
+
puts " ❌ Token decode error: #{e.message}"
|
39
|
+
nil
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def current_user
|
44
|
+
return @current_user if defined?(@current_user)
|
45
|
+
|
46
|
+
if decoded_token
|
47
|
+
user_id = decoded_token[0]['user_id']
|
48
|
+
@current_user = User.find_by(id: user_id)
|
49
|
+
else
|
50
|
+
@current_user = nil
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def current_admin
|
55
|
+
current_user && (current_user.admin? || (decoded_token && decoded_token[0]['admin']))
|
56
|
+
end
|
57
|
+
|
58
|
+
def authorized
|
59
|
+
# Check if token is expired first
|
60
|
+
if @token_expired
|
61
|
+
render json: { error: 'Token has expired' }, status: :unauthorized
|
62
|
+
return false
|
63
|
+
end
|
64
|
+
|
65
|
+
unless current_user
|
66
|
+
render json: { message: 'Please log in' }, status: :unauthorized
|
67
|
+
return false
|
68
|
+
end
|
69
|
+
|
70
|
+
true
|
71
|
+
end
|
72
|
+
|
73
|
+
def admin_authorized
|
74
|
+
authorized && current_admin
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -36,7 +36,7 @@ class AuthController < ApplicationController
|
|
36
36
|
end
|
37
37
|
|
38
38
|
# Issue new access + refresh token
|
39
|
-
new_access_token = encode_token({ user_id: rt.user_id }
|
39
|
+
new_access_token = encode_token({ user_id: rt.user_id })
|
40
40
|
new_refresh_token = rt.user.generate_refresh_token
|
41
41
|
|
42
42
|
# revoke the old token
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module RailsAuthGenerator
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :jwt_secret,
|
4
|
+
:access_token_expiry,
|
5
|
+
:refresh_token_expiry,
|
6
|
+
:enable_roles
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@jwt_secret = nil
|
10
|
+
@access_token_expiry = 15.minutes
|
11
|
+
@refresh_token_expiry = 30.days
|
12
|
+
@enable_roles = false
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/rails_auth_generator.rb
CHANGED
@@ -1,5 +1,15 @@
|
|
1
1
|
require "rails_auth_generator/version"
|
2
|
+
require "rails_auth_generator/configuration"
|
2
3
|
|
3
4
|
module RailsAuthGenerator
|
4
5
|
class Error < StandardError; end
|
6
|
+
|
7
|
+
class << self
|
8
|
+
attr_accessor :configuration
|
9
|
+
|
10
|
+
def configure
|
11
|
+
self.configuration ||= Configuration.new
|
12
|
+
yield(configuration)
|
13
|
+
end
|
14
|
+
end
|
5
15
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_auth_generator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zeyad Hassan
|
@@ -29,6 +29,48 @@ dependencies:
|
|
29
29
|
- - "<"
|
30
30
|
- !ruby/object:Gem::Version
|
31
31
|
version: '9.0'
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: bcrypt
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - "~>"
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '3.1'
|
39
|
+
type: :runtime
|
40
|
+
prerelease: false
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - "~>"
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '3.1'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: devise
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - "~>"
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '4.9'
|
53
|
+
type: :runtime
|
54
|
+
prerelease: false
|
55
|
+
version_requirements: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '4.9'
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: jwt
|
62
|
+
requirement: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - "~>"
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '2.5'
|
67
|
+
type: :runtime
|
68
|
+
prerelease: false
|
69
|
+
version_requirements: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - "~>"
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '2.5'
|
32
74
|
email:
|
33
75
|
- " studying.zezo@gmail.com"
|
34
76
|
executables: []
|
@@ -46,17 +88,20 @@ files:
|
|
46
88
|
- bin/setup
|
47
89
|
- lib/generators/auth/USAGE
|
48
90
|
- lib/generators/auth/auth_generator.rb
|
49
|
-
- lib/generators/auth/templates/
|
91
|
+
- lib/generators/auth/templates/concerns/authenticatable.rb
|
92
|
+
- lib/generators/auth/templates/controllers/auth_controller.rb
|
93
|
+
- lib/generators/auth/templates/controllers/password_resets_controller.rb
|
94
|
+
- lib/generators/auth/templates/controllers/users_controller.rb
|
95
|
+
- lib/generators/auth/templates/initializers/rails_auth_generator.rb
|
50
96
|
- lib/generators/auth/templates/mailers/application_mailer.rb
|
51
97
|
- lib/generators/auth/templates/mailers/user_mailer.rb
|
52
98
|
- lib/generators/auth/templates/migrations/create_refresh_token.rb
|
53
99
|
- lib/generators/auth/templates/migrations/create_user.rb
|
54
|
-
- lib/generators/auth/templates/
|
55
|
-
- lib/generators/auth/templates/
|
56
|
-
- lib/generators/auth/templates/
|
57
|
-
- lib/generators/auth/templates/user_serializer.rb
|
58
|
-
- lib/generators/auth/templates/users_controller.rb
|
100
|
+
- lib/generators/auth/templates/models/refresh_token.rb
|
101
|
+
- lib/generators/auth/templates/models/user.rb
|
102
|
+
- lib/generators/auth/templates/serializers/user_serializer.rb
|
59
103
|
- lib/rails_auth_generator.rb
|
104
|
+
- lib/rails_auth_generator/configuration.rb
|
60
105
|
- lib/rails_auth_generator/version.rb
|
61
106
|
homepage: https://github.com/Zeyad-Hassan-1/authJWT.git
|
62
107
|
licenses:
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|