omniauth-xauth 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +14 -1
- data/.rspec +2 -0
- data/README.md +53 -0
- data/lib/omniauth-xauth/version.rb +1 -1
- data/lib/omniauth/strategies/xauth.rb +20 -8
- data/omniauth-xauth.gemspec +7 -5
- data/spec/omniauth/strategies/xauth_spec.rb +148 -0
- data/spec/spec_helper.rb +16 -0
- metadata +54 -14
data/.gitignore
CHANGED
data/.rspec
ADDED
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# OmniAuth XAuth
|
2
|
+
|
3
|
+
OmniAuth XAuth strategy for use in [OmniAuth](https://github.com/intridea/omniauth) 1.0 strategy development.
|
4
|
+
|
5
|
+
This gem contains a generic XAuth strategy for OmniAuth. It is meant to
|
6
|
+
serve as a building block strategy for other strategies and not to be
|
7
|
+
used independently (since it has no inherent way to gather uid and user
|
8
|
+
info).
|
9
|
+
|
10
|
+
The XAuth form is rendered as an [OmniAuth Form](http://rubydoc.info/github/intridea/omniauth/master/OmniAuth/Form)
|
11
|
+
and can be styled as such.
|
12
|
+
|
13
|
+
## Creating an XAuth Strategy
|
14
|
+
|
15
|
+
To create an OmniAuth XAuth strategy using this gem, you can simply
|
16
|
+
subclass it and add a few extra methods like so:
|
17
|
+
|
18
|
+
require 'omniauth-xauth'
|
19
|
+
|
20
|
+
module OmniAuth
|
21
|
+
module Strategies
|
22
|
+
class SomeSite < OmniAuth::Strategies::XAuth
|
23
|
+
option :client_options, {
|
24
|
+
:site => 'http://www.service.com/',
|
25
|
+
:access_token_url => 'https://www.service.com/oauth/access_token'
|
26
|
+
}
|
27
|
+
option :xauth_options, { :title => 'XAuth Login Form Header'}
|
28
|
+
|
29
|
+
|
30
|
+
# This is where you pass the options you would pass when
|
31
|
+
# initializing your consumer from the OAuth gem.
|
32
|
+
|
33
|
+
|
34
|
+
uid { raw_info['uid'] }
|
35
|
+
info do
|
36
|
+
{
|
37
|
+
:name => raw_info['name'],
|
38
|
+
:email => raw_info['email']
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
extra do
|
43
|
+
{
|
44
|
+
'raw_info' => raw_info
|
45
|
+
}
|
46
|
+
end
|
47
|
+
|
48
|
+
def raw_info
|
49
|
+
@raw_info ||= MultiJson.decode(access_token.get('/me.json').body)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -1,19 +1,22 @@
|
|
1
|
-
require 'omniauth
|
1
|
+
require 'omniauth'
|
2
2
|
require 'multi_json'
|
3
|
+
require 'oauth'
|
3
4
|
|
4
5
|
module OmniAuth
|
5
6
|
module Strategies
|
6
|
-
|
7
|
-
# This code is originally from oa-omniauth.gem and applied some fixes for OmniAuth 1.0.
|
8
7
|
class XAuth
|
9
8
|
include OmniAuth::Strategy
|
10
9
|
|
11
10
|
args [:consumer_key, :consumer_secret]
|
12
|
-
|
11
|
+
option :consumer_key, nil
|
12
|
+
option :consumer_secret, nil
|
13
|
+
option :client_options, {}
|
13
14
|
option :consumer_options, {}
|
15
|
+
option :xauth_options, { :title => 'OmniAuth XAuth' }
|
16
|
+
|
17
|
+
attr_reader :access_token
|
14
18
|
|
15
19
|
def request_phase
|
16
|
-
session['oauth'] ||= {}
|
17
20
|
if env['REQUEST_METHOD'] == 'GET'
|
18
21
|
get_credentials
|
19
22
|
else
|
@@ -23,25 +26,34 @@ module OmniAuth
|
|
23
26
|
end
|
24
27
|
|
25
28
|
def get_credentials
|
26
|
-
OmniAuth::Form.build(
|
29
|
+
OmniAuth::Form.build(options.xauth_options) do
|
27
30
|
text_field 'Username', 'username'
|
28
31
|
password_field 'Password', 'password'
|
29
32
|
end.to_response
|
30
33
|
end
|
31
34
|
|
32
35
|
def consumer
|
33
|
-
::OAuth::Consumer.new(consumer_key, consumer_secret,
|
36
|
+
consumer = ::OAuth::Consumer.new(options.consumer_key, options.consumer_secret, options.client_options)
|
37
|
+
consumer.http.open_timeout = options.open_timeout if options.open_timeout
|
38
|
+
consumer.http.read_timeout = options.read_timeout if options.read_timeout
|
39
|
+
consumer
|
34
40
|
end
|
35
41
|
|
36
42
|
def callback_phase
|
43
|
+
raise OmniAuth::NoSessionError.new("Session Expired") if session['omniauth.xauth'].nil?
|
44
|
+
|
37
45
|
@access_token = consumer.get_access_token(nil, {}, session['omniauth.xauth'])
|
38
46
|
super
|
39
|
-
rescue ::Net::HTTPFatalError => e
|
47
|
+
rescue ::Net::HTTPFatalError, ::OpenSSL::SSL::SSLError => e
|
40
48
|
fail!(:service_unavailable, e)
|
41
49
|
rescue ::OAuth::Unauthorized => e
|
42
50
|
fail!(:invalid_credentials, e)
|
43
51
|
rescue ::MultiJson::DecodeError => e
|
44
52
|
fail!(:invalid_response, e)
|
53
|
+
rescue ::OmniAuth::NoSessionError => e
|
54
|
+
fail!(:session_expired, e)
|
55
|
+
rescue => e
|
56
|
+
puts e.backtrace
|
45
57
|
ensure
|
46
58
|
session['omniauth.xauth'] = nil
|
47
59
|
end
|
data/omniauth-xauth.gemspec
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
require "omniauth-xauth/version"
|
2
|
+
require File.expand_path('../lib/omniauth-xauth/version', __FILE__)
|
4
3
|
|
5
4
|
Gem::Specification.new do |s|
|
6
5
|
s.name = "omniauth-xauth"
|
@@ -18,7 +17,10 @@ Gem::Specification.new do |s|
|
|
18
17
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
18
|
s.require_paths = ["lib"]
|
20
19
|
|
21
|
-
s.add_runtime_dependency
|
22
|
-
s.add_runtime_dependency
|
23
|
-
s.
|
20
|
+
s.add_runtime_dependency 'omniauth', '~> 1.0'
|
21
|
+
s.add_runtime_dependency 'oauth'
|
22
|
+
s.add_development_dependency 'rspec', '~> 2.8'
|
23
|
+
s.add_development_dependency 'webmock'
|
24
|
+
s.add_development_dependency 'simplecov'
|
25
|
+
s.add_development_dependency 'rack-test'
|
24
26
|
end
|
@@ -0,0 +1,148 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "OmniAuth::Strategies::XAuth" do
|
4
|
+
class MyOAuthProvider < OmniAuth::Strategies::XAuth
|
5
|
+
option :client_options, { :site => 'https://api.example.org', :title => 'xAuth', :access_token_url => 'https://api.example.org/oauth/access_token' }
|
6
|
+
option :consumer_options, {}
|
7
|
+
uid { 1 }
|
8
|
+
info{ { 'name' => 'ohai' } }
|
9
|
+
end
|
10
|
+
|
11
|
+
def app
|
12
|
+
Rack::Builder.new {
|
13
|
+
use OmniAuth::Test::PhonySession
|
14
|
+
use OmniAuth::Builder do
|
15
|
+
provider MyOAuthProvider, 'abc', 'def', :name => 'example.org'
|
16
|
+
end
|
17
|
+
run lambda { |env| [404, {'Content-Type' => 'text/plain'}, [env.key?('omniauth.auth').to_s]] }
|
18
|
+
}.to_app
|
19
|
+
end
|
20
|
+
|
21
|
+
def session
|
22
|
+
last_request.env['rack.session']
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should add a camelization for itself' do
|
26
|
+
OmniAuth::Utils.camelize('xauth').should == 'XAuth'
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '/auth/{name}' do
|
30
|
+
context 'GET' do
|
31
|
+
before do
|
32
|
+
get '/auth/example.org'
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should render an Omniauth::Form' do
|
36
|
+
last_response.should be_ok
|
37
|
+
last_response.body.should include('Username')
|
38
|
+
last_response.body.should include('Password')
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'POST' do
|
43
|
+
before do
|
44
|
+
post '/auth/example.org', :username => 'joe', :password => 'passw0rd'
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should redirect to the callback url' do
|
48
|
+
last_response.should be_redirect
|
49
|
+
last_response.headers['Location'].should eq('/auth/example.org/callback')
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'sets the xauth credentials to the "omniauth.xauth" session' do
|
53
|
+
session['omniauth.xauth'].should be
|
54
|
+
session['omniauth.xauth']['x_auth_username'].should eq('joe')
|
55
|
+
session['omniauth.xauth']['x_auth_password'].should eq('passw0rd')
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe '/auth/{name}/callback' do
|
62
|
+
context 'Success' do
|
63
|
+
before do
|
64
|
+
stub_request(:post, 'https://api.example.org/oauth/access_token').
|
65
|
+
to_return(:body => "oauth_token=yourtoken&oauth_token_secret=yoursecret")
|
66
|
+
get '/auth/example.org/callback', {}, {'rack.session' => { 'omniauth.xauth' => { 'x_auth_mode' => 'client_auth', 'x_auth_username' => 'username', 'x_auth_password' => 'password' }}}
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'should clear "omniauth.xauth" from the session' do
|
70
|
+
session['omniauth.xauth'].should be_nil
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'should exchange the request token for an access token' do
|
74
|
+
last_request.env['omniauth.auth']['provider'].should == 'example.org'
|
75
|
+
last_request.env['omniauth.auth']['extra']['access_token'].should be_kind_of(OAuth::AccessToken)
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'should call through to the master app' do
|
79
|
+
last_response.body.should == 'true'
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context "bad gateway (or any 5xx) for access_token" do
|
84
|
+
before do
|
85
|
+
stub_request(:post, 'https://api.example.org/oauth/access_token').
|
86
|
+
to_raise(::Net::HTTPFatalError.new(%Q{502 "Bad Gateway"}, nil))
|
87
|
+
get '/auth/example.org/callback', {:oauth_verifier => 'dudeman'}, {'rack.session' => { 'omniauth.xauth' => { 'x_auth_mode' => 'client_auth', 'x_auth_username' => 'username', 'x_auth_password' => 'password' }}}
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'should call fail! with :service_unavailable' do
|
91
|
+
last_request.env['omniauth.error'].should be_kind_of(::Net::HTTPFatalError)
|
92
|
+
last_request.env['omniauth.error.type'] = :service_unavailable
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context "SSL failure" do
|
97
|
+
before do
|
98
|
+
stub_request(:post, 'https://api.example.org/oauth/access_token').
|
99
|
+
to_raise(::OpenSSL::SSL::SSLError.new("SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed"))
|
100
|
+
get '/auth/example.org/callback', {:oauth_verifier => 'dudeman'}, {'rack.session' => { 'omniauth.xauth' => { 'x_auth_mode' => 'client_auth', 'x_auth_username' => 'username', 'x_auth_password' => 'password' }}}
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'should call fail! with :service_unavailable' do
|
104
|
+
last_request.env['omniauth.error'].should be_kind_of(::OpenSSL::SSL::SSLError)
|
105
|
+
last_request.env['omniauth.error.type'] = :service_unavailable
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
context 'Unauthorized failure' do
|
110
|
+
before do
|
111
|
+
stub_request(:post, 'https://api.example.org/oauth/access_token').
|
112
|
+
to_raise(::OAuth::Unauthorized.new("Unauthorized"))
|
113
|
+
get '/auth/example.org/callback', {}, {'rack.session' => { 'omniauth.xauth' => { 'x_auth_mode' => 'client_auth', 'x_auth_username' => 'username', 'x_auth_password' => 'password' }}}
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'should call fail! with :service_unavailable' do
|
117
|
+
last_request.env['omniauth.error'].should be_kind_of(::OAuth::Unauthorized)
|
118
|
+
last_request.env['omniauth.error.type'] = :invalid_credentials
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
context 'JSON Parse error' do
|
123
|
+
before do
|
124
|
+
stub_request(:post, 'https://api.example.org/oauth/access_token').
|
125
|
+
to_raise(::MultiJson::DecodeError.new("Parse Error", 'foo', 'bar'))
|
126
|
+
get '/auth/example.org/callback', {}, {'rack.session' => { 'omniauth.xauth' => { 'x_auth_mode' => 'client_auth', 'x_auth_username' => 'username', 'x_auth_password' => 'password' }}}
|
127
|
+
end
|
128
|
+
|
129
|
+
it 'should call fail! with :service_unavailable' do
|
130
|
+
last_request.env['omniauth.error'].should be_kind_of(::MultiJson::DecodeError)
|
131
|
+
last_request.env['omniauth.error.type'] = :invalid_response
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
describe '/auth/{name}/callback with expired session' do
|
137
|
+
before do
|
138
|
+
stub_request(:post, 'https://api.example.org/oauth/access_token').
|
139
|
+
to_return(:body => "oauth_token=yourtoken&oauth_token_secret=yoursecret")
|
140
|
+
get '/auth/example.org/callback', {:oauth_verifier => 'dudeman'}, {'rack.session' => {}}
|
141
|
+
end
|
142
|
+
|
143
|
+
it 'should call fail! with :session_expired' do
|
144
|
+
last_request.env['omniauth.error'].should be_kind_of(::OmniAuth::NoSessionError)
|
145
|
+
last_request.env['omniauth.error.type'] = :session_expired
|
146
|
+
end
|
147
|
+
end
|
148
|
+
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-xauth'
|
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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: omniauth-xauth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,22 +9,22 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-02-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: omniauth
|
16
|
-
requirement: &
|
16
|
+
requirement: &70135755385160 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
|
-
- -
|
19
|
+
- - ~>
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: '0'
|
21
|
+
version: '1.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70135755385160
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: oauth
|
27
|
-
requirement: &
|
27
|
+
requirement: &70135755383940 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,18 +32,51 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70135755383940
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
|
-
name:
|
38
|
-
requirement: &
|
37
|
+
name: rspec
|
38
|
+
requirement: &70135755383300 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '2.8'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70135755383300
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: webmock
|
49
|
+
requirement: &70135755382480 !ruby/object:Gem::Requirement
|
39
50
|
none: false
|
40
51
|
requirements:
|
41
52
|
- - ! '>='
|
42
53
|
- !ruby/object:Gem::Version
|
43
54
|
version: '0'
|
44
|
-
type: :
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70135755382480
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: simplecov
|
60
|
+
requirement: &70135755381180 !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: *70135755381180
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rack-test
|
71
|
+
requirement: &70135755380380 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
45
78
|
prerelease: false
|
46
|
-
version_requirements: *
|
79
|
+
version_requirements: *70135755380380
|
47
80
|
description: Abstract XAuth strategy for OmniAuth
|
48
81
|
email:
|
49
82
|
- aereal@kerare.org
|
@@ -52,12 +85,16 @@ extensions: []
|
|
52
85
|
extra_rdoc_files: []
|
53
86
|
files:
|
54
87
|
- .gitignore
|
88
|
+
- .rspec
|
55
89
|
- Gemfile
|
90
|
+
- README.md
|
56
91
|
- Rakefile
|
57
92
|
- lib/omniauth-xauth.rb
|
58
93
|
- lib/omniauth-xauth/version.rb
|
59
94
|
- lib/omniauth/strategies/xauth.rb
|
60
95
|
- omniauth-xauth.gemspec
|
96
|
+
- spec/omniauth/strategies/xauth_spec.rb
|
97
|
+
- spec/spec_helper.rb
|
61
98
|
homepage: https://github.com/aereal/omniauth-xauth
|
62
99
|
licenses: []
|
63
100
|
post_install_message:
|
@@ -78,8 +115,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
115
|
version: '0'
|
79
116
|
requirements: []
|
80
117
|
rubyforge_project: omniauth-xauth
|
81
|
-
rubygems_version: 1.8.
|
118
|
+
rubygems_version: 1.8.17
|
82
119
|
signing_key:
|
83
120
|
specification_version: 3
|
84
121
|
summary: Abstract XAuth strategy for OmniAuth
|
85
|
-
test_files:
|
122
|
+
test_files:
|
123
|
+
- spec/omniauth/strategies/xauth_spec.rb
|
124
|
+
- spec/spec_helper.rb
|
125
|
+
has_rdoc:
|