omniauth-google-oauth2-access-token 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0c21cb00daaff778fb5537433c65023e00eadd87
4
+ data.tar.gz: 2cc3530829415ee0d008b216e8514f31c606d6f5
5
+ SHA512:
6
+ metadata.gz: ff0c393cf089de440db7d41e9af01cfcba506125899077d2bac423baa1a72bd0f06c200ecefa5bd26fe7547985143b116da65c6e69ff9e3616fa60c693acf7fd
7
+ data.tar.gz: f77ea92331fb3d1be7d9cb924e7dd0e6169ffb112f3c4ef22e15a8f6e6da232cbc844fd419b0963d19fec2168371a1a462d39afff85de76369c85a2185f33e2f
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ Gemfile.lock
6
+ coverage
7
+ InstalledFiles
8
+ lib/bundler/man
9
+ pkg
10
+ rdoc
11
+ spec/reports
12
+ test/tmp
13
+ test/version_tmp
14
+ tmp
15
+
16
+ # YARD artifacts
17
+ .yardoc
18
+ _yardoc
19
+ doc/
20
+
21
+ # emacs
22
+ *.*~
23
+ *~
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in omniauth-google-oauth2-access-token.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Masaaki Isozu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # OmniAuth Google OAuth2 Access Token Strategy
2
+
3
+ Google OAuth2 Access Token Strategy in OmniAuth 1.0.
4
+
5
+ This strategy is fully inspired by [omniauth-facebook-access-token](https://github.com/SoapSeller/omniauth-facebook-access-token).
6
+
7
+ Find your API key at: https://code.google.com/apis/console/
8
+
9
+ Read the Google docs for more details: https://developers.google.com/accounts/docs/OAuth2
10
+
11
+ ## Installation
12
+
13
+ Add to your `Gemfile`:
14
+
15
+ ```ruby
16
+ gem 'omniauth-google-oauth2-access-token'
17
+ ```
18
+
19
+ Then `bundle install`.
20
+
21
+ ## Usage
22
+
23
+ ### Server-Side
24
+
25
+ `OmniAuth::Strategies::GoogleOauth2AccessToken` is simply a Rack middleware.
26
+ Find the detailed instructions in OmniAuth 1.0 docs: https://github.com/intridea/omniauth.
27
+ A brief example, using the middleware in a Rails application at `config/initializers/omniauth.rb`:
28
+
29
+ ```ruby
30
+ Rails.application.config.middleware.use OmniAuth::Builder do
31
+ provider :google_oauth2_access_token, ENV['GOOGLE_CLIENT_KEY'], ENV['GOOGLE_CLIENT_SECRET']
32
+ end
33
+ ```
34
+
35
+ ### Client-Side
36
+
37
+ Request the `access_token` to the provider, then login via ajax GET/POST call to `/auth/google_oauth2_access_token/callback` while providing `access_token` parameter.
38
+
39
+ ## License
40
+
41
+ Copyright (c) 2013 by Masaaki Isozu
42
+
43
+ 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:
44
+
45
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
46
+
47
+ 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.
@@ -0,0 +1,2 @@
1
+ require 'omniauth-google-oauth2-access-token/version'
2
+ require 'omniauth/strategies/google-oauth2-access-token'
@@ -0,0 +1,5 @@
1
+ module OmniAuth
2
+ module GoogleOAuth2AccessToken
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,181 @@
1
+ require 'oauth2'
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class GoogleOauth2AccessToken
6
+ include OmniAuth::Strategy
7
+
8
+ BASE_SCOPE_URL = "https://www.googleapis.com/auth/"
9
+ DEFAULT_SCOPE = "userinfo.email,userinfo.profile"
10
+
11
+ option :name, 'google_oauth2_access_token'
12
+ option :skip_friends, true
13
+ option :authorize_options, [:access_type, :hd, :login_hint, :prompt, :scope, :state, :redirect_uri]
14
+ option :client_options, {
15
+ :site => 'https://accounts.google.com',
16
+ :authorize_url => '/o/oauth2/auth',
17
+ :token_url => '/o/oauth2/token',
18
+ :ssl => { :version => "SSLv3" }
19
+ }
20
+
21
+ args [:client_id, :client_secret]
22
+ option :client_id, nil
23
+ option :client_secret, nil
24
+
25
+ option :access_token_options, {
26
+ :header_format => 'OAuth %s',
27
+ :param_name => 'access_token'
28
+ }
29
+
30
+ attr_accessor :access_token
31
+
32
+ def authorize_params
33
+ super.tap do |params|
34
+ options[:authorize_options].each do |k|
35
+ params[k] = request.params[k.to_s] unless [nil, ''].include?(request.params[k.to_s])
36
+ end
37
+
38
+ raw_scope = params[:scope] || DEFAULT_SCOPE
39
+ scope_list = raw_scope.split(" ").map {|item| item.split(",")}.flatten
40
+ scope_list.map! { |s| s =~ /^https?:\/\// ? s : "#{BASE_SCOPE_URL}#{s}" }
41
+ params[:scope] = scope_list.join(" ")
42
+ params[:access_type] = 'offline' if params[:access_type].nil?
43
+
44
+ session['omniauth.state'] = params[:state] if params['state']
45
+ end
46
+ end
47
+
48
+ uid { raw_info['id'] }
49
+
50
+ info do
51
+ prune!({
52
+ :name => raw_info['name'],
53
+ :email => verified_email,
54
+ :first_name => raw_info['given_name'],
55
+ :last_name => raw_info['family_name'],
56
+ :image => image_url(options),
57
+ :urls => {
58
+ 'Google' => raw_info['link']
59
+ }
60
+ })
61
+ end
62
+
63
+ extra do
64
+ hash = {}
65
+ hash['raw_info'] = raw_info unless skip_info?
66
+ prune! hash
67
+ end
68
+
69
+ def raw_info
70
+ @raw_info ||= access_token.get('https://www.googleapis.com/oauth2/v1/userinfo').parsed
71
+ end
72
+
73
+ def client
74
+ ::OAuth2::Client.new(options.client_id, options.client_secret, deep_symbolize(options.client_options))
75
+ end
76
+
77
+ def request_phase
78
+ form = OmniAuth::Form.new(:title => "User Token", :url => callback_path)
79
+ form.text_field "Access Token", "access_token"
80
+ form.button "Sign In"
81
+ form.to_response
82
+ end
83
+
84
+ def callback_phase
85
+ if !request.params['access_token'] || request.params['access_token'].to_s.empty?
86
+ raise ArgumentError.new("No access token provided.")
87
+ end
88
+
89
+ self.access_token = build_access_token
90
+ self.access_token = self.access_token.refresh! if self.access_token.expired?
91
+
92
+ # TODO: Validate the token
93
+
94
+ # Validate that the token belong to the application
95
+ # Rails.logger.info "---------------bef"
96
+ # Rails.logger.info self.access_token.get('/app')
97
+ # Rails.logger.info "---------------af"
98
+ # app_raw = self.access_token.get('/app').parsed
99
+ # Rails.logger.info "---------------2nd"
100
+ # Rails.logger.info app_raw
101
+ # if app_raw["id"] != options.client_id.to_s
102
+ # Rails.logger.info "client_id=#{options.client_id}"
103
+ # raise ArgumentError.new("Access token doesn't belong to the client.")
104
+ # end
105
+
106
+ # Preserve compatibility with the google provider in normal case
107
+ hash = auth_hash
108
+ hash[:provider] = "google"
109
+ self.env['omniauth.auth'] = hash
110
+ call_app!
111
+
112
+ rescue ::OAuth2::Error => e
113
+ fail!(:invalid_credentials, e)
114
+ rescue ::MultiJson::DecodeError => e
115
+ fail!(:invalid_response, e)
116
+ rescue ::Timeout::Error, ::Errno::ETIMEDOUT => e
117
+ fail!(:timeout, e)
118
+ rescue ::SocketError => e
119
+ fail!(:failed_to_connect, e)
120
+ end
121
+
122
+ protected
123
+
124
+ def deep_symbolize(hash)
125
+ hash.inject({}) do |h, (k,v)|
126
+ h[k.to_sym] = v.is_a?(Hash) ? deep_symbolize(v) : v
127
+ h
128
+ end
129
+ end
130
+
131
+ def build_access_token
132
+ hash = request.params.slice("access_token", "refresh_token", "expires_in", "token_type")
133
+ ::OAuth2::AccessToken.from_hash(
134
+ client,
135
+ hash.update(options.access_token_options)
136
+ )
137
+ end
138
+
139
+ private
140
+
141
+ def prune!(hash)
142
+ hash.delete_if do |_, v|
143
+ prune!(v) if v.is_a?(Hash)
144
+ v.nil? || (v.respond_to?(:empty?) && v.empty?)
145
+ end
146
+ end
147
+
148
+ def verified_email
149
+ raw_info['verified_email'] ? raw_info['email'] : nil
150
+ end
151
+
152
+ def image_url(options)
153
+ original_url = raw_info['picture']
154
+ return original_url if original_url.nil? || (!options[:image_size] && !options[:image_aspect_ratio])
155
+
156
+ image_params = []
157
+ if options[:image_size].is_a?(Integer)
158
+ image_params << "s#{options[:image_size]}"
159
+ elsif options[:image_size].is_a?(Hash)
160
+ image_params << "w#{options[:image_size][:width]}" if options[:image_size][:width]
161
+ image_params << "h#{options[:image_size][:height]}" if options[:image_size][:height]
162
+ end
163
+ image_params << 'c' if options[:image_aspect_ratio] == 'square'
164
+
165
+ params_index = original_url.index('/photo.jpg')
166
+ original_url.insert(params_index, ('/' + image_params.join('-')))
167
+ end
168
+
169
+ def verify_token(id_token, access_token)
170
+ return false unless (id_token && access_token)
171
+
172
+ raw_response = client.request(:get, 'https://www.googleapis.com/oauth2/v2/tokeninfo', :params => {
173
+ :id_token => id_token,
174
+ :access_token => access_token
175
+ }).parsed
176
+ raw_response['issued_to'] == options.client_id
177
+ end
178
+
179
+ end
180
+ end
181
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/omniauth-google-oauth2-access-token/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.add_dependency 'omniauth', '~> 1.0'
6
+ gem.add_dependency 'oauth2', '~> 0.8.0'
7
+
8
+ gem.authors = ["Masaaki Isozu"]
9
+ gem.email = ["m.isozu@gmail.com"]
10
+ gem.license = 'MIT'
11
+ gem.description = %q{A Google using access-token strategy for OmniAuth. Can be used for client side Google login. }
12
+ gem.summary = %q{A Google OAuth2 using access-token strategy for OmniAuth.}
13
+ gem.homepage = "https://github.com/isozu/omniauth-google-oauth2-access-token"
14
+
15
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ gem.files = `git ls-files`.split("\n")
17
+ gem.name = "omniauth-google-oauth2-access-token"
18
+ gem.require_paths = ["lib"]
19
+ gem.version = OmniAuth::GoogleOAuth2AccessToken::VERSION
20
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-google-oauth2-access-token
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Masaaki Isozu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: omniauth
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: oauth2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.8.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 0.8.0
41
+ description: 'A Google using access-token strategy for OmniAuth. Can be used for client
42
+ side Google login. '
43
+ email:
44
+ - m.isozu@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - .gitignore
50
+ - .ruby-version
51
+ - Gemfile
52
+ - LICENSE
53
+ - README.md
54
+ - lib/omniauth-google-oauth2-access-token.rb
55
+ - lib/omniauth-google-oauth2-access-token/version.rb
56
+ - lib/omniauth/strategies/google-oauth2-access-token.rb
57
+ - omniauth-google-oauth2-access-token.gemspec
58
+ homepage: https://github.com/isozu/omniauth-google-oauth2-access-token
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.0.3
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: A Google OAuth2 using access-token strategy for OmniAuth.
82
+ test_files: []
83
+ has_rdoc: