diff --git a/.idea/.name b/.idea/.name deleted file mode 100644 index ed8293f3e..000000000 --- a/.idea/.name +++ /dev/null @@ -1 +0,0 @@ -admi_facile \ No newline at end of file diff --git a/.idea/admi_facile.iml b/.idea/admi_facile.iml deleted file mode 100644 index e2a0d8910..000000000 --- a/.idea/admi_facile.iml +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/codeStyleSettings.xml b/.idea/codeStyleSettings.xml deleted file mode 100644 index 84c6b86a0..000000000 --- a/.idea/codeStyleSettings.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml deleted file mode 100644 index 96cc43efa..000000000 --- a/.idea/compiler.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml deleted file mode 100644 index e7bedf337..000000000 --- a/.idea/copyright/profiles_settings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index e9d9e0f6c..000000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - - - - - Android - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index fedaa1825..000000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml deleted file mode 100644 index e96534fb2..000000000 --- a/.idea/uiDesigner.xml +++ /dev/null @@ -1,124 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 6564d52db..000000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 25e3f8612..516ad3e33 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -422,3 +422,6 @@ DEPENDENCIES unicorn web-console (~> 2.0) webmock + +BUNDLED WITH + 1.10.4 diff --git a/app/controllers/fc_controller.rb b/app/controllers/fc_controller.rb deleted file mode 100644 index 4d786a11e..000000000 --- a/app/controllers/fc_controller.rb +++ /dev/null @@ -1,22 +0,0 @@ -class FcController < ApplicationController - def index - - client = OpenIDConnect::Client.new( - identifier: FRANCE_CONNECT.identifier, - secret: FRANCE_CONNECT.secret, - redirect_uri: 'http://localhost:3000', - authorization_endpoint: 'https://fce.integ01.dev-franceconnect.fr/api/v1/authorize', - token_endpoint: 'https://fce.integ01.dev-franceconnect.fr/api/v1/token', - userinfo_endpoint: 'https://fce.integ01.dev-franceconnect.fr/api/v1/userinfo' - ) - - session[:state] = SecureRandom.hex(16) - session[:nonce] = SecureRandom.hex(16) - authorization_uri = client.authorization_uri( - state: session[:state], - nonce: session[:nonce] - ) - redirect_to authorization_uri - - end -end \ No newline at end of file diff --git a/app/controllers/france_connect_controller.rb b/app/controllers/france_connect_controller.rb new file mode 100644 index 000000000..02103054a --- /dev/null +++ b/app/controllers/france_connect_controller.rb @@ -0,0 +1,27 @@ +class FranceConnectController < ApplicationController + def index + client = FranceConnectClient.new + + session[:state] = SecureRandom.hex(16) + session[:nonce] = SecureRandom.hex(16) + + authorization_uri = client.authorization_uri( + scope: [:profile, :email], + state: session[:state], + nonce: session[:nonce] + ) + redirect_to authorization_uri + end + + def callback + user_infos = FranceConnectService.retrive_user(params[:code]) + + unless user_infos.nil? + @user = User.find_for_france_connect(user_infos.email) + + sign_in @user + + redirect_to(controller: 'users/dossiers', action: :index) + end + end +end \ No newline at end of file diff --git a/app/controllers/test_open_id_controller.rb b/app/controllers/test_open_id_controller.rb deleted file mode 100644 index b43a55e46..000000000 --- a/app/controllers/test_open_id_controller.rb +++ /dev/null @@ -1,31 +0,0 @@ -class TestOpenIdController < ApplicationController - def show - - - client = OpenIDConnect::Client.new( - identifier: FRANCE_CONNECT.identifier, - secret: FRANCE_CONNECT.secret, - redirect_uri: 'http://localhost:3000', - authorization_endpoint: 'https://fce.integ01.dev-franceconnect.fr/api/v1/authorize', - token_endpoint: 'https://fce.integ01.dev-franceconnect.fr/api/v1/token', - userinfo_endpoint: 'https://fce.integ01.dev-franceconnect.fr/api/v1/userinfo' - ) - - - client.authorization_code = params[:code] - begin - access_token = client.access_token!(client_auth_method: :secret) - - id_token = OpenIDConnect::ResponseObject::IdToken.decode access_token.id_token, FRANCE_CONNECT.secret - - puts id_token - userinfo = access_token.userinfo! - puts userinfo - rescue Exception => e - - puts e.message - end - - - end -end \ No newline at end of file diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb deleted file mode 100644 index 6e98c15d1..000000000 --- a/app/controllers/users/omniauth_callbacks_controller.rb +++ /dev/null @@ -1,28 +0,0 @@ -class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController - # You should configure your model like this: - # devise :omniauthable, omniauth_providers: [:twitter] - - # You should also create an action method in this controller like this: - # def twitter - # end - - # More info at: - # https://github.com/plataformatec/devise#omniauth - - # GET|POST /resource/auth/twitter - # def passthru - # super - # end - - # GET|POST /users/auth/twitter/callback - # def failure - # super - # end - - # protected - - # The path used when omniauth fails - # def after_omniauth_failure_path_for(scope) - # super(scope) - # end -end diff --git a/app/models/france_connect_client.rb b/app/models/france_connect_client.rb new file mode 100644 index 000000000..1dae10435 --- /dev/null +++ b/app/models/france_connect_client.rb @@ -0,0 +1,20 @@ +class FranceConnectClient < OpenIDConnect::Client + + def initialize params={} + @redirect_uri = 'http://localhost:3000/france_connect/callback' + @authorization_endpoint = 'https://fce.integ01.dev-franceconnect.fr/api/v1/authorize' + @token_endpoint = 'https://fce.integ01.dev-franceconnect.fr/api/v1/token' + @userinfo_endpoint = 'https://fce.integ01.dev-franceconnect.fr/api/v1/userinfo' + + super( + identifier: FRANCE_CONNECT.identifier, + secret: FRANCE_CONNECT.secret, + redirect_uri: @redirect_uri, + authorization_endpoint: @authorization_endpoint, + token_endpoint: @token_endpoint, + userinfo_endpoint: @userinfo_endpoint + ) + + self.authorization_code = params[:code] if params.has_key? :code + end +end diff --git a/app/models/user.rb b/app/models/user.rb index 802edb08c..621f42199 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,4 +5,12 @@ class User < ActiveRecord::Base :recoverable, :rememberable, :trackable, :validatable has_many :dossiers + + def self.find_for_france_connect email + user = User.find_by_email(email) + + return user unless user.nil? + + User.create(email: email, password: Devise.friendly_token[0,20]) + end end diff --git a/app/services/france_connect_service.rb b/app/services/france_connect_service.rb new file mode 100644 index 000000000..1c3bb200b --- /dev/null +++ b/app/services/france_connect_service.rb @@ -0,0 +1,12 @@ +class FranceConnectService + def self.retrive_user code + client = FranceConnectClient.new code: code + + begin + access_token = client.access_token!(client_auth_method: :secret) + access_token.userinfo! + rescue Exception => e + Rails.logger.error(e.message) + end + end +end diff --git a/app/views/users/shared/_links.html.erb b/app/views/users/shared/_links.html.erb index cd795adb6..7acc0d13e 100644 --- a/app/views/users/shared/_links.html.erb +++ b/app/views/users/shared/_links.html.erb @@ -6,6 +6,8 @@ <%= link_to "Sign up", new_registration_path(resource_name) %>
<% end -%> +<%= link_to "Connect with France Connect", '/france_connect' %>
+ <%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %> <%= link_to "Forgot your password?", new_password_path(resource_name) %>
<% end -%> diff --git a/config/routes.rb b/config/routes.rb index d9a081e7a..5b0f0af52 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,18 +5,21 @@ Rails.application.routes.draw do } devise_for :gestionnaires, controllers: { - sessions: 'gestionnaires/sessions' - }, skip: [:password, :registrations] + sessions: 'gestionnaires/sessions' + }, skip: [:password, :registrations] + root 'users/dossiers#index' + # root 'users/france_connect_callbacks#login' - # root 'users/dossiers#index' - root 'test_open_id#show' - get '/fc' => 'fc#index' + + get 'france_connect' => 'france_connect#index' + get 'france_connect/callback' => 'france_connect#callback' namespace :users do get 'siret' => 'siret#index' + resources :dossiers do get '/description' => 'description#show' get '/description/error' => 'description#error' @@ -29,7 +32,6 @@ Rails.application.routes.draw do post '/commentaire' => 'commentaires#create' end resource :dossiers - end @@ -52,13 +54,13 @@ Rails.application.routes.draw do get 'backoffice' => 'backoffice#index' -namespace :backoffice do - get 'sign_in' => '/gestionnaires/sessions#new' - resources :dossiers do - post 'confirme' => 'dossiers#confirme' + namespace :backoffice do + get 'sign_in' => '/gestionnaires/sessions#new' + resources :dossiers do + post 'confirme' => 'dossiers#confirme' + end + resources :commentaires, only: [:create] end - resources :commentaires, only: [:create] -end # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes".