oauth2_api_client 3.0.0 → 3.2.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
  SHA256:
3
- metadata.gz: 54c4982a38d552df8c467fcbd0064eba0d1c35e1dd58fffb07a1d8fbe338c8b1
4
- data.tar.gz: 446b18222a7f5bd1259d2482304180ad0a0e73fd811004464a46b265ac946537
3
+ metadata.gz: 61d5d7602f4fd19918ac64b34d46dbf636d149da28605a296a284d48b9f46506
4
+ data.tar.gz: 90febed5c60202e3406988c77fdb1ecb103adbf4b0216b4452c10639f0128ffe
5
5
  SHA512:
6
- metadata.gz: 42adc9e66943d44b12b9c277042dadaa0cc6e17713962cf6feb2ecc623806e1e7ff19e0bc6265191bd33d552175d3490af9855b152ddba5e70e870f27a6ed391
7
- data.tar.gz: 4f6beef17b91cac08e5a2c89d4e5e4cc26a91b90217e82c026e84dec99a99f0211902fdff761dbe801b837c9ae019bc01d959f3883537a7440370fabc2f44d3a
6
+ metadata.gz: a5385487519625a1c65afa4c6c208a779f1373ba76d5695b07338cdcb5bc160ef4141fed85d9ba9364f6c0d5a7f605cc351db4eb7a57bb3d41cbad8cd1343dbd
7
+ data.tar.gz: b63e215e39f98570b2604dc4352505a359e307355755aa11f8ea823953c6f083060072958895c0d847ff082d7ec460338219236b3f4abcc3829ce918ece1de37
@@ -0,0 +1,21 @@
1
+ on: push
2
+ name: test
3
+ jobs:
4
+ test:
5
+ runs-on: ubuntu-latest
6
+ strategy:
7
+ fail-fast: false
8
+ matrix:
9
+ ruby:
10
+ - 2.6
11
+ - 2.7
12
+ - 3.0
13
+ steps:
14
+ - uses: actions/checkout@v1
15
+ - uses: actions/setup-ruby@v1
16
+ with:
17
+ ruby-version: ${{ matrix.ruby }}
18
+ - run: gem install bundler
19
+ - run: bundle
20
+ - run: bundle exec rspec
21
+ - run: bundle exec rubocop
data/.rubocop.yml CHANGED
@@ -1,6 +1,12 @@
1
1
  AllCops:
2
2
  NewCops: enable
3
3
 
4
+ Gemspec/RequiredRubyVersion:
5
+ Enabled: false
6
+
7
+ Gemspec/RequireMFA:
8
+ Enabled: false
9
+
4
10
  Metrics/ParameterLists:
5
11
  Enabled: false
6
12
 
data/CHANGELOG.md CHANGED
@@ -1,6 +1,18 @@
1
1
 
2
2
  # CHANGELOG
3
3
 
4
+ # v3.2.0
5
+
6
+ * Allow passing `nil` as token for unprotected APIs
7
+
8
+ # v3.1.1
9
+
10
+ * Added oauth2 version constraint
11
+
12
+ # v3.1.0
13
+
14
+ * Added uri to `Oauth2ApiClient::ReponseError` exception message
15
+
4
16
  # v3.0.0
5
17
 
6
18
  * [BREAKING] Renamed `Oauth2ApiClient::HttpError` to
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # oauth2_api_client
2
2
 
3
- [![Build Status](https://secure.travis-ci.org/mrkamel/oauth2_api_client.svg?branch=master)](http://travis-ci.org/mrkamel/oauth2_api_client)
3
+ [![Build](https://github.com/mrkamel/oauth2_api_client/workflows/test/badge.svg)](https://github.com/mrkamel/oauth2_api_client/actions?query=workflow%3Atest+branch%3Amaster)
4
4
  [![Gem Version](https://badge.fury.io/rb/oauth2_api_client.svg)](http://badge.fury.io/rb/oauth2_api_client)
5
5
 
6
6
  Oauth2ApiClient is a small, but powerful client around
@@ -12,10 +12,17 @@ oauth2 for authentication.
12
12
  client = Oauth2ApiClient.new(base_url: "https://api.example.com", token "oauth2 token")
13
13
 
14
14
  client.post("/orders", json: { address: "..." }).status.success?
15
- client.headers("User-Agent" => "API Client").timeout(read: 5, write: 5).get("/orders").parse
15
+ client.headers("User-Agent" => "API Client").timeout(read: 5, write: 5).get("/orders").parse(:json)
16
16
  # ...
17
17
  ```
18
18
 
19
+ In case an API is unprotected and you still want to use Oauth2ApiClient, you
20
+ can simply not pass any token:
21
+
22
+ ```ruby
23
+ client = Oauth2ApiClient.new(base_url: "...")
24
+ ```
25
+
19
26
  Oauth2ApiClient is capable of generating oauth2 tokens, when a client id,
20
27
  client secret and oauth token url is given with automatic token caching and
21
28
  renewal on expiry, including retry of the current request.
@@ -65,11 +65,13 @@ class Oauth2ApiClient
65
65
  attr_reader :response
66
66
 
67
67
  def initialize(response)
68
+ super()
69
+
68
70
  @response = response
69
71
  end
70
72
 
71
73
  def to_s
72
- "#{self.class.name} (#{response.code}): #{response.body}"
74
+ "#{self.class.name} (#{response.code}, #{response.uri}): #{response.body}"
73
75
  end
74
76
 
75
77
  # @api private
@@ -1,3 +1,3 @@
1
1
  class Oauth2ApiClient
2
- VERSION = "3.0.0"
2
+ VERSION = "3.2.0"
3
3
  end
@@ -1,7 +1,6 @@
1
1
  require "ruby2_keywords"
2
2
  require "oauth2"
3
3
  require "http"
4
- require "ruby2_keywords"
5
4
  require "active_support"
6
5
 
7
6
  require "oauth2_api_client/version"
@@ -28,7 +27,7 @@ class Oauth2ApiClient
28
27
  # )
29
28
  #
30
29
  # client.post("/orders", json: { address: "..." }).status.success?
31
- # client.headers("User-Agent" => "API Client").timeout(read: 5, write: 5).get("/orders").parse
30
+ # client.headers("User-Agent" => "API Client").timeout(read: 5, write: 5).get("/orders").parse(:json)
32
31
  #
33
32
  # @example
34
33
  # client = Oauth2ApiClient.new(
@@ -41,7 +40,7 @@ class Oauth2ApiClient
41
40
  # )
42
41
  # )
43
42
 
44
- def initialize(base_url:, base_request: HTTP, token:)
43
+ def initialize(base_url:, token: nil, base_request: HTTP)
45
44
  @base_url = base_url
46
45
  @token = token
47
46
  @request = base_request
@@ -52,6 +51,8 @@ class Oauth2ApiClient
52
51
  # @return [String] The token
53
52
 
54
53
  def token
54
+ return if @token.nil?
55
+
55
56
  @token.respond_to?(:to_str) ? @token.to_str : @token.token
56
57
  end
57
58
 
@@ -75,7 +76,10 @@ class Oauth2ApiClient
75
76
 
76
77
  def execute(verb, path, options = {})
77
78
  with_retry do
78
- response = @request.auth("Bearer #{token}").send(verb, "#{@base_url}#{path}", options)
79
+ request = @request
80
+ request = request.auth("Bearer #{token}") if token
81
+
82
+ response = request.send(verb, "#{@base_url}#{path}", options)
79
83
 
80
84
  return response if response.status.success?
81
85
 
@@ -15,7 +15,6 @@ Gem::Specification.new do |spec|
15
15
 
16
16
  spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
18
  spec.require_paths = ["lib"]
20
19
 
21
20
  spec.add_development_dependency "bundler"
@@ -26,6 +25,6 @@ Gem::Specification.new do |spec|
26
25
 
27
26
  spec.add_dependency "activesupport"
28
27
  spec.add_dependency "http"
29
- spec.add_dependency "oauth2"
28
+ spec.add_dependency "oauth2", ">= 1.4.2"
30
29
  spec.add_dependency "ruby2_keywords"
31
30
  end
@@ -3,9 +3,9 @@ require File.expand_path("../spec_helper", __dir__)
3
3
  RSpec.describe Oauth2ApiClient::ResponseError do
4
4
  describe "#to_s" do
5
5
  it "returns the message" do
6
- response = double(code: 401, body: "unauthorized")
6
+ response = double(code: 401, body: "unauthorized", uri: "http://example.com/")
7
7
 
8
- expect(described_class.new(response).to_s).to eq("Oauth2ApiClient::ResponseError (401): unauthorized")
8
+ expect(described_class.new(response).to_s).to eq("Oauth2ApiClient::ResponseError (401, http://example.com/): unauthorized")
9
9
  end
10
10
  end
11
11
 
@@ -84,7 +84,7 @@ RSpec.describe Oauth2ApiClient do
84
84
  expect(client.get("/path", params: { key: "value" }).to_s).to eq("ok")
85
85
  end
86
86
 
87
- it "passes the token in the authentication header" do
87
+ it "passes the token in the authorization header" do
88
88
  stub_request(:get, "http://localhost/api/path")
89
89
  .with(headers: { "Authorization" => "Bearer access_token" })
90
90
  .to_return(status: 200, body: "ok", headers: {})
@@ -94,6 +94,16 @@ RSpec.describe Oauth2ApiClient do
94
94
  expect(client.get("/path").to_s).to eq("ok")
95
95
  end
96
96
 
97
+ it "does not pass any authorization header when no token is provided" do
98
+ stub_request(:get, "http://localhost/api/path")
99
+ .with { |request| !request.headers.keys.map(&:to_s).map(&:downcase).include?("authorization") }
100
+ .to_return(status: 200, body: "ok", headers: {})
101
+
102
+ client = described_class.new(base_url: "http://localhost/api")
103
+
104
+ expect(client.get("/path").to_s).to eq("ok")
105
+ end
106
+
97
107
  it "retries the request when an http unauthorized status is returned" do
98
108
  stub_request(:get, "http://localhost/api/path")
99
109
  .to_return({ status: 401, body: "unauthorized" }, { status: 200, body: "ok" })
@@ -123,7 +133,7 @@ RSpec.describe Oauth2ApiClient do
123
133
  )
124
134
  )
125
135
 
126
- expect { client.get("/path") }.to raise_error("Oauth2ApiClient::ResponseError::Unauthorized (401): unauthorized")
136
+ expect { client.get("/path") }.to raise_error("Oauth2ApiClient::ResponseError::Unauthorized (401, http://localhost/api/path): unauthorized")
127
137
  end
128
138
  end
129
139
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oauth2_api_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Vetter
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-23 00:00:00.000000000 Z
11
+ date: 2022-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -114,14 +114,14 @@ dependencies:
114
114
  requirements:
115
115
  - - ">="
116
116
  - !ruby/object:Gem::Version
117
- version: '0'
117
+ version: 1.4.2
118
118
  type: :runtime
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
- version: '0'
124
+ version: 1.4.2
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: ruby2_keywords
127
127
  requirement: !ruby/object:Gem::Requirement
@@ -144,9 +144,9 @@ executables: []
144
144
  extensions: []
145
145
  extra_rdoc_files: []
146
146
  files:
147
+ - ".github/workflows/test.yml"
147
148
  - ".gitignore"
148
149
  - ".rubocop.yml"
149
- - ".travis.yml"
150
150
  - CHANGELOG.md
151
151
  - Gemfile
152
152
  - LICENSE.txt
@@ -165,7 +165,7 @@ homepage: https://github.com/mrkamel/oauth2_api_client
165
165
  licenses:
166
166
  - MIT
167
167
  metadata: {}
168
- post_install_message:
168
+ post_install_message:
169
169
  rdoc_options: []
170
170
  require_paths:
171
171
  - lib
@@ -180,12 +180,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
180
180
  - !ruby/object:Gem::Version
181
181
  version: '0'
182
182
  requirements: []
183
- rubygems_version: 3.0.3
184
- signing_key:
183
+ rubygems_version: 3.3.3
184
+ signing_key:
185
185
  specification_version: 4
186
186
  summary: Small but powerful client around oauth2 and http-rb to interact with APIs
187
- test_files:
188
- - spec/oauth2_api_client/response_error_spec.rb
189
- - spec/oauth2_api_client/token_provider_spec.rb
190
- - spec/oauth2_api_client_spec.rb
191
- - spec/spec_helper.rb
187
+ test_files: []
data/.travis.yml DELETED
@@ -1,10 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- rvm:
4
- - ruby-2.6.2
5
- - ruby-2.7.1
6
- install:
7
- - travis_retry bundle install
8
- script:
9
- - bundle exec rspec
10
- - bundle exec rubocop