restful_api_authentication 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +5 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +121 -0
- data/Rakefile +2 -0
- data/lib/generators/restful_api_authentication/install/install_generator.rb +56 -0
- data/lib/generators/restful_api_authentication/install/templates/create_rest_client.rb +12 -0
- data/lib/generators/restful_api_authentication/install/templates/rest_client.rb +36 -0
- data/lib/generators/restful_api_authentication/install/templates/restful_api_authentication.yml +18 -0
- data/lib/restful_api_authentication.rb +56 -0
- data/lib/restful_api_authentication/checker.rb +83 -0
- data/lib/restful_api_authentication/railtie.rb +41 -0
- data/lib/restful_api_authentication/version.rb +26 -0
- data/restful_api_authentication.gemspec +23 -0
- metadata +96 -0
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 David Kiger
|
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,121 @@
|
|
1
|
+
# RestfulApiAuthentication
|
2
|
+
|
3
|
+
RestfulApiAuthentication is a gem which implements a standard api_key/secret authentication system for your Ruby on Rails RESTful web services.
|
4
|
+
|
5
|
+
With most RESTful Web API's, it is important to know which app is using your resources and that only the apps you allow access those resources. This gem allows you to easily add this layer of authentication to any Rails RESTful resource you want, and it even includes protection against various forms of attack.
|
6
|
+
|
7
|
+
## Requirements
|
8
|
+
|
9
|
+
1. Rails 3.2.0+
|
10
|
+
2. ActiveRecord database (sqlite, MySQL, etc.)
|
11
|
+
|
12
|
+
## Dependencies
|
13
|
+
|
14
|
+
1. Rails 3.2.0+
|
15
|
+
2. UUID Gem 2.3.5+
|
16
|
+
3. Chronic Gem 0.6.7+
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
Add this line to your application's Gemfile:
|
21
|
+
|
22
|
+
gem 'restful_api_authentication'
|
23
|
+
|
24
|
+
And then execute:
|
25
|
+
|
26
|
+
$ bundle
|
27
|
+
|
28
|
+
Or install it yourself as:
|
29
|
+
|
30
|
+
$ gem install restful_api_authentication
|
31
|
+
|
32
|
+
Run Rails generator:
|
33
|
+
|
34
|
+
$ rails g restful_api_authentication:install
|
35
|
+
|
36
|
+
Run the migration task:
|
37
|
+
|
38
|
+
$ rake db:migrate
|
39
|
+
|
40
|
+
Update the configuration (if you like) by editing the `config/restful_api_authentication.yml` file.
|
41
|
+
|
42
|
+
## Usage
|
43
|
+
|
44
|
+
### How It Works From A Client's Perspective
|
45
|
+
|
46
|
+
Before anyone can use a resource which is protected using this gem, that person/app must have a valid API key and secret. These are generated and stored as a RestClient model in your app. The easiest way to generate this is to use the Rails console:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
new_app = RestClient.create(:name => "My New App", :description => "This is my new application that will access my RESTful API.")
|
50
|
+
new_app.api_key
|
51
|
+
new_app.secret
|
52
|
+
```
|
53
|
+
|
54
|
+
In order to authenticate with your web service, the new application must include the following HTTP headers with each request:
|
55
|
+
* x-timestamp
|
56
|
+
* x-api-key
|
57
|
+
* x-signature
|
58
|
+
|
59
|
+
The x-timestamp should be the date and time the request is sent. It should be in UTC time and be formatted as "YYYY-MM-DD HH:MM:SS UTC". For example: `2012-03-31 15:37:32 UTC`
|
60
|
+
|
61
|
+
The x-api-key should be the same as the API key generated above. It should look something like `0f0721f0-5cc9-012f-c884-68a86d3dfd0`.
|
62
|
+
|
63
|
+
The x-signature is generated by concatenating the secret generated above, the API request URL, and the x-timestamp into a single string and then using the SHA256 hash algorithm to generate a hash of this string. The x-signature is this hash.
|
64
|
+
|
65
|
+
Here is an example in Ruby code using the HTTParty gem:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
require 'httparty'
|
69
|
+
require 'digest/sha2'
|
70
|
+
|
71
|
+
class MyTestApi
|
72
|
+
include HTTParty
|
73
|
+
|
74
|
+
API_KEY = "e4a80df0-5cca-012f-c884-68a86d3dfd02"
|
75
|
+
SECRET = "473287f8298dba7163a897908958f7c0eae733e25d2e027992ea2edc9bed2fa8"
|
76
|
+
|
77
|
+
def auth_headers(request_uri)
|
78
|
+
timestamp = Time.now.utc.strftime "%Y-%m-%d %H:%M:%S UTC"
|
79
|
+
signature_string = SECRET + request_uri + timestamp
|
80
|
+
digest = Digest::SHA256.new << signature_string
|
81
|
+
signature = digest.to_s
|
82
|
+
{ "x-api-key" => API_KEY, "x-timestamp" => timestamp, "x-signature" => signature }
|
83
|
+
end
|
84
|
+
|
85
|
+
def authenticate_test
|
86
|
+
request_uri = "https://api.mywebservice.com/help/authenticate"
|
87
|
+
self.class.post(request_uri, { :headers => auth_headers(request_uri) })
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
api = MyTestApi.new
|
93
|
+
result = api.authenticate_test
|
94
|
+
puts result.inspect
|
95
|
+
```
|
96
|
+
|
97
|
+
### Configuration
|
98
|
+
|
99
|
+
In the `config/restful_api_authentication.yml` file you will find several things that you can change. The defaults are usually fine for most cases.
|
100
|
+
|
101
|
+
### Requiring Authentication
|
102
|
+
|
103
|
+
To require authentication for a specific resource (controller) of your RESTful web service, add this at the top of your controller just under where you open the controller class:
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
include RestfulApiAuthentication
|
107
|
+
respond_to :json, :xml
|
108
|
+
before_filter :authenticated?
|
109
|
+
```
|
110
|
+
|
111
|
+
If you want to protect your entire web service, add those same lines to your ApplicationController class.
|
112
|
+
|
113
|
+
If the headers are not provided or the application fails to authenticate, your web service will deliver a 401 Unauthorized response.
|
114
|
+
|
115
|
+
## Contributing
|
116
|
+
|
117
|
+
1. Fork it
|
118
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
119
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
120
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
121
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Copyright (c) 2012 David Kiger
|
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.
|
23
|
+
|
24
|
+
require 'rails/generators/migration'
|
25
|
+
|
26
|
+
module RestfulApiAuthentication
|
27
|
+
module Generators
|
28
|
+
class InstallGenerator < ::Rails::Generators::Base
|
29
|
+
include Rails::Generators::Migration
|
30
|
+
source_root File.expand_path('../templates', __FILE__)
|
31
|
+
desc "This generator installs a restful_api_authentication.yml file, creates a RestClient model, and generates migrations for the RestfulApiAuthentication gem."
|
32
|
+
|
33
|
+
def self.next_migration_number(path)
|
34
|
+
unless @prev_migration_nr
|
35
|
+
@prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
|
36
|
+
else
|
37
|
+
@prev_migration_nr += 1
|
38
|
+
end
|
39
|
+
@prev_migration_nr.to_s
|
40
|
+
end
|
41
|
+
|
42
|
+
def copy_migrations
|
43
|
+
migration_template "create_rest_client.rb", "db/migrate/create_rest_client.rb"
|
44
|
+
end
|
45
|
+
|
46
|
+
def copy_the_config_file
|
47
|
+
copy_file "restful_api_authentication.yml", "config/restful_api_authentication.yml"
|
48
|
+
end
|
49
|
+
|
50
|
+
def copy_the_rest_client_model
|
51
|
+
copy_file "rest_client.rb", "app/models/rest_client.rb"
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
class RestClient < ActiveRecord::Base
|
2
|
+
|
3
|
+
validates :name, :presence => true
|
4
|
+
validates :description, :presence => true
|
5
|
+
validates :api_key, :presence => true, :uniqueness => true
|
6
|
+
validates :secret, :presence => true
|
7
|
+
|
8
|
+
# white list fields for mass assignment
|
9
|
+
attr_accessible :name, :description
|
10
|
+
|
11
|
+
# set default values on save
|
12
|
+
before_validation :set_defaults
|
13
|
+
|
14
|
+
# generates a new API key
|
15
|
+
def gen_api_key
|
16
|
+
u = UUID.new
|
17
|
+
self.api_key = u.generate
|
18
|
+
end
|
19
|
+
|
20
|
+
# generates a new secret
|
21
|
+
def gen_secret
|
22
|
+
u = UUID.new
|
23
|
+
d = Digest::SHA256.new << u.generate
|
24
|
+
self.secret = d.to_s
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def set_defaults
|
30
|
+
self.gen_api_key if self.api_key.nil? || self.api_key == ""
|
31
|
+
self.gen_secret if self.secret.nil? || self.secret == ""
|
32
|
+
self.is_master = false if self.is_master.nil?
|
33
|
+
return true
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
data/lib/generators/restful_api_authentication/install/templates/restful_api_authentication.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
defaults: &DEFAULTS
|
2
|
+
request_window: 10 # request window in minutes - between 5 and 10 is usually best; must be at least 2
|
3
|
+
header_names: # names of HTTP headers that must be sent on all requests requiring authentication
|
4
|
+
timestamp: "x-timestamp"
|
5
|
+
signature: "x-signature"
|
6
|
+
api_key: "x-api-key"
|
7
|
+
|
8
|
+
test:
|
9
|
+
<<: *DEFAULTS
|
10
|
+
|
11
|
+
cucumber:
|
12
|
+
<<: *DEFAULTS
|
13
|
+
|
14
|
+
development:
|
15
|
+
<<: *DEFAULTS
|
16
|
+
|
17
|
+
production:
|
18
|
+
<<: *DEFAULTS
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Copyright (c) 2012 David Kiger
|
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.
|
23
|
+
|
24
|
+
require 'digest'
|
25
|
+
require 'chronic'
|
26
|
+
require 'rails'
|
27
|
+
require File.expand_path('../restful_api_authentication/version.rb', __FILE__)
|
28
|
+
require File.expand_path('../restful_api_authentication/checker.rb', __FILE__)
|
29
|
+
require File.expand_path('../restful_api_authentication/railtie.rb', __FILE__)
|
30
|
+
|
31
|
+
module RestfulApiAuthentication
|
32
|
+
|
33
|
+
# before filter to ensure the request has valid client authentication headers
|
34
|
+
# returns a 401 not authorized if the authentication headers are missing or invalid
|
35
|
+
def authenticated?
|
36
|
+
checker = RestfulApiAuthentication::Checker.new(request.headers, request.fullpath)
|
37
|
+
if checker.authorized?
|
38
|
+
return true
|
39
|
+
else
|
40
|
+
respond_with(["not authorized"], :status => 401, :location => nil)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# before filter to ensure the request has valid client authentication headers
|
45
|
+
# client must have is_master flag set to true to pass authentication
|
46
|
+
# returns a 401 not authorized if the authentication headers are missing or invalid
|
47
|
+
def authenticated_master?
|
48
|
+
checker = RestfulApiAuthentication::Checker.new(request.headers, request.fullpath, :require_master => true)
|
49
|
+
if checker.authorized?
|
50
|
+
return true
|
51
|
+
else
|
52
|
+
respond_with(["not authorized"], :status => 401, :location => nil)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Copyright (c) 2012 David Kiger
|
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.
|
23
|
+
|
24
|
+
module RestfulApiAuthentication
|
25
|
+
class Checker
|
26
|
+
cattr_accessor :header_timestamp, :header_signature, :header_api_key, :time_window
|
27
|
+
attr_accessor :http_headers, :request_uri
|
28
|
+
|
29
|
+
def initialize(http_headers, request_uri)
|
30
|
+
@http_headers = http_headers
|
31
|
+
@request_uri = request_uri
|
32
|
+
end
|
33
|
+
|
34
|
+
# Checks if the current request passes authorization
|
35
|
+
def authorized?(options = {})
|
36
|
+
raise "Configuration values not found. Please run rails g restful_api_authentication:install to generate a config file." if @@header_timestamp.nil? || @@header_signature.nil? || @@header_api_key.nil? || @@time_window.nil?
|
37
|
+
return_val = false
|
38
|
+
if headers_have_values? && in_time_window?
|
39
|
+
if (options[:require_master] == true)
|
40
|
+
return_val = true if test_hash == @http_headers[@@header_signature] && is_master?
|
41
|
+
else
|
42
|
+
return_val = true if test_hash == @http_headers[@@header_signature]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
return_val
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
# determines if a RestClient has master privileges or not
|
51
|
+
def is_master?
|
52
|
+
client = RestClient.where(:api_key => @http_headers[@@header_api_key]).first
|
53
|
+
client.is_master
|
54
|
+
end
|
55
|
+
|
56
|
+
# determines if given timestamp is within a specific window of minutes
|
57
|
+
def in_time_window?
|
58
|
+
@@time_window = 4 if @@time_window < 4
|
59
|
+
minutes = (@@time_window / 2).floor
|
60
|
+
ts = Chronic.parse @http_headers[@@header_timestamp]
|
61
|
+
before = Time.now.utc - 60*minutes
|
62
|
+
after = Time.now.utc + 60*minutes
|
63
|
+
ts > before && ts < after
|
64
|
+
end
|
65
|
+
|
66
|
+
# checks that incoming parameters have the keys we expect
|
67
|
+
def headers_have_values?
|
68
|
+
!@http_headers[@@header_api_key].nil? && !@http_headers[@@header_signature].nil? && !@http_headers[@@header_timestamp].nil?
|
69
|
+
end
|
70
|
+
|
71
|
+
# generates the string that is hashed to produce the signature
|
72
|
+
def str_to_hash
|
73
|
+
client = RestClient.where(:api_key => @http_headers[@@header_api_key]).first
|
74
|
+
client.nil? ? "" : client.secret + @request_uri.gsub( /\?.*/, "" ) + @http_headers[@@header_timestamp]
|
75
|
+
end
|
76
|
+
|
77
|
+
# generates the hash that is compared to the incoming signature
|
78
|
+
def test_hash
|
79
|
+
(Digest::SHA256.new << str_to_hash).to_s
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Copyright (c) 2012 David Kiger
|
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.
|
23
|
+
|
24
|
+
module RestfulApiAuthentication
|
25
|
+
class Railtie < Rails::Railtie
|
26
|
+
initializer "restful_api_authentication_railtie.config_initializer" do
|
27
|
+
if File.exists? Rails.root.join('config', 'restful_api_authentication.yml')
|
28
|
+
config_data = YAML::load_file(Rails.root.join('config', 'restful_api_authentication.yml'))[Rails.env]
|
29
|
+
RestfulApiAuthentication::Checker.time_window = config_data['request_window']
|
30
|
+
RestfulApiAuthentication::Checker.header_timestamp = config_data['header_names']['timestamp']
|
31
|
+
RestfulApiAuthentication::Checker.header_signature = config_data['header_names']['signature']
|
32
|
+
RestfulApiAuthentication::Checker.header_api_key = config_data['header_names']['api_key']
|
33
|
+
else
|
34
|
+
RestfulApiAuthentication::Checker.time_window = nil
|
35
|
+
RestfulApiAuthentication::Checker.header_timestamp = nil
|
36
|
+
RestfulApiAuthentication::Checker.header_signature = nil
|
37
|
+
RestfulApiAuthentication::Checker.header_api_key = nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Copyright (c) 2012 David Kiger
|
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.
|
23
|
+
|
24
|
+
module RestfulApiAuthentication
|
25
|
+
VERSION = "0.1.0"
|
26
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/restful_api_authentication/version.rb', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.required_rubygems_version = Gem::Requirement.new(">= 0") if gem.respond_to? :required_rubygems_version=
|
6
|
+
gem.authors = ["Dave Kiger"]
|
7
|
+
gem.email = ["davejkiger@gmail.com"]
|
8
|
+
gem.description = %q{A gem which implements a standard api_key / secret authentication system for your Ruby on Rails RESTful web services.}
|
9
|
+
gem.summary = %q{With most RESTful Web API's, it is important to know which app is using your resources and that only the apps you allow access those resources. This gem allows you to easily add this layer of authentication to any Rails RESTful resource you want, and it even includes protection against various forms of attack.}
|
10
|
+
gem.homepage = "https://github.com/davejkiger/restful_api_authentication"
|
11
|
+
|
12
|
+
#gem.files = `git ls-files`.split($\)
|
13
|
+
gem.files = Dir.glob("{bin,lib}/**/*") + %w(CHANGELOG.md Gemfile LICENSE Rakefile README.md restful_api_authentication.gemspec)
|
14
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
15
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
16
|
+
gem.name = "restful_api_authentication"
|
17
|
+
gem.require_paths = ["lib"]
|
18
|
+
gem.version = RestfulApiAuthentication::VERSION
|
19
|
+
|
20
|
+
gem.add_runtime_dependency(%q<rails>, [">= 3.2.0"])
|
21
|
+
gem.add_runtime_dependency(%q<uuid>, [">= 2.3.5"])
|
22
|
+
gem.add_runtime_dependency(%q<chronic>, [">= 0.6.7"])
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: restful_api_authentication
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dave Kiger
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: &70290931746180 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70290931746180
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: uuid
|
27
|
+
requirement: &70290931745340 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.3.5
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70290931745340
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: chronic
|
38
|
+
requirement: &70290931744600 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 0.6.7
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70290931744600
|
47
|
+
description: A gem which implements a standard api_key / secret authentication system
|
48
|
+
for your Ruby on Rails RESTful web services.
|
49
|
+
email:
|
50
|
+
- davejkiger@gmail.com
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- lib/generators/restful_api_authentication/install/install_generator.rb
|
56
|
+
- lib/generators/restful_api_authentication/install/templates/create_rest_client.rb
|
57
|
+
- lib/generators/restful_api_authentication/install/templates/rest_client.rb
|
58
|
+
- lib/generators/restful_api_authentication/install/templates/restful_api_authentication.yml
|
59
|
+
- lib/restful_api_authentication/checker.rb
|
60
|
+
- lib/restful_api_authentication/railtie.rb
|
61
|
+
- lib/restful_api_authentication/version.rb
|
62
|
+
- lib/restful_api_authentication.rb
|
63
|
+
- CHANGELOG.md
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE
|
66
|
+
- Rakefile
|
67
|
+
- README.md
|
68
|
+
- restful_api_authentication.gemspec
|
69
|
+
homepage: https://github.com/davejkiger/restful_api_authentication
|
70
|
+
licenses: []
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 1.8.17
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: With most RESTful Web API's, it is important to know which app is using your
|
93
|
+
resources and that only the apps you allow access those resources. This gem allows
|
94
|
+
you to easily add this layer of authentication to any Rails RESTful resource you
|
95
|
+
want, and it even includes protection against various forms of attack.
|
96
|
+
test_files: []
|