omniauth-gov 0.1.1 → 0.1.3

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: 2c9a9ae0696ab3fb7d9222da4661a679b1bc42066249a45b6afc6a1afde776bc
4
- data.tar.gz: bbf1941d7543a850a32ca0866123b72d458a5a9219cba7111b4b867d6772a14d
3
+ metadata.gz: 4382955c4f28a771ef8edfbd054972b94f0dbd4158e739c2db9d75c9d16b2752
4
+ data.tar.gz: e142eddcfbb8cd54009c5c89a4e01d83d17f4c313b396b5251ffb90a101654a7
5
5
  SHA512:
6
- metadata.gz: f6b50defa7d35e5a31a071e4a01a3ef57d739757d87a261b3a6b00a77a62c2b9816cad787b54de8f1b5c60b640c873b22734b5e54b7c0e940a2c6bb4fd5991c7
7
- data.tar.gz: 672a6a0cdaeaa2c1274a698918f667f65816a3db2f0382e8d0d0e1ef50e96c9d5cd8ffcc4a5703a9b964b1d49cc1b9c59deb4f34b4bd532c3f4c8456d0ac1ac3
6
+ metadata.gz: '097c8c434da9ca2adde612b779846a2866de394e7ea44219c52aababbac2039d4d757dcded9dce6900a91b420f715a6db82d95ffe7a81776542e85a87c4499db'
7
+ data.tar.gz: f57cd09fb38f19707ea336b6576cde37608bf750a897be5884010df9fddc3ea582156e28ace70ddd452b93ba9d559e6528d28327738bc6a1d886009e58aa4c36
data/README.md CHANGED
@@ -2,72 +2,128 @@
2
2
 
3
3
  # OmniAuth Gov
4
4
 
5
- This is the official OmniAuth strategy for authenticating to GitHub. To
6
- use it, you'll need to sign up for an OAuth2 Application ID and Secret
7
- on the [GitHub OAuth Apps Page](https://github.com/settings/developers).
5
+ Estratégia omniauth para integração do Login Único do governo brasileiro ao autentiador devise.
8
6
 
9
- ## Installation
7
+ ## Instalação
10
8
 
11
9
  ```ruby
12
- gem 'omniauth-gov', '~> 0.1.0'
10
+ gem 'omniauth', '1.9.1'
11
+ gem "omniauth-rails_csrf_protection", '0.1.2'
12
+ gem 'omniauth-oauth2'
13
+ gem 'omniauth-gov', '~> 0.1.3'
13
14
  ```
14
15
 
15
- ## Basic Usage
16
+ ## Configuração devise
17
+
18
+ Em `config/initializers/devise.rb.rb`
16
19
 
17
20
  ```ruby
18
- use OmniAuth::Builder do
19
- provider :gov, ENV['GOV_KEY'], ENV['GOV_SECRET']
20
- end
21
+ Devise.setup do |config|
22
+ # ...
23
+ config.omniauth :gov,
24
+ ENV['client_id'],
25
+ ENV['client_secret'],
26
+ scope: 'openid+email+profile+govbr_confiabilidades+',
27
+ callback_path: '/callback-da-aplicacao'
28
+
29
+ config.omniauth_path_prefix = '/prefixo-devise/prefixo-omniauth'
30
+ end
21
31
  ```
22
32
 
33
+ ## Initializer
34
+ Em `config/initializer/omniauth.rb`
23
35
 
24
- ## Basic Usage Rails
25
-
26
- In `config/initializers/gov.rb`
36
+ ```ruby
37
+ OmniAuth.config.full_host = "<host-da-aplicacao-com-protocolo>"
38
+ OmniAuth.config.logger = Rails.logger
39
+ ```
27
40
 
41
+ ## Route
42
+ Em `config/routes.rb`
28
43
  ```ruby
29
- Rails.application.config.middleware.use OmniAuth::Builder do
30
- provider :gov, ENV['GOV_KEY'], ENV['GOV_SECRET']
44
+ # ...
45
+ devise_for :users, controllers: {
46
+ # ...
47
+ :omniauth_callbacks => 'auth/omniauth_callbacks'
48
+ }
49
+
50
+ # opcional: redirecionar url de callback para o callback do devise
51
+ devise_scope :user do
52
+ get 'url-de-callback', to: 'auth/omniauth_callbacks#gov'
31
53
  end
32
- ```
33
54
 
55
+ ```
34
56
 
35
- ## Gov Enterprise Usage
57
+ ## Controller
58
+ Em `controllers/auth/omniauth_callbacks_controller.rb`
36
59
 
37
60
  ```ruby
38
- provider :gov, ENV['GOV_KEY'], ENV['GOV_SECRET'],
39
- {
40
- :client_options => {
41
- :site => 'https://YOURDOMAIN.com/api/v3',
42
- :authorize_url => 'https://YOURDOMAIN.com/login/oauth/authorize',
43
- :token_url => 'https://YOURDOMAIN.com/login/oauth/access_token',
44
- }
45
- }
46
- ```
61
+ # frozen_string_literal: true
47
62
 
48
- ## Scopes
63
+ class Auth::OmniauthCallbacksController < Devise::OmniauthCallbacksController
64
+ skip_before_action :verify_authenticity_token
49
65
 
50
- GitHub API v3 lets you set scopes to provide granular access to different types of data:
66
+ def gov
67
+ @user = User.from_gov_br_omniauth(request.env["omniauth.auth"]["info"])
68
+
69
+ if @user.id.present?
70
+ sign_in_and_redirect @user, :event => :authentication
71
+ set_flash_message(:notice, :success, :kind => "Login Unico") if is_navigational_format?
72
+ else
73
+ end
74
+ end
75
+
76
+ def failure
77
+ redirect_to root_path
78
+ end
51
79
 
52
- ```ruby
53
- use OmniAuth::Builder do
54
- provider :gov, ENV['GOV_KEY'], ENV['GOV_SECRET'], scope: "openid+email+profile+govbr_confiabilidades"
55
80
  end
56
81
  ```
57
82
 
58
- More info on [Scopes](https://docs.github.com/en/developers/apps/scopes-for-oauth-apps).
59
-
60
-
61
- ## Semver
62
- This project adheres to Semantic Versioning 2.0.0. Any violations of this scheme are considered to be bugs.
63
- All changes will be tracked [here](https://github.com/omniauth/omniauth-gov/releases).
64
-
65
- ## License
83
+ ## Model User
84
+ Em `model/user.rb`
85
+ ```ruby
86
+ devise :database_authenticatable,
87
+ # ...
88
+ :omniauthable, omniauth_providers: %i[gov]
89
+
90
+ # ...
91
+ def self.from_gov_br_omniauth(info)
92
+ # Exemplo hash info
93
+ # {
94
+ # "id": 1702579345,
95
+ # "cpf": '99999999999',
96
+ # "nome_social": 'Nome Social',
97
+ # "email_verified": true,
98
+ # "profile": 'https://servicos.staging.acesso.gov.br/',
99
+ # "username": '99999999999',
100
+ # "picture": raw_info["picture"],
101
+ # "name": raw_info["name"],
102
+ # "email": raw_info["email"],
103
+ # }
104
+ user = User.find_by_email(info["email"]) # ou outra chave
105
+
106
+ unless user.nil?
107
+ user.update_attributes(provider: 'login-unico', uid: info["id"])
108
+ else
109
+ name = info["name"]
110
+ email = info["email"]
111
+ user = User.new do |user|
112
+ user.name = name
113
+ user.email = email
114
+ end
115
+ user.skip_confirmation!
116
+ user.save
117
+ end
118
+
119
+ return user
120
+ end
66
121
 
67
- Copyright (c) 2011 Michael Bleigh and Intridea, Inc.
122
+ ```
68
123
 
124
+ ## Licença
69
125
  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:
70
126
 
71
127
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
72
128
 
73
- 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.
@@ -1,50 +1,82 @@
1
1
  require 'omniauth-oauth2'
2
2
 
3
- module OmniAuth
3
+ module Omniauth
4
4
  module Strategies
5
5
  class Gov < OmniAuth::Strategies::OAuth2
6
6
  option :client_options, {
7
- site: 'https://sso.staging.acesso.gov.br',
8
- authorize_url: 'https://sso.staging.acesso.gov.br/authorize',
9
- token_url: 'https://sso.staging.acesso.gov.br/token'
7
+ site: 'https://sso.acesso.gov.br',
8
+ authorize_url: 'https://sso.acesso.gov.br/authorize',
9
+ token_url: 'https://sso.acesso.gov.br/token'
10
10
  }
11
-
12
11
  option :pkce, true
13
12
 
14
- option :pkce_options, {
15
- :code_challenge => proc { |verifier|
16
- Base64.urlsafe_encode64(
17
- Digest::SHA2.digest(verifier),
18
- :padding => false,
19
- )
20
- },
21
- :code_challenge_method => "S256",
22
- }
23
-
24
- uid{ raw_info['id'] }
13
+ credentials do
14
+ hash = {"access_token" => access_token.token}
15
+ hash["id_token"] = access_token.params["id_token"]
16
+ hash["refresh_token"] = access_token.refresh_token if access_token.expires? && access_token.refresh_token
17
+ hash["expires_at"] = access_token.expires_at if access_token.expires?
18
+ hash["expires"] = access_token.expires?
19
+ hash
20
+ end
25
21
 
26
22
  info do
27
- {
28
- :name => raw_info['name'],
29
- :email => raw_info['email'],
30
- :cpf => raw_info['sub']
31
- }
23
+ prune!({
24
+ "id": raw_info['auth_time'],
25
+ "cpf": raw_info["sub"],
26
+ "nome_social": raw_info["social_name"],
27
+ "email_verified": raw_info["email_verified"],
28
+ "profile": raw_info["profile"],
29
+ "username": raw_info["preferred_username"],
30
+ "picture": raw_info["picture"],
31
+ "name": raw_info["name"],
32
+ "email": raw_info["email"],
33
+ })
32
34
  end
33
35
 
36
+ uid { raw_info['auth_time'] }
37
+
34
38
  extra do
35
39
  {
36
- 'raw_info' => raw_info, 'uid' => uid
40
+ 'raw_info': raw_info
37
41
  }
38
42
  end
39
-
43
+
40
44
  def raw_info
41
- @raw_info ||= access_token.get('id_token').parsed
45
+ @raw_info ||= JWT.decode(credentials["id_token"], nil, false)[0]
42
46
  end
43
47
 
44
- def uid
45
- @uid ||= access_token.get('access_token/jti').parsed
48
+ def prune!(hash)
49
+ hash.delete_if do |_, value|
50
+ prune!(value) if value.is_a?(Hash)
51
+ value.nil? || (value.respond_to?(:empty?) && value.empty?)
52
+ end
46
53
  end
47
54
 
55
+ def authorize_params # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
56
+ options.authorize_params[:state] = SecureRandom.hex(24)
57
+ options.authorize_params[:client_id] = options[:client_id]
58
+ options.authorize_params[:scope] = options[:scope]
59
+ options.authorize_params[:response_type] = 'code'
60
+ options.authorize_params[:nonce] = SecureRandom.hex[0..11]
61
+ params = options.authorize_params
62
+ .merge(options_for("authorize"))
63
+ .merge(pkce_authorize_params)
64
+
65
+ session["omniauth.pkce.verifier"] = options.pkce_verifier if options.pkce
66
+ session["omniauth.state"] = params[:state]
67
+
68
+ params
69
+ end
70
+
71
+ def build_access_token
72
+ verifier = request.params["code"]
73
+
74
+ atoken = client.auth_code.get_token(
75
+ 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)}" })
78
+ atoken
79
+ end
48
80
  end
49
81
  end
50
82
  end
@@ -1,5 +1,5 @@
1
1
  module OmniAuth
2
2
  module Gov
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniauth-gov
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Ricardo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-08 00:00:00.000000000 Z
11
+ date: 2024-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: omniauth