Intégration France Connect - sans les tests

This commit is contained in:
Xavier J 2015-10-06 11:21:20 +02:00
parent 40d5802d4a
commit fd60692ade
19 changed files with 86 additions and 337 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -6,6 +6,8 @@
<%= link_to "Sign up", new_registration_path(resource_name) %><br />
<% end -%>
<%= link_to "Connect with France Connect", '/france_connect' %><br />
<%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %>
<%= link_to "Forgot your password?", new_password_path(resource_name) %><br />
<% end -%>