Commit 140cb054 authored by Guy Bertrand's avatar Guy Bertrand
Browse files

Add module to git

parents
<?php
class FederatedIdentityValidationModuleFrontController extends ModuleFrontController {
public function initContent() {
$module_name = $this->module->name;
$validation_control = isset($_GET["back"]) ? htmlspecialchars(trim($_GET["back"])) : 'my-account';
$validation_controller_name = 'validation'.(isset($_GET["back"])?'?back='.$validation_control:'');
$validation_url = $this->context->link->getModuleLink($module_name, $validation_controller_name, array(), true);
$id_base_url = 'https://id.federatedidentity.com';
if(!Configuration::get('ENABLE_FEDID')) {
//Login with FedID is not enabled
Tools::redirect('index.php?controller=authentication');
}
$app_id = Configuration::get('FEDID_APPID');
$app_secret = Configuration::get('FEDID_APPSECRET');
if(!isset($_GET["code"])){
//No code, we redirect to the FedID page
Tools::redirect($id_base_url.'/auth/realms/epik/protocol/openid-connect/auth?client_id='.$app_id.'&response_mode=query&response_type=code&login=true&redirect_uri='.urlencode($validation_url));
}
$code = $_GET["code"];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $id_base_url.'/auth/realms/epik/protocol/openid-connect/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=authorization_code&client_id='.$app_id.'&client_secret='.$app_secret.'&code='.$code.'&redirect_uri='.urlencode($validation_url));
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$resultreq = json_decode($result,true);
if(isset($resultreq["error"])){
Tools::redirect('index.php?controller=authentication&error=token');
}
$token = $resultreq["access_token"];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $id_base_url.'/auth/realms/epik/protocol/openid-connect/userinfo');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "access_token=".$token);
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$resultres2 = json_decode($result);
$email = $resultres2->email;
if(strlen($email)<=3){
Tools::redirect('index.php?controller=authentication&error=userinfo');
}
$customer = new Customer();
//Check if the client exists
if(Customer::customerExists($email)){ //Customer exists
$customer = $customer->getByEmail(trim($email));
} else { //Customer doesn't exist yet
echo "new client";
$customer->is_guest = 0;
$customer->active = 1;
$customer->lastname = $resultres2->family_name;
$customer->firstname = $resultres2->given_name;
$customer->email = $email;
$customer->passwd = "jkdhjggsvqwcqvbOtfwfw";
if($customer->add()){
echo "added";
} else {
die("An error occured");
}
}
$customer->logged = 1;
Hook::exec('authentication');
$this->context->customer = $customer;
$this->context->customer->logged = 1;
$this->context->cookie->id_customer = $customer->id;
$this->context->cookie->customer_lastname = $customer->lastname;
$this->context->cookie->customer_firstname = $customer->firstname;
$this->context->cookie->logged = 1;
$this->context->cookie->check_cgv = 1;
$this->context->cookie->is_guest = 0;
$this->context->cookie->passwd = $customer->passwd;
$this->context->cookie->email = $customer->email;
$this->context->cookie->registerSession(new CustomerSession());
Tools::redirect('index.php?controller='.$validation_control);
}
}
\ No newline at end of file
.federatedIdentityBtn {
font-weight: bold;
font-size: 13px;
line-height: 19px;
text-align: center;
color: #28c255;
border: 1px solid #28c255;
border-radius: 5px;
display: flex;
align-items: center;
justify-content: center;
max-width: 280px;
margin: 10px auto;
transition: all 0.3s;
}
.federatedIdentityBtn>i {
width: 22px;
height: 22px;
display: inline-block;
background: url('../images/sso-identity.svg') center center no-repeat;
background-size: contain;
}
.federatedIdentityBtn>span {
padding: 10px 5px;
}
.federatedIdentityBtn:hover {
color: #fff!important;
background: #28c255;
}
.federatedIdentityBtn:hover>i {
background: url('../images/sso-identity-w.svg') center center no-repeat;
background-size: contain;
}
.other-login-options {
text-align: center;
color: grey;
margin: -3px 0 5px 0;
}
\ No newline at end of file
<?php
/**
* FederatedIdentity - Prestashop Module
* Allows users to login with the FederatedIdentity SSO.
*
* @author Epik.com <support@epik.com>
* @version 1.0.3
*/
if (!defined('_PS_VERSION_')) {
exit;
}
class FederatedIdentity extends Module {
public function __construct() {
$this->name = 'federatedidentity';
$this->tab = 'front_office_features';
$this->version = '1.0.3';
$this->author = 'Epik.com';
$this->controllers = array('validation');
$this->need_instance = 0;
$this->ps_versions_compliancy = [
'min' => '1.7',
'max' => _PS_VERSION_
];
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('FederatedIdentity');
$this->description = $this->l('Allow your shoppers to login on your store through the FederatedIdentity SSO.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
}
public function install() {
if (!parent::install() ||
!$this->registerHook('displayCustomerLoginFormAfter') ||
!$this->registerHook('actionFrontControllerSetMedia') ||
version_compare(_PS_VERSION_, '1.7.6', '>=') === true && !$this->registerHook('displayPersonalInformationTop')
)
return false;
return true;
}
public function uninstall() {
if(!parent::uninstall()){
return false;
} else {
Configuration::deleteByName('ENABLE_FEDID');
Configuration::deleteByName('FEDID_APPID');
Configuration::deleteByName('FEDID_APPSECRET');
return true;
}
}
public function hookDisplayCustomerLoginFormAfter(array $params=[]) {
//If FEDID login is enabled
if(Configuration::get('ENABLE_FEDID'))
echo $this->getFedIDLoginBtn(isset($_GET["back"]) ? $_GET["back"] : '').'<div class="other-login-options">or</div>';
else
return '';
}
public function getFedIDLoginBtn($validation_back){
$validation_full_link = '';
if(strlen($validation_back) > 0){
$validation_full_link = '?back='.htmlspecialchars(trim($validation_back));
}
return '<a href="'.$this->context->link->getModuleLink($this->name, 'validation'.$validation_full_link, array(), true).'"
class="federatedIdentityBtn">
<i></i>
<span>Login with FederatedIdentity</span></a>';
}
public function hookActionFrontControllerSetMedia($params) {
//Register the FedID login BTN css
if ( in_array($this->context->controller->php_self,['authentication','order']) ) {
$this->context->controller->registerStylesheet(
'federatedidentity-style',
'modules/'.$this->name.'/css/login-button.css',
[
'media' => 'all',
'priority' => 200,
]
);
}
}
public function hookDisplayPersonalInformationTop(array $params=[]) {
//If FEDID login is enabled && user is not logged in
if(Configuration::get('ENABLE_FEDID') && $this->context->customer->logged==0)
echo $this->getFedIDLoginBtn('order');
else
return '';
}
public function getContent() {
$output = null;
if (Tools::isSubmit('submit'.$this->name)) {
$enableFedId = boolval(Tools::getValue('ENABLE_FEDID'));
$fedId_app = trim(Tools::getValue('FEDID_APPID'));
$fedId_secret = trim(Tools::getValue('FEDID_APPSECRET'));
if (($enableFedId!=false && $enableFedId!=true) ||
(empty($fedId_app) || empty($fedId_secret))) {
$output .= $this->displayError($this->l('Invalid Configuration value - APP ID & APP SECRET can\'t be left blank'));
} else {
Configuration::updateValue('ENABLE_FEDID', $enableFedId);
Configuration::updateValue('FEDID_APPID', $fedId_app);
Configuration::updateValue('FEDID_APPSECRET', $fedId_secret);
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
}
return $output.$this->displayForm();
}
public function displayForm() {
// Get default language
$defaultLang = (int)Configuration::get('PS_LANG_DEFAULT');
// Init Fields form array
$fieldsForm[0]['form'] = [
'legend' => [
'title' => $this->l('Settings'),
],
'input' => [
[
'type' => 'switch',
'label' => $this->l('Enable "Login with FederatedIdentity?"'),
'desc' => $this->l('Allow users on your shop to login with their FederatedIdentity.com account.'),
'name' => 'ENABLE_FEDID',
'is_bool' => true,
'required' => true,
'values' => [
[
'id' => 'ENABLE_FEDID_on',
'value' => 1,
'label' => $this->trans('Enabled', [], 'Admin.Global')
],
[
'id' => 'ENABLE_FEDID_off',
'value' => 0,
'label' => $this->trans('Disabled', [], 'Admin.Global')
]
]
], [
'type' => 'text',
'label' => $this->l('Application ID'),
'name' => 'FEDID_APPID',
'required' => true
], [
'type' => 'text',
'label' => $this->l('Application Secret'),
'name' => 'FEDID_APPSECRET',
'required' => true
]
],
'submit' => [
'title' => $this->l('Save'),
'class' => 'btn btn-default pull-right'
]
];
$helper = new HelperForm();
// Module, token and currentIndex
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
// Language
$helper->default_form_language = $defaultLang;
$helper->allow_employee_form_lang = $defaultLang;
// Title and toolbar
$helper->title = $this->displayName;
$helper->show_toolbar = true;
$helper->toolbar_scroll = true;
$helper->submit_action = 'submit'.$this->name;
$helper->toolbar_btn = [
'save' => [
'desc' => $this->l('Save'),
'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
'&token='.Tools::getAdminTokenLite('AdminModules'),
],
'back' => [
'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
'desc' => $this->l('Back to list')
]
];
// Load current value
$helper->fields_value['ENABLE_FEDID'] = Tools::getValue('ENABLE_FEDID', Configuration::get('ENABLE_FEDID'));
$helper->fields_value['FEDID_APPID'] = Tools::getValue('FEDID_APPID', Configuration::get('FEDID_APPID'));
$helper->fields_value['FEDID_APPSECRET'] = Tools::getValue('FEDID_APPSECRET', Configuration::get('FEDID_APPSECRET'));
return $helper->generateForm($fieldsForm);
}
}
<svg width="20" height="20" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.545 20A5.455 5.455 0 0020 14.545v-9.09A5.455 5.455 0 0014.545 0h-9.09A5.455 5.455 0 000 5.455v9.09A5.455 5.455 0 005.455 20h9.09zM4.615 6.154h2.052v2.051H4.616V6.154zm9.868 6.774c-.601.612-1.536.918-2.804.918H9.743V6.154h1.936c1.268 0 2.203.309 2.804.926.6.618.901 1.59.901 2.92 0 1.34-.3 2.316-.901 2.928zm1.185-6.956c-.8-.865-1.997-1.316-3.591-1.354l-.22-.003H8.204v10.77h3.653c1.704 0 2.975-.455 3.81-1.365.837-.91 1.255-2.252 1.255-4.028 0-1.776-.418-3.116-1.254-4.02zM4.75 12.308h1.917l-1.618 3.077H3.59l1.161-3.077z" fill="#fff"/></svg>
\ No newline at end of file
<svg width="20" height="20" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.545 20A5.455 5.455 0 0020 14.545v-9.09A5.455 5.455 0 0014.545 0h-9.09A5.455 5.455 0 000 5.455v9.09A5.455 5.455 0 005.455 20h9.09zM4.615 6.154h2.051v2.051h-2.05V6.154zm9.868 6.774c-.6.612-1.535.918-2.803.918H9.744V6.154h1.936c1.268 0 2.203.309 2.803.926.601.618.902 1.59.902 2.92 0 1.34-.3 2.316-.902 2.928zm1.186-6.956c-.8-.865-1.997-1.316-3.592-1.354l-.22-.003H8.206v10.77h3.652c1.705 0 2.975-.455 3.812-1.365.836-.91 1.254-2.252 1.254-4.028 0-1.776-.418-3.116-1.254-4.02zM4.75 12.308h1.917l-1.618 3.077H3.59l1.161-3.077z" fill="#28C255"/></svg>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment