rack-oauth_proxy 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/rack/oauth_proxy.rb +0 -1
- data/lib/rack/oauth_proxy/client.rb +21 -36
- data/lib/rack/oauth_proxy/client/request.rb +20 -18
- data/lib/rack/oauth_proxy/version.rb +1 -1
- data/rack-oauth_proxy.gemspec +2 -1
- data/spec/rack/oauth_proxy/client_spec.rb +34 -9
- metadata +30 -3
- data/lib/rack/oauth_proxy/client/response.rb +0 -42
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c33bd03642050ec16695d9edd4eb0d128c1b26d
|
4
|
+
data.tar.gz: 18abbeca292daa9d059f18cfa3b13d86d86b04f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2834913040a3706d41932e707744bbddec7bc2d522364fe0f8656c8c0c5ef93d12cce00adf69523b3495dc83700dde5d80e6a17125cba5a1a1232872563be432
|
7
|
+
data.tar.gz: b037d89e8c295df21a9c4cc88ccf02dd9807f01442c979d13c2f69784a65735d0fb81ccc3dcfff72b61ca8e06f5a7fb9a628e2b65c62b15884838ccd36eccd06
|
data/CHANGELOG.md
CHANGED
data/lib/rack/oauth_proxy.rb
CHANGED
@@ -3,7 +3,6 @@ require "rack/oauth_proxy/access_tokens/invalid"
|
|
3
3
|
require "rack/oauth_proxy/access_tokens/valid"
|
4
4
|
require "rack/oauth_proxy/client"
|
5
5
|
require "rack/oauth_proxy/client/request"
|
6
|
-
require "rack/oauth_proxy/client/response"
|
7
6
|
require "rack/oauth_proxy/version"
|
8
7
|
|
9
8
|
module Rack
|
@@ -1,60 +1,45 @@
|
|
1
|
-
require "
|
1
|
+
require "faraday"
|
2
|
+
require "faraday_middleware"
|
2
3
|
|
3
4
|
module Rack
|
4
5
|
class OauthProxy
|
5
6
|
class Client
|
6
|
-
|
7
|
-
OPEN_TIMEOUT = 1
|
7
|
+
attr_reader :options
|
8
8
|
|
9
9
|
def initialize(options = {})
|
10
10
|
@options = options
|
11
11
|
end
|
12
12
|
|
13
13
|
def fetch(env)
|
14
|
-
request = Request.new(env)
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
header = {
|
19
|
-
"Authorization" => request.authorization,
|
20
|
-
"Host" => host,
|
21
|
-
"Resource-Owner-Id" => request.resource_owner_id,
|
22
|
-
"Scopes" => request.scopes,
|
23
|
-
}.reject {|key, value| value.nil? }
|
24
|
-
raw_response = http_client.get(path, header)
|
25
|
-
response = Response.new(raw_response)
|
26
|
-
if response.valid_as_access_token?
|
27
|
-
AccessTokens::Valid.new(response.to_hash)
|
28
|
-
else
|
29
|
-
AccessTokens::Invalid.new
|
30
|
-
end
|
31
|
-
else
|
32
|
-
AccessTokens::Invalid.new
|
33
|
-
end
|
34
|
-
rescue Timeout::Error
|
14
|
+
request = Request.new(env, options)
|
15
|
+
response = connection.get(url, request.params, request.header)
|
16
|
+
AccessTokens::Valid.new(response.body)
|
17
|
+
rescue
|
35
18
|
AccessTokens::Invalid.new
|
36
19
|
end
|
37
20
|
|
38
21
|
private
|
39
22
|
|
40
|
-
def
|
41
|
-
@
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
client.read_timeout = READ_TIMEOUT
|
47
|
-
client.open_timeout = OPEN_TIMEOUT
|
48
|
-
client.use_ssl = true if uri.scheme == 'https'
|
49
|
-
client
|
23
|
+
def connection
|
24
|
+
@connection ||= Faraday.new(headers: header) do |connection|
|
25
|
+
connection.adapter :net_http
|
26
|
+
connection.response :raise_error
|
27
|
+
connection.response :json
|
28
|
+
end
|
50
29
|
end
|
51
30
|
|
52
31
|
def url
|
53
|
-
|
32
|
+
options[:url] or raise NoUrlError
|
54
33
|
end
|
55
34
|
|
56
35
|
def host
|
57
|
-
|
36
|
+
options[:host]
|
37
|
+
end
|
38
|
+
|
39
|
+
def header
|
40
|
+
{
|
41
|
+
"Host" => host,
|
42
|
+
}.reject {|key, value| value.nil? }
|
58
43
|
end
|
59
44
|
|
60
45
|
class NoUrlError < StandardError
|
@@ -1,40 +1,42 @@
|
|
1
1
|
require "active_support/core_ext/hash/slice"
|
2
|
-
require "
|
3
|
-
require "active_support/core_ext/object/to_query"
|
2
|
+
require "json"
|
4
3
|
require "rack"
|
5
4
|
|
6
5
|
module Rack
|
7
6
|
class OauthProxy
|
8
7
|
class Client
|
9
8
|
class Request
|
10
|
-
|
9
|
+
DEFAULT_PROPAGATED_HEADER_FIELDS = ["Authorization"]
|
10
|
+
|
11
|
+
attr_reader :env, :options
|
12
|
+
|
13
|
+
def initialize(env, options = {})
|
11
14
|
@env = env
|
15
|
+
@options = options
|
12
16
|
end
|
13
17
|
|
14
|
-
def
|
15
|
-
|
16
|
-
rack_request.params["access_token"].present? ||
|
17
|
-
rack_request.params["bearer_token"].present?
|
18
|
+
def header
|
19
|
+
header_with_nil_value.reject {|key, value| value.nil? }
|
18
20
|
end
|
19
21
|
|
20
|
-
def
|
21
|
-
|
22
|
+
def header_with_nil_value
|
23
|
+
propagated_header_fields.inject({}) do |result, field|
|
24
|
+
result.merge(field => env["HTTP_" + field.gsub("-", "_").upcase])
|
25
|
+
end
|
22
26
|
end
|
23
27
|
|
24
|
-
def
|
25
|
-
rack_request.params.slice("access_token", "bearer_token")
|
28
|
+
def params
|
29
|
+
rack_request.params.slice("access_token", "bearer_token")
|
26
30
|
end
|
27
31
|
|
28
|
-
|
29
|
-
@env["HTTP_AUTHORIZATION"]
|
30
|
-
end
|
32
|
+
private
|
31
33
|
|
32
|
-
def
|
33
|
-
@env
|
34
|
+
def rack_request
|
35
|
+
@rack_request ||= Rack::Request.new(@env)
|
34
36
|
end
|
35
37
|
|
36
|
-
def
|
37
|
-
|
38
|
+
def propagated_header_fields
|
39
|
+
options[:propagated_header_fields] || DEFAULT_PROPAGATED_HEADER_FIELDS
|
38
40
|
end
|
39
41
|
end
|
40
42
|
end
|
data/rack-oauth_proxy.gemspec
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# coding: utf-8
|
2
1
|
lib = File.expand_path("../lib", __FILE__)
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
3
|
require "rack/oauth_proxy/version"
|
@@ -18,8 +17,10 @@ Gem::Specification.new do |spec|
|
|
18
17
|
spec.require_paths = ["lib"]
|
19
18
|
|
20
19
|
spec.add_dependency "activesupport"
|
20
|
+
spec.add_dependency "faraday_middleware"
|
21
21
|
spec.add_dependency "rack"
|
22
22
|
spec.add_development_dependency "bundler", "~> 1.5"
|
23
|
+
spec.add_development_dependency "pry"
|
23
24
|
spec.add_development_dependency "rake"
|
24
25
|
spec.add_development_dependency "rspec", "2.14.1"
|
25
26
|
spec.add_development_dependency "webmock"
|
@@ -1,9 +1,19 @@
|
|
1
1
|
require "spec_helper"
|
2
|
+
require "securerandom"
|
2
3
|
require "stringio"
|
3
4
|
|
4
5
|
describe Rack::OauthProxy::Client do
|
6
|
+
before do
|
7
|
+
stub_request(:get, url).to_return(status: 401, body: {}.to_json)
|
8
|
+
stub_request(:get, url).with(headers: { "Authorization" => "Bearer #{token}" }).to_return(response)
|
9
|
+
end
|
10
|
+
|
5
11
|
let(:client) do
|
6
|
-
described_class.new(
|
12
|
+
described_class.new(options)
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:options) do
|
16
|
+
{ url: url }
|
7
17
|
end
|
8
18
|
|
9
19
|
let(:url) do
|
@@ -13,6 +23,7 @@ describe Rack::OauthProxy::Client do
|
|
13
23
|
let(:env) do
|
14
24
|
{
|
15
25
|
"HTTP_AUTHORIZATION" => "Bearer #{token}",
|
26
|
+
"HTTP_DUMMY" => "dummy",
|
16
27
|
"rack.input" => StringIO.new,
|
17
28
|
}
|
18
29
|
end
|
@@ -21,34 +32,48 @@ describe Rack::OauthProxy::Client do
|
|
21
32
|
SecureRandom.hex(32)
|
22
33
|
end
|
23
34
|
|
35
|
+
let(:response) do
|
36
|
+
{
|
37
|
+
status: status,
|
38
|
+
body: body,
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
let(:status) do
|
43
|
+
200
|
44
|
+
end
|
45
|
+
|
46
|
+
let(:body) do
|
47
|
+
{}.to_json
|
48
|
+
end
|
49
|
+
|
24
50
|
context "#fetch" do
|
25
51
|
context "when authentication succeeded" do
|
26
|
-
before do
|
27
|
-
stub_request(:get, url).to_return(status: 200, body: {}.to_json)
|
28
|
-
end
|
29
|
-
|
30
52
|
it "returns valid access token" do
|
31
53
|
client.fetch(env).should be_a Rack::OauthProxy::AccessTokens::Valid
|
54
|
+
a_request(:get, url).with(headers: { "Authorization" => "Bearer #{token}" }).should have_been_made
|
32
55
|
end
|
33
56
|
end
|
34
57
|
|
35
|
-
context "
|
58
|
+
context "when authentication failed" do
|
36
59
|
before do
|
37
60
|
env.delete("HTTP_AUTHORIZATION")
|
38
61
|
end
|
39
62
|
|
40
63
|
it "returns invalid access token" do
|
41
64
|
client.fetch(env).should be_a Rack::OauthProxy::AccessTokens::Invalid
|
65
|
+
a_request(:get, url).should have_been_made
|
42
66
|
end
|
43
67
|
end
|
44
68
|
|
45
|
-
context "
|
69
|
+
context "with propagated header fields option" do
|
46
70
|
before do
|
47
|
-
|
71
|
+
options[:propagated_header_fields] = ["Dummy"]
|
48
72
|
end
|
49
73
|
|
50
|
-
it "
|
74
|
+
it "propagates specified fields" do
|
51
75
|
client.fetch(env).should be_a Rack::OauthProxy::AccessTokens::Invalid
|
76
|
+
a_request(:get, url).with(headers: { "DUMMY" => "dummy" }).should have_been_made
|
52
77
|
end
|
53
78
|
end
|
54
79
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-oauth_proxy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryo Nakamura
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: faraday_middleware
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rack
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -52,6 +66,20 @@ dependencies:
|
|
52
66
|
- - "~>"
|
53
67
|
- !ruby/object:Gem::Version
|
54
68
|
version: '1.5'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
55
83
|
- !ruby/object:Gem::Dependency
|
56
84
|
name: rake
|
57
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -114,7 +142,6 @@ files:
|
|
114
142
|
- lib/rack/oauth_proxy/access_tokens/valid.rb
|
115
143
|
- lib/rack/oauth_proxy/client.rb
|
116
144
|
- lib/rack/oauth_proxy/client/request.rb
|
117
|
-
- lib/rack/oauth_proxy/client/response.rb
|
118
145
|
- lib/rack/oauth_proxy/version.rb
|
119
146
|
- rack-oauth_proxy.gemspec
|
120
147
|
- spec/rack/oauth_proxy/client_spec.rb
|
@@ -1,42 +0,0 @@
|
|
1
|
-
require "json"
|
2
|
-
|
3
|
-
module Rack
|
4
|
-
class OauthProxy
|
5
|
-
class Client
|
6
|
-
class Response
|
7
|
-
def initialize(raw)
|
8
|
-
@raw = raw
|
9
|
-
end
|
10
|
-
|
11
|
-
def valid_as_access_token?
|
12
|
-
ok? && json? && hash?
|
13
|
-
end
|
14
|
-
|
15
|
-
def to_hash
|
16
|
-
parsed_body
|
17
|
-
end
|
18
|
-
|
19
|
-
private
|
20
|
-
|
21
|
-
def ok?
|
22
|
-
@raw.code == "200"
|
23
|
-
end
|
24
|
-
|
25
|
-
def json?
|
26
|
-
parsed_body
|
27
|
-
true
|
28
|
-
rescue JSON::ParserError
|
29
|
-
false
|
30
|
-
end
|
31
|
-
|
32
|
-
def hash?
|
33
|
-
parsed_body.is_a?(Hash)
|
34
|
-
end
|
35
|
-
|
36
|
-
def parsed_body
|
37
|
-
@parsed_body ||= JSON.parse(@raw.body)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|