model_driven_api 2.3.1 → 2.3.6

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: 3acc4504dc5bf9c20c0057659e911095eaad75d7b63d03b5632d8e02b847525f
4
- data.tar.gz: 3227192e26753f9160424739c3182bb08f503ccb2650e23291fea8894f15566d
3
+ metadata.gz: 4f077fe5622ad57731dec49be396e6a30cd59d643a56c959f7ada5d7b1515302
4
+ data.tar.gz: dd18358e8de2ed6813383ee84910b35557207c6de2f31b7244c8a222e382c8f3
5
5
  SHA512:
6
- metadata.gz: c059a8c693d2605dab964bb2aa0e159975afb172aba8885bdd0bd0fc4208fa2535ecb7fb857e694de1a74cf7f1169c0a8d617098d926d9b28a07aced9d9902d0
7
- data.tar.gz: 677f5956846a1b6a9304491acde68d2c1988d5ddd81bd8d830a67224b11dad63619c9133e2bf3eef1597c8b0798bbe9036a1994344b183ca61979e00a577769e
6
+ metadata.gz: f6a0cac8c48611a3e78f0f6fd9e6a1ce6dcb99d82d54170803cf42f638cddd7fb6a5ee3bfc0edf6c3900a0d7627e4c6936f011c1e91fb6522599a844677577d4
7
+ data.tar.gz: 046ca81694a0051a68368b52a7808ffc961c28a4d336dc5e92fcebfe4e4ec7370bd54b8dc4e0c40a0f2f9c179ae0f8ea2adfae509c0394086dc357e6c97a1213
data/README.md CHANGED
@@ -422,7 +422,7 @@ Once loaded the tests inside the insomnia application, please right click on the
422
422
 
423
423
  ## TODO
424
424
 
425
- * Add a Trust management for API consumers, to have some low level interactions happen between API client and server done without the need for giving a USERNAME and a PASSWORD.
425
+ * Document the new feature (from version 2.3.3) to add Authentication methods which override the JWT described above. Useful for Webhooks and machine2machine trusted dialogues.
426
426
 
427
427
  ## References
428
428
  Thanks to all these people for ideas:
@@ -1,7 +1,7 @@
1
1
  class AuthenticateUser
2
2
  class AccessDenied < StandardError
3
- def message
4
- "AuthenticationError"
3
+ def message more = "AuthenticationError"
4
+ more
5
5
  end
6
6
  end
7
7
  prepend SimpleCommand
@@ -21,7 +21,7 @@ class Api::V2::ApplicationController < ActionController::API
21
21
 
22
22
  # Normal Index Action with Ransack querying
23
23
  @q = (@model.column_names.include?("user_id") ? @model.where(user_id: current_user.id) : @model).ransack(@query.presence|| params[:q])
24
- @records_all = @q.result(distinct: true)
24
+ @records_all = @q.result # (distinct: true) Removing, but I'm not sure, with it I cannot sort in postgres for associated records (throws an exception on misuse of sort with distinct)
25
25
  page = (@page.presence || params[:page])
26
26
  per = (@per.presence || params[:per])
27
27
  pages_info = (@pages_info.presence || params[:pages_info])
@@ -107,15 +107,33 @@ class Api::V2::ApplicationController < ActionController::API
107
107
  # call an unwanted method in the AR Model.
108
108
  resource = "custom_action_#{params[:do]}"
109
109
  raise NoMethodError unless @model.respond_to?(resource)
110
- return true, MultiJson.dump(params[:id].blank? ? @model.send(resource, params) : @model.send(resource, params[:id].to_i, params))
110
+ # return true, MultiJson.dump(params[:id].blank? ? @model.send(resource, params) : @model.send(resource, params[:id].to_i, params))
111
+ return true, MultiJson.dump(@model.send(resource, params))
111
112
  end
112
113
  # if it's here there is no custom action in the request querystring
113
114
  return false
114
115
  end
116
+
117
+ def class_exists?(class_name)
118
+ klass = Module.const_get(class_name)
119
+ return klass.is_a?(Class)
120
+ rescue NameError
121
+ return false
122
+ end
115
123
 
116
124
  def authenticate_request
117
- @current_user = AuthorizeApiRequest.call(request.headers).result
118
- return unauthenticated! unless @current_user
125
+ # puts request.headers.inspect
126
+ @current_user = nil
127
+ # puts "Are there wbehooks headers to check for? #{Settings.ns(:security).allowed_authorization_headers}"
128
+ Settings.ns(:security).allowed_authorization_headers.split(",").each do |header|
129
+ # puts "Found header #{header}: #{request.headers[header.underscore.dasherize]}"
130
+ check_authorization("Authorize#{header}".constantize.call(request.headers, request.raw_post)) if request.headers[header.underscore.dasherize]
131
+ end
132
+
133
+ # This is the default one, if the header doesn't have a valid form for one of the other Auth methods, then use this Auth Class
134
+ check_authorization AuthorizeApiRequest.call(request.headers) unless @current_user
135
+ return unauthenticated!(OpenStruct.new({message: @auth_errors})) unless @current_user
136
+
119
137
  current_user = @current_user
120
138
  params[:current_user_id] = @current_user.id
121
139
  # Now every time the user fires off a successful GET request,
@@ -147,6 +165,14 @@ class Api::V2::ApplicationController < ActionController::API
147
165
  return not_found! if (!@model.new.is_a? ActiveRecord::Base rescue false)
148
166
  end
149
167
 
168
+ def check_authorization cmd
169
+ if cmd.success?
170
+ @current_user = cmd.result
171
+ else
172
+ @auth_errors = cmd.errors
173
+ end
174
+ end
175
+
150
176
  # Nullifying strong params for API
151
177
  def params
152
178
  request.parameters
@@ -2,13 +2,15 @@ module ApiExceptionManagement
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  included do
5
- rescue_from NoMethodError, with: :not_found!
6
- rescue_from CanCan::AccessDenied, with: :unauthorized!
7
- rescue_from AuthenticateUser::AccessDenied, with: :unauthenticated!
8
- rescue_from ActionController::RoutingError, with: :not_found!
9
- rescue_from ActiveModel::ForbiddenAttributesError, with: :fivehundred!
10
- rescue_from ActiveRecord::RecordInvalid, with: :invalid!
11
- rescue_from ActiveRecord::RecordNotFound, with: :not_found!
5
+ if Rails.env.production?
6
+ rescue_from NoMethodError, with: :not_found!
7
+ rescue_from CanCan::AccessDenied, with: :unauthorized!
8
+ rescue_from AuthenticateUser::AccessDenied, with: :unauthenticated!
9
+ rescue_from ActionController::RoutingError, with: :not_found!
10
+ rescue_from ActiveModel::ForbiddenAttributesError, with: :fivehundred!
11
+ rescue_from ActiveRecord::RecordInvalid, with: :invalid!
12
+ rescue_from ActiveRecord::RecordNotFound, with: :not_found!
13
+ end
12
14
 
13
15
  def unauthenticated! exception = AuthenticateUser::AccessDenied.new
14
16
  response.headers['WWW-Authenticate'] = "Token realm=Application"
@@ -33,7 +35,7 @@ module ApiExceptionManagement
33
35
 
34
36
  def api_error(status: 500, errors: [])
35
37
  # puts errors.full_messages if !Rails.env.production? && errors.respond_to?(:full_messages)
36
- head status && return if errors.empty?
38
+ head status && return if errors.blank?
37
39
 
38
40
  # For retrocompatibility, I try to send back only strings, as errors
39
41
  errors_response = if errors.respond_to?(:full_messages)
@@ -1,3 +1,3 @@
1
1
  module ModelDrivenApi
2
- VERSION = "#{`git describe --tags $(git rev-list --tags --max-count=1)`}"
2
+ VERSION = "#{`git describe --tags $(git rev-list --tags --max-count=1)`.chomp}"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model_driven_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.1
4
+ version: 2.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriele Tassoni
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-03 00:00:00.000000000 Z
11
+ date: 2021-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thecore_backend_commons