1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-10 21:24:20 +01:00
fab-manager/doc/sso_authentication.md

144 lines
5.4 KiB
Markdown
Raw Normal View History

2016-09-12 17:29:44 +02:00
# How to add an authentication method to the Fab-Manager ?
2016-03-23 18:39:41 +01:00
2016-09-12 17:29:44 +02:00
First, take a look at the [OmniAuth list of strategies](https://github.com/intridea/omniauth/wiki/List-of-Strategies) for the Strategy or Developer Strategy you want to add to the Fab-Manager.
2016-03-23 18:39:41 +01:00
For this guide, we will consider you want to add a generic *developer strategy*, like LDAP.
Create the OmniAuth implementation ( **lib/omni_auth/strategies/ldap_provider.rb** )
2016-09-12 17:29:44 +02:00
```ruby
# first require the OmniAuth gem you added to your Gemfile (see the link above for a list of gems)
require 'omniauth-ldap'
module OmniAuth
module Strategies
# in the class name, replace Ldap with the kind of authentication you are implementing
class SsoLdapProvider < OmniAuth::Strategies::LDAP
# implement the logic here, see the gem specific documentation for more details
2016-03-23 18:39:41 +01:00
end
2016-09-12 17:29:44 +02:00
end
end
```
2016-03-23 18:39:41 +01:00
Create the ActiveRecord models ( **from the terminal** )
2016-09-12 17:29:44 +02:00
```bash
# in the models names, replace Ldap with the kind of authentication you are implementing
# replace ldap_fields with the fields you need for implementing LDAP or whatever you are implementing
rails g model LdapProvider ...ldap_fields
rails g model LdapMapping ldap_provider:belongs_to local_field:string local_model:string ...ldap_fields
```
2016-03-23 18:39:41 +01:00
Complete the Provider Model ( **app/model/ldap_provider.rb** )
2016-09-12 17:29:44 +02:00
```ruby
class LdapProvider < ActiveRecord::Base
has_one :auth_provider, as: :providable
has_many :ldap_mappings, dependent: :destroy
accepts_nested_attributes_for :ldap_mappings, allow_destroy: true
2016-09-12 17:55:07 +02:00
# return the fields you want to protect from being directly managed by the Fab-Manager, typically mapped fields
2016-09-12 17:29:44 +02:00
def protected_fields
fields = []
ldap_mappings.each do |mapping|
fields.push(mapping.local_model+'.'+mapping.local_field)
2016-03-23 18:39:41 +01:00
end
2016-09-12 17:29:44 +02:00
fields
end
2016-09-12 17:55:07 +02:00
# return the link, that the current user will have to follow, to edit his profile on the SSO
2016-09-12 17:29:44 +02:00
def profile_url
# you can also create a profile_url field in the Database model
end
end
```
2016-09-12 17:55:07 +02:00
Whitelist your implementation's fields in the controller ( **app/controllers/api/auth_providers_controller.rb** )
2016-03-23 18:39:41 +01:00
2016-09-12 17:29:44 +02:00
```ruby
class API::AuthProvidersController < API::ApiController
...
private
def provider_params
if params['auth_provider']['providable_type'] == DatabaseProvider.name
...
elsif if params['auth_provider']['providable_type'] == LdapProvider.name
params.require(:auth_provider).permit(:name, :providable_type, providable_attributes: [
# list here your LdapProvider model's fields, followed by the mappings :
ldap_mappings_attributes: [
:id, :local_model, :local_field, ...
# add your other customs LdapMapping fields, don't forget the :_destroy symbol if
# you want your admin to be able to remove mappings
]
])
end
end
end
```
2016-03-23 18:39:41 +01:00
List the fields to display in the JSON API view ( **app/views/api/auth_providers/show.json.jbuilder** )
2016-09-12 17:29:44 +02:00
```ruby
json.partial! 'api/auth_providers/auth_provider', auth_provider: @provider
...
if @provider.providable_type == LdapProvider.name
json.providable_attributes do
json.extract! @provider.providable, :id, ... # list LdapProvider fields here
json.ldap_mappings_attributes @provider.providable.ldap_mappings do |m|
json.extract! m, :id, :local_model, :local_field, ... # list LdapMapping fields here
2016-03-23 18:39:41 +01:00
end
2016-09-12 17:29:44 +02:00
end
end
```
2016-03-23 18:39:41 +01:00
Configure the initializer ( **config/initializers/devise.rb** )
2016-09-12 17:29:44 +02:00
```ruby
require_relative '../../lib/omni_auth/omni_auth'
...
elsif active_provider.providable_type == LdapProvider.name
config.omniauth OmniAuth::Strategies::SsoLdapProvider.name.to_sym, # pass here the required parameters, see the gem documentation for details
end
```
2016-03-23 18:39:41 +01:00
Finally you have to create an admin interface with AngularJS:
- **app/assets/templates/admin/authentifications/_ldap.html.erb** must contains html input fields (partial html form) for the LdapProvider fields configuration
- **app/assets/templates/admin/authentifications/_ldap_mapping.html.erb** must contains html partial to configure the LdapMappings, see _oauth2_mapping.html.erb for a working example
- **app/assets/javascript/controllers/admin/authentifications.coffee**
2016-09-12 17:29:44 +02:00
```coffeescript
## list of supported authentication methods
METHODS = {
...
'LdapProvider' : 'LDAP' # add the name of your ActiveRecord model class here as a hash key, associated with a human readable name as a hash value (string)
}
2016-03-23 18:39:41 +01:00
2016-09-12 17:29:44 +02:00
Application.Controllers.controller "newAuthentificationController", ...
$scope.updateProvidable = ->
...
if $scope.provider.providable_type == 'LdapProvider'
# you may want to do some stuff to initialize your provider here
2016-03-23 18:39:41 +01:00
2016-09-12 17:29:44 +02:00
$scope.registerProvider = ->
...
# === LdapProvider ===
else if $scope.provider.providable_type == 'LdapProvider'
# here you may want to do some data validation
# then: save the settings
AuthProvider.save auth_provider: $scope.provider, (provider) ->
# register was a success, display a message, redirect, etc.
```
2016-03-23 18:39:41 +01:00
And to include this interface into the existing one ( **app/assets/templates/admin/authentifications/edit.html.erb**)
2016-09-12 17:29:44 +02:00
```html
<form role="form" name="providerForm" class="form-horizontal" novalidate>
...
<!-- Add the following ng-include inside the providerForm -->
<ng-include src="'<%= asset_path 'admin/authentifications/_ldap.html'%>'" ng-if="provider.providable_type == 'LdapProvider'"></ng-include>
</form>
```
2016-09-12 17:55:07 +02:00
Do not forget that you can find examples and inspiration in the OAuth 2.0 implementation.