omniauth 0.3.0.rc3 → 1.0.0.beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,34 @@
1
+ module OmniAuth
2
+
3
+ module Test
4
+
5
+ module StrategyMacros
6
+
7
+ def sets_an_auth_hash
8
+ it 'should set an auth hash' do
9
+ last_request.env['omniauth.auth'].should be_kind_of(Hash)
10
+ end
11
+ end
12
+
13
+ def sets_provider_to(provider)
14
+ it "should set the provider to #{provider}" do
15
+ (last_request.env['omniauth.auth'] || {})['provider'].should == provider
16
+ end
17
+ end
18
+
19
+ def sets_uid_to(uid)
20
+ it "should set the UID to #{uid}" do
21
+ (last_request.env['omniauth.auth'] || {})['uid'].should == uid
22
+ end
23
+ end
24
+
25
+ def sets_user_info_to(user_info)
26
+ it "should set the user_info to #{user_info}" do
27
+ (last_request.env['omniauth.auth'] || {})['user_info'].should == user_info
28
+ end
29
+ end
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,49 @@
1
+ require 'rack'
2
+ require 'omniauth/test'
3
+
4
+ module OmniAuth
5
+
6
+ module Test
7
+
8
+ # Support for testing OmniAuth strategies.
9
+ #
10
+ # @example Usage
11
+ # class MyStrategyTest < Test::Unit::TestCase
12
+ # include OmniAuth::Test::StrategyTestCase
13
+ # def strategy
14
+ # # return the parameters to a Rack::Builder map call:
15
+ # [MyStrategy.new, :some, :configuration, :options => 'here']
16
+ # end
17
+ # setup do
18
+ # post '/auth/my_strategy/callback', :user => { 'name' => 'Dylan', 'id' => '445' }
19
+ # end
20
+ # end
21
+ module StrategyTestCase
22
+
23
+ def app
24
+ strat = self.strategy
25
+ resp = self.app_response
26
+ Rack::Builder.new {
27
+ use OmniAuth::Test::PhonySession
28
+ use *strat
29
+ run lambda {|env| [404, {'Content-Type' => 'text/plain'}, [resp || env.key?('omniauth.auth').to_s]] }
30
+ }.to_app
31
+ end
32
+
33
+ def app_response
34
+ nil
35
+ end
36
+
37
+ def session
38
+ last_request.env['rack.session']
39
+ end
40
+
41
+ def strategy
42
+ raise NotImplementedError.new('Including specs must define #strategy')
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,12 @@
1
+ module OmniAuth
2
+
3
+ # Support for testing OmniAuth strategies.
4
+ module Test
5
+
6
+ autoload :PhonySession, 'omniauth/test/phony_session'
7
+ autoload :StrategyMacros, 'omniauth/test/strategy_macros'
8
+ autoload :StrategyTestCase, 'omniauth/test/strategy_test_case'
9
+
10
+ end
11
+
12
+ end
@@ -1,19 +1,3 @@
1
1
  module OmniAuth
2
- module Version
3
- unless defined?(::OmniAuth::Version::MAJOR)
4
- MAJOR = 0
5
- end
6
- unless defined?(::OmniAuth::Version::MINOR)
7
- MINOR = 3
8
- end
9
- unless defined?(::OmniAuth::Version::PATCH)
10
- PATCH = 0
11
- end
12
- unless defined?(::OmniAuth::Version::PRE)
13
- PRE = "rc3"
14
- end
15
- unless defined?(::OmniAuth::Version::STRING)
16
- STRING = [MAJOR, MINOR, PATCH, PRE].compact.join('.')
17
- end
18
- end
2
+ VERSION = "1.0.0.beta1"
19
3
  end
data/lib/omniauth.rb CHANGED
@@ -1,6 +1,137 @@
1
- require 'omniauth/core'
2
- require 'omniauth/basic'
3
- require 'omniauth/oauth'
4
- require 'omniauth/openid'
5
- require 'omniauth/enterprise'
6
- require 'omniauth/more'
1
+ require 'rack'
2
+ require 'singleton'
3
+
4
+ module OmniAuth
5
+ module Strategies
6
+ autoload :Developer, 'omniauth/strategies/developer'
7
+ end
8
+
9
+ autoload :Builder, 'omniauth/builder'
10
+ autoload :Strategy, 'omniauth/strategy'
11
+ autoload :Test, 'omniauth/test'
12
+ autoload :Form, 'omniauth/form'
13
+ autoload :AuthHash, 'omniauth/auth_hash'
14
+
15
+ def self.strategies
16
+ @@strategies ||= []
17
+ end
18
+
19
+ class Configuration
20
+ include Singleton
21
+
22
+ @@defaults = {
23
+ :camelizations => {},
24
+ :path_prefix => '/auth',
25
+ :on_failure => Proc.new do |env|
26
+ message_key = env['omniauth.error.type']
27
+ new_path = "#{OmniAuth.config.path_prefix}/failure?message=#{message_key}"
28
+ [302, {'Location' => new_path, 'Content-Type'=> 'text/html'}, []]
29
+ end,
30
+ :form_css => Form::DEFAULT_CSS,
31
+ :test_mode => false,
32
+ :allowed_request_methods => [:get, :post],
33
+ :mock_auth => {
34
+ :default => {
35
+ 'provider' => 'default',
36
+ 'uid' => '1234',
37
+ 'name' => 'Bob Example'
38
+ }
39
+ }
40
+ }
41
+
42
+ def self.defaults
43
+ @@defaults
44
+ end
45
+
46
+ def initialize
47
+ @@defaults.each_pair{|k,v| self.send("#{k}=",v)}
48
+ end
49
+
50
+ def on_failure(&block)
51
+ if block_given?
52
+ @on_failure = block
53
+ else
54
+ @on_failure
55
+ end
56
+ end
57
+
58
+ def add_mock(provider, mock={})
59
+ # Stringify keys recursively one level.
60
+ mock.keys.each do |key|
61
+ mock[key.to_s] = mock.delete(key)
62
+ end
63
+ mock.each_pair do |key, val|
64
+ if val.is_a? Hash
65
+ val.keys.each do |subkey|
66
+ val[subkey.to_s] = val.delete(subkey)
67
+ end
68
+ end
69
+ end
70
+
71
+ # Merge with the default mock and ensure provider is correct.
72
+ mock = self.mock_auth[:default].dup.merge(mock)
73
+ mock["provider"] = provider.to_s
74
+
75
+ # Add it to the mocks.
76
+ self.mock_auth[provider.to_sym] = mock
77
+ end
78
+
79
+ # This is a convenience method to be used by strategy authors
80
+ # so that they can add special cases to the camelization utility
81
+ # method that allows OmniAuth::Builder to work.
82
+ #
83
+ # @param name [String] The underscored name, e.g. `oauth`
84
+ # @param camelized [String] The properly camelized name, e.g. 'OAuth'
85
+ def add_camelization(name, camelized)
86
+ self.camelizations[name.to_s] = camelized.to_s
87
+ end
88
+
89
+ attr_writer :on_failure
90
+ attr_accessor :path_prefix, :allowed_request_methods, :form_css, :test_mode, :mock_auth, :full_host, :camelizations
91
+ end
92
+
93
+ def self.config
94
+ Configuration.instance
95
+ end
96
+
97
+ def self.configure
98
+ yield config
99
+ end
100
+
101
+ def self.mock_auth_for(provider)
102
+ config.mock_auth[provider.to_sym] || config.mock_auth[:default]
103
+ end
104
+
105
+ module Utils
106
+ module_function
107
+
108
+ def form_css
109
+ "<style type='text/css'>#{OmniAuth.config.form_css}</style>"
110
+ end
111
+
112
+ def deep_merge(hash, other_hash)
113
+ target = hash.dup
114
+
115
+ other_hash.keys.each do |key|
116
+ if other_hash[key].is_a? ::Hash and hash[key].is_a? ::Hash
117
+ target[key] = deep_merge(target[key],other_hash[key])
118
+ next
119
+ end
120
+
121
+ target[key] = other_hash[key]
122
+ end
123
+
124
+ target
125
+ end
126
+
127
+ def camelize(word, first_letter_in_uppercase = true)
128
+ return OmniAuth.config.camelizations[word.to_s] if OmniAuth.config.camelizations[word.to_s]
129
+
130
+ if first_letter_in_uppercase
131
+ word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
132
+ else
133
+ word.first + camelize(word)[1..-1]
134
+ end
135
+ end
136
+ end
137
+ end
data/omniauth.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+ require File.expand_path('../lib/omniauth/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = 'omniauth'
6
+ gem.description = %q{A generalized Rack framework for multiple-provider authentication.}
7
+ gem.authors = ['Michael Bleigh', 'Erik Michaels-Ober']
8
+ gem.email = ['michael@intridea.com', 'sferik@gmail.com']
9
+
10
+ gem.add_runtime_dependency 'rack'
11
+ gem.add_runtime_dependency 'hashie', '~> 1.2'
12
+
13
+ gem.add_development_dependency 'maruku', '~> 0.6'
14
+ gem.add_development_dependency 'simplecov', '~> 0.4'
15
+ gem.add_development_dependency 'rack-test', '~> 0.5'
16
+ gem.add_development_dependency 'rake', '~> 0.8'
17
+ gem.add_development_dependency 'rdiscount', '~> 1.6'
18
+ gem.add_development_dependency 'rspec', '~> 2.5'
19
+ gem.add_development_dependency 'yard', '~> 0.7'
20
+
21
+ gem.version = OmniAuth::VERSION
22
+ gem.files = `git ls-files`.split("\n")
23
+ gem.homepage = 'http://github.com/intridea/omniauth'
24
+ gem.require_paths = ['lib']
25
+ gem.required_rubygems_version = Gem::Requirement.new('>= 1.3.6') if gem.respond_to? :required_rubygems_version=
26
+ gem.summary = gem.description
27
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
28
+ end
@@ -0,0 +1,108 @@
1
+ require 'spec_helper'
2
+
3
+ describe OmniAuth::AuthHash do
4
+ subject{ OmniAuth::AuthHash.new }
5
+ it 'should convert a supplied info key into an InfoHash object' do
6
+ subject.info = {:first_name => 'Awesome'}
7
+ subject.info.should be_kind_of(OmniAuth::AuthHash::InfoHash)
8
+ subject.info.first_name.should == 'Awesome'
9
+ end
10
+
11
+ describe '#valid?' do
12
+ subject{ OmniAuth::AuthHash.new(:uid => '123', :provider => 'example', :info => {:name => 'Steven'}) }
13
+
14
+ it 'should be valid with the right parameters' do
15
+ subject.should be_valid
16
+ end
17
+
18
+ it 'should require a uid' do
19
+ subject.uid = nil
20
+ subject.should_not be_valid
21
+ end
22
+
23
+ it 'should require a provider' do
24
+ subject.provider = nil
25
+ subject.should_not be_valid
26
+ end
27
+
28
+ it 'should require a name in the user info hash' do
29
+ subject.info.name = nil
30
+ subject.should_not be_valid?
31
+ end
32
+ end
33
+
34
+ describe '#name' do
35
+ subject{ OmniAuth::AuthHash.new(
36
+ :info => {
37
+ :name => 'Phillip J. Fry',
38
+ :first_name => 'Phillip',
39
+ :last_name => 'Fry',
40
+ :nickname => 'meatbag',
41
+ :email => 'fry@planetexpress.com'
42
+ })}
43
+
44
+ it 'should default to the name key' do
45
+ subject.info.name.should == 'Phillip J. Fry'
46
+ end
47
+
48
+ it 'should fall back to go to first_name last_name concatenation' do
49
+ subject.info.name = nil
50
+ subject.info.name.should == 'Phillip Fry'
51
+ end
52
+
53
+ it 'should display only a first or last name if only that is available' do
54
+ subject.info.name = nil
55
+ subject.info.first_name = nil
56
+ subject.info.name.should == 'Fry'
57
+ end
58
+
59
+ it 'should display the nickname if no name, first, or last is available' do
60
+ subject.info.name = nil
61
+ %w(first_name last_name).each{|k| subject.info[k] = nil}
62
+ subject.info.name.should == 'meatbag'
63
+ end
64
+
65
+ it 'should display the email if no name, first, last, or nick is available' do
66
+ subject.info.name = nil
67
+ %w(first_name last_name nickname).each{|k| subject.info[k] = nil}
68
+ subject.info.name.should == 'fry@planetexpress.com'
69
+ end
70
+ end
71
+
72
+ describe '#to_hash' do
73
+ subject{ OmniAuth::AuthHash.new(:uid => '123', :provider => 'test', :name => 'Bob Example')}
74
+ let(:hash){ subject.to_hash }
75
+
76
+ it 'should be a plain old hash' do
77
+ hash.class.should == ::Hash
78
+ end
79
+
80
+ it 'should have string keys' do
81
+ hash.keys.should be_include('uid')
82
+ end
83
+
84
+ it 'should convert an info hash as well' do
85
+ subject.info = {:first_name => 'Bob', :last_name => 'Example'}
86
+ subject.info.class.should == OmniAuth::AuthHash::InfoHash
87
+ subject.to_hash['info'].class.should == ::Hash
88
+ end
89
+
90
+ it 'should supply the calculated name in the converted hash' do
91
+ subject.info = {:first_name => 'Bob', :last_name => 'Examplar'}
92
+ hash['info']['name'].should == 'Bob Examplar'
93
+ end
94
+
95
+ it 'should not pollute the URL hash with "name" etc' do
96
+ subject.info = {'urls' => {'Homepage' => "http://homepage.com"}}
97
+ subject.to_hash['info']['urls'].should == {'Homepage' => "http://homepage.com"}
98
+ end
99
+ end
100
+
101
+ describe OmniAuth::AuthHash::InfoHash do
102
+ describe '#valid?' do
103
+ it 'should be valid if there is a name' do
104
+ OmniAuth::AuthHash::InfoHash.new(:name => 'Awesome').should be_valid
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,20 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe OmniAuth::Builder do
4
+ describe '#provider' do
5
+ it 'should translate a symbol to a constant' do
6
+ OmniAuth::Strategies.should_receive(:const_get).with('MyStrategy').and_return(Class.new)
7
+ OmniAuth::Builder.new(nil) do
8
+ provider :my_strategy
9
+ end
10
+ end
11
+
12
+ it 'should also just accept a class' do
13
+ class ::ExampleClass; end
14
+
15
+ lambda{ OmniAuth::Builder.new(nil) do
16
+ provider ::ExampleClass
17
+ end }.should_not raise_error
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe OmniAuth::Strategies::Developer do
4
+ let(:app){ Rack::Builder.new do |b|
5
+ b.use Rack::Session::Cookie
6
+ b.use OmniAuth::Strategies::Developer
7
+ b.run lambda{|env| [200, {}, ['Not Found']]}
8
+ end.to_app }
9
+
10
+ context 'request phase' do
11
+ before(:each){ get '/auth/developer' }
12
+
13
+ it 'should display a form' do
14
+ last_response.status.should == 200
15
+ last_response.body.should be_include("<form")
16
+ end
17
+
18
+ it 'should have the callback as the action for the form' do
19
+ last_response.body.should be_include("action='/auth/developer/callback'")
20
+ end
21
+
22
+ it 'should have a text field for each of the fields' do
23
+ last_response.body.scan('<input').size.should == 2
24
+ end
25
+ end
26
+
27
+ context 'callback phase' do
28
+ let(:auth_hash){ last_request.env['omniauth.auth'] }
29
+
30
+ context 'with default options' do
31
+ before do
32
+ post '/auth/developer/callback', :name => 'Example User', :email => 'user@example.com'
33
+ end
34
+
35
+ it 'should set the name in the auth hash' do
36
+ auth_hash.info.name.should == 'Example User'
37
+ end
38
+
39
+ it 'should set the email in the auth hash' do
40
+ auth_hash.info.email.should == 'user@example.com'
41
+ end
42
+
43
+ it 'should set the uid to the email' do
44
+ auth_hash.uid.should == 'user@example.com'
45
+ end
46
+ end
47
+
48
+ context 'with custom options' do
49
+ let(:app){ Rack::Builder.new do |b|
50
+ b.use Rack::Session::Cookie
51
+ b.use OmniAuth::Strategies::Developer, :fields => [:first_name, :last_name], :uid_field => :last_name
52
+ b.run lambda{|env| [200, {}, ['Not Found']]}
53
+ end.to_app }
54
+
55
+ before do
56
+ @options = {:uid_field => :last_name, :fields => [:first_name, :last_name]}
57
+ post '/auth/developer/callback', :first_name => 'Example', :last_name => 'User'
58
+ end
59
+
60
+ it 'should set info fields properly' do
61
+ auth_hash.info.name.should == 'Example User'
62
+ end
63
+
64
+ it 'should set the uid properly' do
65
+ auth_hash.uid.should == 'User'
66
+ end
67
+ end
68
+ end
69
+ end