omniauth-allied_modders 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
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
18
+ .powenv
19
+ .idea/
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.2@omniauth-allied-modders --create
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :example do
6
+ gem 'sinatra'
7
+ end
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # OmniAuth AlliedModders Strategy
2
+
3
+ Strategy to auth with AlliedModders via OAuth2 in OmniAuth.
4
+
5
+ ## Installing
6
+
7
+ Add to your `Gemfile`:
8
+
9
+ ```ruby
10
+ gem "omniauth-allied_modders"
11
+ ```
12
+
13
+ Then `bundle install`.
14
+
15
+ ## Usage
16
+
17
+ Add the middleware to a Rails app in `config/initializers/omniauth.rb`:
18
+
19
+ ```ruby
20
+ Rails.application.config.middleware.use OmniAuth::Builder do
21
+ provider :allied_modders, "CLIENT_ID", "CLIENT_SECRET"
22
+ end
23
+ ```
24
+
25
+ Auth Url is: /auth/allied_modders
26
+
27
+ ## Thanks
28
+
29
+ This was written by Josh Ellithorpe. I merely made some edits to the original Google-OAuth2 strategy.
30
+
31
+ ## License
32
+
33
+ Copyright (c) 2012 by Josh Ellithorpe
34
+
35
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
36
+
37
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
38
+
39
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,43 @@
1
+ # Sample app for Google OAuth2 Strategy
2
+ # Make sure to setup the ENV variables GOOGLE_KEY and GOOGLE_SECRET
3
+ # Run with "bundle exec rackup"
4
+
5
+ require 'rubygems'
6
+ require 'bundler'
7
+ require 'sinatra'
8
+ require 'omniauth'
9
+ require 'omniauth-allied-modders'
10
+
11
+ OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
12
+
13
+ class App < Sinatra::Base
14
+ get '/' do
15
+ <<-HTML
16
+ <ul>
17
+ <li><a href='/auth/allied_modders'>Sign in with Google</a></li>
18
+ </ul>
19
+ HTML
20
+ end
21
+
22
+ get '/auth/:provider/callback' do
23
+ content_type 'text/plain'
24
+ request.env['omniauth.auth'].to_hash.inspect rescue "No Data"
25
+ end
26
+
27
+ get '/auth/failure' do
28
+ content_type 'text/plain'
29
+ request.env['omniauth.auth'].to_hash.inspect rescue "No Data"
30
+ end
31
+ end
32
+
33
+ use Rack::Session::Cookie, :secret => ENV['RACK_COOKIE_SECRET']
34
+
35
+ use OmniAuth::Builder do
36
+ # Regular usage
37
+ provider :allied_modders, ENV['ALLIEDMODDERS_CLIENT_ID'], ENV['ALLIEDMODDERS_SECRET'], {}
38
+
39
+ # Custom scope supporting youtube
40
+ # provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'], {:scope => 'http://gdata.youtube.com,userinfo.email,userinfo.profile,plus.me', :access_type => 'online', :approval_prompt => ''}
41
+ end
42
+
43
+ run App.new
@@ -0,0 +1,6 @@
1
+ Rails.application.config.middleware.use OmniAuth::Builder do
2
+ # If you don't need a refresh token -- if you're only using Google for account creation/auth and don't need google services -- set the access_type to 'online'.
3
+ # Also, set the approval prompt to an empty string, since otherwise it will be set to 'force', which makes users manually approve to the Oauth every time they log in.
4
+ # See http://googleappsdeveloper.blogspot.com/2011/10/upcoming-changes-to-oauth-20-endpoint.html
5
+ provider :allied_modders, ENV['ALLIEDMODDERS_CLIENT_ID'], ENV['ALLIEDMODDERS_SECRET'], {access_type: 'online', approval_prompt: ''}
6
+ end
@@ -0,0 +1 @@
1
+ require "omniauth/allied_modders"
@@ -0,0 +1 @@
1
+ require 'omniauth/strategies/allied_modders'
@@ -0,0 +1,5 @@
1
+ module OmniAuth
2
+ module AlliedModders
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,77 @@
1
+ require 'omniauth/strategies/oauth2'
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class AlliedModders < OmniAuth::Strategies::OAuth2
6
+
7
+ # Possible scopes: userinfo.email,userinfo.profile,plus.me
8
+ DEFAULT_SCOPE = ""
9
+
10
+ option :name, 'allied_modders'
11
+ option :authorize_options, [:scope, :approval_prompt, :access_type, :state, :hd]
12
+
13
+ option :client_options, {
14
+ :site => 'https://forums.alliedmods.net',
15
+ :authorize_url => '/oauth/auth.php',
16
+ :token_url => '/oauth/token.php'
17
+ }
18
+
19
+ def authorize_params
20
+ base_scope_url = ""
21
+ super.tap do |params|
22
+ # Read the params if passed directly to omniauth_authorize_path
23
+ %w(scope approval_prompt access_type state hd).each do |k|
24
+ params[k.to_sym] = request.params[k] unless [nil, ''].include?(request.params[k])
25
+ end
26
+ scopes = (params[:scope] || DEFAULT_SCOPE).split(",")
27
+ scopes.map! { |s| s =~ /^https?:\/\// ? s : "#{base_scope_url}#{s}" }
28
+ params[:scope] = scopes.join(' ')
29
+ # This makes sure we get a refresh_token.
30
+ # http://googlecode.blogspot.com/2011/10/upcoming-changes-to-oauth-20-endpoint.html
31
+ params[:access_type] = 'offline' if params[:access_type].nil?
32
+ params[:approval_prompt] = 'force' if params[:approval_prompt].nil?
33
+ # Override the state per request
34
+ session['omniauth.state'] = params[:state] if request.params['state']
35
+ end
36
+ end
37
+
38
+ uid{ raw_info['id'] || verified_email }
39
+
40
+ info do
41
+ prune!({
42
+ :name => raw_info['username'],
43
+ :nickname => raw_info['username'],
44
+ :email => verified_email,
45
+ :image => raw_info['avatar'],
46
+ :urls => {
47
+ 'AlliedModders' => "https://forums.alliedmods.net/member.php?u=#{uid}"
48
+ }
49
+ })
50
+ end
51
+
52
+ extra do
53
+ hash = {}
54
+ hash[:raw_info] = raw_info unless skip_info?
55
+ prune! hash
56
+ end
57
+
58
+ def raw_info
59
+ @raw_info ||= access_token.get('https://forums.alliedmods.net/oauth/userinfo.php').parsed
60
+ end
61
+
62
+ private
63
+
64
+ def prune!(hash)
65
+ hash.delete_if do |_, value|
66
+ prune!(value) if value.is_a?(Hash)
67
+ value.nil? || (value.respond_to?(:empty?) && value.empty?)
68
+ end
69
+ end
70
+
71
+ def verified_email
72
+ raw_info['verified_email'] ? raw_info['email'] : nil
73
+ end
74
+
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/omniauth/allied_modders/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.add_dependency 'omniauth', '~> 1.0'
6
+
7
+ gem.authors = ["Mitch Dempsey"]
8
+ gem.email = ["mitch@mitchdempsey.com"]
9
+ gem.description = %q{A AlliedModders oauth2 strategy for OmniAuth 1.0}
10
+ gem.summary = %q{A AlliedModders oauth2 strategy for OmniAuth 1.0}
11
+ gem.homepage = ""
12
+
13
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
14
+ gem.files = `git ls-files`.split("\n")
15
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ gem.name = "omniauth-allied_modders"
17
+ gem.require_paths = ["lib"]
18
+ gem.version = OmniAuth::AlliedModders::VERSION
19
+
20
+ gem.add_runtime_dependency 'omniauth-oauth2'
21
+
22
+ gem.add_development_dependency 'rspec', '~> 2.6.0'
23
+ gem.add_development_dependency 'rake'
24
+ end
@@ -0,0 +1,155 @@
1
+ require 'spec_helper'
2
+ require 'omniauth-allied-modders'
3
+
4
+ describe OmniAuth::Strategies::AlliedModders do
5
+ def app; lambda{|env| [200, {}, ["Hello."]]} end
6
+
7
+ before :each do
8
+ OmniAuth.config.test_mode = true
9
+ @request = double('Request')
10
+ @request.stub(:params) { {} }
11
+ @request.stub(:cookies) { {} }
12
+ @request.stub(:env) { {} }
13
+ end
14
+
15
+ after do
16
+ OmniAuth.config.test_mode = false
17
+ end
18
+
19
+ subject do
20
+ args = ['appid', 'secret', @options || {}].compact
21
+ OmniAuth::Strategies::AlliedModders.new(app, *args).tap do |strategy|
22
+ strategy.stub(:request) { @request }
23
+ end
24
+ end
25
+
26
+ it_should_behave_like 'an oauth2 strategy'
27
+
28
+ describe '#client' do
29
+ it 'has correct Google site' do
30
+ subject.client.site.should eq('https://forums.alliedmods.net')
31
+ end
32
+
33
+ it 'has correct authorize url' do
34
+ subject.client.options[:authorize_url].should eq('/oauth/auth.php')
35
+ end
36
+
37
+ it 'has correct token url' do
38
+ subject.client.options[:token_url].should eq('/oauth/token.php')
39
+ end
40
+ end
41
+
42
+ describe '#callback_path' do
43
+ it 'has the correct callback path' do
44
+ subject.callback_path.should eq('/auth/allied_modders/callback')
45
+ end
46
+ end
47
+
48
+ describe '#authorize_params' do
49
+ %w(approval_prompt access_type state hd).each do |k|
50
+ it "should set the #{k} authorize option dynamically in the request" do
51
+ @options = {k.to_sym => ''}
52
+ subject.stub(:request) { double('Request', {:params => { k => 'something' }, :env => {}}) }
53
+ subject.authorize_params[k].should eq('something')
54
+ end
55
+ end
56
+
57
+ describe 'scope' do
58
+ it 'should expand scope shortcuts' do
59
+ @options = { :authorize_options => [:scope], :scope => 'userinfo.email'}
60
+ subject.authorize_params['scope'].should eq('https://www.googleapis.com/auth/userinfo.email')
61
+ end
62
+
63
+ it 'should leave full scopes as is' do
64
+ @options = { :authorize_options => [:scope], :scope => 'https://www.googleapis.com/auth/userinfo.profile'}
65
+ subject.authorize_params['scope'].should eq('https://www.googleapis.com/auth/userinfo.profile')
66
+ end
67
+
68
+ it 'should join scopes' do
69
+ @options = { :authorize_options => [:scope], :scope => 'userinfo.profile,userinfo.email'}
70
+ subject.authorize_params['scope'].should eq('https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email')
71
+ end
72
+
73
+ it 'should set default scope to userinfo.email,userinfo.profile' do
74
+ @options = { :authorize_options => [:scope]}
75
+ subject.authorize_params['scope'].should eq('https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile')
76
+ end
77
+
78
+ it 'should dynamically set the scope in the request' do
79
+ @options = {:scope => 'http://example.com'}
80
+ subject.stub(:request) { double('Request', {:params => { 'scope' => 'userinfo.email' }, :env => {}}) }
81
+ subject.authorize_params['scope'].should eq('https://www.googleapis.com/auth/userinfo.email')
82
+ end
83
+ end
84
+
85
+ describe 'approval_prompt' do
86
+ it 'should set the approval_prompt parameter if present' do
87
+ @options = {:approval_prompt => 'prompt'}
88
+ subject.authorize_params['approval_prompt'].should eq('prompt')
89
+ end
90
+
91
+ it 'should default to "force"' do
92
+ @options = {}
93
+ subject.authorize_params['approval_prompt'].should eq('force')
94
+ end
95
+ end
96
+
97
+ describe 'access_type' do
98
+ it 'should set the access_type parameter if present' do
99
+ @options = {:access_type => 'type'}
100
+ subject.authorize_params['access_type'].should eq('type')
101
+ end
102
+
103
+ it 'should default to "offline"' do
104
+ @options = {}
105
+ subject.authorize_params['access_type'].should eq('offline')
106
+ end
107
+ end
108
+
109
+ describe 'state' do
110
+ it 'should set the state parameter' do
111
+ @options = {:state => 'some_state'}
112
+ subject.authorize_params['state'].should eq('some_state')
113
+ subject.session['omniauth.state'].should eq('some_state')
114
+ end
115
+
116
+ it 'should set the omniauth.state dynamically' do
117
+ subject.stub(:request) { double('Request', {:params => { 'state' => 'some_state' }, :env => {}}) }
118
+ subject.authorize_params['state'].should eq('some_state')
119
+ subject.session['omniauth.state'].should eq('some_state')
120
+ end
121
+ end
122
+
123
+ describe 'hd' do
124
+ it 'should set the hd (hosted domain) parameter if present' do
125
+ @options = {:hd => 'example.com'}
126
+ subject.authorize_params['hd'].should eq('example.com')
127
+ end
128
+ end
129
+ end
130
+
131
+ describe 'raw info' do
132
+ it 'should include raw_info in extras hash by default' do
133
+ subject.stub(:raw_info) { { :foo => 'bar' } }
134
+ subject.extra[:raw_info].should eq({ :foo => 'bar' })
135
+ end
136
+
137
+ it 'should not include raw_info in extras hash when skip_info is specified' do
138
+ @options = { :skip_info => true }
139
+ subject.extra.should_not have_key(:raw_info)
140
+ end
141
+ end
142
+
143
+ describe 'populate auth hash url' do
144
+ it 'should populate url map in auth hash if link present in raw_info' do
145
+ subject.stub(:raw_info) { { 'name' => 'Foo', 'link' => 'https://plus.google.com/123456' } }
146
+ subject.info[:urls]['Google'].should eq('https://plus.google.com/123456')
147
+ end
148
+
149
+ it 'should not populate url map in auth hash if no link present in raw_info' do
150
+ subject.stub(:raw_info) { { 'name' => 'Foo' } }
151
+ subject.info.should_not have_key(:urls)
152
+ end
153
+ end
154
+
155
+ end
@@ -0,0 +1,7 @@
1
+ require 'bundler/setup'
2
+ require 'rspec'
3
+
4
+ Dir[File.expand_path('../support/**/*', __FILE__)].each { |f| require f }
5
+
6
+ RSpec.configure do |config|
7
+ 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 => 'http://bar', :foo => 'baz' }
19
+ subject.authorize_params['scope'].should eq('http://bar')
20
+ subject.authorize_params['foo'].should eq('baz')
21
+ end
22
+ end
23
+
24
+ describe '#token_params' do
25
+ it 'should include any token params passed in the :token_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 :token_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,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-allied_modders
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mitch Dempsey
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: omniauth
16
+ requirement: &2156740420 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2156740420
25
+ - !ruby/object:Gem::Dependency
26
+ name: omniauth-oauth2
27
+ requirement: &2156753500 !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: *2156753500
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &2156752580 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 2.6.0
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2156752580
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: &2156751880 !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: *2156751880
58
+ description: A AlliedModders oauth2 strategy for OmniAuth 1.0
59
+ email:
60
+ - mitch@mitchdempsey.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - .gitignore
66
+ - .rvmrc
67
+ - Gemfile
68
+ - README.md
69
+ - Rakefile
70
+ - examples/config.ru
71
+ - examples/omni_auth.rb
72
+ - lib/omniauth-allied_modders.rb
73
+ - lib/omniauth/allied_modders.rb
74
+ - lib/omniauth/allied_modders/version.rb
75
+ - lib/omniauth/strategies/allied_modders.rb
76
+ - omniauth-contrib.gemspec
77
+ - spec/omniauth/strategies/allied_modders_spec.rb
78
+ - spec/spec_helper.rb
79
+ - spec/support/shared_examples.rb
80
+ homepage: ''
81
+ licenses: []
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ segments:
93
+ - 0
94
+ hash: 2762080650861133142
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ segments:
102
+ - 0
103
+ hash: 2762080650861133142
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 1.8.15
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: A AlliedModders oauth2 strategy for OmniAuth 1.0
110
+ test_files:
111
+ - spec/omniauth/strategies/allied_modders_spec.rb
112
+ - spec/spec_helper.rb
113
+ - spec/support/shared_examples.rb