omniauth-oauth2 1.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of omniauth-oauth2 might be problematic. Click here for more details.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +13 -0
- data/Guardfile +11 -0
- data/README.md +78 -0
- data/Rakefile +9 -0
- data/lib/omniauth-oauth2.rb +2 -0
- data/lib/omniauth-oauth2/version.rb +5 -0
- data/lib/omniauth/strategies/oauth2.rb +94 -0
- data/omniauth-oauth2.gemspec +26 -0
- data/spec/omniauth/strategies/oauth2_spec.rb +43 -0
- data/spec/spec_helper.rb +16 -0
- metadata +136 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source 'http://rubygems.org'
|
2
|
+
|
3
|
+
gem 'omniauth', '1.0.0.pr2', :git => 'git://github.com/intridea/omniauth.git'
|
4
|
+
# Specify your gem's dependencies in omniauth-oauth2.gemspec
|
5
|
+
gemspec
|
6
|
+
|
7
|
+
group :development, :test do
|
8
|
+
gem 'guard'
|
9
|
+
gem 'guard-rspec'
|
10
|
+
gem 'guard-bundler'
|
11
|
+
gem 'growl'
|
12
|
+
gem 'rb-fsevent'
|
13
|
+
end
|
data/Guardfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
# OmniAuth OAuth2
|
2
|
+
|
3
|
+
**Note:** This gem is designed to work with the unreleased OmniAuth 1.0
|
4
|
+
library. It will not be officially released on RubyGems.org until
|
5
|
+
OmniAuth 1.0 is released.
|
6
|
+
|
7
|
+
This gem contains a generic OAuth2 strategy for OmniAuth. It is meant to
|
8
|
+
serve as a building block strategy for other strategies and not to be
|
9
|
+
used independently (since it has no inherent way to gather uid and user
|
10
|
+
info).
|
11
|
+
|
12
|
+
## Creating an OAuth2 Strategy
|
13
|
+
|
14
|
+
To create an OmniAuth OAuth2 strategy using this gem, you can simply
|
15
|
+
subclass it and add a few extra methods like so:
|
16
|
+
|
17
|
+
require 'omniauth-oauth'
|
18
|
+
|
19
|
+
module OmniAuth
|
20
|
+
module Strategies
|
21
|
+
class SomeSite < OmniAuth::Strategies::OAuth2
|
22
|
+
# Give your strategy a name.
|
23
|
+
option :name, "some_site"
|
24
|
+
|
25
|
+
# This is where you pass the options you would pass when
|
26
|
+
# initializing your consumer from the OAuth gem.
|
27
|
+
option :client_options, {:site => "https://api.somesite.com"}
|
28
|
+
|
29
|
+
# These are called after authentication has succeeded. If
|
30
|
+
# possible, you should try to set the UID without making
|
31
|
+
# additional calls (if the user id is returned with the token
|
32
|
+
# or as a URI parameter). This may not be possible with all
|
33
|
+
# providers.
|
34
|
+
uid{ raw_info['id'] }
|
35
|
+
|
36
|
+
info do
|
37
|
+
{
|
38
|
+
:name => raw_info['name'],
|
39
|
+
:email => raw_info['email']
|
40
|
+
}
|
41
|
+
end
|
42
|
+
|
43
|
+
extra do
|
44
|
+
{
|
45
|
+
'raw_info' => raw_info
|
46
|
+
}
|
47
|
+
end
|
48
|
+
|
49
|
+
def raw_info
|
50
|
+
@raw_info ||= access_token.get('/me').parsed
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
That's pretty much it!
|
57
|
+
|
58
|
+
## License
|
59
|
+
|
60
|
+
Copyright (C) 2011 by Michael Bleigh and Intridea, Inc.
|
61
|
+
|
62
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
63
|
+
of this software and associated documentation files (the "Software"), to deal
|
64
|
+
in the Software without restriction, including without limitation the rights
|
65
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
66
|
+
copies of the Software, and to permit persons to whom the Software is
|
67
|
+
furnished to do so, subject to the following conditions:
|
68
|
+
|
69
|
+
The above copyright notice and this permission notice shall be included in
|
70
|
+
all copies or substantial portions of the Software.
|
71
|
+
|
72
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
73
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
74
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
75
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
76
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
77
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
78
|
+
THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
require 'uri'
|
3
|
+
require 'oauth2'
|
4
|
+
require 'omniauth'
|
5
|
+
|
6
|
+
module OmniAuth
|
7
|
+
module Strategies
|
8
|
+
# Authentication strategy for connecting with APIs constructed using
|
9
|
+
# the [OAuth 2.0 Specification](http://tools.ietf.org/html/draft-ietf-oauth-v2-10).
|
10
|
+
# You must generally register your application with the provider and
|
11
|
+
# utilize an application id and secret in order to authenticate using
|
12
|
+
# OAuth 2.0.
|
13
|
+
class OAuth2
|
14
|
+
include OmniAuth::Strategy
|
15
|
+
|
16
|
+
args [:client_id, :client_secret]
|
17
|
+
|
18
|
+
option :client_id, nil
|
19
|
+
option :client_secret, nil
|
20
|
+
option :client_options, {}
|
21
|
+
option :authorize_params, {}
|
22
|
+
option :authorize_options, [:scope]
|
23
|
+
option :token_params, {}
|
24
|
+
option :token_options, []
|
25
|
+
|
26
|
+
attr_accessor :access_token
|
27
|
+
|
28
|
+
def client
|
29
|
+
::OAuth2::Client.new(options.client_id, options.client_secret, options.client_options.inject({}){|h,(k,v)| h[k.to_sym] = v; h})
|
30
|
+
end
|
31
|
+
|
32
|
+
def callback_url
|
33
|
+
full_host + script_name + callback_path
|
34
|
+
end
|
35
|
+
|
36
|
+
credentials do
|
37
|
+
hash = {'token' => access_token.token}
|
38
|
+
hash.merge!('refresh_token' => access_token.refresh_token) if access_token.expires?
|
39
|
+
hash
|
40
|
+
end
|
41
|
+
|
42
|
+
def request_phase
|
43
|
+
redirect client.auth_code.authorize_url({:redirect_uri => callback_url}.merge(authorize_params))
|
44
|
+
end
|
45
|
+
|
46
|
+
def authorize_params
|
47
|
+
options.authorize_params.merge(options.authorize_options.inject({}){|h,k| h[k.to_sym] = options[k] if options[k]; h})
|
48
|
+
end
|
49
|
+
|
50
|
+
def token_params
|
51
|
+
options.token_params.merge(options.token_options.inject({}){|h,k| h[k.to_sym] = options[k] if options[k]; h})
|
52
|
+
end
|
53
|
+
|
54
|
+
def callback_phase
|
55
|
+
if request.params['error'] || request.params['error_reason']
|
56
|
+
raise CallbackError.new(request.params['error'], request.params['error_description'] || request.params['error_reason'], request.params['error_uri'])
|
57
|
+
end
|
58
|
+
|
59
|
+
self.access_token = build_access_token
|
60
|
+
self.access_token = client.auth_code.refresh_token(access_token.refresh_token) if access_token.expired?
|
61
|
+
|
62
|
+
super
|
63
|
+
rescue ::OAuth2::Error, CallbackError => e
|
64
|
+
fail!(:invalid_credentials, e)
|
65
|
+
rescue ::MultiJson::DecodeError => e
|
66
|
+
fail!(:invalid_response, e)
|
67
|
+
rescue ::Timeout::Error, ::Errno::ETIMEDOUT => e
|
68
|
+
fail!(:timeout, e)
|
69
|
+
end
|
70
|
+
|
71
|
+
protected
|
72
|
+
|
73
|
+
def build_access_token
|
74
|
+
verifier = request.params['code']
|
75
|
+
client.auth_code.get_token(verifier, {:redirect_uri => callback_url}.merge(options.token_params.to_hash(:symbolize_keys => true)))
|
76
|
+
rescue ::OAuth2::Error => e
|
77
|
+
raise e.response.inspect
|
78
|
+
end
|
79
|
+
|
80
|
+
# An error that is indicated in the OAuth 2.0 callback.
|
81
|
+
# This could be a `redirect_uri_mismatch` or other
|
82
|
+
class CallbackError < StandardError
|
83
|
+
attr_accessor :error, :error_reason, :error_uri
|
84
|
+
|
85
|
+
def initialize(error, error_reason=nil, error_uri=nil)
|
86
|
+
self.error = error
|
87
|
+
self.error_reason = error_reason
|
88
|
+
self.error_uri = error_uri
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
OmniAuth.config.add_camelization 'oauth2', 'OAuth2'
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/omniauth-oauth2/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.add_dependency 'hashie', '~> 1.2'
|
6
|
+
gem.add_dependency 'omniauth', '~> 1.0.0.beta1'
|
7
|
+
gem.add_dependency 'oauth2', '~> 0.5.0'
|
8
|
+
|
9
|
+
gem.add_development_dependency 'rspec', '~> 2.6'
|
10
|
+
gem.add_development_dependency 'rack-test'
|
11
|
+
gem.add_development_dependency 'webmock'
|
12
|
+
gem.add_development_dependency 'simplecov'
|
13
|
+
|
14
|
+
gem.authors = ["Michael Bleigh"]
|
15
|
+
gem.email = ["michael@intridea.com"]
|
16
|
+
gem.description = %q{An abstract OAuth2 strategy for OmniAuth.}
|
17
|
+
gem.summary = %q{An abstract OAuth2 strategy for OmniAuth.}
|
18
|
+
gem.homepage = "https://github.com/intridea/omniauth-oauth2"
|
19
|
+
|
20
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
21
|
+
gem.files = `git ls-files`.split("\n")
|
22
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
23
|
+
gem.name = "omniauth-oauth2"
|
24
|
+
gem.require_paths = ["lib"]
|
25
|
+
gem.version = OmniAuth::OAuth2::VERSION
|
26
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe OmniAuth::Strategies::OAuth2 do
|
4
|
+
def app; lambda{|env| [200, {}, ["Hello."]]} end
|
5
|
+
let(:fresh_strategy){ Class.new(OmniAuth::Strategies::OAuth2) }
|
6
|
+
|
7
|
+
describe '#client' do
|
8
|
+
subject{ fresh_strategy }
|
9
|
+
|
10
|
+
it 'should be initialized with symbolized client_options' do
|
11
|
+
instance = subject.new(app, :client_options => {'authorize_url' => 'https://example.com'})
|
12
|
+
instance.client.options[:authorize_url].should == 'https://example.com'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#authorize_params' do
|
17
|
+
subject { fresh_strategy }
|
18
|
+
|
19
|
+
it 'should include any authorize params passed in the :authorize_params option' do
|
20
|
+
instance = subject.new('abc', 'def', :authorize_params => {:foo => 'bar', :baz => 'zip'})
|
21
|
+
instance.authorize_params.should == {'foo' => 'bar', 'baz' => 'zip'}
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should include top-level options that are marked as :authorize_options' do
|
25
|
+
instance = subject.new('abc', 'def', :authorize_options => [:scope, :foo], :scope => 'bar', :foo => 'baz')
|
26
|
+
instance.authorize_params.should == {'scope' => 'bar', 'foo' => 'baz'}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#token_params' do
|
31
|
+
subject { fresh_strategy }
|
32
|
+
|
33
|
+
it 'should include any authorize params passed in the :authorize_params option' do
|
34
|
+
instance = subject.new('abc', 'def', :token_params => {:foo => 'bar', :baz => 'zip'})
|
35
|
+
instance.token_params.should == {'foo' => 'bar', 'baz' => 'zip'}
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should include top-level options that are marked as :authorize_options' do
|
39
|
+
instance = subject.new('abc', 'def', :token_options => [:scope, :foo], :scope => 'bar', :foo => 'baz')
|
40
|
+
instance.token_params.should == {'scope' => 'bar', 'foo' => 'baz'}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
$:.unshift File.expand_path('..', __FILE__)
|
2
|
+
$:.unshift File.expand_path('../../lib', __FILE__)
|
3
|
+
require 'simplecov'
|
4
|
+
SimpleCov.start
|
5
|
+
require 'rspec'
|
6
|
+
require 'rack/test'
|
7
|
+
require 'webmock/rspec'
|
8
|
+
require 'omniauth'
|
9
|
+
require 'omniauth-oauth2'
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
config.include WebMock::API
|
13
|
+
config.include Rack::Test::Methods
|
14
|
+
config.extend OmniAuth::Test::StrategyMacros, :type => :strategy
|
15
|
+
end
|
16
|
+
|
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: omniauth-oauth2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0.beta1
|
5
|
+
prerelease: 6
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Michael Bleigh
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-10-19 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: hashie
|
16
|
+
requirement: &70318691424740 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.2'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70318691424740
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: omniauth
|
27
|
+
requirement: &70318691424240 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.0.0.beta1
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70318691424240
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: oauth2
|
38
|
+
requirement: &70318691423780 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 0.5.0
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70318691423780
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
requirement: &70318691423320 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.6'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70318691423320
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rack-test
|
60
|
+
requirement: &70318691422940 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70318691422940
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: webmock
|
71
|
+
requirement: &70318691422480 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *70318691422480
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: simplecov
|
82
|
+
requirement: &70318691422060 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *70318691422060
|
91
|
+
description: An abstract OAuth2 strategy for OmniAuth.
|
92
|
+
email:
|
93
|
+
- michael@intridea.com
|
94
|
+
executables: []
|
95
|
+
extensions: []
|
96
|
+
extra_rdoc_files: []
|
97
|
+
files:
|
98
|
+
- .gitignore
|
99
|
+
- .rspec
|
100
|
+
- Gemfile
|
101
|
+
- Guardfile
|
102
|
+
- README.md
|
103
|
+
- Rakefile
|
104
|
+
- lib/omniauth-oauth2.rb
|
105
|
+
- lib/omniauth-oauth2/version.rb
|
106
|
+
- lib/omniauth/strategies/oauth2.rb
|
107
|
+
- omniauth-oauth2.gemspec
|
108
|
+
- spec/omniauth/strategies/oauth2_spec.rb
|
109
|
+
- spec/spec_helper.rb
|
110
|
+
homepage: https://github.com/intridea/omniauth-oauth2
|
111
|
+
licenses: []
|
112
|
+
post_install_message:
|
113
|
+
rdoc_options: []
|
114
|
+
require_paths:
|
115
|
+
- lib
|
116
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
117
|
+
none: false
|
118
|
+
requirements:
|
119
|
+
- - ! '>='
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
none: false
|
124
|
+
requirements:
|
125
|
+
- - ! '>'
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: 1.3.1
|
128
|
+
requirements: []
|
129
|
+
rubyforge_project:
|
130
|
+
rubygems_version: 1.8.10
|
131
|
+
signing_key:
|
132
|
+
specification_version: 3
|
133
|
+
summary: An abstract OAuth2 strategy for OmniAuth.
|
134
|
+
test_files:
|
135
|
+
- spec/omniauth/strategies/oauth2_spec.rb
|
136
|
+
- spec/spec_helper.rb
|