oauth2-aptible 0.9.4.aptible
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/CONTRIBUTING.md +18 -0
- data/LICENSE.md +20 -0
- data/README.md +137 -0
- data/Rakefile +39 -0
- data/lib/oauth2.rb +10 -0
- data/lib/oauth2/access_token.rb +173 -0
- data/lib/oauth2/client.rb +173 -0
- data/lib/oauth2/error.rb +24 -0
- data/lib/oauth2/response.rb +90 -0
- data/lib/oauth2/strategy/assertion.rb +72 -0
- data/lib/oauth2/strategy/auth_code.rb +33 -0
- data/lib/oauth2/strategy/base.rb +16 -0
- data/lib/oauth2/strategy/client_credentials.rb +36 -0
- data/lib/oauth2/strategy/implicit.rb +29 -0
- data/lib/oauth2/strategy/password.rb +27 -0
- data/lib/oauth2/version.rb +15 -0
- data/oauth2.gemspec +27 -0
- data/spec/helper.rb +29 -0
- data/spec/oauth2/access_token_spec.rb +172 -0
- data/spec/oauth2/client_spec.rb +205 -0
- data/spec/oauth2/response_spec.rb +101 -0
- data/spec/oauth2/strategy/assertion_spec.rb +56 -0
- data/spec/oauth2/strategy/auth_code_spec.rb +88 -0
- data/spec/oauth2/strategy/base_spec.rb +7 -0
- data/spec/oauth2/strategy/client_credentials_spec.rb +81 -0
- data/spec/oauth2/strategy/implicit_spec.rb +28 -0
- data/spec/oauth2/strategy/password_spec.rb +57 -0
- metadata +174 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 98ebc222ea2c9839e4e08a9064625ec9f05d86bb
|
4
|
+
data.tar.gz: 3f08dafc2ef55a43b89c22c5dcc4793e5ad65bb3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b283b2542f6aa1aea41e6a5ccf7cff7c87d73296dce247f58292147f5b6c4d0aa0c41930e36e43c192996a596d6df428ccad13917ad17b8816c275a8bebbcabf
|
7
|
+
data.tar.gz: a4aaa784dc71bc350bfe7ee05c90157f6ca9404db3a37f2ac1efb7f6977c71b795a3cdfafe4618f9a1a8acbd145a200123e140d50cb73abc7b356e963738c36f
|
data/.document
ADDED
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
## Submitting a Pull Request
|
2
|
+
1. [Fork the repository.][fork]
|
3
|
+
2. [Create a topic branch.][branch]
|
4
|
+
3. Add specs for your unimplemented feature or bug fix.
|
5
|
+
4. Run `bundle exec rake spec`. If your specs pass, return to step 3.
|
6
|
+
5. Implement your feature or bug fix.
|
7
|
+
6. Run `bundle exec rake`. If your specs fail, return to step 5.
|
8
|
+
7. Run `open coverage/index.html`. If your changes are not completely covered
|
9
|
+
by your tests, return to step 3.
|
10
|
+
8. Add documentation for your feature or bug fix.
|
11
|
+
9. Run `bundle exec rake verify_measurements`. If your changes are not 100%
|
12
|
+
documented, go back to step 8.
|
13
|
+
10. Commit and push your changes.
|
14
|
+
11. [Submit a pull request.][pr]
|
15
|
+
|
16
|
+
[fork]: http://help.github.com/fork-a-repo/
|
17
|
+
[branch]: http://learn.github.com/p/branching.html
|
18
|
+
[pr]: http://help.github.com/send-pull-requests/
|
data/LICENSE.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011-2013 Michael Bleigh and Intridea, Inc.
|
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,137 @@
|
|
1
|
+
# OAuth2
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/oauth2.png)][gem]
|
4
|
+
[![Build Status](https://secure.travis-ci.org/intridea/oauth2.png?branch=master)][travis]
|
5
|
+
[![Dependency Status](https://gemnasium.com/intridea/oauth2.png?travis)][gemnasium]
|
6
|
+
[![Code Climate](https://codeclimate.com/github/intridea/oauth2.png)][codeclimate]
|
7
|
+
[![Coverage Status](https://coveralls.io/repos/intridea/oauth2/badge.png?branch=master)][coveralls]
|
8
|
+
|
9
|
+
[gem]: https://rubygems.org/gems/oauth2
|
10
|
+
[travis]: http://travis-ci.org/intridea/oauth2
|
11
|
+
[gemnasium]: https://gemnasium.com/intridea/oauth2
|
12
|
+
[codeclimate]: https://codeclimate.com/github/intridea/oauth2
|
13
|
+
[coveralls]: https://coveralls.io/r/intridea/oauth2
|
14
|
+
|
15
|
+
A Ruby wrapper for the OAuth 2.0 specification. This is a work in progress,
|
16
|
+
being built first to solve the pragmatic process of connecting to existing
|
17
|
+
OAuth 2.0 endpoints (e.g. Facebook) with the goal of building it up to meet
|
18
|
+
the entire specification over time.
|
19
|
+
|
20
|
+
## Installation
|
21
|
+
gem install oauth2
|
22
|
+
|
23
|
+
## Resources
|
24
|
+
* [View Source on GitHub][code]
|
25
|
+
* [Report Issues on GitHub][issues]
|
26
|
+
* [Read More at the Wiki][wiki]
|
27
|
+
|
28
|
+
[code]: https://github.com/intridea/oauth2
|
29
|
+
[issues]: https://github.com/intridea/oauth2/issues
|
30
|
+
[wiki]: https://wiki.github.com/intridea/oauth2
|
31
|
+
|
32
|
+
## Usage Examples
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
require 'oauth2'
|
36
|
+
client = OAuth2::Client.new('client_id', 'client_secret', :site => 'https://example.org')
|
37
|
+
|
38
|
+
client.auth_code.authorize_url(:redirect_uri => 'http://localhost:8080/oauth2/callback')
|
39
|
+
# => "https://example.org/oauth/authorization?response_type=code&client_id=client_id&redirect_uri=http://localhost:8080/oauth2/callback"
|
40
|
+
|
41
|
+
token = client.auth_code.get_token('authorization_code_value', :redirect_uri => 'http://localhost:8080/oauth2/callback', :headers => {'Authorization' => 'Basic some_password'})
|
42
|
+
response = token.get('/api/resource', :params => { 'query_foo' => 'bar' })
|
43
|
+
response.class.name
|
44
|
+
# => OAuth2::Response
|
45
|
+
```
|
46
|
+
## OAuth2::Response
|
47
|
+
The AccessToken methods #get, #post, #put and #delete and the generic #request
|
48
|
+
will return an instance of the #OAuth2::Response class.
|
49
|
+
|
50
|
+
This instance contains a #parsed method that will parse the response body and
|
51
|
+
return a Hash if the Content-Type is application/x-www-form-urlencoded or if
|
52
|
+
the body is a JSON object. It will return an Array if the body is a JSON
|
53
|
+
array. Otherwise, it will return the original body string.
|
54
|
+
|
55
|
+
The original response body, headers, and status can be accessed via their
|
56
|
+
respective methods.
|
57
|
+
|
58
|
+
## OAuth2::AccessToken
|
59
|
+
If you have an existing Access Token for a user, you can initialize an instance
|
60
|
+
using various class methods including the standard new, from_hash (if you have
|
61
|
+
a hash of the values), or from_kvform (if you have an
|
62
|
+
application/x-www-form-urlencoded encoded string of the values).
|
63
|
+
|
64
|
+
## OAuth2::Error
|
65
|
+
On 400+ status code responses, an OAuth2::Error will be raised. If it is a
|
66
|
+
standard OAuth2 error response, the body will be parsed and #code and #description will contain the values provided from the error and
|
67
|
+
error_description parameters. The #response property of OAuth2::Error will
|
68
|
+
always contain the OAuth2::Response instance.
|
69
|
+
|
70
|
+
If you do not want an error to be raised, you may use :raise_errors => false
|
71
|
+
option on initialization of the client. In this case the OAuth2::Response
|
72
|
+
instance will be returned as usual and on 400+ status code responses, the
|
73
|
+
Response instance will contain the OAuth2::Error instance.
|
74
|
+
|
75
|
+
## Authorization Grants
|
76
|
+
Currently the Authorization Code, Implicit, Resource Owner Password Credentials, Client Credentials, and Assertion
|
77
|
+
authentication grant types have helper strategy classes that simplify client
|
78
|
+
use. They are available via the #auth_code, #implicit, #password, #client_credentials, and #assertion methods respectively.
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
auth_url = client.auth_code.authorize_url(:redirect_uri => 'http://localhost:8080/oauth/callback')
|
82
|
+
token = client.auth_code.get_token('code_value', :redirect_uri => 'http://localhost:8080/oauth/callback')
|
83
|
+
|
84
|
+
auth_url = client.implicit.authorize_url(:redirect_uri => 'http://localhost:8080/oauth/callback')
|
85
|
+
# get the token params in the callback and
|
86
|
+
token = OAuth2::AccessToken.from_kvform(client, query_string)
|
87
|
+
|
88
|
+
token = client.password.get_token('username', 'password')
|
89
|
+
|
90
|
+
token = client.client_credentials.get_token
|
91
|
+
|
92
|
+
token = client.assertion.get_token(assertion_params)
|
93
|
+
```
|
94
|
+
|
95
|
+
If you want to specify additional headers to be sent out with the
|
96
|
+
request, add a 'headers' hash under 'params':
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
token = client.auth_code.get_token('code_value', :redirect_uri => 'http://localhost:8080/oauth/callback', :headers => {'Some' => 'Header'})
|
100
|
+
```
|
101
|
+
|
102
|
+
You can always use the #request method on the OAuth2::Client instance to make
|
103
|
+
requests for tokens for any Authentication grant type.
|
104
|
+
|
105
|
+
## Supported Ruby Versions
|
106
|
+
This library aims to support and is [tested against][travis] the following Ruby
|
107
|
+
implementations:
|
108
|
+
|
109
|
+
* Ruby 1.8.7
|
110
|
+
* Ruby 1.9.2
|
111
|
+
* Ruby 1.9.3
|
112
|
+
* Ruby 2.0.0
|
113
|
+
* Ruby 2.1.0
|
114
|
+
* [JRuby][]
|
115
|
+
* [Rubinius][]
|
116
|
+
|
117
|
+
[jruby]: http://jruby.org/
|
118
|
+
[rubinius]: http://rubini.us/
|
119
|
+
|
120
|
+
If something doesn't work on one of these interpreters, it's a bug.
|
121
|
+
|
122
|
+
This library may inadvertently work (or seem to work) on other Ruby
|
123
|
+
implementations, however support will only be provided for the versions listed
|
124
|
+
above.
|
125
|
+
|
126
|
+
If you would like this library to support another Ruby version, you may
|
127
|
+
volunteer to be a maintainer. Being a maintainer entails making sure all tests
|
128
|
+
run and pass on that implementation. When something breaks on your
|
129
|
+
implementation, you will be responsible for providing patches in a timely
|
130
|
+
fashion. If critical issues for a particular implementation exist at the time
|
131
|
+
of a major release, support for that Ruby version may be dropped.
|
132
|
+
|
133
|
+
## License
|
134
|
+
Copyright (c) 2011-2013 Michael Bleigh and Intridea, Inc. See [LICENSE][] for
|
135
|
+
details.
|
136
|
+
|
137
|
+
[license]: LICENSE.md
|
data/Rakefile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
RSpec::Core::RakeTask.new(:spec)
|
6
|
+
|
7
|
+
task :test => :spec
|
8
|
+
|
9
|
+
namespace :doc do
|
10
|
+
require 'rdoc/task'
|
11
|
+
require File.expand_path('../lib/oauth2/version', __FILE__)
|
12
|
+
RDoc::Task.new do |rdoc|
|
13
|
+
rdoc.rdoc_dir = 'rdoc'
|
14
|
+
rdoc.title = "oauth2 #{OAuth2::Version}"
|
15
|
+
rdoc.main = 'README.md'
|
16
|
+
rdoc.rdoc_files.include('README.md', 'LICENSE.md', 'lib/**/*.rb')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
begin
|
21
|
+
require 'rubocop/rake_task'
|
22
|
+
Rubocop::RakeTask.new
|
23
|
+
rescue LoadError
|
24
|
+
task :rubocop do
|
25
|
+
$stderr.puts 'Rubocop is disabled'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
require 'yardstick/rake/measurement'
|
30
|
+
Yardstick::Rake::Measurement.new do |measurement|
|
31
|
+
measurement.output = 'measurement/report.txt'
|
32
|
+
end
|
33
|
+
|
34
|
+
require 'yardstick/rake/verify'
|
35
|
+
Yardstick::Rake::Verify.new do |verify|
|
36
|
+
verify.threshold = 58.9
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => [:spec, :rubocop, :verify_measurements]
|
data/lib/oauth2.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'oauth2/error'
|
2
|
+
require 'oauth2/client'
|
3
|
+
require 'oauth2/strategy/base'
|
4
|
+
require 'oauth2/strategy/auth_code'
|
5
|
+
require 'oauth2/strategy/implicit'
|
6
|
+
require 'oauth2/strategy/password'
|
7
|
+
require 'oauth2/strategy/client_credentials'
|
8
|
+
require 'oauth2/strategy/assertion'
|
9
|
+
require 'oauth2/access_token'
|
10
|
+
require 'oauth2/response'
|
@@ -0,0 +1,173 @@
|
|
1
|
+
module OAuth2
|
2
|
+
class AccessToken
|
3
|
+
attr_reader :client, :token, :expires_in, :expires_at, :params
|
4
|
+
attr_accessor :options, :refresh_token
|
5
|
+
|
6
|
+
class << self
|
7
|
+
# Initializes an AccessToken from a Hash
|
8
|
+
#
|
9
|
+
# @param [Client] the OAuth2::Client instance
|
10
|
+
# @param [Hash] a hash of AccessToken property values
|
11
|
+
# @return [AccessToken] the initalized AccessToken
|
12
|
+
def from_hash(client, hash)
|
13
|
+
new(client, hash.delete('access_token') || hash.delete(:access_token), hash)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Initializes an AccessToken from a key/value application/x-www-form-urlencoded string
|
17
|
+
#
|
18
|
+
# @param [Client] client the OAuth2::Client instance
|
19
|
+
# @param [String] kvform the application/x-www-form-urlencoded string
|
20
|
+
# @return [AccessToken] the initalized AccessToken
|
21
|
+
def from_kvform(client, kvform)
|
22
|
+
from_hash(client, Rack::Utils.parse_query(kvform))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Initalize an AccessToken
|
27
|
+
#
|
28
|
+
# @param [Client] client the OAuth2::Client instance
|
29
|
+
# @param [String] token the Access Token value
|
30
|
+
# @param [Hash] opts the options to create the Access Token with
|
31
|
+
# @option opts [String] :refresh_token (nil) the refresh_token value
|
32
|
+
# @option opts [FixNum, String] :expires_in (nil) the number of seconds in which the AccessToken will expire
|
33
|
+
# @option opts [FixNum, String] :expires_at (nil) the epoch time in seconds in which AccessToken will expire
|
34
|
+
# @option opts [Symbol] :mode (:header) the transmission mode of the Access Token parameter value
|
35
|
+
# one of :header, :body or :query
|
36
|
+
# @option opts [String] :header_format ('Bearer %s') the string format to use for the Authorization header
|
37
|
+
# @option opts [String] :param_name ('access_token') the parameter name to use for transmission of the
|
38
|
+
# Access Token value in :body or :query transmission mode
|
39
|
+
def initialize(client, token, opts = {})
|
40
|
+
@client = client
|
41
|
+
@token = token.to_s
|
42
|
+
[:refresh_token, :expires_in, :expires_at].each do |arg|
|
43
|
+
instance_variable_set("@#{arg}", opts.delete(arg) || opts.delete(arg.to_s))
|
44
|
+
end
|
45
|
+
@expires_in ||= opts.delete('expires')
|
46
|
+
@expires_in &&= @expires_in.to_i
|
47
|
+
@expires_at &&= @expires_at.to_i
|
48
|
+
@expires_at ||= Time.now.to_i + @expires_in if @expires_in
|
49
|
+
@options = {:mode => opts.delete(:mode) || :header,
|
50
|
+
:header_format => opts.delete(:header_format) || 'Bearer %s',
|
51
|
+
:param_name => opts.delete(:param_name) || 'access_token'}
|
52
|
+
@params = opts
|
53
|
+
end
|
54
|
+
|
55
|
+
# Indexer to additional params present in token response
|
56
|
+
#
|
57
|
+
# @param [String] key entry key to Hash
|
58
|
+
def [](key)
|
59
|
+
@params[key]
|
60
|
+
end
|
61
|
+
|
62
|
+
# Whether or not the token expires
|
63
|
+
#
|
64
|
+
# @return [Boolean]
|
65
|
+
def expires?
|
66
|
+
!!@expires_at
|
67
|
+
end
|
68
|
+
|
69
|
+
# Whether or not the token is expired
|
70
|
+
#
|
71
|
+
# @return [Boolean]
|
72
|
+
def expired?
|
73
|
+
expires? && (expires_at < Time.now.to_i)
|
74
|
+
end
|
75
|
+
|
76
|
+
# Refreshes the current Access Token
|
77
|
+
#
|
78
|
+
# @return [AccessToken] a new AccessToken
|
79
|
+
# @note options should be carried over to the new AccessToken
|
80
|
+
def refresh!(params = {})
|
81
|
+
fail('A refresh_token is not available') unless refresh_token
|
82
|
+
params.merge!(:client_id => @client.id,
|
83
|
+
:client_secret => @client.secret,
|
84
|
+
:grant_type => 'refresh_token',
|
85
|
+
:refresh_token => refresh_token)
|
86
|
+
new_token = @client.get_token(params)
|
87
|
+
new_token.options = options
|
88
|
+
new_token.refresh_token = refresh_token unless new_token.refresh_token
|
89
|
+
new_token
|
90
|
+
end
|
91
|
+
|
92
|
+
# Convert AccessToken to a hash which can be used to rebuild itself with AccessToken.from_hash
|
93
|
+
#
|
94
|
+
# @return [Hash] a hash of AccessToken property values
|
95
|
+
def to_hash
|
96
|
+
params.merge(:access_token => token, :refresh_token => refresh_token, :expires_at => expires_at)
|
97
|
+
end
|
98
|
+
|
99
|
+
# Make a request with the Access Token
|
100
|
+
#
|
101
|
+
# @param [Symbol] verb the HTTP request method
|
102
|
+
# @param [String] path the HTTP URL path of the request
|
103
|
+
# @param [Hash] opts the options to make the request with
|
104
|
+
# @see Client#request
|
105
|
+
def request(verb, path, opts = {}, &block)
|
106
|
+
self.token = opts
|
107
|
+
@client.request(verb, path, opts, &block)
|
108
|
+
end
|
109
|
+
|
110
|
+
# Make a GET request with the Access Token
|
111
|
+
#
|
112
|
+
# @see AccessToken#request
|
113
|
+
def get(path, opts = {}, &block)
|
114
|
+
request(:get, path, opts, &block)
|
115
|
+
end
|
116
|
+
|
117
|
+
# Make a POST request with the Access Token
|
118
|
+
#
|
119
|
+
# @see AccessToken#request
|
120
|
+
def post(path, opts = {}, &block)
|
121
|
+
request(:post, path, opts, &block)
|
122
|
+
end
|
123
|
+
|
124
|
+
# Make a PUT request with the Access Token
|
125
|
+
#
|
126
|
+
# @see AccessToken#request
|
127
|
+
def put(path, opts = {}, &block)
|
128
|
+
request(:put, path, opts, &block)
|
129
|
+
end
|
130
|
+
|
131
|
+
# Make a PATCH request with the Access Token
|
132
|
+
#
|
133
|
+
# @see AccessToken#request
|
134
|
+
def patch(path, opts = {}, &block)
|
135
|
+
request(:patch, path, opts, &block)
|
136
|
+
end
|
137
|
+
|
138
|
+
# Make a DELETE request with the Access Token
|
139
|
+
#
|
140
|
+
# @see AccessToken#request
|
141
|
+
def delete(path, opts = {}, &block)
|
142
|
+
request(:delete, path, opts, &block)
|
143
|
+
end
|
144
|
+
|
145
|
+
# Get the headers hash (includes Authorization token)
|
146
|
+
def headers
|
147
|
+
{'Authorization' => options[:header_format] % token}
|
148
|
+
end
|
149
|
+
|
150
|
+
private
|
151
|
+
|
152
|
+
def token=(opts) # rubocop:disable MethodLength
|
153
|
+
case options[:mode]
|
154
|
+
when :header
|
155
|
+
opts[:headers] ||= {}
|
156
|
+
opts[:headers].merge!(headers)
|
157
|
+
when :query
|
158
|
+
opts[:params] ||= {}
|
159
|
+
opts[:params][options[:param_name]] = token
|
160
|
+
when :body
|
161
|
+
opts[:body] ||= {}
|
162
|
+
if opts[:body].is_a?(Hash)
|
163
|
+
opts[:body][options[:param_name]] = token
|
164
|
+
else
|
165
|
+
opts[:body] << "&#{options[:param_name]}=#{token}"
|
166
|
+
end
|
167
|
+
# @todo support for multi-part (file uploads)
|
168
|
+
else
|
169
|
+
fail("invalid :mode option of #{options[:mode]}")
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
@@ -0,0 +1,173 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module OAuth2
|
4
|
+
# The OAuth2::Client class
|
5
|
+
class Client
|
6
|
+
attr_reader :id, :secret, :site
|
7
|
+
attr_accessor :options
|
8
|
+
attr_writer :connection
|
9
|
+
|
10
|
+
# Instantiate a new OAuth 2.0 client using the
|
11
|
+
# Client ID and Client Secret registered to your
|
12
|
+
# application.
|
13
|
+
#
|
14
|
+
# @param [String] client_id the client_id value
|
15
|
+
# @param [String] client_secret the client_secret value
|
16
|
+
# @param [Hash] opts the options to create the client with
|
17
|
+
# @option opts [String] :site the OAuth2 provider site host
|
18
|
+
# @option opts [String] :authorize_url ('/oauth/authorize') absolute or relative URL path to the Authorization endpoint
|
19
|
+
# @option opts [String] :token_url ('/oauth/token') absolute or relative URL path to the Token endpoint
|
20
|
+
# @option opts [Symbol] :token_method (:post) HTTP method to use to request token (:get or :post)
|
21
|
+
# @option opts [Hash] :connection_opts ({}) Hash of connection options to pass to initialize Faraday with
|
22
|
+
# @option opts [FixNum] :max_redirects (5) maximum number of redirects to follow
|
23
|
+
# @option opts [Boolean] :raise_errors (true) whether or not to raise an OAuth2::Error
|
24
|
+
# on responses with 400+ status codes
|
25
|
+
# @yield [builder] The Faraday connection builder
|
26
|
+
def initialize(client_id, client_secret, opts = {}, &block)
|
27
|
+
_opts = opts.dup
|
28
|
+
@id = client_id
|
29
|
+
@secret = client_secret
|
30
|
+
@site = _opts.delete(:site)
|
31
|
+
ssl = _opts.delete(:ssl)
|
32
|
+
@options = {:authorize_url => '/oauth/authorize',
|
33
|
+
:token_url => '/oauth/token',
|
34
|
+
:token_method => :post,
|
35
|
+
:connection_opts => {},
|
36
|
+
:connection_build => block,
|
37
|
+
:max_redirects => 5,
|
38
|
+
:raise_errors => true}.merge(_opts)
|
39
|
+
@options[:connection_opts][:ssl] = ssl if ssl
|
40
|
+
end
|
41
|
+
|
42
|
+
# Set the site host
|
43
|
+
#
|
44
|
+
# @param [String] the OAuth2 provider site host
|
45
|
+
def site=(value)
|
46
|
+
@connection = nil
|
47
|
+
@site = value
|
48
|
+
end
|
49
|
+
|
50
|
+
# The Faraday connection object
|
51
|
+
def connection
|
52
|
+
@connection ||= begin
|
53
|
+
conn = Faraday.new(site, options[:connection_opts])
|
54
|
+
conn.build do |b|
|
55
|
+
options[:connection_build].call(b)
|
56
|
+
end if options[:connection_build]
|
57
|
+
conn
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# The authorize endpoint URL of the OAuth2 provider
|
62
|
+
#
|
63
|
+
# @param [Hash] params additional query parameters
|
64
|
+
def authorize_url(params = nil)
|
65
|
+
connection.build_url(options[:authorize_url], params).to_s
|
66
|
+
end
|
67
|
+
|
68
|
+
# The token endpoint URL of the OAuth2 provider
|
69
|
+
#
|
70
|
+
# @param [Hash] params additional query parameters
|
71
|
+
def token_url(params = nil)
|
72
|
+
connection.build_url(options[:token_url], params).to_s
|
73
|
+
end
|
74
|
+
|
75
|
+
# Makes a request relative to the specified site root.
|
76
|
+
#
|
77
|
+
# @param [Symbol] verb one of :get, :post, :put, :delete
|
78
|
+
# @param [String] url URL path of request
|
79
|
+
# @param [Hash] opts the options to make the request with
|
80
|
+
# @option opts [Hash] :params additional query parameters for the URL of the request
|
81
|
+
# @option opts [Hash, String] :body the body of the request
|
82
|
+
# @option opts [Hash] :headers http request headers
|
83
|
+
# @option opts [Boolean] :raise_errors whether or not to raise an OAuth2::Error on 400+ status
|
84
|
+
# code response for this request. Will default to client option
|
85
|
+
# @option opts [Symbol] :parse @see Response::initialize
|
86
|
+
# @yield [req] The Faraday request
|
87
|
+
def request(verb, url, opts = {}) # rubocop:disable CyclomaticComplexity, MethodLength
|
88
|
+
url = connection.build_url(url, opts[:params]).to_s
|
89
|
+
|
90
|
+
response = connection.run_request(verb, url, opts[:body], opts[:headers]) do |req|
|
91
|
+
yield(req) if block_given?
|
92
|
+
end
|
93
|
+
response = Response.new(response, :parse => opts[:parse])
|
94
|
+
|
95
|
+
case response.status
|
96
|
+
when 301, 302, 303, 307
|
97
|
+
opts[:redirect_count] ||= 0
|
98
|
+
opts[:redirect_count] += 1
|
99
|
+
return response if opts[:redirect_count] > options[:max_redirects]
|
100
|
+
if response.status == 303
|
101
|
+
verb = :get
|
102
|
+
opts.delete(:body)
|
103
|
+
end
|
104
|
+
request(verb, response.headers['location'], opts)
|
105
|
+
when 200..299, 300..399
|
106
|
+
# on non-redirecting 3xx statuses, just return the response
|
107
|
+
response
|
108
|
+
when 400..599
|
109
|
+
error = Error.new(response)
|
110
|
+
fail(error) if opts.fetch(:raise_errors, options[:raise_errors])
|
111
|
+
response.error = error
|
112
|
+
response
|
113
|
+
else
|
114
|
+
error = Error.new(response)
|
115
|
+
fail(error, "Unhandled status code value of #{response.status}")
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
# Initializes an AccessToken by making a request to the token endpoint
|
120
|
+
#
|
121
|
+
# @param [Hash] params a Hash of params for the token endpoint
|
122
|
+
# @param [Hash] access token options, to pass to the AccessToken object
|
123
|
+
# @param [Class] class of access token for easier subclassing OAuth2::AccessToken
|
124
|
+
# @return [AccessToken] the initalized AccessToken
|
125
|
+
def get_token(params, access_token_opts = {}, access_token_class = AccessToken)
|
126
|
+
opts = {:raise_errors => options[:raise_errors], :parse => params.delete(:parse)}
|
127
|
+
if options[:token_method] == :post
|
128
|
+
headers = params.delete(:headers)
|
129
|
+
opts[:body] = params
|
130
|
+
opts[:headers] = {'Content-Type' => 'application/x-www-form-urlencoded'}
|
131
|
+
opts[:headers].merge!(headers) if headers
|
132
|
+
else
|
133
|
+
opts[:params] = params
|
134
|
+
end
|
135
|
+
response = request(options[:token_method], token_url, opts)
|
136
|
+
error = Error.new(response)
|
137
|
+
fail(error) if options[:raise_errors] && !(response.parsed.is_a?(Hash) && response.parsed['access_token'])
|
138
|
+
access_token_class.from_hash(self, response.parsed.merge(access_token_opts))
|
139
|
+
end
|
140
|
+
|
141
|
+
# The Authorization Code strategy
|
142
|
+
#
|
143
|
+
# @see http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-4.1
|
144
|
+
def auth_code
|
145
|
+
@auth_code ||= OAuth2::Strategy::AuthCode.new(self)
|
146
|
+
end
|
147
|
+
|
148
|
+
# The Implicit strategy
|
149
|
+
#
|
150
|
+
# @see http://tools.ietf.org/html/draft-ietf-oauth-v2-26#section-4.2
|
151
|
+
def implicit
|
152
|
+
@implicit ||= OAuth2::Strategy::Implicit.new(self)
|
153
|
+
end
|
154
|
+
|
155
|
+
# The Resource Owner Password Credentials strategy
|
156
|
+
#
|
157
|
+
# @see http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-4.3
|
158
|
+
def password
|
159
|
+
@password ||= OAuth2::Strategy::Password.new(self)
|
160
|
+
end
|
161
|
+
|
162
|
+
# The Client Credentials strategy
|
163
|
+
#
|
164
|
+
# @see http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-4.4
|
165
|
+
def client_credentials
|
166
|
+
@client_credentials ||= OAuth2::Strategy::ClientCredentials.new(self)
|
167
|
+
end
|
168
|
+
|
169
|
+
def assertion
|
170
|
+
@assertion ||= OAuth2::Strategy::Assertion.new(self)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|