omniauth-gov 0.1.3 → 0.1.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4382955c4f28a771ef8edfbd054972b94f0dbd4158e739c2db9d75c9d16b2752
4
- data.tar.gz: e142eddcfbb8cd54009c5c89a4e01d83d17f4c313b396b5251ffb90a101654a7
3
+ metadata.gz: 9a78473bf035ebd11bccaff000615db2459c9b117a43f206be6c3222ffc5f768
4
+ data.tar.gz: 280a14547cb019b1adae1dc052992d3fc6db111826449a54fcd34daf6b56ad48
5
5
  SHA512:
6
- metadata.gz: '097c8c434da9ca2adde612b779846a2866de394e7ea44219c52aababbac2039d4d757dcded9dce6900a91b420f715a6db82d95ffe7a81776542e85a87c4499db'
7
- data.tar.gz: f57cd09fb38f19707ea336b6576cde37608bf750a897be5884010df9fddc3ea582156e28ace70ddd452b93ba9d559e6528d28327738bc6a1d886009e58aa4c36
6
+ metadata.gz: 4920fcdd29bb499c30c921f982be4517268c72d70287a66e8c4ac3a6c0080fc61c2d3865bd3ef5bc029355095cf6b59bdfb7442d3c6410c74358cb24c6b6f815
7
+ data.tar.gz: 5fd852141ac9740ca2bed96c707f1f98cc287f8ea652cea10a8be1f811071a49705a2460f5766ad20836321b9389b987e6a19ef4d0fa575c123d1440ab53308a
data/README.md CHANGED
@@ -10,7 +10,7 @@ Estratégia omniauth para integração do Login Único do governo brasileiro ao
10
10
  gem 'omniauth', '1.9.1'
11
11
  gem "omniauth-rails_csrf_protection", '0.1.2'
12
12
  gem 'omniauth-oauth2'
13
- gem 'omniauth-gov', '~> 0.1.3'
13
+ gem 'omniauth-gov', '~> 0.1.5'
14
14
  ```
15
15
 
16
16
  ## Configuração devise
@@ -126,4 +126,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
126
126
 
127
127
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
128
128
 
129
- 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.
129
+ 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,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GovBr
4
+ # FlatParamsEncoder manages URI params as a flat hash. Any Array values repeat
5
+ # the parameter multiple times.
6
+ module ParamsEncoder
7
+ class << self
8
+ extend Forwardable
9
+ def_delegators :'Faraday::Utils', :escape, :unescape
10
+ end
11
+
12
+ # Encode converts the given param into a URI querystring. Keys and values
13
+ # will converted to strings and appropriately escaped for the URI.
14
+ #
15
+ # @param params [Hash] query arguments to convert.
16
+ #
17
+ # @example
18
+ #
19
+ # encode({a: %w[one two three], b: true, c: "C"})
20
+ # # => 'a=one&a=two&a=three&b=true&c=C'
21
+ #
22
+ # @return [String] the URI querystring (without the leading '?')
23
+ def self.encode(params)
24
+ return nil if params.nil?
25
+
26
+ unless params.is_a?(Array)
27
+ unless params.respond_to?(:to_hash)
28
+ raise TypeError,
29
+ "Can't convert #{params.class} into Hash."
30
+ end
31
+ params = params.to_hash
32
+ params = params.map do |key, value|
33
+ key = key.to_s if key.is_a?(Symbol)
34
+ [key, value]
35
+ end
36
+
37
+ # Only to be used for non-Array inputs. Arrays should preserve order.
38
+ params.sort! if @sort_params
39
+ end
40
+
41
+ # The params have form [['key1', 'value1'], ['key2', 'value2']].
42
+ buffer = +''
43
+ params.each do |key, value|
44
+ encoded_key = escape(key)
45
+ if value.nil?
46
+ buffer << "#{encoded_key}&"
47
+ elsif value.is_a?(Array)
48
+ if value.empty?
49
+ buffer << "#{encoded_key}=&"
50
+ else
51
+ value.each do |sub_value|
52
+ encoded_value = escape(sub_value)
53
+ buffer << "#{encoded_key}=#{encoded_value}&"
54
+ end
55
+ end
56
+ else
57
+ encoded_value = (key == 'scope') ? value : escape(value)
58
+ buffer << "#{encoded_key}=#{encoded_value}&"
59
+ end
60
+ end
61
+ buffer.chop
62
+ end
63
+
64
+ # Decode converts the given URI querystring into a hash.
65
+ #
66
+ # @param query [String] query arguments to parse.
67
+ #
68
+ # @example
69
+ #
70
+ # decode('a=one&a=two&a=three&b=true&c=C')
71
+ # # => {"a"=>["one", "two", "three"], "b"=>"true", "c"=>"C"}
72
+ #
73
+ # @return [Hash] parsed keys and value strings from the querystring.
74
+ def self.decode(query)
75
+ return nil if query.nil?
76
+
77
+ empty_accumulator = {}
78
+
79
+ split_query = (query.split('&').map do |pair|
80
+ pair.split('=', 2) if pair && !pair.empty?
81
+ end).compact
82
+ split_query.each_with_object(empty_accumulator.dup) do |pair, accu|
83
+ pair[0] = unescape(pair[0])
84
+ pair[1] = true if pair[1].nil?
85
+ if pair[1].respond_to?(:to_str)
86
+ pair[1] = unescape(pair[1].to_str.tr('+', ' '))
87
+ end
88
+ if accu[pair[0]].is_a?(Array)
89
+ accu[pair[0]] << pair[1]
90
+ elsif accu[pair[0]]
91
+ accu[pair[0]] = [accu[pair[0]], pair[1]]
92
+ else
93
+ accu[pair[0]] = pair[1]
94
+ end
95
+ end
96
+ end
97
+
98
+ class << self
99
+ attr_accessor :sort_params
100
+ end
101
+
102
+ # Useful default for OAuth and caching.
103
+ @sort_params = true
104
+ end
105
+ end
@@ -52,6 +52,11 @@ module Omniauth
52
52
  end
53
53
  end
54
54
 
55
+ def client
56
+ options.client_options.merge!({connection_opts: {request: {params_encoder: GovBr::ParamsEncoder}}})
57
+ ::OAuth2::Client.new(options.client_id, options.client_secret, deep_symbolize(options.client_options))
58
+ end
59
+
55
60
  def authorize_params # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
56
61
  options.authorize_params[:state] = SecureRandom.hex(24)
57
62
  options.authorize_params[:client_id] = options[:client_id]
@@ -70,11 +75,12 @@ module Omniauth
70
75
 
71
76
  def build_access_token
72
77
  verifier = request.params["code"]
78
+ redirect_uri = "#{OmniAuth.config.full_host}/#{options.callback_path}".gsub!(%r{/+}, '/')
73
79
 
74
80
  atoken = client.auth_code.get_token(
75
81
  verifier,
76
- {"grant_type": "authorization_code", "code": verifier, "redirect_uri": OmniAuth.config.full_host+options.callback_path, "code_verifier": session["omniauth.pkce.verifier"]},
77
- {"Content-Type" => "application/x-www-form-urlencoded", "Authorization" => "Basic #{Base64.strict_encode64(Settings.reload!.omniauth.client_id+":"+Settings.reload!.omniauth.client_secret)}" })
82
+ {"grant_type": "authorization_code", "code": verifier, "redirect_uri": redirect_uri, "code_verifier": session["omniauth.pkce.verifier"]},
83
+ {"Content-Type" => "application/x-www-form-urlencoded", "Authorization" => "Basic #{Base64.strict_encode64(options.client_id+":"+options.client_secret)}" })
78
84
  atoken
79
85
  end
80
86
  end
@@ -1,5 +1,5 @@
1
1
  module OmniAuth
2
2
  module Gov
3
- VERSION = "0.1.3"
3
+ VERSION = "0.1.5"
4
4
  end
5
5
  end
data/lib/omniauth-gov.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require "omniauth-gov/version"
2
- require 'omniauth/strategies/gov'
2
+ require 'omniauth/strategies/gov'
3
+ require 'gov_br/params_encoder'
data/omniauth-gov.gemspec CHANGED
@@ -2,8 +2,8 @@
2
2
  require File.expand_path('../lib/omniauth-gov/version', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |gem|
5
- gem.authors = ["Jonas Ricardo"]
6
- gem.email = ["jonas.campos@yahoo.com.br"]
5
+ gem.authors = ["Jonas Ricardo", "Renato de Souza"]
6
+ gem.email = ["jonas.campos@yahoo.com.br", "renatocdesouza@gmail.com"]
7
7
  gem.description = %q{Official OmniAuth strategy for GitHub.}
8
8
  gem.summary = %q{Official OmniAuth strategy for GitHub.}
9
9
  gem.homepage = "https://github.com/jonasrscampos/omniauth-gov"
@@ -19,6 +19,7 @@ Gem::Specification.new do |gem|
19
19
  gem.add_dependency 'omniauth', '1.9.1'
20
20
  gem.add_dependency 'omniauth-oauth2'
21
21
  gem.add_development_dependency 'rspec', '~> 3.5'
22
+ gem.add_development_dependency 'faraday', '~> 2.9'
22
23
  gem.add_development_dependency 'rack-test'
23
24
  gem.add_development_dependency 'simplecov'
24
25
  gem.add_development_dependency 'webmock'
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniauth-gov
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Ricardo
8
- autorequire:
8
+ - Renato de Souza
9
+ autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2024-03-07 00:00:00.000000000 Z
12
+ date: 2024-07-30 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: omniauth
@@ -52,6 +53,20 @@ dependencies:
52
53
  - - "~>"
53
54
  - !ruby/object:Gem::Version
54
55
  version: '3.5'
56
+ - !ruby/object:Gem::Dependency
57
+ name: faraday
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '2.9'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '2.9'
55
70
  - !ruby/object:Gem::Dependency
56
71
  name: rack-test
57
72
  requirement: !ruby/object:Gem::Requirement
@@ -97,6 +112,7 @@ dependencies:
97
112
  description: Official OmniAuth strategy for GitHub.
98
113
  email:
99
114
  - jonas.campos@yahoo.com.br
115
+ - renatocdesouza@gmail.com
100
116
  executables: []
101
117
  extensions: []
102
118
  extra_rdoc_files: []
@@ -109,17 +125,16 @@ files:
109
125
  - LICENSE.txt
110
126
  - README.md
111
127
  - Rakefile
128
+ - lib/gov_br/params_encoder.rb
112
129
  - lib/omniauth-gov.rb
113
130
  - lib/omniauth-gov/version.rb
114
131
  - lib/omniauth/strategies/gov.rb
115
132
  - omniauth-gov.gemspec
116
- - spec/omniauth/strategies/github_spec.rb
117
- - spec/spec_helper.rb
118
133
  homepage: https://github.com/jonasrscampos/omniauth-gov
119
134
  licenses:
120
135
  - MIT
121
136
  metadata: {}
122
- post_install_message:
137
+ post_install_message:
123
138
  rdoc_options: []
124
139
  require_paths:
125
140
  - lib
@@ -134,8 +149,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
149
  - !ruby/object:Gem::Version
135
150
  version: '0'
136
151
  requirements: []
137
- rubygems_version: 3.3.5
138
- signing_key:
152
+ rubygems_version: 3.4.19
153
+ signing_key:
139
154
  specification_version: 4
140
155
  summary: Official OmniAuth strategy for GitHub.
141
156
  test_files: []
@@ -1,183 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe OmniAuth::Strategies::GitHub do
4
- let(:access_token) { instance_double('AccessToken', :options => {}, :[] => 'user') }
5
- let(:parsed_response) { instance_double('ParsedResponse') }
6
- let(:response) { instance_double('Response', :parsed => parsed_response) }
7
-
8
- let(:enterprise_site) { 'https://some.other.site.com/api/v3' }
9
- let(:enterprise_authorize_url) { 'https://some.other.site.com/login/oauth/authorize' }
10
- let(:enterprise_token_url) { 'https://some.other.site.com/login/oauth/access_token' }
11
- let(:enterprise) do
12
- OmniAuth::Strategies::GitHub.new('GITHUB_KEY', 'GITHUB_SECRET',
13
- {
14
- :client_options => {
15
- :site => enterprise_site,
16
- :authorize_url => enterprise_authorize_url,
17
- :token_url => enterprise_token_url
18
- }
19
- }
20
- )
21
- end
22
-
23
- subject do
24
- OmniAuth::Strategies::GitHub.new({})
25
- end
26
-
27
- before(:each) do
28
- allow(subject).to receive(:access_token).and_return(access_token)
29
- end
30
-
31
- context 'client options' do
32
- it 'should have correct site' do
33
- expect(subject.options.client_options.site).to eq('https://api.github.com')
34
- end
35
-
36
- it 'should have correct authorize url' do
37
- expect(subject.options.client_options.authorize_url).to eq('https://github.com/login/oauth/authorize')
38
- end
39
-
40
- it 'should have correct token url' do
41
- expect(subject.options.client_options.token_url).to eq('https://github.com/login/oauth/access_token')
42
- end
43
-
44
- describe 'should be overrideable' do
45
- it 'for site' do
46
- expect(enterprise.options.client_options.site).to eq(enterprise_site)
47
- end
48
-
49
- it 'for authorize url' do
50
- expect(enterprise.options.client_options.authorize_url).to eq(enterprise_authorize_url)
51
- end
52
-
53
- it 'for token url' do
54
- expect(enterprise.options.client_options.token_url).to eq(enterprise_token_url)
55
- end
56
- end
57
- end
58
-
59
- context '#email_access_allowed?' do
60
- it 'should not allow email if scope is nil' do
61
- expect(subject.options['scope']).to be_nil
62
- expect(subject).to_not be_email_access_allowed
63
- end
64
-
65
- it 'should allow email if scope is user' do
66
- subject.options['scope'] = 'user'
67
- expect(subject).to be_email_access_allowed
68
- end
69
-
70
- it 'should allow email if scope is a bunch of stuff including user' do
71
- subject.options['scope'] = 'public_repo,user,repo,delete_repo,gist'
72
- expect(subject).to be_email_access_allowed
73
- end
74
-
75
- it 'should not allow email if scope does not grant email access' do
76
- subject.options['scope'] = 'repo,user:follow'
77
- expect(subject).to_not be_email_access_allowed
78
- end
79
-
80
- it 'should assume email access not allowed if scope is something currently not documented' do
81
- subject.options['scope'] = 'currently_not_documented'
82
- expect(subject).to_not be_email_access_allowed
83
- end
84
- end
85
-
86
- context '#email' do
87
- it 'should return email from raw_info if available' do
88
- allow(subject).to receive(:raw_info).and_return({ 'email' => 'you@example.com' })
89
- expect(subject.email).to eq('you@example.com')
90
- end
91
-
92
- it 'should return nil if there is no raw_info and email access is not allowed' do
93
- allow(subject).to receive(:raw_info).and_return({})
94
- expect(subject.email).to be_nil
95
- end
96
-
97
- it 'should not return the primary email if there is no raw_info and email access is allowed' do
98
- emails = [
99
- { 'email' => 'secondary@example.com', 'primary' => false },
100
- { 'email' => 'primary@example.com', 'primary' => true }
101
- ]
102
- allow(subject).to receive(:raw_info).and_return({})
103
- subject.options['scope'] = 'user'
104
- allow(subject).to receive(:emails).and_return(emails)
105
- expect(subject.email).to be_nil
106
- end
107
-
108
- it 'should not return the first email if there is no raw_info and email access is allowed' do
109
- emails = [
110
- { 'email' => 'first@example.com', 'primary' => false },
111
- { 'email' => 'second@example.com', 'primary' => false }
112
- ]
113
- allow(subject).to receive(:raw_info).and_return({})
114
- subject.options['scope'] = 'user'
115
- allow(subject).to receive(:emails).and_return(emails)
116
- expect(subject.email).to be_nil
117
- end
118
- end
119
-
120
- context '#raw_info' do
121
- it 'should use relative paths' do
122
- expect(access_token).to receive(:get).with('user').and_return(response)
123
- expect(subject.raw_info).to eq(parsed_response)
124
- end
125
-
126
- it 'should use the header auth mode' do
127
- expect(access_token).to receive(:get).with('user').and_return(response)
128
- subject.raw_info
129
- expect(access_token.options[:mode]).to eq(:header)
130
- end
131
- end
132
-
133
- context '#emails' do
134
- it 'should use relative paths' do
135
- expect(access_token).to receive(:get).with('user/emails', :headers => {
136
- 'Accept' => 'application/vnd.github.v3'
137
- }).and_return(response)
138
-
139
- subject.options['scope'] = 'user'
140
- expect(subject.emails).to eq(parsed_response)
141
- end
142
-
143
- it 'should use the header auth mode' do
144
- expect(access_token).to receive(:get).with('user/emails', :headers => {
145
- 'Accept' => 'application/vnd.github.v3'
146
- }).and_return(response)
147
-
148
- subject.options['scope'] = 'user'
149
- subject.emails
150
- expect(access_token.options[:mode]).to eq(:header)
151
- end
152
- end
153
-
154
- context '#info.email' do
155
- it 'should use any available email' do
156
- allow(subject).to receive(:raw_info).and_return({})
157
- allow(subject).to receive(:email).and_return('you@example.com')
158
- expect(subject.info['email']).to eq('you@example.com')
159
- end
160
- end
161
-
162
- context '#info.urls' do
163
- it 'should use html_url from raw_info' do
164
- allow(subject).to receive(:raw_info).and_return({ 'login' => 'me', 'html_url' => 'http://enterprise/me' })
165
- expect(subject.info['urls']['GitHub']).to eq('http://enterprise/me')
166
- end
167
- end
168
-
169
- context '#extra.scope' do
170
- it 'returns the scope on the returned access_token' do
171
- expect(subject.scope).to eq('user')
172
- end
173
- end
174
-
175
- describe '#callback_url' do
176
- it 'is a combination of host, script name, and callback path' do
177
- allow(subject).to receive(:full_host).and_return('https://example.com')
178
- allow(subject).to receive(:script_name).and_return('/sub_uri')
179
-
180
- expect(subject.callback_url).to eq('https://example.com/sub_uri/auth/github/callback')
181
- end
182
- end
183
- end
data/spec/spec_helper.rb DELETED
@@ -1,16 +0,0 @@
1
- $:.unshift File.expand_path('..', __FILE__)
2
- $:.unshift File.expand_path('../../lib', __FILE__)
3
- require 'simplecov'
4
- SimpleCov.start
5
- require 'rspec'
6
- require 'rack/test'
7
- require 'webmock/rspec'
8
- require 'omniauth'
9
- require 'omniauth-gov'
10
-
11
- RSpec.configure do |config|
12
- config.include WebMock::API
13
- config.include Rack::Test::Methods
14
- config.extend OmniAuth::Test::StrategyMacros, :type => :strategy
15
- end
16
-