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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3c73a32dcfadaf698e4d904a110a4ab140a235699d6d71b1f064127624dc4832
4
- data.tar.gz: f99c45c100d53fc494bdfef2f886dbb1385951caebfbf5754d6a496f019f1fe3
3
+ metadata.gz: 83d29c7872f8a719a67efea2d255348bf1d45b59cfed6729a8eba63cd58a3679
4
+ data.tar.gz: 0d351060548decf558e0d7332ca4c580729070d80ddfd94c7656da1cb45e815f
5
5
  SHA512:
6
- metadata.gz: 1642e0c66e19c6ea3c8a383d3000329d5bba051803c026c40767e05ebadce23bff75e0983fc4a720a3681b28566d41d3d3e0d2f61f2a23f602b89ada4b078681
7
- data.tar.gz: 0b31e00c37e1a7511c35b6add9c91428791f8e0c13976f83044d5e52dcb43ef0a70071a04828fdd472e5d783239f7eaeb63669a61b6d3258026e83b47057a050
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
- if !args.email.blank? && !args.password.blank?
11
- @email = args.email
12
- @password = args.password
13
- elsif !args.access_token.blank?
14
- @access_token = args.access_token
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
- if !api_user.blank? && result = JsonWebToken.encode(user_id: api_user.id)
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(valid: false)
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
@@ -17,7 +17,7 @@ class Api::V2::InfoController < Api::V2::ApplicationController
17
17
 
18
18
 
19
19
  # api :GET, '/api/v2/info/heartbeat'
20
- # Just keeps the session alive
20
+ # Just keeps the session alive by returning a new token
21
21
  def heartbeat
22
22
  head :ok
23
23
  end
@@ -1,7 +1,3 @@
1
1
  class UsedToken < ApplicationRecord
2
2
  belongs_to :user, inverse_of: :used_tokens
3
-
4
- rails_admin do
5
- visible false
6
- end
7
3
  end
data/config/routes.rb CHANGED
@@ -12,6 +12,7 @@ Rails.application.routes.draw do
12
12
  get :translations
13
13
  get :schema
14
14
  get :dsl
15
+ get :heartbeat
15
16
  end
16
17
 
17
18
  post "authenticate" => "authentication#authenticate"
@@ -0,0 +1,7 @@
1
+ class RenameValidToIsValidInUsedToken < ActiveRecord::Migration[6.0]
2
+ def change
3
+ change_table :used_tokens do |t|
4
+ t.rename :valid, :is_valid
5
+ end
6
+ end
7
+ end
@@ -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
- UsedToken.create(token: result, user_id: payload[:user_id])
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, valid: true)
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.3.15
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