omniauth-google-oauth2-moses 0.1.10
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.rvmrc +1 -0
- data/Gemfile +7 -0
- data/README.md +15 -0
- data/Rakefile +7 -0
- data/examples/config.ru +40 -0
- data/examples/omni_auth.rb +6 -0
- data/lib/omniauth-google-oauth2.rb +1 -0
- data/lib/omniauth/google_oauth2.rb +1 -0
- data/lib/omniauth/google_oauth2/version.rb +5 -0
- data/lib/omniauth/strategies/google_oauth2.rb +73 -0
- data/omniauth-contrib.gemspec +24 -0
- data/spec/omniauth/strategies/google_oauth2_spec.rb +64 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/support/shared_examples.rb +37 -0
- metadata +144 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use ree@omniauth-google-oauth2 --create
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# OmniAuth Google OAuth2 Strategy
|
2
|
+
|
3
|
+
Strategy to auth with Google via OAuth2 in OmniAuth.
|
4
|
+
|
5
|
+
Get your API key at https://code.google.com/apis/console/
|
6
|
+
|
7
|
+
## License
|
8
|
+
|
9
|
+
Copyright (c) 2011 by Josh Ellithorpe
|
10
|
+
|
11
|
+
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:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
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
data/examples/config.ru
ADDED
@@ -0,0 +1,40 @@
|
|
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-google-oauth2'
|
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/google_oauth2'>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
|
+
provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'], {
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
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 :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'], {access_type: 'online', approval_prompt: ''}
|
6
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "omniauth/google_oauth2"
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'omniauth/strategies/google_oauth2'
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'omniauth/strategies/oauth2'
|
2
|
+
|
3
|
+
module OmniAuth
|
4
|
+
module Strategies
|
5
|
+
class GoogleOauth2 < OmniAuth::Strategies::OAuth2
|
6
|
+
|
7
|
+
# Possible scopes: userinfo.email,userinfo.profile,plus.me
|
8
|
+
DEFAULT_SCOPE = "userinfo.email,userinfo.profile"
|
9
|
+
|
10
|
+
option :name, 'google_oauth2'
|
11
|
+
option :authorize_options, [:scope, :approval_prompt, :access_type]
|
12
|
+
|
13
|
+
option :client_options, {
|
14
|
+
:site => 'https://accounts.google.com',
|
15
|
+
:authorize_url => '/o/oauth2/auth',
|
16
|
+
:token_url => '/o/oauth2/token'
|
17
|
+
}
|
18
|
+
|
19
|
+
def authorize_params
|
20
|
+
base_scope_url = "https://www.googleapis.com/auth/"
|
21
|
+
super.tap do |params|
|
22
|
+
scopes = (params[:scope] || DEFAULT_SCOPE).split(",")
|
23
|
+
scopes.map! { |s| s =~ /^https?:\/\// ? s : "#{base_scope_url}#{s}" }
|
24
|
+
params[:scope] = scopes.join(' ')
|
25
|
+
# This makes sure we get a refresh_token.
|
26
|
+
# http://googlecode.blogspot.com/2011/10/upcoming-changes-to-oauth-20-endpoint.html
|
27
|
+
params[:access_type] = 'offline' if params[:access_type].nil?
|
28
|
+
params[:approval_prompt] = 'force' if params[:approval_prompt].nil?
|
29
|
+
params[:state] = request.params['state'] if request.params['state']
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
uid{ raw_info['id'] || verified_email }
|
34
|
+
|
35
|
+
info do
|
36
|
+
prune!({
|
37
|
+
:name => raw_info['name'],
|
38
|
+
:email => verified_email,
|
39
|
+
:first_name => raw_info['given_name'],
|
40
|
+
:last_name => raw_info['family_name'],
|
41
|
+
:image => raw_info['picture'],
|
42
|
+
:urls => {
|
43
|
+
'Profile' => raw_info['link']
|
44
|
+
}
|
45
|
+
})
|
46
|
+
end
|
47
|
+
|
48
|
+
extra do
|
49
|
+
prune!({
|
50
|
+
'raw_info' => raw_info
|
51
|
+
})
|
52
|
+
end
|
53
|
+
|
54
|
+
def raw_info
|
55
|
+
@raw_info ||= access_token.get('https://www.googleapis.com/oauth2/v1/userinfo').parsed
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def prune!(hash)
|
61
|
+
hash.delete_if do |_, value|
|
62
|
+
prune!(value) if value.is_a?(Hash)
|
63
|
+
value.nil? || (value.respond_to?(:empty?) && value.empty?)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def verified_email
|
68
|
+
raw_info['verified_email'] ? raw_info['email'] : nil
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/omniauth/google_oauth2/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.add_dependency 'omniauth', '~> 1.0'
|
6
|
+
|
7
|
+
gem.authors = ["Josh Ellithorpe", "Yury Korolev", "Moses Hohman"]
|
8
|
+
gem.email = ["quest@mac.com"]
|
9
|
+
gem.description = %q{A Google oauth2 strategy for OmniAuth 1.0}
|
10
|
+
gem.summary = %q{A Google 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-google-oauth2-moses"
|
17
|
+
gem.require_paths = ["lib"]
|
18
|
+
gem.version = OmniAuth::GoogleOauth2::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,64 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'omniauth-google-oauth2'
|
3
|
+
|
4
|
+
describe OmniAuth::Strategies::GoogleOauth2 do
|
5
|
+
before(:each) do
|
6
|
+
@request = double('Rack::Request')
|
7
|
+
@request.stub(:params) { {} }
|
8
|
+
end
|
9
|
+
|
10
|
+
subject do
|
11
|
+
OmniAuth::Strategies::GoogleOauth2.new(nil, @options || {}).tap do |strategy|
|
12
|
+
strategy.stub(:request) { @request }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it_should_behave_like 'an oauth2 strategy'
|
17
|
+
|
18
|
+
describe '#client' do
|
19
|
+
it 'has correct Google site' do
|
20
|
+
subject.client.site.should eq('https://accounts.google.com')
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'has correct authorize url' do
|
24
|
+
subject.client.options[:authorize_url].should eq('/o/oauth2/auth')
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'has correct token url' do
|
28
|
+
subject.client.options[:token_url].should eq('/o/oauth2/token')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#callback_path' do
|
33
|
+
it "has the correct callback path" do
|
34
|
+
subject.callback_path.should eq('/auth/google_oauth2/callback')
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe '#authorize_params' do
|
39
|
+
it 'should expand scope shortcuts' do
|
40
|
+
@options = { :authorize_options => [:scope], :scope => 'userinfo.email'}
|
41
|
+
subject.authorize_params['scope'].should eq('https://www.googleapis.com/auth/userinfo.email')
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should leave full scopes as is' do
|
45
|
+
@options = { :authorize_options => [:scope], :scope => 'https://www.googleapis.com/auth/userinfo.profile'}
|
46
|
+
subject.authorize_params['scope'].should eq('https://www.googleapis.com/auth/userinfo.profile')
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should join scopes' do
|
50
|
+
@options = { :authorize_options => [:scope], :scope => 'userinfo.profile,userinfo.email'}
|
51
|
+
subject.authorize_params['scope'].should eq('https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email')
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should set default scope to userinfo.email,userinfo.profile' do
|
55
|
+
@options = { :authorize_options => [:scope]}
|
56
|
+
subject.authorize_params['scope'].should eq('https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile')
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'should include request.params[:state] when present' do
|
60
|
+
@request.stub(:params) { { 'state' => 'some_state' } }
|
61
|
+
subject.authorize_params[:state].should eq('some_state')
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -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,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: omniauth-google-oauth2-moses
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 15
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 10
|
10
|
+
version: 0.1.10
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Josh Ellithorpe
|
14
|
+
- Yury Korolev
|
15
|
+
- Moses Hohman
|
16
|
+
autorequire:
|
17
|
+
bindir: bin
|
18
|
+
cert_chain: []
|
19
|
+
|
20
|
+
date: 2012-02-02 00:00:00 -06:00
|
21
|
+
default_executable:
|
22
|
+
dependencies:
|
23
|
+
- !ruby/object:Gem::Dependency
|
24
|
+
name: omniauth
|
25
|
+
prerelease: false
|
26
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
27
|
+
none: false
|
28
|
+
requirements:
|
29
|
+
- - ~>
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
hash: 15
|
32
|
+
segments:
|
33
|
+
- 1
|
34
|
+
- 0
|
35
|
+
version: "1.0"
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id001
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: omniauth-oauth2
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: 3
|
47
|
+
segments:
|
48
|
+
- 0
|
49
|
+
version: "0"
|
50
|
+
type: :runtime
|
51
|
+
version_requirements: *id002
|
52
|
+
- !ruby/object:Gem::Dependency
|
53
|
+
name: rspec
|
54
|
+
prerelease: false
|
55
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ~>
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 23
|
61
|
+
segments:
|
62
|
+
- 2
|
63
|
+
- 6
|
64
|
+
- 0
|
65
|
+
version: 2.6.0
|
66
|
+
type: :development
|
67
|
+
version_requirements: *id003
|
68
|
+
- !ruby/object:Gem::Dependency
|
69
|
+
name: rake
|
70
|
+
prerelease: false
|
71
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 3
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
type: :development
|
81
|
+
version_requirements: *id004
|
82
|
+
description: A Google oauth2 strategy for OmniAuth 1.0
|
83
|
+
email:
|
84
|
+
- quest@mac.com
|
85
|
+
executables: []
|
86
|
+
|
87
|
+
extensions: []
|
88
|
+
|
89
|
+
extra_rdoc_files: []
|
90
|
+
|
91
|
+
files:
|
92
|
+
- .gitignore
|
93
|
+
- .rvmrc
|
94
|
+
- Gemfile
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- examples/config.ru
|
98
|
+
- examples/omni_auth.rb
|
99
|
+
- lib/omniauth-google-oauth2.rb
|
100
|
+
- lib/omniauth/google_oauth2.rb
|
101
|
+
- lib/omniauth/google_oauth2/version.rb
|
102
|
+
- lib/omniauth/strategies/google_oauth2.rb
|
103
|
+
- omniauth-contrib.gemspec
|
104
|
+
- spec/omniauth/strategies/google_oauth2_spec.rb
|
105
|
+
- spec/spec_helper.rb
|
106
|
+
- spec/support/shared_examples.rb
|
107
|
+
has_rdoc: true
|
108
|
+
homepage: ""
|
109
|
+
licenses: []
|
110
|
+
|
111
|
+
post_install_message:
|
112
|
+
rdoc_options: []
|
113
|
+
|
114
|
+
require_paths:
|
115
|
+
- lib
|
116
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
117
|
+
none: false
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
hash: 3
|
122
|
+
segments:
|
123
|
+
- 0
|
124
|
+
version: "0"
|
125
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
none: false
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
hash: 3
|
131
|
+
segments:
|
132
|
+
- 0
|
133
|
+
version: "0"
|
134
|
+
requirements: []
|
135
|
+
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 1.6.2
|
138
|
+
signing_key:
|
139
|
+
specification_version: 3
|
140
|
+
summary: A Google oauth2 strategy for OmniAuth 1.0
|
141
|
+
test_files:
|
142
|
+
- spec/omniauth/strategies/google_oauth2_spec.rb
|
143
|
+
- spec/spec_helper.rb
|
144
|
+
- spec/support/shared_examples.rb
|