model_driven_api 2.3.15 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/commands/authenticate_user.rb +11 -10
- data/app/controllers/api/v2/authentication_controller.rb +3 -2
- data/app/controllers/api/v2/info_controller.rb +1 -1
- data/app/models/used_token.rb +0 -4
- data/config/routes.rb +1 -0
- data/db/migrate/20210528111450_rename_valid_to_is_valid_in_used_token.rb +7 -0
- data/lib/json_web_token.rb +3 -2
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83d29c7872f8a719a67efea2d255348bf1d45b59cfed6729a8eba63cd58a3679
|
4
|
+
data.tar.gz: 0d351060548decf558e0d7332ca4c580729070d80ddfd94c7656da1cb45e815f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd8488a506791ee0394b4f148c8df23a47728603cc9e56ed2de894ff74cd8ed3bb701e3077c9ba2c512cb83478373448c7d34552eefd5995f64c1a5fcd43ad40
|
7
|
+
data.tar.gz: 7afcfeb678f4c9d7609468e8e8fad1c5b59a54c41164c2598e201409cd49024be8d6b5f2dea08d1e58778d1905f410f1a826cea278b9b3e251ab850d853690ca
|
@@ -7,21 +7,23 @@ class AuthenticateUser
|
|
7
7
|
prepend SimpleCommand
|
8
8
|
|
9
9
|
def initialize(*args)
|
10
|
-
|
11
|
-
|
12
|
-
@
|
13
|
-
|
14
|
-
|
10
|
+
first_arg = args.first
|
11
|
+
if !first_arg[:email].blank? && !first_arg[:password].blank?
|
12
|
+
@email = first_arg[:email]
|
13
|
+
@password = first_arg[:password]
|
14
|
+
elsif !first_arg[:access_token].blank?
|
15
|
+
@access_token = first_arg[:access_token]
|
15
16
|
end
|
16
17
|
end
|
17
18
|
|
18
19
|
def call
|
19
|
-
|
20
|
+
current_u = api_user
|
21
|
+
if !current_u.blank? && result = JsonWebToken.encode(user_id: current_u.id)
|
20
22
|
# The token is created and the api_user exists => Invalidating all the previous tokens
|
21
23
|
# Since this is a new login and I don't care from where it comes, new logins always
|
22
24
|
# Invalidate older tokens
|
23
|
-
UsedToken.where(user_id: api_user.id).update(
|
24
|
-
return result
|
25
|
+
UsedToken.where(user_id: api_user.id).update(is_valid: false) if ENV["ALLOW_MULTISESSIONS"] == "false"
|
26
|
+
return {jwt: result, user: current_u}
|
25
27
|
end
|
26
28
|
nil
|
27
29
|
end
|
@@ -33,8 +35,7 @@ class AuthenticateUser
|
|
33
35
|
def api_user
|
34
36
|
if !email.blank? && !password.blank?
|
35
37
|
user = User.find_by(email: email)
|
36
|
-
|
37
|
-
# Verify the password. You can create a blank method for now.
|
38
|
+
# Verify the password.
|
38
39
|
raise AccessDenied if user.blank? && user.authenticate(password).blank?
|
39
40
|
elsif !access_token.blank?
|
40
41
|
user = User.find_by(access_token: access_token)
|
@@ -5,8 +5,9 @@ class Api::V2::AuthenticationController < ActionController::API
|
|
5
5
|
command = !params[:atoken].blank? && User.column_names.include?("access_token") ? AuthenticateUser.call(access_token: params[:atoken]) : AuthenticateUser.call(email: params[:auth][:email], password: params[:auth][:password])
|
6
6
|
|
7
7
|
if command.success?
|
8
|
-
response.headers['Token'] = command.result
|
9
|
-
head :ok
|
8
|
+
response.headers['Token'] = command.result[:jwt]
|
9
|
+
# head :ok
|
10
|
+
render json: command.result[:user].to_json(User.json_attrs), status: 200
|
10
11
|
end
|
11
12
|
end
|
12
13
|
end
|
data/app/models/used_token.rb
CHANGED
data/config/routes.rb
CHANGED
data/lib/json_web_token.rb
CHANGED
@@ -3,13 +3,14 @@ class JsonWebToken
|
|
3
3
|
def encode(payload, expiry = 15.minutes.from_now.to_i)
|
4
4
|
result = ::JWT.encode(payload.merge(exp: expiry), ::Rails.application.credentials.dig(:secret_key_base).presence||ENV["SECRET_KEY_BASE"])
|
5
5
|
# Store the created token into the DB for later checks if is invalid
|
6
|
-
|
6
|
+
# In a public environment management, without login, it has no interest, so I don't pollute the DB
|
7
|
+
UsedToken.find_or_create_by(token: result, user_id: payload[:user_id]) if ENV["ALLOW_MULTISESSIONS"] == "false"
|
7
8
|
result
|
8
9
|
end
|
9
10
|
|
10
11
|
def decode(token)
|
11
12
|
# Check if the passed token is present and valid into the UsedToken
|
12
|
-
raise "Token is invalidated by new login" unless UsedToken.exists?(token: token,
|
13
|
+
raise "Token is invalidated by new login" unless UsedToken.exists?(token: token, is_valid: true) if ENV["ALLOW_MULTISESSIONS"] == "false"
|
13
14
|
body = ::JWT.decode(token, ::Rails.application.credentials.dig(:secret_key_base).presence||ENV["SECRET_KEY_BASE"])[0]
|
14
15
|
::HashWithIndifferentAccess.new body
|
15
16
|
rescue
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: model_driven_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gabriele Tassoni
|
@@ -147,6 +147,7 @@ files:
|
|
147
147
|
- config/initializers/wrap_parameters.rb
|
148
148
|
- config/routes.rb
|
149
149
|
- db/migrate/20210519145438_create_used_tokens.rb
|
150
|
+
- db/migrate/20210528111450_rename_valid_to_is_valid_in_used_token.rb
|
150
151
|
- lib/concerns/api_exception_management.rb
|
151
152
|
- lib/concerns/model_driven_api_role.rb
|
152
153
|
- lib/concerns/model_driven_api_user.rb
|