sworn 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.
@@ -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
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sworn.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Martin Svangren
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.
@@ -0,0 +1,29 @@
1
+ # Sworn
2
+
3
+ Sworn is Rack middleware to handle OAuth 1.0a signed requests.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sworn'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sworn
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,17 @@
1
+ require "sworn/configuration"
2
+ require "sworn/middleware"
3
+ require "sworn/replay_protector/custom"
4
+ require "sworn/verifier"
5
+ require "sworn/version"
6
+
7
+ module Sworn
8
+ class << self
9
+ def configure
10
+ yield(configuration)
11
+ end
12
+
13
+ def configuration
14
+ @configuration ||= Configuration.new
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,28 @@
1
+ module Sworn
2
+ class Configuration
3
+ # A hash of consumer keys and their secrets
4
+ attr_accessor :consumers
5
+
6
+ # Maximum timestamp drift allowed
7
+ attr_accessor :max_drift
8
+
9
+ # A Proc that takes an OAuth options hash and returns true if the request
10
+ # is replayed, and false if it is not
11
+ attr_reader :replay_protector
12
+
13
+ def replay_protector=(*args)
14
+ klass, *parameters = args.flatten
15
+ @replay_protector = klass.new(parameters)
16
+ end
17
+
18
+ # A hash of access tokens and their secrets
19
+ attr_accessor :tokens
20
+
21
+ def initialize
22
+ consumers = Hash.new
23
+ max_drift = 30
24
+ replay_protector = Sworn::ReplayProtector::Custom, lambda { |_| false }
25
+ tokens = Hash.new
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,32 @@
1
+ require "simple_oauth"
2
+
3
+ module Sworn
4
+ class Middleware
5
+ attr_reader :verifier_class
6
+
7
+ def initialize(app, options = {})
8
+ @app = app
9
+ @verifier_class = options.fetch(:verifier_class) { Verifier }
10
+ end
11
+
12
+ def call(env)
13
+ request = Rack::Request.new(env)
14
+ verifier = verifier_class.new(request)
15
+
16
+ return bad_request if verifier.unsigned?
17
+ return not_authorized if verifier.expired?
18
+ return not_authorized if verifier.replayed?
19
+ return not_authorized unless verifier.valid?
20
+
21
+ return @app.call(env)
22
+ end
23
+
24
+ def bad_request
25
+ [400, {}, ["Bad request"]]
26
+ end
27
+
28
+ def not_authorized
29
+ [401, {}, ["Not authorized"]]
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,13 @@
1
+ module Sworn
2
+ module ReplayProtector
3
+ class Custom
4
+ def initialize(*options)
5
+ @evaluator, _ = options.flatten
6
+ end
7
+
8
+ def replayed?(oauth)
9
+ @evaluator.call(oauth)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,40 @@
1
+ module Sworn
2
+ class Verifier
3
+ attr_accessor :config, :oauth, :request
4
+
5
+ def initialize(request, options = {})
6
+ @config = options.fetch(:config) { Sworn.configuration }
7
+ @request = request
8
+ @oauth = SimpleOAuth::Header.parse(request.env["HTTP_AUTHORIZATION"])
9
+ end
10
+
11
+ def unsigned?
12
+ oauth.empty?
13
+ end
14
+
15
+ def expired?
16
+ timestamp = oauth.fetch(:timestamp).to_i
17
+ now = Time.now.to_i
18
+ window = (now - config.max_drift .. now + config.max_drift)
19
+ !window.include?(timestamp)
20
+ end
21
+
22
+ def replayed?
23
+ config.replay_protector.replayed?(oauth)
24
+ end
25
+
26
+ def valid?
27
+ consumer_key = oauth[:consumer_key]
28
+ consumer_secret = config.consumers[consumer_key]
29
+ access_token = oauth[:token]
30
+ token_secret = config.tokens[access_token]
31
+
32
+ valid = SimpleOAuth::Header.new(
33
+ request.request_method,
34
+ request.url,
35
+ request.params,
36
+ oauth
37
+ ).valid?(:consumer_secret => consumer_secret, :token_secret => token_secret)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module Sworn
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'sworn'
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+ require 'rack/test'
3
+ require 'simple_oauth'
4
+
5
+ describe Sworn::Middleware do
6
+ include Rack::Test::Methods
7
+
8
+ def dummy_app
9
+ lambda { |_| [200, {}, "Hello"] }
10
+ end
11
+
12
+ def app
13
+ Sworn.configure do |config|
14
+ config.consumers = { "consumer" => "consumersecret" }
15
+ config.tokens = { "token" => "tokensecret" }
16
+ config.max_drift = 30
17
+ config.replay_protector = Sworn::ReplayProtector::Custom, lambda { |oauth|
18
+ @store ||= Set.new
19
+ return true if @store.include?(oauth)
20
+ @store << oauth
21
+ false
22
+ }
23
+ end
24
+
25
+ Sworn::Middleware.new dummy_app
26
+ end
27
+
28
+ def oauth_signature(options = {})
29
+ method = options.delete(:method) { "GET" }
30
+ url = options.delete(:url) { "http://example.org/" }
31
+ params = options.delete(:params) { Hash.new }
32
+
33
+ options[:consumer_key] ||= "consumer"
34
+ options[:consumer_secret] ||= "consumersecret"
35
+
36
+ SimpleOAuth::Header.new(method, url, params, options)
37
+ end
38
+
39
+ it "returns 400 when signature is missing" do
40
+ get "/"
41
+ expect(last_response.status).to eq 400
42
+ end
43
+
44
+ it "returns 401 when signature is invalid" do
45
+ get "/", {}, { 'HTTP_AUTHORIZATION' => 'OAuth oauth_consumer_key="invalid", oauth_token="", oauth_nonce="abc", oauth_timestamp="123", oauth_signature_method="HMAC-SHA1", oauth_version="1.0", oauth_signature="nowayjose"' }
46
+ expect(last_response.status).to eq 401
47
+ end
48
+
49
+ it "returns 401 when signature timestamp is out of bounds" do
50
+ get "/", {}, { 'HTTP_AUTHORIZATION' => oauth_signature(:timestamp => (Time.now.to_i - 60).to_s) }
51
+ expect(last_response.status).to eq 401
52
+ end
53
+
54
+ it "returns 401 when signature is replayed" do
55
+ replayed = oauth_signature
56
+ get "/", {}, { 'HTTP_AUTHORIZATION' => replayed }
57
+ expect(last_response.status).to eq 200
58
+ get "/", {}, { 'HTTP_AUTHORIZATION' => replayed }
59
+ expect(last_response.status).to eq 401
60
+ end
61
+
62
+ it "returns 200 for valid consumer-only signature" do
63
+ get "/", {}, { 'HTTP_AUTHORIZATION' => oauth_signature }
64
+ expect(last_response.status).to eq 200
65
+ end
66
+
67
+ it "returns 200 for valid consumer + access token signature" do
68
+ get "/", {}, { 'HTTP_AUTHORIZATION' => oauth_signature(:token => "token", :token_secret => "tokensecret") }
69
+ expect(last_response.status).to eq 200
70
+ end
71
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sworn do
4
+ it "has a version number" do
5
+ expect(Sworn::VERSION).not_to be_nil
6
+ end
7
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sworn/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sworn"
8
+ spec.version = Sworn::VERSION
9
+ spec.authors = ["Martin Svangren"]
10
+ spec.email = ["martin@masv.net"]
11
+ spec.description = %q{Sworn is Rack middleware to handle OAuth 1.0a signed requests}
12
+ spec.summary = %q{Rack middleware for OAuth 1.0a signed requests}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
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 "rack-test"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+
26
+ spec.add_runtime_dependency "rack"
27
+ spec.add_runtime_dependency "simple_oauth"
28
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sworn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Martin Svangren
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-05-26 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: rack-test
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: rake
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
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rack
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: simple_oauth
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Sworn is Rack middleware to handle OAuth 1.0a signed requests
111
+ email:
112
+ - martin@masv.net
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - .travis.yml
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - lib/sworn.rb
124
+ - lib/sworn/configuration.rb
125
+ - lib/sworn/middleware.rb
126
+ - lib/sworn/replay_protector/custom.rb
127
+ - lib/sworn/verifier.rb
128
+ - lib/sworn/version.rb
129
+ - spec/spec_helper.rb
130
+ - spec/sworn/middleware_spec.rb
131
+ - spec/sworn_spec.rb
132
+ - sworn.gemspec
133
+ homepage: ''
134
+ licenses:
135
+ - MIT
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ! '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ none: false
148
+ requirements:
149
+ - - ! '>='
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 1.8.23
155
+ signing_key:
156
+ specification_version: 3
157
+ summary: Rack middleware for OAuth 1.0a signed requests
158
+ test_files:
159
+ - spec/spec_helper.rb
160
+ - spec/sworn/middleware_spec.rb
161
+ - spec/sworn_spec.rb