faraday_middleware-oauth2_refresh 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +4 -0
- data/LICENSE.txt +20 -0
- data/README.md +53 -0
- data/Rakefile +19 -0
- data/VERSION.txt +1 -0
- data/lib/faraday_middleware-oauth2_refresh.rb +1 -0
- data/lib/faraday_middleware/oauth2_refresh.rb +38 -0
- data/test/test_setup.rb +18 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 75da60b7dc91240b31f66f29485c5f5ff4bba802
|
4
|
+
data.tar.gz: 6ee13c0e2e6e021cea753fa34263a387d874b8f7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ed3a230f557a09efc3837c296e4cddcd87863a68e876d436528ef62d29288d6c4d8b4063133e3b52bfe5b39182d5ac2f5499f210b41e44d1c4ea7ee062c25a06
|
7
|
+
data.tar.gz: 7fbb42a498997098e567b619f43c875a3929070b5a95bccfbefad9868c2c8c5afbc30d957f5663123b40493435361a5f05625f49b382aea73a88b02c3c30c3fa
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2015 John Wang
|
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,53 @@
|
|
1
|
+
# FaradayMiddleware::OAuth2Refresh
|
2
|
+
|
3
|
+
Faraday middleware to manage OAuth token authorization with token refresh.
|
4
|
+
|
5
|
+
## Description
|
6
|
+
|
7
|
+
This gem is a piece of Faraday middleware that adds OAuth token handling using the [oauth2 gem](https://github.com/intridea/oauth2).
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'faraday_middleware-oauth2_refresh'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
```sh
|
20
|
+
$ bundle
|
21
|
+
```
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
```sh
|
26
|
+
$ gem install faraday_middleware-oauth2_refresh
|
27
|
+
```
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
require 'oauth2'
|
33
|
+
require 'faraday_middleware/oauth2_refresh'
|
34
|
+
|
35
|
+
client = OAuth2::Client.new('my_client_id', 'my_client_secret', :site => 'https://example.com' )
|
36
|
+
token = client.password.get_token('username', 'password', { :headers => { 'Authorization' => 'Basic my_api_key' } })
|
37
|
+
|
38
|
+
conn = Faraday.new(:url => "http://example.com") do |builder|
|
39
|
+
builder.request :oauth2_refresh, token
|
40
|
+
builder.adapter Faraday.default_adapter
|
41
|
+
end
|
42
|
+
|
43
|
+
conn.get "/foo" # sends token
|
44
|
+
```
|
45
|
+
|
46
|
+
## Change Log
|
47
|
+
|
48
|
+
- **2015-05-30**: 0.0.1
|
49
|
+
- Initial public release
|
50
|
+
|
51
|
+
## Problems, Comments, Suggestions?
|
52
|
+
|
53
|
+
All of the above are most welcome on Github.
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
desc 'Default: run unit tests.'
|
5
|
+
task :default => :test
|
6
|
+
|
7
|
+
desc 'Test the library.'
|
8
|
+
Rake::TestTask.new do |t|
|
9
|
+
t.libs << 'lib'
|
10
|
+
t.pattern = 'test/**/test_*.rb'
|
11
|
+
t.verbose = false
|
12
|
+
end
|
13
|
+
|
14
|
+
desc 'Generate YARD documentation.'
|
15
|
+
task :gendoc do
|
16
|
+
# puts 'yard doc generation disabled until JRuby build native extensions for redcarpet or yard removes the dependency.'
|
17
|
+
system "yardoc"
|
18
|
+
system "yard stats --list-undoc"
|
19
|
+
end
|
data/VERSION.txt
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'faraday_middleware/oauth2_refresh'
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require 'faraday'
|
3
|
+
require 'forwardable'
|
4
|
+
|
5
|
+
module FaradayMiddleware
|
6
|
+
class OAuth2Refresh < Faraday::Middleware
|
7
|
+
AUTH_HEADER = 'Authorization'.freeze
|
8
|
+
|
9
|
+
attr_reader :oauth2_token
|
10
|
+
|
11
|
+
extend Forwardable
|
12
|
+
|
13
|
+
def call(env)
|
14
|
+
if @oauth2_token.expired?
|
15
|
+
@oauth2_token = @oauth2_token.refresh!({:headers => {'Authorization' => 'Basic ' + get_api_key()}})
|
16
|
+
end
|
17
|
+
|
18
|
+
if @oauth2_token.token.to_s.length>0
|
19
|
+
env[:request_headers][AUTH_HEADER] = %(Bearer #{@oauth2_token.token.to_s})
|
20
|
+
end
|
21
|
+
|
22
|
+
@app.call env
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_api_key()
|
26
|
+
api_key = Base64::encode64("#{@oauth2_token.client.id.to_s}:#{@oauth2_token.client.secret.to_s}").gsub(/[\s\t\r\n]/,'')
|
27
|
+
return api_key
|
28
|
+
end
|
29
|
+
|
30
|
+
def initialize(app=nil, token=nil, options={})
|
31
|
+
super(app)
|
32
|
+
@oauth2_token = token
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
Faraday::Request.register_middleware :oauth2_refresh => FaradayMiddleware::OAuth2Refresh
|
data/test/test_setup.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'faraday'
|
3
|
+
require 'faraday_middleware'
|
4
|
+
require 'faraday_middleware-oauth2_refresh'
|
5
|
+
|
6
|
+
class OAuth2Test < Test::Unit::TestCase
|
7
|
+
def testSetup
|
8
|
+
|
9
|
+
conn = Faraday.new 'http://example.com/api' do |conn|
|
10
|
+
conn.request :oauth2_refresh
|
11
|
+
end
|
12
|
+
|
13
|
+
ref = FaradayMiddleware::OAuth2Refresh.new
|
14
|
+
|
15
|
+
assert_equal 'FaradayMiddleware::OAuth2Refresh', ref.class.name
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: faraday_middleware-oauth2_refresh
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Wang
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.9'
|
20
|
+
- - '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0.9'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0.9'
|
30
|
+
- - '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0.9'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: faraday_middleware
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ~>
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
- - '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
type: :runtime
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ~>
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
- - '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: forwardable
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
type: :runtime
|
61
|
+
prerelease: false
|
62
|
+
version_requirements: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
description: This Faraday middleware gem adds OAuth2 token handling with token refresh
|
68
|
+
email: johncwang@gmail.com
|
69
|
+
executables: []
|
70
|
+
extensions: []
|
71
|
+
extra_rdoc_files: []
|
72
|
+
files:
|
73
|
+
- CHANGELOG.md
|
74
|
+
- LICENSE.txt
|
75
|
+
- README.md
|
76
|
+
- Rakefile
|
77
|
+
- VERSION.txt
|
78
|
+
- lib/faraday_middleware-oauth2_refresh.rb
|
79
|
+
- lib/faraday_middleware/oauth2_refresh.rb
|
80
|
+
- test/test_setup.rb
|
81
|
+
homepage: https://github.com/grokify/
|
82
|
+
licenses: []
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.1.5
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: Faraday OAuth2 request middleware with token refresh
|
104
|
+
test_files: []
|