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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b1ed93602b7c511210f8a959e0ff4460bb27bc30
4
- data.tar.gz: 0fa3909de26d926aed9852cac86e0edd73443e54
3
+ metadata.gz: 6c33bd03642050ec16695d9edd4eb0d128c1b26d
4
+ data.tar.gz: 18abbeca292daa9d059f18cfa3b13d86d86b04f8
5
5
  SHA512:
6
- metadata.gz: c51c3a74817b05ca080bb42657da82bc217580f1fe6b7c2f03eb7ea4ffcfac2965708033ce1663c84e712064debcfe8338ade1039e0bdc5b6da6a9a984683475
7
- data.tar.gz: 4f1a0d043ebc144ae09c83f7cee454c8c24e480cabfee93ccbe4c066cf206ccccf51c562301d11dff4024af00c35fabecbeed4171345f7b39ebfddd585f42c8d
6
+ metadata.gz: 2834913040a3706d41932e707744bbddec7bc2d522364fe0f8656c8c0c5ef93d12cce00adf69523b3495dc83700dde5d80e6a17125cba5a1a1232872563be432
7
+ data.tar.gz: b037d89e8c295df21a9c4cc88ccf02dd9807f01442c979d13c2f69784a65735d0fb81ccc3dcfff72b61ca8e06f5a7fb9a628e2b65c62b15884838ccd36eccd06
@@ -1,3 +1,7 @@
1
+ ## 0.1.0
2
+ * Add `:propagated_header_fields` option
3
+ * Use fadaday
4
+
1
5
  ## 0.0.3
2
6
  * Fix :bug: of `?access_token=...` handling
3
7
 
@@ -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 "net/http"
1
+ require "faraday"
2
+ require "faraday_middleware"
2
3
 
3
4
  module Rack
4
5
  class OauthProxy
5
6
  class Client
6
- READ_TIMEOUT = 1
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
- if request.has_any_valid_credentials?
16
- path = "#{uri.path}"
17
- path << "?#{request.to_query}" if request.to_query.present?
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 uri
41
- @uri ||= URI.parse(url)
42
- end
43
-
44
- def http_client
45
- client = Net::HTTP.new(uri.host, uri.port)
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
- @options[:url] or raise NoUrlError
32
+ options[:url] or raise NoUrlError
54
33
  end
55
34
 
56
35
  def host
57
- @options[:host]
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 "active_support/core_ext/object/blank"
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
- def initialize(env)
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 has_any_valid_credentials?
15
- authorization.present? ||
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 rack_request
21
- @rack_request ||= Rack::Request.new(@env)
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 to_query
25
- rack_request.params.slice("access_token", "bearer_token").to_query
28
+ def params
29
+ rack_request.params.slice("access_token", "bearer_token")
26
30
  end
27
31
 
28
- def authorization
29
- @env["HTTP_AUTHORIZATION"]
30
- end
32
+ private
31
33
 
32
- def resource_owner_id
33
- @env["HTTP_RESOURCE_OWNER_ID"]
34
+ def rack_request
35
+ @rack_request ||= Rack::Request.new(@env)
34
36
  end
35
37
 
36
- def scopes
37
- @env["HTTP_SCOPES"]
38
+ def propagated_header_fields
39
+ options[:propagated_header_fields] || DEFAULT_PROPAGATED_HEADER_FIELDS
38
40
  end
39
41
  end
40
42
  end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class OauthProxy
3
- VERSION = "0.0.3"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -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(url: url)
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 "without no credentials in request" do
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 "when authentication failed" do
69
+ context "with propagated header fields option" do
46
70
  before do
47
- stub_request(:get, url).to_return(status: 401, body: {}.to_json)
71
+ options[:propagated_header_fields] = ["Dummy"]
48
72
  end
49
73
 
50
- it "returns invalid access token" do
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.3
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-18 00:00:00.000000000 Z
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