omniauth-oauth 1.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format=progress
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'omniauth', :git => 'git://github.com/intridea/omniauth.git'
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem 'guard'
8
+ gem 'guard-rspec'
9
+ gem 'guard-bundler'
10
+ gem 'growl'
11
+ gem 'rb-fsevent'
12
+ end
data/Guardfile ADDED
@@ -0,0 +1,11 @@
1
+ guard 'rspec', :version => 2 do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
6
+
7
+
8
+ guard 'bundler' do
9
+ watch('Gemfile')
10
+ watch(/^.+\.gemspec/)
11
+ end
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # OmniAuth OAuth
2
+
3
+ **Note:** This gem is designed to work with the in-beta 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 OAuth 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 OAuth Strategy
13
+
14
+ To create an OmniAuth OAuth 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::OAuth
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{ request.params['user_id'] }
35
+
36
+ info do
37
+ {
38
+ :name => raw_info['name'],
39
+ :location => raw_info['city']
40
+ }
41
+ end
42
+
43
+ extra do
44
+ {
45
+ 'raw_info' => raw_info
46
+ }
47
+ end
48
+
49
+ def raw_info
50
+ @raw_info ||= MultiJson.decode(access_token.get('/me.json')).body
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,9 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ desc 'Default: run specs.'
6
+ task :default => :spec
7
+
8
+ desc "Run specs"
9
+ RSpec::Core::RakeTask.new
@@ -0,0 +1,3 @@
1
+ require "omniauth-oauth/version"
2
+ require 'omniauth/strategies/oauth'
3
+
@@ -0,0 +1,5 @@
1
+ module OmniAuth
2
+ module OAuth
3
+ VERSION = "1.0.0.beta1"
4
+ end
5
+ end
@@ -0,0 +1,81 @@
1
+ require 'multi_json'
2
+ require 'oauth'
3
+ require 'omniauth'
4
+
5
+ module OmniAuth
6
+ module Strategies
7
+ class OAuth
8
+ include OmniAuth::Strategy
9
+
10
+ args [:consumer_key, :consumer_secret]
11
+ option :consumer_key, nil
12
+ option :consumer_secret, nil
13
+ option :client_options, {}
14
+ option :open_timeout, 30
15
+ option :read_timeout, 30
16
+ option :authorize_params, {}
17
+
18
+ attr_reader :access_token
19
+
20
+ def consumer
21
+ consumer = ::OAuth::Consumer.new(options.consumer_key, options.consumer_secret, options.client_options)
22
+ consumer.http.open_timeout = options.open_timeout if options.open_timeout
23
+ consumer.http.read_timeout = options.read_timeout if options.read_timeout
24
+ consumer
25
+ end
26
+
27
+ def request_phase
28
+ request_token = consumer.get_request_token(:oauth_callback => callback_url)
29
+ session['oauth'] ||= {}
30
+ session['oauth'][name.to_s] = {'callback_confirmed' => request_token.callback_confirmed?, 'request_token' => request_token.token, 'request_secret' => request_token.secret}
31
+
32
+ if request_token.callback_confirmed?
33
+ redirect request_token.authorize_url(options[:authorize_params])
34
+ else
35
+ redirect request_token.authorize_url(options[:authorize_params].merge(:oauth_callback => callback_url))
36
+ end
37
+
38
+ rescue ::Timeout::Error => e
39
+ fail!(:timeout, e)
40
+ rescue ::Net::HTTPFatalError, ::OpenSSL::SSL::SSLError => e
41
+ fail!(:service_unavailable, e)
42
+ end
43
+
44
+ def callback_phase
45
+ raise OmniAuth::NoSessionError.new("Session Expired") if session['oauth'].nil?
46
+
47
+ request_token = ::OAuth::RequestToken.new(consumer, session['oauth'][name.to_s].delete('request_token'), session['oauth'][name.to_s].delete('request_secret'))
48
+
49
+ opts = {}
50
+ if session['oauth'][name.to_s]['callback_confirmed']
51
+ opts[:oauth_verifier] = request['oauth_verifier']
52
+ else
53
+ opts[:oauth_callback] = callback_url
54
+ end
55
+
56
+ @access_token = request_token.get_access_token(opts)
57
+ super
58
+ rescue ::Timeout::Error => e
59
+ fail!(:timeout, e)
60
+ rescue ::Net::HTTPFatalError, ::OpenSSL::SSL::SSLError => e
61
+ fail!(:service_unavailable, e)
62
+ rescue ::OAuth::Unauthorized => e
63
+ fail!(:invalid_credentials, e)
64
+ rescue ::NoMethodError, ::MultiJson::DecodeError => e
65
+ fail!(:invalid_response, e)
66
+ rescue ::OmniAuth::NoSessionError => e
67
+ fail!(:session_expired, e)
68
+ end
69
+
70
+ credentials do
71
+ {'token' => access_token.token, 'secret' => access_token.secret}
72
+ end
73
+
74
+ extra do
75
+ {'access_token' => access_token}
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ OmniAuth.config.add_camelization 'oauth', 'OAuth'
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/omniauth-oauth/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Michael Bleigh"]
6
+ gem.email = ["michael@intridea.com"]
7
+ gem.description = %q{A generic OAuth (1.0/1.0a) strategy for OmniAuth.}
8
+ gem.summary = %q{A generic OAuth (1.0/1.0a) strategy for OmniAuth.}
9
+ gem.homepage = "https://github.com/intridea/omniauth-oauth"
10
+
11
+ gem.add_runtime_dependency 'omniauth', '~> 1.0.0.beta1'
12
+ gem.add_runtime_dependency 'oauth'
13
+ gem.add_development_dependency 'rspec', '~> 2.6'
14
+ gem.add_development_dependency 'webmock'
15
+ gem.add_development_dependency 'simplecov'
16
+ gem.add_development_dependency 'rack-test'
17
+
18
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ gem.files = `git ls-files`.split("\n")
20
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ gem.name = "omniauth-oauth"
22
+ gem.require_paths = ["lib"]
23
+ gem.version = OmniAuth::OAuth::VERSION
24
+ end
@@ -0,0 +1,140 @@
1
+ require 'spec_helper'
2
+
3
+ describe "OmniAuth::Strategies::OAuth" do
4
+ class MyOAuthProvider < OmniAuth::Strategies::OAuth
5
+ uid{ access_token.token }
6
+ info{ {'name' => access_token.token} }
7
+ end
8
+
9
+ def app
10
+ Rack::Builder.new {
11
+ use OmniAuth::Test::PhonySession
12
+ use OmniAuth::Builder do
13
+ provider MyOAuthProvider, 'abc', 'def', :client_options => {:site => 'https://api.example.org'}, :name => 'example.org'
14
+ provider MyOAuthProvider, 'abc', 'def', :client_options => {:site => 'https://api.example.org'}, :authorize_params => {:abc => 'def'}, :name => 'example.org_with_authorize_params'
15
+ end
16
+ run lambda { |env| [404, {'Content-Type' => 'text/plain'}, [env.key?('omniauth.auth').to_s]] }
17
+ }.to_app
18
+ end
19
+
20
+ def session
21
+ last_request.env['rack.session']
22
+ end
23
+
24
+ before do
25
+ stub_request(:post, 'https://api.example.org/oauth/request_token').
26
+ to_return(:body => "oauth_token=yourtoken&oauth_token_secret=yoursecret&oauth_callback_confirmed=true")
27
+ end
28
+
29
+ it 'should add a camelization for itself' do
30
+ OmniAuth::Utils.camelize('oauth').should == 'OAuth'
31
+ end
32
+
33
+ describe '/auth/{name}' do
34
+ context 'successful' do
35
+ before do
36
+ get '/auth/example.org'
37
+ end
38
+
39
+ it 'should redirect to authorize_url' do
40
+ last_response.should be_redirect
41
+ last_response.headers['Location'].should == 'https://api.example.org/oauth/authorize?oauth_token=yourtoken'
42
+ end
43
+
44
+ it 'should redirect to authorize_url with authorize_params when set' do
45
+ get '/auth/example.org_with_authorize_params'
46
+ last_response.should be_redirect
47
+ [
48
+ 'https://api.example.org/oauth/authorize?abc=def&oauth_token=yourtoken',
49
+ 'https://api.example.org/oauth/authorize?oauth_token=yourtoken&abc=def'
50
+ ].should be_include(last_response.headers['Location'])
51
+ end
52
+
53
+ it 'should set appropriate session variables' do
54
+ session['oauth'].should == {"example.org" => {'callback_confirmed' => true, 'request_token' => 'yourtoken', 'request_secret' => 'yoursecret'}}
55
+ end
56
+ end
57
+
58
+ context 'unsuccessful' do
59
+ before do
60
+ stub_request(:post, 'https://api.example.org/oauth/request_token').
61
+ to_raise(::Net::HTTPFatalError.new(%Q{502 "Bad Gateway"}, nil))
62
+ get '/auth/example.org'
63
+ end
64
+
65
+ it 'should call fail! with :service_unavailable' do
66
+ last_request.env['omniauth.error'].should be_kind_of(::Net::HTTPFatalError)
67
+ last_request.env['omniauth.error.type'] = :service_unavailable
68
+ end
69
+
70
+ context "SSL failure" do
71
+ before do
72
+ stub_request(:post, 'https://api.example.org/oauth/request_token').
73
+ to_raise(::OpenSSL::SSL::SSLError.new("SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed"))
74
+ get '/auth/example.org'
75
+ end
76
+
77
+ it 'should call fail! with :service_unavailable' do
78
+ last_request.env['omniauth.error'].should be_kind_of(::OpenSSL::SSL::SSLError)
79
+ last_request.env['omniauth.error.type'] = :service_unavailable
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ describe '/auth/{name}/callback' do
86
+ before do
87
+ stub_request(:post, 'https://api.example.org/oauth/access_token').
88
+ to_return(:body => "oauth_token=yourtoken&oauth_token_secret=yoursecret")
89
+ get '/auth/example.org/callback', {:oauth_verifier => 'dudeman'}, {'rack.session' => {'oauth' => {"example.org" => {'callback_confirmed' => true, 'request_token' => 'yourtoken', 'request_secret' => 'yoursecret'}}}}
90
+ end
91
+
92
+ it 'should exchange the request token for an access token' do
93
+ last_request.env['omniauth.auth']['provider'].should == 'example.org'
94
+ last_request.env['omniauth.auth']['extra']['access_token'].should be_kind_of(OAuth::AccessToken)
95
+ end
96
+
97
+ it 'should call through to the master app' do
98
+ last_response.body.should == 'true'
99
+ end
100
+
101
+ context "bad gateway (or any 5xx) for access_token" do
102
+ before do
103
+ stub_request(:post, 'https://api.example.org/oauth/access_token').
104
+ to_raise(::Net::HTTPFatalError.new(%Q{502 "Bad Gateway"}, nil))
105
+ get '/auth/example.org/callback', {:oauth_verifier => 'dudeman'}, {'rack.session' => {'oauth' => {"example.org" => {'callback_confirmed' => true, 'request_token' => 'yourtoken', 'request_secret' => 'yoursecret'}}}}
106
+ end
107
+
108
+ it 'should call fail! with :service_unavailable' do
109
+ last_request.env['omniauth.error'].should be_kind_of(::Net::HTTPFatalError)
110
+ last_request.env['omniauth.error.type'] = :service_unavailable
111
+ end
112
+ end
113
+
114
+ context "SSL failure" do
115
+ before do
116
+ stub_request(:post, 'https://api.example.org/oauth/access_token').
117
+ to_raise(::OpenSSL::SSL::SSLError.new("SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed"))
118
+ get '/auth/example.org/callback', {:oauth_verifier => 'dudeman'}, {'rack.session' => {'oauth' => {"example.org" => {'callback_confirmed' => true, 'request_token' => 'yourtoken', 'request_secret' => 'yoursecret'}}}}
119
+ end
120
+
121
+ it 'should call fail! with :service_unavailable' do
122
+ last_request.env['omniauth.error'].should be_kind_of(::OpenSSL::SSL::SSLError)
123
+ last_request.env['omniauth.error.type'] = :service_unavailable
124
+ end
125
+ end
126
+ end
127
+
128
+ describe '/auth/{name}/callback with expired session' do
129
+ before do
130
+ stub_request(:post, 'https://api.example.org/oauth/access_token').
131
+ to_return(:body => "oauth_token=yourtoken&oauth_token_secret=yoursecret")
132
+ get '/auth/example.org/callback', {:oauth_verifier => 'dudeman'}, {'rack.session' => {}}
133
+ end
134
+
135
+ it 'should call fail! with :session_expired' do
136
+ last_request.env['omniauth.error'].should be_kind_of(::OmniAuth::NoSessionError)
137
+ last_request.env['omniauth.error.type'] = :session_expired
138
+ end
139
+ end
140
+ end
@@ -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-oauth'
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,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-oauth
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: omniauth
16
+ requirement: &70140877791080 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.0.beta1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70140877791080
25
+ - !ruby/object:Gem::Dependency
26
+ name: oauth
27
+ requirement: &70140877790660 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70140877790660
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70140877790120 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '2.6'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70140877790120
47
+ - !ruby/object:Gem::Dependency
48
+ name: webmock
49
+ requirement: &70140877789700 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70140877789700
58
+ - !ruby/object:Gem::Dependency
59
+ name: simplecov
60
+ requirement: &70140877789240 !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: *70140877789240
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack-test
71
+ requirement: &70140877788820 !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: *70140877788820
80
+ description: A generic OAuth (1.0/1.0a) strategy for OmniAuth.
81
+ email:
82
+ - michael@intridea.com
83
+ executables: []
84
+ extensions: []
85
+ extra_rdoc_files: []
86
+ files:
87
+ - .gitignore
88
+ - .rspec
89
+ - Gemfile
90
+ - Guardfile
91
+ - README.md
92
+ - Rakefile
93
+ - lib/omniauth-oauth.rb
94
+ - lib/omniauth-oauth/version.rb
95
+ - lib/omniauth/strategies/oauth.rb
96
+ - omniauth-oauth.gemspec
97
+ - spec/omniauth/strategies/oauth_spec.rb
98
+ - spec/spec_helper.rb
99
+ homepage: https://github.com/intridea/omniauth-oauth
100
+ licenses: []
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>'
115
+ - !ruby/object:Gem::Version
116
+ version: 1.3.1
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 1.8.10
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: A generic OAuth (1.0/1.0a) strategy for OmniAuth.
123
+ test_files:
124
+ - spec/omniauth/strategies/oauth_spec.rb
125
+ - spec/spec_helper.rb