1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-08 23:46:14 +01:00
fab-manager/lib/omni_auth/strategies/sso_oauth2_provider.rb

155 lines
4.5 KiB
Ruby
Raw Normal View History

2019-01-31 17:15:26 +01:00
# frozen_string_literal: true
2016-03-23 18:39:41 +01:00
require 'omniauth-oauth2'
require 'jsonpath'
require 'sso_logger'
2016-03-23 18:39:41 +01:00
2019-01-31 17:15:26 +01:00
module OmniAuth::Strategies
# Authentication strategy provided trough oAuth 2.0
class SsoOauth2Provider < OmniAuth::Strategies::OAuth2
2016-03-23 18:39:41 +01:00
2019-01-31 17:15:26 +01:00
def self.active_provider
active_provider = AuthProvider.active
if active_provider.providable_type != OAuth2Provider.name
raise "Trying to instantiate the wrong provider: Expected OAuth2Provider, received #{active_provider.providable_type}"
2016-03-23 18:39:41 +01:00
end
2019-01-31 17:15:26 +01:00
active_provider
end
2016-03-23 18:39:41 +01:00
2019-01-31 17:15:26 +01:00
# Strategy name.
option :name, active_provider.strategy_name
2016-03-23 18:39:41 +01:00
2019-01-31 17:15:26 +01:00
option :client_options,
site: active_provider.providable.base_url,
authorize_url: active_provider.providable.authorization_endpoint,
token_url: active_provider.providable.token_endpoint
2016-03-23 18:39:41 +01:00
2021-11-02 11:24:10 +01:00
def authorize_params
super.tap do |params|
params[:scope] = active_provider.providable.scopes
2021-11-02 11:24:10 +01:00
end
end
def callback_url
url = Rails.application.config.action_controller.default_url_options
"#{url[:protocol]}://#{url[:host]}#{script_name}#{callback_path}"
end
2016-03-23 18:39:41 +01:00
2019-01-31 17:15:26 +01:00
uid { parsed_info['user.uid'.to_sym] }
2016-03-23 18:39:41 +01:00
2019-01-31 17:15:26 +01:00
info do
{
mapping: parsed_info
}
end
extra do
{
raw_info: raw_info
}
end
# retrieve data from various url, querying each only once
def raw_info
logger = SsoLogger.new
2019-01-31 17:15:26 +01:00
@raw_info ||= {}
logger.debug "[raw_info] @raw_infos = #{@raw_info&.to_json}"
2019-01-31 17:15:26 +01:00
unless @raw_info.size.positive?
OmniAuth::Strategies::SsoOauth2Provider.active_provider.auth_provider_mappings.each do |mapping|
logger.debug "mapping = #{mapping&.to_json}"
next if @raw_info.key?(mapping.api_endpoint.to_sym)
logger.debug "api_endpoint = #{mapping.api_endpoint.to_sym}"
logger.debug "access_token = #{access_token&.to_json}"
logger.debug "token get = #{access_token.get(mapping.api_endpoint)}"
logger.debug "parsed = #{access_token.get(mapping.api_endpoint).parsed}"
@raw_info[mapping.api_endpoint.to_sym] = access_token.get(mapping.api_endpoint).parsed
2016-03-23 18:39:41 +01:00
end
end
2019-01-31 17:15:26 +01:00
@raw_info
end
2016-03-23 18:39:41 +01:00
2019-01-31 17:15:26 +01:00
def parsed_info
logger = SsoLogger.new
2019-01-31 17:15:26 +01:00
@parsed_info ||= {}
logger.debug "[parsed_info] @parsed_info = #{@parsed_info.to_json}"
2019-01-31 17:15:26 +01:00
unless @parsed_info.size.positive?
OmniAuth::Strategies::SsoOauth2Provider.active_provider.auth_provider_mappings.each do |mapping|
2019-01-31 17:15:26 +01:00
raw_data = ::JsonPath.new(mapping.api_field).on(raw_info[mapping.api_endpoint.to_sym]).first
logger.debug "@parsed_info[#{local_sym(mapping)}] mapped from #{raw_data}"
2019-01-31 17:15:26 +01:00
if mapping.transformation
case mapping.transformation['type']
## INTEGER
when 'integer'
@parsed_info[local_sym(mapping)] = map_integer(mapping.transformation, raw_data)
2019-01-31 17:15:26 +01:00
## BOOLEAN
when 'boolean'
@parsed_info[local_sym(mapping)] = map_boolean(mapping.transformation, raw_data)
2019-01-31 17:15:26 +01:00
## DATE
when 'date'
@params[local_sym(mapping)] = map_date(mapping.transformation, raw_data)
2019-01-31 17:15:26 +01:00
## OTHER TRANSFORMATIONS (not supported)
else
@parsed_info[local_sym(mapping)] = raw_data
end
2019-01-31 17:15:26 +01:00
## NO TRANSFORMATION
else
@parsed_info[local_sym(mapping)] = raw_data
2016-03-23 18:39:41 +01:00
end
end
end
2019-01-31 17:15:26 +01:00
@parsed_info
end
private
2016-03-23 18:39:41 +01:00
2019-01-31 17:15:26 +01:00
def local_sym(mapping)
(mapping.local_model + '.' + mapping.local_field).to_sym
end
def map_integer(transformation, raw_data)
2019-01-31 17:15:26 +01:00
value = nil
transformation['mapping'].each do |m|
if m['from'] == raw_data
2019-01-31 17:15:26 +01:00
value = m['to']
break
end
2019-01-31 17:15:26 +01:00
end
# if no transformation had set any value, return the raw value
value || raw_data
2019-01-31 17:15:26 +01:00
end
def map_boolean(transformation, raw_data)
return false if raw_data == transformation['false_value']
2019-01-31 17:15:26 +01:00
true if raw_data == transformation['true_value']
2019-01-31 17:15:26 +01:00
end
def map_date(transformation, raw_data)
2019-01-31 17:15:26 +01:00
case transformation['format']
when 'iso8601'
DateTime.iso8601(raw_data)
2019-01-31 17:15:26 +01:00
when 'rfc2822'
DateTime.rfc2822(raw_data)
2019-01-31 17:15:26 +01:00
when 'rfc3339'
DateTime.rfc3339(raw_data)
2019-01-31 17:15:26 +01:00
when 'timestamp-s'
DateTime.strptime(raw_data, '%s')
2019-01-31 17:15:26 +01:00
when 'timestamp-ms'
DateTime.strptime(raw_data, '%Q')
2019-01-31 17:15:26 +01:00
else
DateTime.parse(raw_data)
2019-01-31 17:15:26 +01:00
end
2016-03-23 18:39:41 +01:00
end
end
2019-01-31 17:15:26 +01:00
end