au_pair 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .console_history
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Bantik
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,53 @@
1
+ # AuPair
2
+
3
+ AuPair provides token-based authentication and versioning for Rails API applications.
4
+
5
+ ## Installation
6
+
7
+ In your Gemfile:
8
+
9
+ gem 'au_pair'
10
+
11
+ ## API Authentication
12
+
13
+ AuPair's authentication functionality allows you to limit access to your API to those clients that provide
14
+ registered names and associated tokens through their request, either via headers or path variables.
15
+
16
+ For header-based authentication, clients must set the `x-api-vendor` and `x-api-token` headers.
17
+
18
+ For path-based authentication, clients pass in `api_token` and `api_vendor` parameters through the request.
19
+
20
+ To set up tokens, create a configuration file in config/initializers/au_pair.rb to specify client apps and their associated auth tokens:
21
+
22
+ AuPair.configure do |config|
23
+
24
+ config.tokens = {
25
+ 'my_sample_app' => '12345'
26
+ }
27
+
28
+ end
29
+
30
+ Then in your application controller, or in individual controllers if you want to limit authentication to certain actions:
31
+
32
+ class ApplicationController < ActionController::Base
33
+ include AuPair::Authenticates
34
+ before_filter :authenticate!
35
+ end
36
+
37
+ ## API Versioning Support
38
+
39
+ Specify groups of routes per API version In your routes file:
40
+
41
+ constraints(AuPair::ApiConstraint.new('v1')) do
42
+ resources :widgets
43
+ end
44
+
45
+ Client apps can then specify the API version that they want to use by passing in an `x-api-version` header or an `api_version` URL parameter.
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/au_pair.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'au_pair/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "au_pair"
8
+ spec.version = AuPair::VERSION
9
+ spec.authors = ["Corey Ehmke"]
10
+ spec.email = ["cehmke@apartments.com"]
11
+ spec.description = %q{Provides API versioning and token authentication services to Apts.com client apps}
12
+ spec.summary = %q{API versioning and token authentication}
13
+ spec.homepage = ""
14
+ spec.license = "Private"
15
+
16
+ spec.files = `git ls-files`.split($/)
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.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
data/lib/au_pair.rb ADDED
@@ -0,0 +1,25 @@
1
+ require "au_pair/version"
2
+ require "au_pair/authentication_token"
3
+ require "au_pair/authenticates"
4
+ require 'au_pair/api_constraint'
5
+
6
+ module AuPair
7
+
8
+ def self.configure(&block)
9
+ @config = Configuration.new
10
+ yield(config)
11
+ end
12
+
13
+ def self.config
14
+ @config || Configuration.new
15
+ end
16
+
17
+ def self.tokens
18
+ config.tokens
19
+ end
20
+
21
+ class Configuration
22
+ attr_accessor :tokens
23
+ end
24
+
25
+ end
@@ -0,0 +1,28 @@
1
+ class AuPair::ApiConstraint
2
+
3
+ attr_accessor :numeric_version
4
+
5
+ def initialize(path_part)
6
+ @path_part = path_part.downcase
7
+ @numeric_version = path_part.gsub(/.?([0-9]+)/, "\\1").to_i
8
+ end
9
+
10
+ def matches?(request)
11
+ path_matches?(request) || header_matches?(request) || param_matches?(request)
12
+ end
13
+
14
+ private
15
+
16
+ def path_matches?(request)
17
+ ! (request.path =~ /\/#{@path_part}\//).nil?
18
+ end
19
+
20
+ def header_matches?(request)
21
+ ! (request.headers['x-api-version'] =~ /#{@numeric_version}/).nil?
22
+ end
23
+
24
+ def param_matches?(request)
25
+ request.request_parameters["api_version"].to_i == numeric_version || request.query_parameters["api_version"].to_i == numeric_version
26
+ end
27
+
28
+ end
@@ -0,0 +1,16 @@
1
+ module AuPair::Authenticates
2
+
3
+ def token
4
+ request.headers['x-api-token'] || params[:api_token]
5
+ end
6
+
7
+ def vendor_name
8
+ request.headers['x-api-vendor'] || params[:api_vendor]
9
+ end
10
+
11
+ def authenticate!
12
+ return true if AuPair::AuthenticationToken.valid?(token, vendor_name)
13
+ render(:json => {'error' => 'Invalid authentication token.'}, :status => 401) and return
14
+ end
15
+
16
+ end
@@ -0,0 +1,7 @@
1
+ class AuPair::AuthenticationToken
2
+
3
+ def self.valid?(key, vendor)
4
+ AuPair.tokens[vendor] == key
5
+ end
6
+
7
+ end
@@ -0,0 +1,3 @@
1
+ module AuPair
2
+ VERSION = "1.0.0"
3
+ end
data/spec/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe AuPair::AuthenticationToken do
4
+
5
+ context 'token validation from config' do
6
+
7
+ before do
8
+ AuPair.configure{ |config| config.tokens = {'foo' => '1234', 'bar' => '5678'} }
9
+ end
10
+
11
+ it 'identifies a valid token' do
12
+ expect(AuPair::AuthenticationToken.valid?('1234', 'foo')).to be_true
13
+ end
14
+
15
+ it 'detects an invalid token' do
16
+ expect(AuPair::AuthenticationToken.valid?('4567', 'foo')).to be_false
17
+ end
18
+
19
+ it 'detects an invalid vendor' do
20
+ expect(AuPair::AuthenticationToken.valid?('4567', 'food')).to be_false
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe AuPair do
4
+
5
+ it 'accepts tokens as part of its configuration' do
6
+ tokens = [{'foo' => '1234'}]
7
+ AuPair.configure{ |config| config.tokens = tokens }
8
+ expect(AuPair.config.tokens).to eq tokens
9
+ end
10
+
11
+ end
@@ -0,0 +1,2 @@
1
+ require 'rspec'
2
+ require 'au_pair'
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: au_pair
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Corey Ehmke
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Provides API versioning and token authentication services to Apts.com
63
+ client apps
64
+ email:
65
+ - cehmke@apartments.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - .rspec
72
+ - Gemfile
73
+ - LICENSE.txt
74
+ - README.md
75
+ - Rakefile
76
+ - au_pair.gemspec
77
+ - lib/au_pair.rb
78
+ - lib/au_pair/api_constraint.rb
79
+ - lib/au_pair/authenticates.rb
80
+ - lib/au_pair/authentication_token.rb
81
+ - lib/au_pair/version.rb
82
+ - spec/.rspec
83
+ - spec/authentication_token_spec.rb
84
+ - spec/configuration_spec.rb
85
+ - spec/spec_helper.rb
86
+ homepage: ''
87
+ licenses:
88
+ - Private
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ segments:
100
+ - 0
101
+ hash: -3911207844321360357
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ segments:
109
+ - 0
110
+ hash: -3911207844321360357
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 1.8.24
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: API versioning and token authentication
117
+ test_files:
118
+ - spec/.rspec
119
+ - spec/authentication_token_spec.rb
120
+ - spec/configuration_spec.rb
121
+ - spec/spec_helper.rb