rack-token_auth 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-token_auth.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 iain
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,83 @@
1
+ # Rack::TokenAuth
2
+
3
+ Rack middleware for using the Authorization header with token authentication.
4
+
5
+ Tokens are passed in the *Authorization* header, and look like this:
6
+
7
+ ```
8
+ Token token="my secret token", option_a="value_a", option_b="value_b"
9
+ ```
10
+
11
+ If you use Rails, you can use the [Rails built-in
12
+ methods](http://api.rubyonrails.org/classes/ActionController/HttpAuthentication/Token.html)
13
+ and you don't need this gem at all.
14
+
15
+ ## Usage
16
+
17
+ Add to your middleware chain, add it to `config.ru`:
18
+
19
+ ``` ruby
20
+ require 'rack/token_auth'
21
+
22
+ use Rack::TokenAuth do |token, options|
23
+ token == "my secret token"
24
+ end
25
+
26
+ run YourApp
27
+ ```
28
+
29
+ If the block returns true, the rest of app will be invoked, if the block
30
+ returns false, the request will halt with a 401 (Unauthorized) response.
31
+
32
+ If you're using Rails, add to `config/environments/production.rb`:
33
+
34
+ ``` ruby
35
+ config.middleware.use Rack::TokenAuth do |token, options|
36
+ # etc...
37
+ end
38
+ ```
39
+
40
+ ### Optional configuration
41
+
42
+ The response in case of an unauthorized request can be modified, by specifying
43
+ a Rack app, like this:
44
+
45
+ ``` ruby
46
+ unauthorized_app = lambda { |env| [ 401, {}, ["Please speak to our sales dep. for access"] }
47
+ use Rack::TokenAuth, :unauthorized_app => unauthorized_app do |token, options|
48
+ # etc...
49
+ end
50
+ ```
51
+
52
+ If the authorization header is malformed, the middleware chain will also be
53
+ halted and a 400 response will be returned. You can also specify this:
54
+
55
+ ``` ruby
56
+ unprocessable_header_app = lambda { |env| [ 400, {}, ["You idiot!"] }
57
+ use Rack::TokenAuth, :unprocessable_header_app => unprocessable_header_app do |token, options|
58
+ # etc...
59
+ end
60
+ ```
61
+
62
+ ## Installation
63
+
64
+ Add this line to your application's Gemfile:
65
+
66
+ gem 'rack-token_auth'
67
+
68
+ And then execute:
69
+
70
+ $ bundle
71
+
72
+ Or install it yourself as:
73
+
74
+ $ gem install rack-token_auth
75
+
76
+
77
+ ## Contributing
78
+
79
+ 1. Fork it
80
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
81
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
82
+ 4. Push to the branch (`git push origin my-new-feature`)
83
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1 @@
1
+ require 'rack/token_auth'
@@ -0,0 +1,63 @@
1
+ require 'rack/token_auth/version'
2
+
3
+ module Rack
4
+ class TokenAuth
5
+
6
+ UnprocessableHeader = Class.new(ArgumentError)
7
+
8
+ def initialize(app, options = {}, &block)
9
+ @app = app
10
+ @options = options
11
+ @block = block
12
+ end
13
+
14
+ def call(env)
15
+ if @block.call(*token_and_options(env["HTTP_AUTHORIZATION"]))
16
+ @app.call(env)
17
+ else
18
+ unauthorized_app.call(env)
19
+ end
20
+ rescue UnprocessableHeader => error
21
+ unprocessable_header_app.call(env)
22
+ end
23
+
24
+ def unauthorized_app
25
+ @options.fetch(:unauthorized_app) { default_unauthorized_app }
26
+ end
27
+
28
+ def unprocessable_header_app
29
+ @options.fetch(:unprocessable_header_app) { default_unprocessable_header_app }
30
+ end
31
+
32
+ def default_unprocessable_header_app
33
+ lambda { |env| Rack::Response.new("Unprocessable Authorization header", 400) }
34
+ end
35
+
36
+ def default_unauthorized_app
37
+ lambda { |env| Rack::Response.new("Unauthorized", 401) }
38
+ end
39
+
40
+ # Taken and adapted from Rails
41
+ # https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/metal/http_authentication.rb
42
+ def token_and_options(header)
43
+ token = header.to_s.match(/^Token (.*)/) { |m| m[1] }
44
+ if token
45
+ begin
46
+ values = Hash[token.split(',').map do |value|
47
+ value.strip! # remove any spaces between commas and values
48
+ key, value = value.split(/\=\"?/) # split key=value pairs
49
+ value.chomp!('"') # chomp trailing " in value
50
+ value.gsub!(/\\\"/, '"') # unescape remaining quotes
51
+ [key, value]
52
+ end]
53
+ [values.delete("token"), values]
54
+ rescue => error
55
+ raise UnprocessableHeader, error
56
+ end
57
+ else
58
+ [nil,{}]
59
+ end
60
+ end
61
+
62
+ end
63
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class TokenAuth
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/token_auth/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "rack-token_auth"
8
+ gem.version = Rack::TokenAuth::VERSION
9
+ gem.authors = ["iain"]
10
+ gem.email = ["iain@iain.nl"]
11
+ gem.description = %q{Rack middleware for using the Authorization header with token authentication}
12
+ gem.summary = %q{Rack middleware for using the Authorization header with token authentication}
13
+ gem.homepage = "https://github.com/iain/rack-token_auth"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "rack"
21
+
22
+ gem.add_development_dependency "rake"
23
+ gem.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,90 @@
1
+ require 'rack'
2
+ require 'rack/token_auth'
3
+
4
+ describe Rack::TokenAuth do
5
+
6
+ Endpoint = Rack::Response.new("OK")
7
+
8
+ describe "parsing the authorization header" do
9
+
10
+ let(:block) { lambda { |token| } }
11
+ let(:app) { build_app(&block) }
12
+
13
+ it "evaluates the block with token and options" do
14
+ block.should_receive(:call).with("abc", "foo" => "bar")
15
+ app.call("HTTP_AUTHORIZATION" => %(Token token="abc", foo="bar"))
16
+ end
17
+
18
+ it "handles absent header" do
19
+ block.should_receive(:call).with(nil, {})
20
+ app.call({})
21
+ end
22
+
23
+ it "handles other authorization header" do
24
+ block.should_receive(:call).with(nil, {})
25
+ app.call("HTTP_AUTHORIZATION" => %(Basic QWxhZGluOnNlc2FtIG9wZW4=))
26
+ end
27
+
28
+ it "handles misformed authorization header" do
29
+ block.should_not_receive(:call)
30
+ result = app.call("HTTP_AUTHORIZATION" => %(Token foobar))
31
+ result.status.should eq 400
32
+ end
33
+
34
+ it "allows specifying the unprocessable header app" do
35
+ unprocessable_header_app = mock :unprocessable_header_app
36
+ app = build_app(:unprocessable_header_app => unprocessable_header_app)
37
+
38
+ unprocessable_header_app.should_receive(:call)
39
+ app.call("HTTP_AUTHORIZATION" => %(Token foobar))
40
+ end
41
+
42
+ end
43
+
44
+ context "when block returns false" do
45
+
46
+ let(:env) { mock :env, :[] => true }
47
+
48
+ it "doesn't call the rest of the app" do
49
+ app = build_app do false end
50
+ Endpoint.should_not_receive(:call)
51
+ app.call(env)
52
+ end
53
+
54
+ it "has a default response" do
55
+ app = build_app do false end
56
+ result = app.call(env)
57
+ result.body.should eq ["Unauthorized"]
58
+ result.status.should eq 401
59
+ end
60
+
61
+ it "is able to set the unauthorized app" do
62
+ unauthorized_app = mock :unauthorized_app
63
+ app = build_app :unauthorized_app => unauthorized_app do false end
64
+
65
+ unauthorized_app.should_receive(:call).with(env)
66
+ app.call(env)
67
+ end
68
+
69
+ end
70
+
71
+ context "when the block returns true" do
72
+
73
+ let(:env) { mock :env, :[] => true }
74
+
75
+ it "calls the rest of your app" do
76
+ app = build_app do true end
77
+ Endpoint.should_receive(:call).with(env)
78
+ app.call(env)
79
+ end
80
+
81
+ end
82
+
83
+ def build_app(*args, &block)
84
+ Rack::Builder.new {
85
+ use Rack::TokenAuth, *args, &block
86
+ run Endpoint
87
+ }
88
+ end
89
+
90
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-token_auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - iain
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
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: Rack middleware for using the Authorization header with token authentication
63
+ email:
64
+ - iain@iain.nl
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - lib/rack-token_auth.rb
76
+ - lib/rack/token_auth.rb
77
+ - lib/rack/token_auth/version.rb
78
+ - rack-token_auth.gemspec
79
+ - spec/rack_token_auth_spec.rb
80
+ homepage: https://github.com/iain/rack-token_auth
81
+ licenses: []
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.23
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Rack middleware for using the Authorization header with token authentication
104
+ test_files:
105
+ - spec/rack_token_auth_spec.rb