omniauth-geni 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+
3
+ gemspec
4
+
5
+ gem 'jruby-openssl', :platform => :jruby
data/Gemfile.lock ADDED
@@ -0,0 +1,46 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ omniauth-geni (1.0.0)
5
+ omniauth-oauth2 (~> 1.0.0)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ addressable (2.2.6)
11
+ diff-lcs (1.1.3)
12
+ faraday (0.7.6)
13
+ addressable (~> 2.2)
14
+ multipart-post (~> 1.1)
15
+ rack (~> 1.1)
16
+ hashie (1.2.0)
17
+ multi_json (1.0.4)
18
+ multipart-post (1.1.4)
19
+ oauth2 (0.5.2)
20
+ faraday (~> 0.7)
21
+ multi_json (~> 1.0)
22
+ omniauth (1.0.2)
23
+ hashie (~> 1.2)
24
+ rack
25
+ omniauth-oauth2 (1.0.0)
26
+ oauth2 (~> 0.5.0)
27
+ omniauth (~> 1.0)
28
+ rack (1.4.1)
29
+ rake (0.9.2.2)
30
+ rspec (2.7.0)
31
+ rspec-core (~> 2.7.0)
32
+ rspec-expectations (~> 2.7.0)
33
+ rspec-mocks (~> 2.7.0)
34
+ rspec-core (2.7.1)
35
+ rspec-expectations (2.7.0)
36
+ diff-lcs (~> 1.1.2)
37
+ rspec-mocks (2.7.0)
38
+
39
+ PLATFORMS
40
+ ruby
41
+
42
+ DEPENDENCIES
43
+ jruby-openssl
44
+ omniauth-geni!
45
+ rake
46
+ rspec (~> 2.7.0)
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # OmniAuth Geni
2
+
3
+ Geni OAuth2 Strategy for OmniAuth 1.0.
4
+
5
+ Supports the OAuth 2.0 server-side. Read the Geni docs for more details: http://www.geni.com/platform/developer/help
6
+
7
+ ## Installing
8
+
9
+ Add to your `Gemfile`:
10
+
11
+ ```ruby
12
+ gem 'omniauth-geni'
13
+ ```
14
+
15
+ Then `bundle install`.
16
+
17
+ ## Usage
18
+
19
+ `OmniAuth::Strategies::Geni` is simply a Rack middleware. Read the OmniAuth 1.0 docs for detailed instructions: https://github.com/intridea/omniauth.
20
+
21
+ Here's a quick example, adding the middleware to a Rails app in `config/initializers/omniauth.rb`:
22
+
23
+ ```ruby
24
+ Rails.application.config.middleware.use OmniAuth::Builder do
25
+ provider :geni, ENV['GENI_KEY'], ENV['GENI_SECRET']
26
+ end
27
+ ```
28
+
29
+ ## Configuring
30
+
31
+ You can configure several options, which you pass in to the `provider` method via a `Hash`:
32
+
33
+ * `scope`: A comma-separated list of permissions you want to request from the user. See the Geni docs for a full list of available permissions. Default: `email`.
34
+ * `display`: The display context to show the authentication page. Options are: `web`, `desktop` and `mobile`. Default: `web`.
35
+
36
+ For example, to request `email` permission and display the authorization page in a mobile app:
37
+
38
+ ```ruby
39
+ Rails.application.config.middleware.use OmniAuth::Builder do
40
+ provider :geni, ENV['GENI_KEY'], ENV['GENI_SECRET'], :scope => 'email', :display => 'mobile'
41
+ end
42
+ ```
43
+
44
+ If you want to set the `display` format on a per-request basis, you can just pass it to the OmniAuth request phase URL, for example: `/auth/geni?display=popup`.
45
+
46
+ ## Authentication Hash
47
+
48
+ Here's an example *Authentication Hash* available in `request.env['omniauth.auth']`:
49
+
50
+ ```ruby
51
+ {
52
+ :provider => 'geni',
53
+ :uid => '123',
54
+ :info => {
55
+ :first_name => 'Alex',
56
+ :last_name => 'Thompson',
57
+ :email => 'alex@sample.com',
58
+ :name => 'Alex Thompson'
59
+ },
60
+ :credentials => {
61
+ :token => 'ABCDEF...', # OAuth 2.0 access_token
62
+ :expires_at => 1321747205 # when the access token expires
63
+ },
64
+ :extra => {
65
+ :profile => {
66
+ :id => '1234567',
67
+ :name => 'Alex Thompson',
68
+ :first_name => 'Alex',
69
+ :last_name => 'Thompson'
70
+ }
71
+ }
72
+ }
73
+ ```
74
+
75
+ The precise information available may depend on the permissions which you request.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/example/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source :rubygems
2
+
3
+ gem 'rack', '~> 1.3.6'
4
+
5
+ gem 'sinatra'
6
+ gem 'omniauth-geni', :path => '../'
@@ -0,0 +1,42 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ omniauth-geni (1.0.0)
5
+ omniauth-oauth2 (~> 1.0.0)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ addressable (2.2.6)
11
+ faraday (0.7.6)
12
+ addressable (~> 2.2)
13
+ multipart-post (~> 1.1)
14
+ rack (~> 1.1)
15
+ hashie (1.2.0)
16
+ multi_json (1.0.4)
17
+ multipart-post (1.1.4)
18
+ oauth2 (0.5.2)
19
+ faraday (~> 0.7)
20
+ multi_json (~> 1.0)
21
+ omniauth (1.0.2)
22
+ hashie (~> 1.2)
23
+ rack
24
+ omniauth-oauth2 (1.0.0)
25
+ oauth2 (~> 0.5.0)
26
+ omniauth (~> 1.0)
27
+ rack (1.3.6)
28
+ rack-protection (1.2.0)
29
+ rack
30
+ sinatra (1.3.2)
31
+ rack (~> 1.3, >= 1.3.6)
32
+ rack-protection (~> 1.2)
33
+ tilt (~> 1.3, >= 1.3.3)
34
+ tilt (1.3.3)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ omniauth-geni!
41
+ rack (~> 1.3.6)
42
+ sinatra
data/example/config.ru ADDED
@@ -0,0 +1,31 @@
1
+ require 'bundler/setup'
2
+ require 'sinatra/base'
3
+ require 'omniauth-geni'
4
+
5
+ SCOPE = 'email'
6
+
7
+ class App < Sinatra::Base
8
+
9
+ get '/' do
10
+ redirect '/auth/geni'
11
+ end
12
+
13
+ get '/auth/:provider/callback' do
14
+ content_type 'application/json'
15
+ MultiJson.encode(request.env)
16
+ end
17
+
18
+ get '/auth/failure' do
19
+ content_type 'application/json'
20
+ MultiJson.encode(request.env)
21
+ end
22
+
23
+ end
24
+
25
+ use Rack::Session::Cookie
26
+
27
+ use OmniAuth::Builder do
28
+ provider :geni, ENV['APP_ID'], ENV['APP_SECRET'], :scope => SCOPE
29
+ end
30
+
31
+ run App.new
@@ -0,0 +1,5 @@
1
+ module OmniAuth
2
+ module Geni
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require 'omniauth/geni/version'
2
+ require 'omniauth/strategies/geni'
@@ -0,0 +1,84 @@
1
+ require 'omniauth-oauth2'
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class Geni < OmniAuth::Strategies::OAuth2
6
+
7
+ option :client_options, {
8
+ :site => 'https://www.geni.com',
9
+ :authorize_url => '/platform/oauth/authorize',
10
+ :token_url => '/platform/oauth/request_token'
11
+ }
12
+
13
+ option :authorize_params, {
14
+
15
+ }
16
+
17
+ option :name, 'geni'
18
+
19
+ option :access_token_options, {
20
+ :header_format => 'OAuth %s',
21
+ :param_name => 'access_token'
22
+ }
23
+
24
+ option :authorize_options, [:scope, :display]
25
+
26
+ def request_phase
27
+ super
28
+ end
29
+
30
+ def build_access_token
31
+ token_params = {
32
+ :code => request.params['code'],
33
+ :redirect_uri => callback_url,
34
+ :client_id => client.id,
35
+ :client_secret => client.secret
36
+ }
37
+ client.get_token(token_params)
38
+ end
39
+
40
+ uid { raw_info['id'] }
41
+
42
+ info do
43
+ prune!({
44
+ 'name' => raw_info['name'],
45
+ 'first_name' => raw_info['first_name'],
46
+ 'last_name' => raw_info['last_name'],
47
+ 'email' => raw_info['email'],
48
+ 'gender' => raw_info['gender'],
49
+ 'mugshot_urls' => raw_info['mugshot_urls'],
50
+ 'name' => raw_info['name'],
51
+ 'url' => raw_info['url'],
52
+ })
53
+ end
54
+
55
+ extra do
56
+ { 'profile' => prune!(raw_info) }
57
+ end
58
+
59
+ def raw_info
60
+ @raw_info ||= access_token.get('/api/profile').parsed
61
+ end
62
+
63
+ def authorize_params
64
+ super.tap do |params|
65
+ params.merge!(:display => request.params['display']) if request.params['display']
66
+ params.merge!(:state => request.params['state']) if request.params['state']
67
+ params[:scope] ||= 'email'
68
+ end
69
+ end
70
+
71
+ private
72
+
73
+ def prune!(hash)
74
+ hash.delete_if do |_, value|
75
+ prune!(value) if value.is_a?(Hash)
76
+ value.nil? || (value.respond_to?(:empty?) && value.empty?)
77
+ end
78
+ end
79
+
80
+ end
81
+ end
82
+ end
83
+
84
+ OmniAuth.config.add_camelization 'geni', 'Geni'
@@ -0,0 +1 @@
1
+ require 'omniauth/geni'
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'omniauth/geni/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'omniauth-geni'
7
+ s.version = OmniAuth::Geni::VERSION
8
+ s.authors = ['Michael Berkovich']
9
+ s.email = ['theiceberk@gmail.com']
10
+ s.summary = 'Geni strategy for OmniAuth'
11
+ s.homepage = 'https://github.com/berk/omniauth-geni'
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
16
+ s.require_paths = ['lib']
17
+
18
+ s.add_runtime_dependency 'omniauth-oauth2', '~> 1.0.0'
19
+
20
+ s.add_development_dependency 'rspec', '~> 2.7.0'
21
+ s.add_development_dependency 'rake'
22
+ end
@@ -0,0 +1,180 @@
1
+ require 'spec_helper'
2
+ require 'omniauth-geni'
3
+
4
+ describe OmniAuth::Strategies::Geni do
5
+ before :each do
6
+ @request = double('Request')
7
+ @request.stub(:params) { {} }
8
+ @request.stub(:cookies) { {} }
9
+
10
+ @client_id = 'abc'
11
+ @client_secret = 'def'
12
+ end
13
+
14
+ subject do
15
+ args = [@client_id, @client_secret, @options].compact
16
+ OmniAuth::Strategies::Geni.new(nil, *args).tap do |strategy|
17
+ strategy.stub(:request) { @request }
18
+ end
19
+ end
20
+
21
+ it_should_behave_like 'an oauth2 strategy'
22
+
23
+ describe '#client' do
24
+ it 'has correct site' do
25
+ subject.client.site.should eq('https://www.geni.com')
26
+ end
27
+
28
+ it 'has correct authorize url' do
29
+ subject.client.options[:authorize_url].should eq('/platform/oauth/authorize')
30
+ end
31
+
32
+ it 'has correct token url' do
33
+ subject.client.options[:token_url].should eq('/platform/oauth/request_token')
34
+ end
35
+ end
36
+
37
+ describe '#authorize_params' do
38
+ it 'includes display parameter from request when present' do
39
+ @request.stub(:params) { { 'display' => 'mobile' } }
40
+ subject.authorize_params.should be_a(Hash)
41
+ subject.authorize_params[:display].should eq('mobile')
42
+ end
43
+
44
+ it 'includes state parameter from request when present' do
45
+ @request.stub(:params) { { 'state' => 'some_state' } }
46
+ subject.authorize_params.should be_a(Hash)
47
+ subject.authorize_params[:state].should eq('some_state')
48
+ end
49
+ end
50
+
51
+ describe '#uid' do
52
+ before :each do
53
+ subject.stub(:raw_info) { { 'id' => '123' } }
54
+ end
55
+
56
+ it 'returns the id from raw_info' do
57
+ subject.uid.should eq('123')
58
+ end
59
+ end
60
+
61
+ describe '#info' do
62
+ before :each do
63
+ @raw_info ||= { 'first_name' => 'Alex' }
64
+ subject.stub(:raw_info) { @raw_info }
65
+ end
66
+
67
+ context 'when optional data is not present in raw info' do
68
+ it 'has no email key' do
69
+ subject.info.should_not have_key('email')
70
+ end
71
+
72
+ it 'has no last name key' do
73
+ subject.info.should_not have_key('last_name')
74
+ end
75
+ end
76
+
77
+ context 'when data is present in raw info' do
78
+ it 'returns first name' do
79
+ subject.info['first_name'].should eq('Alex')
80
+ end
81
+
82
+ it 'returns the email' do
83
+ @raw_info['email'] = 'fred@smith.com'
84
+ subject.info['email'].should eq('fred@smith.com')
85
+ end
86
+ end
87
+ end
88
+
89
+ describe '#raw_info' do
90
+ before :each do
91
+ @access_token = double('OAuth2::AccessToken')
92
+ subject.stub(:access_token) { @access_token }
93
+ end
94
+
95
+ it 'performs a GET to https://www.geni.com/api/profile' do
96
+ @access_token.stub(:get) { double('OAuth2::Response').as_null_object }
97
+ @access_token.should_receive(:get).with('/api/profile')
98
+ subject.raw_info
99
+ end
100
+
101
+ it 'returns a Hash' do
102
+ @access_token.stub(:get).with('/api/profile') do
103
+ raw_response = double('Faraday::Response')
104
+ raw_response.stub(:body) { '{ "ohai": "thar" }' }
105
+ raw_response.stub(:status) { 200 }
106
+ raw_response.stub(:headers) { { 'Content-Type' => 'application/json' } }
107
+ OAuth2::Response.new(raw_response)
108
+ end
109
+ subject.raw_info.should be_a(Hash)
110
+ subject.raw_info['ohai'].should eq('thar')
111
+ end
112
+ end
113
+
114
+ describe '#credentials' do
115
+ before :each do
116
+ @access_token = double('OAuth2::AccessToken')
117
+ @access_token.stub(:token)
118
+ @access_token.stub(:expires?)
119
+ @access_token.stub(:expires_at)
120
+ @access_token.stub(:refresh_token)
121
+ subject.stub(:access_token) { @access_token }
122
+ end
123
+
124
+ it 'returns a Hash' do
125
+ subject.credentials.should be_a(Hash)
126
+ end
127
+
128
+ it 'returns the token' do
129
+ @access_token.stub(:token) { '123' }
130
+ subject.credentials['token'].should eq('123')
131
+ end
132
+
133
+ it 'returns the expiry status' do
134
+ @access_token.stub(:expires?) { true }
135
+ subject.credentials['expires'].should eq(true)
136
+
137
+ @access_token.stub(:expires?) { false }
138
+ subject.credentials['expires'].should eq(false)
139
+ end
140
+
141
+ it 'returns the refresh token and expiry time when expiring' do
142
+ ten_mins_from_now = (Time.now + 600).to_i
143
+ @access_token.stub(:expires?) { true }
144
+ @access_token.stub(:refresh_token) { '321' }
145
+ @access_token.stub(:expires_at) { ten_mins_from_now }
146
+ subject.credentials['refresh_token'].should eq('321')
147
+ subject.credentials['expires_at'].should eq(ten_mins_from_now)
148
+ end
149
+
150
+ it 'does not return the refresh token when it is nil and expiring' do
151
+ @access_token.stub(:expires?) { true }
152
+ @access_token.stub(:refresh_token) { nil }
153
+ subject.credentials['refresh_token'].should be_nil
154
+ subject.credentials.should_not have_key('refresh_token')
155
+ end
156
+
157
+ it 'does not return the refresh token when not expiring' do
158
+ @access_token.stub(:expires?) { false }
159
+ @access_token.stub(:refresh_token) { 'XXX' }
160
+ subject.credentials['refresh_token'].should be_nil
161
+ subject.credentials.should_not have_key('refresh_token')
162
+ end
163
+ end
164
+
165
+ describe '#extra' do
166
+ before :each do
167
+ @raw_info = { 'name' => 'Fred Smith' }
168
+ subject.stub(:raw_info) { @raw_info }
169
+ end
170
+
171
+ it 'returns a Hash' do
172
+ subject.extra.should be_a(Hash)
173
+ end
174
+
175
+ it 'contains raw info' do
176
+ subject.extra.should eq({ 'profile' => @raw_info })
177
+ end
178
+ end
179
+
180
+ end
@@ -0,0 +1,6 @@
1
+ require 'bundler/setup'
2
+ require 'rspec'
3
+ Dir[File.expand_path('../support/**/*', __FILE__)].each { |f| require f }
4
+
5
+ RSpec.configure do |config|
6
+ end
@@ -0,0 +1,37 @@
1
+ # NOTE it would be useful if this lived in omniauth-oauth2 eventually
2
+ shared_examples 'an oauth2 strategy' do
3
+ describe '#client' do
4
+ it 'should be initialized with symbolized client_options' do
5
+ @options = { :client_options => { 'authorize_url' => 'https://example.com' } }
6
+ subject.client.options[:authorize_url].should == 'https://example.com'
7
+ end
8
+ end
9
+
10
+ describe '#authorize_params' do
11
+ it 'should include any authorize params passed in the :authorize_params option' do
12
+ @options = { :authorize_params => { :foo => 'bar', :baz => 'zip' } }
13
+ subject.authorize_params['foo'].should eq('bar')
14
+ subject.authorize_params['baz'].should eq('zip')
15
+ end
16
+
17
+ it 'should include top-level options that are marked as :authorize_options' do
18
+ @options = { :authorize_options => [:scope, :foo], :scope => 'bar', :foo => 'baz' }
19
+ subject.authorize_params['scope'].should eq('bar')
20
+ subject.authorize_params['foo'].should eq('baz')
21
+ end
22
+ end
23
+
24
+ describe '#token_params' do
25
+ it 'should include any authorize params passed in the :authorize_params option' do
26
+ @options = { :token_params => { :foo => 'bar', :baz => 'zip' } }
27
+ subject.token_params['foo'].should eq('bar')
28
+ subject.token_params['baz'].should eq('zip')
29
+ end
30
+
31
+ it 'should include top-level options that are marked as :authorize_options' do
32
+ @options = { :token_options => [:scope, :foo], :scope => 'bar', :foo => 'baz' }
33
+ subject.token_params['scope'].should eq('bar')
34
+ subject.token_params['foo'].should eq('baz')
35
+ end
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-geni
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michael Berkovich
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-09 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: omniauth-oauth2
16
+ requirement: &70128714570740 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70128714570740
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70128714570100 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.7.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70128714570100
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &70128714569340 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70128714569340
47
+ description:
48
+ email:
49
+ - theiceberk@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - Gemfile
55
+ - Gemfile.lock
56
+ - README.md
57
+ - Rakefile
58
+ - example/Gemfile
59
+ - example/Gemfile.lock
60
+ - example/config.ru
61
+ - lib/omniauth-geni.rb
62
+ - lib/omniauth/geni.rb
63
+ - lib/omniauth/geni/version.rb
64
+ - lib/omniauth/strategies/geni.rb
65
+ - omniauth-geni.gemspec
66
+ - spec/omniauth/strategies/geni_spec.rb
67
+ - spec/spec_helper.rb
68
+ - spec/support/shared_examples.rb
69
+ homepage: https://github.com/berk/omniauth-geni
70
+ licenses: []
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 1.8.10
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Geni strategy for OmniAuth
93
+ test_files:
94
+ - spec/omniauth/strategies/geni_spec.rb
95
+ - spec/spec_helper.rb
96
+ - spec/support/shared_examples.rb