apicasso 0.5.2 → 0.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7c1f920e9964f2f638e348b6acb79ee6fd0f922a06d0cc43282696be53325ba9
4
- data.tar.gz: 83e3b5a19723736f4c72e0263159aea8a73ccc5b620ad63c6b0dc2f3adea4734
3
+ metadata.gz: '008a3e92922e32986a11c43d51478f4dd3eb8e44e287abf9027952c6415b00d3'
4
+ data.tar.gz: 146ae0b052be05e8f9a9dbf5c32563d39469e6887c1343d4c4e8ac70206c0708
5
5
  SHA512:
6
- metadata.gz: 2465d5459eae0c3203903659f9e7a01b55086caffc57c245058518c8510e3689cc3a67b1d1f954125b2915121661f01ca171aa45292875a4ac74053ddbb9543b
7
- data.tar.gz: 227061cbc6efc8976acc17bd2d34dd5989aeaab549535c30ccb83835b6e87ff8a76ca1d7a7f17357df9e7d621db51b0f4c22ca3453d370066e842cab18d7f3f2
6
+ metadata.gz: 93ae0a99e84f211f429d6daa80ba0f8690fb7903c155ad6e769f8cef18e8ee2ae8b52783460af857e464d750822a9d2876d23d75fb88323890a817597a950e08
7
+ data.tar.gz: 8ff1d500a8daea84d2b6137cc8a7cefcbf5b71097d9263f8c7a1588ac7c1d7f3efbd1a48b43b9f7156611b9510c8c3f615d1c9af4801c492443401c891e6ea8a
@@ -6,9 +6,8 @@ module Apicasso
6
6
  # application needs to create custom actions.
7
7
  class ApplicationController < ActionController::API
8
8
  include ActionController::HttpAuthentication::Token::ControllerMethods
9
- prepend_before_action :restrict_access, unless: -> { preflight? }
9
+ prepend_before_action :restrict_access
10
10
  prepend_before_action :klasses_allowed
11
- before_action :set_access_control_headers
12
11
  before_action :set_root_resource
13
12
  before_action :bad_request?
14
13
  after_action :register_api_request
@@ -195,30 +194,5 @@ module Apicasso
195
194
  def bad_request?
196
195
  raise ActionController::BadRequest.new('Bad hacker, stop be bully or I will tell to your mom!') unless sql_injection(resource)
197
196
  end
198
-
199
- # @TODO
200
- # Remove this in favor of a more controllable aproach of CORS
201
- def set_access_control_headers
202
- response.headers['Access-Control-Allow-Origin'] = allow_origin
203
- response.headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, PATCH, DELETE, OPTIONS'
204
- response.headers['Access-Control-Allow-Credentials'] = 'true'
205
- response.headers['Access-Control-Allow-Headers'] = 'Origin, Content-Type, Accept, Authorization, Token, Auth-Token, Email, X-User-Token, X-User-Email'
206
- response.headers['Access-Control-Max-Age'] = '1728000'
207
- end
208
-
209
- # A method to allow origin customizing through method overriding
210
- def allow_origin
211
- if request.headers['Referer'].present?
212
- request.protocol + URI(request.headers['Referer']).host
213
- else
214
- request.headers['Origin'] || '*'
215
- end.gsub(/\/$/, '')
216
- end
217
-
218
- # Checks if current request is a CORS preflight check
219
- def preflight?
220
- request.request_method == 'OPTIONS' &&
221
- !request.headers['Authorization'].present?
222
- end
223
197
  end
224
198
  end
@@ -16,7 +16,6 @@ module Apicasso
16
16
  # Example:
17
17
  # GET /sites?sort=+name,-updated_at&q[domain_eq]=domain.com&page=42&per_page=42
18
18
  def index
19
- set_access_control_headers
20
19
  render json: index_json
21
20
  end
22
21
 
@@ -24,7 +23,6 @@ module Apicasso
24
23
  # Common behavior for showing a record, with an addition of
25
24
  # relation/methods including on response
26
25
  def show
27
- set_access_control_headers
28
26
  render json: show_json
29
27
  end
30
28
 
@@ -75,7 +73,7 @@ module Apicasso
75
73
  # Will return a JSON with the schema of the current resource, using
76
74
  # attribute names as keys and attirbute types as values.
77
75
  def schema
78
- render json: resource_schema.to_json unless preflight?
76
+ render json: resource_schema.to_json
79
77
  end
80
78
 
81
79
  private
@@ -56,7 +56,7 @@ module SqlSecurity
56
56
  # Check if value for current class is valid for API consumption
57
57
  def safe_for_sql?(klass, value)
58
58
  klass.column_names.include?(value) ||
59
- DESCENDANTS_UNDERSCORED.include?(value) ||
59
+ DESCENDANTS_UNDERSCORED.include?(value.singularize) ||
60
60
  klass.new.respond_to?(value) ||
61
61
  klass.reflect_on_all_associations.map(&:name).include?(value)
62
62
  end
@@ -0,0 +1,19 @@
1
+ module Apicasso
2
+ # This class exposes the settable attributes of the gem
3
+ class Configuration
4
+ attr_accessor :origins, :headers, :resource, :credentials, :methods,
5
+ :max_age, :expose, :if, :vary
6
+
7
+ def initialize
8
+ @origins = nil
9
+ @headers = nil
10
+ @resource = nil
11
+ @credentials = nil
12
+ @methods = nil
13
+ @max_age = nil
14
+ @expose = nil
15
+ @if = nil
16
+ @vary = nil
17
+ end
18
+ end
19
+ end
@@ -3,6 +3,21 @@
3
3
  module Apicasso
4
4
  # Behavior control for the Apicasso::Engine
5
5
  class Engine < ::Rails::Engine
6
+ require 'rack/cors'
7
+ config.middleware.use Rack::Cors do
8
+ allow do
9
+ origins Apicasso.configuration.origins
10
+ resource Apicasso.configuration.resource,
11
+ headers: Apicasso.configuration.headers,
12
+ methods: Apicasso.configuration.methods,
13
+ credentials: Apicasso.configuration.credentials,
14
+ max_age: Apicasso.configuration.max_age,
15
+ if: Apicasso.configuration.if,
16
+ vary: Apicasso.configuration.vary,
17
+ expose: Apicasso.configuration.expose
18
+ end
19
+ end
20
+
6
21
  config.generators do |g|
7
22
  g.test_framework :rspec, fixture: false
8
23
  g.fixture_replacement :factory_girl, dir: 'spec/factories'
@@ -1,3 +1,3 @@
1
1
  module Apicasso
2
- VERSION = '0.5.2'.freeze
2
+ VERSION = '0.6.0'.freeze
3
3
  end
data/lib/apicasso.rb CHANGED
@@ -10,6 +10,19 @@ require 'apicasso/engine'
10
10
  require 'apicasso/active_record_extension'
11
11
  require 'friendly_id'
12
12
 
13
+ require 'apicasso/configuration'
14
+
15
+ # Load settings defined in initializer
13
16
  module Apicasso
14
- # Your code goes here...
17
+ def self.configuration
18
+ @configuration ||= Configuration.new
19
+ end
20
+
21
+ def self.reset
22
+ @configuration = Configuration.new
23
+ end
24
+
25
+ def self.configure
26
+ yield(configuration)
27
+ end
15
28
  end
@@ -26,6 +26,11 @@ module Apicasso
26
26
  migration_template 'create_apicasso_tables.rb',
27
27
  'db/migrate/create_apicasso_tables.rb'
28
28
  end
29
+
30
+ # Create an initializer with CORS configuration to Apicasso
31
+ def copy_initializer
32
+ copy_file 'apicasso.rb', 'config/initalizers/apicasso.rb'
33
+ end
29
34
  end
30
35
  end
31
36
  end
@@ -0,0 +1,53 @@
1
+ Apicasso.configure do |config|
2
+ # Origins can be specified as a string, a regular expression,
3
+ # or as '*' to allow all origins.
4
+ # Origin response header indicates whether the response can be
5
+ # shared with requesting code from the given origin.
6
+ config.origins = '*'
7
+
8
+ # A Resource path can be specified as exact string match (/path/to/file.txt)
9
+ # or with a '*' wildcard (/all/files/in/*).
10
+ # To include all of a directory's files and the files in its subdirectories,
11
+ # use this form: /assets/**/*.
12
+ config.resource = '*'
13
+
14
+ # The HTTP methods allowed for the resource.
15
+ # Can be a string or array or :any
16
+ config.headers = :any
17
+
18
+ # Sets the Access-Control-Allow-Credentials response header.
19
+ # If a wildcard (*) origin is specified, this option cannot be set to true.
20
+ # Can be a boolean, default: false
21
+ config.credentials = '*'
22
+
23
+ # Sets the Access-Control-Max-Age response header.
24
+ # The Access-Control-Max-Age response header indicates how long the results
25
+ # of a preflight request (that is the information contained in the
26
+ # Access-Control-Allow-Methods and Access-Control-Allow-Headers headers)
27
+ # can be cached.
28
+ # Must be a number
29
+ config.max_age = 1_728_000
30
+
31
+ # The Access-Control-Allow-Methods response header specifies the method or
32
+ # methods allowed when accessing the resource in response to a request.
33
+ # Cam be a string or array or :any
34
+ config.methods = %i[get post delete put patch options]
35
+
36
+ # The Vary HTTP response header determines how to match future request headers
37
+ # to decide whether a cached response can be used rather than requesting a
38
+ # fresh one from the origin server. It is used by the server to indicate which
39
+ # headers it used when selecting a representation of a resource in a content
40
+ # negotiation algorithm.
41
+ # Can be a string or array
42
+ config.vary = nil
43
+
44
+ # The Access-Control-Expose-Headers response header indicates which headers
45
+ # can be exposed as part of the response by listing their names.
46
+ # Can be a string or array
47
+ config.expose = nil
48
+
49
+ # If the result of the proc is true, will process the request as
50
+ # a valid CORS request.
51
+ # Must be a Proc
52
+ config.if = nil
53
+ end
@@ -0,0 +1,11 @@
1
+ Apicasso.configure do |config|
2
+ config.origins = '*'
3
+ config.headers = :any
4
+ config.resource = '*'
5
+ config.credentials = '*'
6
+ config.max_age = 1728000
7
+ config.methods = [:get, :post, :delete, :put, :patch, :options]
8
+ config.vary = nil
9
+ config.expose = nil
10
+ config.if = nil
11
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apicasso
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fernando Bellincanta
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-20 00:00:00.000000000 Z
11
+ date: 2018-11-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -150,6 +150,20 @@ dependencies:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
152
  version: 5.2.0
153
+ - !ruby/object:Gem::Dependency
154
+ name: rack-cors
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
153
167
  - !ruby/object:Gem::Dependency
154
168
  name: rails
155
169
  requirement: !ruby/object:Gem::Requirement
@@ -235,9 +249,11 @@ files:
235
249
  - config/routes.rb
236
250
  - lib/apicasso.rb
237
251
  - lib/apicasso/active_record_extension.rb
252
+ - lib/apicasso/configuration.rb
238
253
  - lib/apicasso/engine.rb
239
254
  - lib/apicasso/version.rb
240
255
  - lib/generators/apicasso/install/install_generator.rb
256
+ - lib/generators/apicasso/install/templates/apicasso.rb
241
257
  - lib/generators/apicasso/install/templates/create_apicasso_tables.rb
242
258
  - spec/apicasso_spec.rb
243
259
  - spec/dummy/Gemfile
@@ -262,12 +278,8 @@ files:
262
278
  - spec/dummy/config/environments/development.rb
263
279
  - spec/dummy/config/environments/production.rb
264
280
  - spec/dummy/config/environments/test.rb
265
- - spec/dummy/config/initializers/application_controller_renderer.rb
266
- - spec/dummy/config/initializers/backtrace_silencers.rb
267
- - spec/dummy/config/initializers/cors.rb
281
+ - spec/dummy/config/initializers/apicasso.rb
268
282
  - spec/dummy/config/initializers/filter_parameter_logging.rb
269
- - spec/dummy/config/initializers/inflections.rb
270
- - spec/dummy/config/initializers/mime_types.rb
271
283
  - spec/dummy/config/initializers/wrap_parameters.rb
272
284
  - spec/dummy/config/locales/en.yml
273
285
  - spec/dummy/config/puma.rb
@@ -334,12 +346,8 @@ test_files:
334
346
  - spec/dummy/config/environments/development.rb
335
347
  - spec/dummy/config/environments/production.rb
336
348
  - spec/dummy/config/environments/test.rb
337
- - spec/dummy/config/initializers/application_controller_renderer.rb
338
- - spec/dummy/config/initializers/backtrace_silencers.rb
339
- - spec/dummy/config/initializers/cors.rb
349
+ - spec/dummy/config/initializers/apicasso.rb
340
350
  - spec/dummy/config/initializers/filter_parameter_logging.rb
341
- - spec/dummy/config/initializers/inflections.rb
342
- - spec/dummy/config/initializers/mime_types.rb
343
351
  - spec/dummy/config/initializers/wrap_parameters.rb
344
352
  - spec/dummy/config/locales/en.yml
345
353
  - spec/dummy/config/puma.rb
@@ -1,8 +0,0 @@
1
- # Be sure to restart your server when you modify this file.
2
-
3
- # ActiveSupport::Reloader.to_prepare do
4
- # ApplicationController.renderer.defaults.merge!(
5
- # http_host: 'example.org',
6
- # https: false
7
- # )
8
- # end
@@ -1,7 +0,0 @@
1
- # Be sure to restart your server when you modify this file.
2
-
3
- # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4
- # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5
-
6
- # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
7
- # Rails.backtrace_cleaner.remove_silencers!
@@ -1,16 +0,0 @@
1
- # Be sure to restart your server when you modify this file.
2
-
3
- # Avoid CORS issues when API is called from the frontend app.
4
- # Handle Cross-Origin Resource Sharing (CORS) in order to accept cross-origin AJAX requests.
5
-
6
- # Read more: https://github.com/cyu/rack-cors
7
-
8
- # Rails.application.config.middleware.insert_before 0, Rack::Cors do
9
- # allow do
10
- # origins 'example.com'
11
- #
12
- # resource '*',
13
- # headers: :any,
14
- # methods: [:get, :post, :put, :patch, :delete, :options, :head]
15
- # end
16
- # end
@@ -1,16 +0,0 @@
1
- # Be sure to restart your server when you modify this file.
2
-
3
- # Add new inflection rules using the following format. Inflections
4
- # are locale specific, and you may define rules for as many different
5
- # locales as you wish. All of these examples are active by default:
6
- # ActiveSupport::Inflector.inflections(:en) do |inflect|
7
- # inflect.plural /^(ox)$/i, '\1en'
8
- # inflect.singular /^(ox)en/i, '\1'
9
- # inflect.irregular 'person', 'people'
10
- # inflect.uncountable %w( fish sheep )
11
- # end
12
-
13
- # These inflection rules are supported but not enabled by default:
14
- # ActiveSupport::Inflector.inflections(:en) do |inflect|
15
- # inflect.acronym 'RESTful'
16
- # end
@@ -1,4 +0,0 @@
1
- # Be sure to restart your server when you modify this file.
2
-
3
- # Add new mime types for use in respond_to blocks:
4
- # Mime::Type.register "text/richtext", :rtf