From ed03eace1921f81867fe6cd739a8c4ca9786ff26 Mon Sep 17 00:00:00 2001
From: Gustavo S Frehse <gsf20@inf.ufpr.br>
Date: Tue, 18 Feb 2025 11:45:04 -0300
Subject: [PATCH] ADD: gov OmniAuth first part

---
 Gemfile                                       | 12 +++----
 Gemfile.lock                                  | 12 ++++++-
 .../v1/omniauth_callbacks_controller.rb       | 35 ++++++++-----------
 config/initializers/devise.rb                 | 27 +++++++-------
 config/initializers/omniauth.rb               | 13 ++++++-
 docker-compose.yml                            |  2 +-
 6 files changed, 58 insertions(+), 43 deletions(-)

diff --git a/Gemfile b/Gemfile
index cd8759d3..6ab5e925 100644
--- a/Gemfile
+++ b/Gemfile
@@ -143,17 +143,17 @@ gem 'feature'
 # mass insert in ActiveRecord
 gem 'activerecord-import'
 
-# social connect
-gem 'omniauth-facebook'
-gem 'omniauth-twitter'
-gem 'omniauth-google-oauth2', '0.8.2'
-
 # govbr
 gem 'omniauth', '1.9.1'
 gem "omniauth-rails_csrf_protection", '0.1.2'
 gem 'omniauth-oauth2'
 gem 'omniauth-gov', '~> 0.1.8'
 
+# social connect
+gem 'omniauth-facebook'
+gem 'omniauth-twitter'
+gem 'omniauth-google-oauth2', '0.8.2'
+
 gem 'faraday'
 gem 'net-http-persistent'
 
@@ -191,4 +191,4 @@ gem 'elasticsearch', '~> 8.6'
 gem 'multipart-post', '~> 2.0'
 
 gem 'faraday-multipart', '~> 1.0', '>= 1.0.4'
-gem 'tzinfo-data'
\ No newline at end of file
+gem 'tzinfo-data'
diff --git a/Gemfile.lock b/Gemfile.lock
index f327c42d..399d9f27 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -318,7 +318,7 @@ GEM
       multi_json (~> 1.3)
       multi_xml (~> 0.5)
       rack (>= 1.2, < 4)
-    omniauth (1.9.2)
+    omniauth (1.9.1)
       hashie (>= 3.4.6)
       rack (>= 1.6.2, < 3)
     omniauth-facebook (9.0.0)
@@ -328,12 +328,18 @@ GEM
       oauth2 (~> 1.1)
       omniauth (~> 1.1)
       omniauth-oauth2 (>= 1.6)
+    omniauth-gov (0.1.8)
+      omniauth (= 1.9.1)
+      omniauth-oauth2
     omniauth-oauth (1.2.0)
       oauth
       omniauth (>= 1.0, < 3)
     omniauth-oauth2 (1.7.3)
       oauth2 (>= 1.4, < 3)
       omniauth (>= 1.9, < 3)
+    omniauth-rails_csrf_protection (0.1.2)
+      actionpack (>= 4.2)
+      omniauth (>= 1.3.1)
     omniauth-twitter (1.4.0)
       omniauth-oauth (~> 1.1)
       rack
@@ -604,8 +610,12 @@ DEPENDENCIES
   minitest-reporters
   multipart-post (~> 2.0)
   net-http-persistent
+  omniauth (= 1.9.1)
   omniauth-facebook
   omniauth-google-oauth2 (= 0.8.2)
+  omniauth-gov (~> 0.1.8)
+  omniauth-oauth2
+  omniauth-rails_csrf_protection (= 0.1.2)
   omniauth-twitter
   paper_trail
   paranoia
diff --git a/app/controllers/v1/omniauth_callbacks_controller.rb b/app/controllers/v1/omniauth_callbacks_controller.rb
index e339e455..c93991cc 100644
--- a/app/controllers/v1/omniauth_callbacks_controller.rb
+++ b/app/controllers/v1/omniauth_callbacks_controller.rb
@@ -34,25 +34,18 @@ require 'open-uri'
       })
     end
 
-  end
-
-# frozen_string_literal: true
-
-class Auth::OmniauthCallbacksController < Devise::OmniauthCallbacksController
-	skip_before_action :verify_authenticity_token
-
-	def gov
-		@user = User.from_gov_br_omniauth(request.env["omniauth.auth"]["info"])
-
-		if @user.id.present?
-			sign_in_and_redirect @user, :event => :authentication
-			set_flash_message(:notice, :success, :kind => "Login Unico") if is_navigational_format?	  
-		else
-		end
-	end
-	
-	def failure
-    redirect_to root_path
-	end
+    def gov
+            @user = User.from_gov_br_omniauth(request.env["omniauth.auth"]["info"])
+
+            if @user.id.present?
+                    sign_in_and_redirect @user, :event => :authentication
+                    set_flash_message(:notice, :success, :kind => "Login Unico") if is_navigational_format?	  
+            else
+            end
+    end
+    
+    def failure
+      redirect_to root_path
+    end
 
-end
+  end
diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb
index 6f272e8a..d8599537 100644
--- a/config/initializers/devise.rb
+++ b/config/initializers/devise.rb
@@ -259,6 +259,20 @@ Devise.setup do |config|
   # config.omniauth :facebook, "745857965514514", "eb465ec961022087f56204e3b86a63ee"
   # config.omniauth :twitter, "pGD4B2uPuDv07yYqshlwDmdeM", "zt2PHrfIhHjk8lRCqlc47BanKrBghjxYyh8XOzaSnPF27rTnNS"
   # config.omniauth :google_oauth2, "314037571286-v3in875omm5vn56uhg77j06vtuic223f.apps.googleusercontent.com", "cJN0MTZisPqkkCLg3kSQDWIx", { }
+  # ...
+
+  config.omniauth :gov,
+    ENV['GOV_KEY'],
+    ENV['GOV_SECRET'],
+  scope: 'openid+email+profile+govbr_confiabilidades+',
+  callback_path: 'v1/auth/gov/callback',
+  client_options: {
+    site: 'https://sso.staging.acesso.gov.br', # Ambiente de produção.
+    authorize_url: 'https://sso.staging.acesso.gov.br/authorize', # Ambiente de produção.
+    token_url: 'https://sso.staging.acesso.gov.br/token' # Ambiente de produção.
+  }
+
+  config.omniauth_path_prefix = '/prefixo-devise/prefixo-omniauth'
 
   # ==> Warden configuration
   # If you want to use other strategies, that are not supported by Devise, or
@@ -288,17 +302,4 @@ Devise.setup do |config|
   # middleware b/c rails-api does not include it.
   # See: http://stackoverflow.com/q/19600905/806956
   config.navigational_formats = [:json]
-
-  config.omniauth :gov,
-      ENV['client_id'],
-      ENV['client_secret'],
-    scope: 'openid+email+profile+govbr_confiabilidades+',
-    callback_path: '/callback-da-aplicacao',
-    client_options: {
-      site: 'https://sso.acesso.gov.br', # Ambiente de produção.
-      authorize_url: 'https://sso.acesso.gov.br/authorize', # Ambiente de produção.
-      token_url: 'https://sso.acesso.gov.br/token' # Ambiente de produção.
-    }
-
-    config.omniauth_path_prefix = '/prefixo-devise/prefixo-omniauth'
 end
diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb
index 0652075e..d4f0470f 100644
--- a/config/initializers/omniauth.rb
+++ b/config/initializers/omniauth.rb
@@ -25,7 +25,18 @@ Rails.application.config.middleware.use OmniAuth::Builder do
       site: 'https://graph.facebook.com/v4.0',
       authorize_url: "https://www.facebook.com/v4.0/dialog/oauth"
     }
-  provider :google_oauth2, ENV['GOOGLE_KEY'],   ENV['GOOGLE_SECRET']
+  provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET']
+ # provider :gov_brasdf,
+ #   ENV['GOV_KEY'],
+ #   ENV['GOV_SECRET'],
+ #   scope: 'openid+email+profile+govbr_confiabilidades+',
+ #   # ACHO que isso nao precisa especificar?
+ #   #callback_path: '/callback-da-aplicacao',
+ #   client_options: {
+ #     site: 'https://sso.staging.acesso.gov.br',
+ #     authorize_url: 'https://sso.staging.acesso.gov.br/authorize',
+ #     token_url: 'https://sso.staging.acesso.gov.br/token'
+ #   }
 end
 
 OmniAuth.config.allowed_request_methods = [:post, :get]
diff --git a/docker-compose.yml b/docker-compose.yml
index 922249d9..8cfa9aba 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -27,7 +27,7 @@ services:
     depends_on:
       - redis
     ports:
-      - "3001:3000"
+      - "3001:3001"
     volumes:
       - .:/app
       - gem_cache:/usr/local/bundle/gems
-- 
GitLab