faraday-oauth2_cached_token 0.1.0
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.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +43 -0
- data/Rakefile +17 -0
- data/lib/faraday-oauth2_cached_token.rb +1 -0
- data/lib/faraday/oauth2_cached_token.rb +41 -0
- data/lib/faraday/oauth2_cached_token/provider.rb +38 -0
- data/lib/faraday/oauth2_cached_token/token.rb +22 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6a19677b5fa6ded3f7beeec7c91f42de93769722
|
4
|
+
data.tar.gz: 63d73cc211658cf22a110a6b56a0f7ac9951e96b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b7bb316d73590a5fd8aa3fa7761d4ff64d5bafbd6f3b948bfa29b74351d45e81a85ed9af146b69ebb0cc0963e035666b6a8f182d29997a409779fb7a23ceabef
|
7
|
+
data.tar.gz: e7f7d352e11e837dba45102a57f66520176268384cc8545d67077f53e012aee7bbe235c08bae95218fbd03e4f6b909c1e9c88ddb21057257aea5dd2c5ae08b1a
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2016 Globo.com
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# OAuth2CachedToken middleware
|
2
|
+
|
3
|
+
Faraday middleware that caches and refreshes OAuth2 tokens as needed
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'faraday-oauth2_cached_token'
|
11
|
+
```
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
conn = Faraday.new(:url => 'http://sushi.com') do |faraday|
|
17
|
+
# You can pass the provider options
|
18
|
+
faraday.request :oauth2_cached_token, provider_options: {
|
19
|
+
id: 'client_id',
|
20
|
+
secret: 'secret',
|
21
|
+
options: {
|
22
|
+
site: 'http://sushi.com',
|
23
|
+
token_url: '/token'
|
24
|
+
}
|
25
|
+
}
|
26
|
+
|
27
|
+
# Or you can construct a provider yourself
|
28
|
+
faraday.request :oauth2_cached_token, provider: Faraday::OAuth2CachedToken::Provider.new({
|
29
|
+
id: 'client_id',
|
30
|
+
secret: 'secret',
|
31
|
+
options: {
|
32
|
+
site: 'http://sushi.com',
|
33
|
+
token_url: '/token'
|
34
|
+
}
|
35
|
+
})
|
36
|
+
|
37
|
+
faraday.adapter Faraday.default_adapter
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
## License
|
42
|
+
|
43
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'Faraday::OAuth2CachedToken'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'faraday/oauth2_cached_token'
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
require 'faraday/oauth2_cached_token/provider'
|
4
|
+
|
5
|
+
module Faraday
|
6
|
+
class OAuth2CachedToken < Middleware
|
7
|
+
INVALID_TOKEN_STATUS = 401
|
8
|
+
|
9
|
+
attr_reader :provider
|
10
|
+
|
11
|
+
def initialize(app, options = {})
|
12
|
+
super(app)
|
13
|
+
@provider = options[:provider] || Provider.new(options[:provider_options] || {})
|
14
|
+
@logger = options[:logger]
|
15
|
+
end
|
16
|
+
|
17
|
+
def call(request_env)
|
18
|
+
inject_header(request_env, @provider.get_token.headers)
|
19
|
+
|
20
|
+
@app.call(request_env).on_complete do |response_env|
|
21
|
+
on_complete(request_env, response_env)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def on_complete(request_env, response_env)
|
28
|
+
if response_env.status == INVALID_TOKEN_STATUS
|
29
|
+
@logger.try(:warn, 'Invalid Token. refreshing and retrying request...')
|
30
|
+
inject_header(request_env, @provider.get_fresh_token.headers)
|
31
|
+
@app.call(request_env)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def inject_header(request_env, header)
|
36
|
+
request_env[:request_headers].merge!(header)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
Faraday::Request.register_middleware oauth2_cached_token: Faraday::OAuth2CachedToken
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'oauth2/client'
|
3
|
+
require 'active_support'
|
4
|
+
|
5
|
+
require 'faraday/oauth2_cached_token/token'
|
6
|
+
|
7
|
+
module Faraday
|
8
|
+
class OAuth2CachedToken < Middleware
|
9
|
+
class Provider
|
10
|
+
def initialize(options)
|
11
|
+
@options = options
|
12
|
+
@cache = options[:store] || ActiveSupport::Cache::MemoryStore.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def get_fresh_token
|
16
|
+
token = Token.from_access_token(oauth2_client.client_credentials.get_token)
|
17
|
+
@cache.write(cache_key, token, expires_in: token.expires_in)
|
18
|
+
token
|
19
|
+
end
|
20
|
+
|
21
|
+
def get_token
|
22
|
+
@cache.read(cache_key) || get_fresh_token
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def cache_key
|
28
|
+
"oauth2_cached_token_#{@options[:id]}"
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def oauth2_client
|
34
|
+
OAuth2::Client.new(@options[:id], @options[:secret], @options[:options])
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module Faraday
|
4
|
+
class OAuth2CachedToken < Middleware
|
5
|
+
class Token
|
6
|
+
attr_reader :expires_in
|
7
|
+
|
8
|
+
def initialize(token, expires_in)
|
9
|
+
@token = token
|
10
|
+
@expires_in = expires_in
|
11
|
+
end
|
12
|
+
|
13
|
+
def headers
|
14
|
+
{'Authorization' => "Bearer #{@token}"}
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.from_access_token(access_token)
|
18
|
+
new(access_token.token, access_token.expires_in)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: faraday-oauth2_cached_token
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rodrigo Willrich
|
8
|
+
- Jean Carlo Emer
|
9
|
+
- Pablo Machado
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2016-07-13 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: faraday
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
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
|
+
requirements:
|
26
|
+
- - "~>"
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '0'
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: oauth2
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - "~>"
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
type: :runtime
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - "~>"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
name: activesupport
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
type: :runtime
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - "~>"
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
description: Faraday middleware that caches and refreshes OAuth2 tokens as needed
|
58
|
+
email:
|
59
|
+
- rodrigo.willrich@corp.globo.com
|
60
|
+
- jean.emer@corp.globo.com
|
61
|
+
- pablo.machado@corp.globo.com
|
62
|
+
executables: []
|
63
|
+
extensions: []
|
64
|
+
extra_rdoc_files: []
|
65
|
+
files:
|
66
|
+
- MIT-LICENSE
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- lib/faraday-oauth2_cached_token.rb
|
70
|
+
- lib/faraday/oauth2_cached_token.rb
|
71
|
+
- lib/faraday/oauth2_cached_token/provider.rb
|
72
|
+
- lib/faraday/oauth2_cached_token/token.rb
|
73
|
+
homepage: http://github.com/globocom/faraday-oauth2_cached_token
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.5.1
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Middleware to handle OAuth2 token caching
|
97
|
+
test_files: []
|