csr_auth 0.0.1
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 +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +85 -0
- data/Rakefile +2 -0
- data/csr_auth.gemspec +23 -0
- data/lib/csr_auth.rb +51 -0
- data/lib/csr_auth/version.rb +3 -0
- data/lib/generators/csr_auth/install/USAGE +5 -0
- data/lib/generators/csr_auth/install/install_generator.rb +10 -0
- data/lib/generators/csr_auth/install/templates/csr_auth.rb +11 -0
- data/lib/generators/install_generator.rb +10 -0
- data/lib/helpers/configuration.rb +28 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4f9b8aad2a317167b51d55f568fca7e2aa108554
|
4
|
+
data.tar.gz: 36c1b030f4b27497bec1119d5064e9b8534e2c33
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8ec1bb34c944e9520f5b59fac776f1450174e06adc1d793a3d155022683c3cb89daa03a77e40b1515d1863c884f74ee8ea0c3df244e500506ae22b7ddae3527a
|
7
|
+
data.tar.gz: 666699812306cf0ff1ff9aa997479a737e757e3d31695cf4cbd896b3f1a858f6a791e7a7f0dbd58709ad7c67949b37d4092638c9361b50501a65f098579c7bcf
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 nishadmenezes
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
# CsrAuth
|
2
|
+
|
3
|
+
Allow CORS(Cross Origin Resource Sharing) via AJAX requests from trusted web applications to your Rails Back-End.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'csr_auth'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install csr_auth
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Generate Initializer
|
24
|
+
|
25
|
+
$ rails generate csr_auth:install
|
26
|
+
|
27
|
+
### Edit Initializer
|
28
|
+
Allowed origins can be specified in the generated Rails intializer:
|
29
|
+
|
30
|
+
config/initializers/csr_auth.rb
|
31
|
+
|
32
|
+
Configure allowed origins and allowed methods for each origin as shown:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
# Tell CsrAuth the origins that will send AJAX requests
|
36
|
+
|
37
|
+
CsrAuth.configuration do |config|
|
38
|
+
# Cross Origin Resource Sharing via remote requests will be allowed only for the specified origins.
|
39
|
+
# By default the value is set to '*', allows all origins and request methods
|
40
|
+
config.allowed_origins = "*"
|
41
|
+
|
42
|
+
# Examples:
|
43
|
+
# config.allowed_origins = {:origin => 'http://localhost:3000', :methods => :all}
|
44
|
+
# config.allowed_origins = {:origin => '127.0.0.1:3000', :methods => :all}, {:origin => 'chrome-extension://my_extension_id', :methods => [:get, :post, :put]}
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
#### Hash Description
|
49
|
+
__:origin__ => The origin that will send cross script requests to your Rails app. You can pass either a string or a regex.
|
50
|
+
|
51
|
+
__:methods__ => The allowed methods for a request from an origin. Value can either be *:all* for all methods or an array of allowed methods - *[:get, :post, :delete]*.
|
52
|
+
|
53
|
+
_"*"_ => Allows requests from all origins and methods.
|
54
|
+
|
55
|
+
__NOTE__: When passing a Regex be sure not to be too inclusive.
|
56
|
+
|
57
|
+
### Modify Application Controller
|
58
|
+
```ruby
|
59
|
+
class ApplicationController < ActionController::Base
|
60
|
+
require 'csr_auth'
|
61
|
+
# Prevent CSRF attacks by raising an exception.
|
62
|
+
# For APIs, you may want to use :null_session instead.
|
63
|
+
protect_from_forgery with: :exception, if: :block_csr?
|
64
|
+
#before_filter :chrome_extension
|
65
|
+
|
66
|
+
private
|
67
|
+
def block_csr?
|
68
|
+
CsrAuth::Filter.block_csr? request
|
69
|
+
end
|
70
|
+
```
|
71
|
+
1. Create a private method like - `block_csr?` and call CsrAuth's filter method passing in the request object - `CsrAuth::Filter.block_csr? request`
|
72
|
+
2. Change line - `protect_from_forgery...` to `protect_from_forgery with: :exception, if: :block_csr?`
|
73
|
+
|
74
|
+
## Configuration Examples:
|
75
|
+
config.allowed_origins = "*"
|
76
|
+
|
77
|
+
config.allowed_origins = {:origin => 'http://localhost:3000', :methods => :all}
|
78
|
+
|
79
|
+
config.allowed_origins = {:origin => '127.0.0.1:3000', :methods => :all}, {:origin => 'chrome-extension://my_extension_id', :methods => [:get, :post, :put]}
|
80
|
+
|
81
|
+
config.allowed_origins = {:origin => /chrome-extension:\\/\\/my_extension_id/, :methods => :get}, {:origin => /http:\\/\\/localhost:3000/, :methods => [:post, :put]}
|
82
|
+
|
83
|
+
|
84
|
+
## License:
|
85
|
+
MIT - [SEE HERE](../master/LICENSE)
|
data/Rakefile
ADDED
data/csr_auth.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'csr_auth/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "csr_auth"
|
8
|
+
spec.version = CsrAuth::VERSION
|
9
|
+
spec.authors = ["nishadmenezes"]
|
10
|
+
spec.email = ["nishadmenezes@gmail.com"]
|
11
|
+
spec.summary = "Allow Cross Origin AJAX requests from trusted web applications to your Rails Back-End."
|
12
|
+
spec.description = ""
|
13
|
+
spec.homepage = "https://github.com/nishadmenezes/csr_auth"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
data/lib/csr_auth.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require "csr_auth/version"
|
2
|
+
require "helpers/configuration"
|
3
|
+
|
4
|
+
module CsrAuth
|
5
|
+
extend Configuration
|
6
|
+
|
7
|
+
define_setting :allowed_origins, "*"
|
8
|
+
|
9
|
+
class Filter
|
10
|
+
|
11
|
+
include CsrAuth
|
12
|
+
|
13
|
+
def self.block_csr?(request)
|
14
|
+
if request.format.html?
|
15
|
+
return true
|
16
|
+
else
|
17
|
+
block = true
|
18
|
+
unless request.headers['origin'].nil?
|
19
|
+
if @@allowed_origins == "*"
|
20
|
+
block = false
|
21
|
+
else
|
22
|
+
@@allowed_origins.each do |origin|
|
23
|
+
if origin[:origin].class == String
|
24
|
+
if request.headers['origin'].start_with? origin[:origin]
|
25
|
+
block = block_method? origin[:methods], request.method
|
26
|
+
break
|
27
|
+
end
|
28
|
+
elsif origin[:origin].class == Regexp
|
29
|
+
if request.headers['origin'] =~ origin[:origin]
|
30
|
+
block = block_method? origin[:methods], request.method
|
31
|
+
break
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
block
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
def self.block_method?(origin_methods, request_method)
|
43
|
+
if origin_methods == :all
|
44
|
+
false
|
45
|
+
else
|
46
|
+
origin_methods = [].push(origin_methods) if origin_methods.class == Symbol
|
47
|
+
origin_methods.exclude?(request_method.downcase.to_sym)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# Tell CsrAuth the origins that will send AJAX requests
|
2
|
+
|
3
|
+
CsrAuth.configuration do |config|
|
4
|
+
# Cross Origin Resource Sharing via remote requests will be allowed only for the specified origins.
|
5
|
+
# By default the value is set to '*', allows all origins and request methods
|
6
|
+
config.allowed_origins = "*"
|
7
|
+
|
8
|
+
# Examples:
|
9
|
+
# config.allowed_origins = {:origin => 'http://localhost:3000', :methods => :all}
|
10
|
+
# config.allowed_origins = {:origin => '127.0.0.1:3000', :methods => :all}, {:origin => 'chrome-extension://my_extension_id', :methods => [:get, :post, :put]}
|
11
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Configuration
|
2
|
+
|
3
|
+
def configuration
|
4
|
+
yield self
|
5
|
+
end
|
6
|
+
|
7
|
+
def define_setting(name, default = nil)
|
8
|
+
class_variable_set("@@#{name}", default)
|
9
|
+
|
10
|
+
define_class_method "#{name}=" do |value|
|
11
|
+
value = [].push(value) if value != "*" && value.class == Hash
|
12
|
+
class_variable_set("@@#{name}", value)
|
13
|
+
end
|
14
|
+
|
15
|
+
define_class_method name do
|
16
|
+
class_variable_get("@@#{name}")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def define_class_method(name, &block)
|
23
|
+
(class << self; self; end).instance_eval do
|
24
|
+
define_method name, &block
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: csr_auth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- nishadmenezes
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: ''
|
42
|
+
email:
|
43
|
+
- nishadmenezes@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- csr_auth.gemspec
|
54
|
+
- lib/csr_auth.rb
|
55
|
+
- lib/csr_auth/version.rb
|
56
|
+
- lib/generators/csr_auth/install/USAGE
|
57
|
+
- lib/generators/csr_auth/install/install_generator.rb
|
58
|
+
- lib/generators/csr_auth/install/templates/csr_auth.rb
|
59
|
+
- lib/generators/install_generator.rb
|
60
|
+
- lib/helpers/configuration.rb
|
61
|
+
homepage: https://github.com/nishadmenezes/csr_auth
|
62
|
+
licenses:
|
63
|
+
- MIT
|
64
|
+
metadata: {}
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 2.2.2
|
82
|
+
signing_key:
|
83
|
+
specification_version: 4
|
84
|
+
summary: Allow Cross Origin AJAX requests from trusted web applications to your Rails
|
85
|
+
Back-End.
|
86
|
+
test_files: []
|