Просмотр исходного кода

Merge branch 'master' of https://github.com/unaio/una

AntonLV 2 лет назад
Родитель
Сommit
bbf6e87a9f

+ 7 - 1
modules/boonex/okta_connect/classes/BxOktaConConfig.php

@@ -14,13 +14,14 @@ class BxOktaConConfig extends BxBaseModConnectConfig
     public $sDomain;
     public $sClientID;
     public $sSecret;
-
     public $sScope = 'openid profile email'; // 'okta.users.read'; 
 
     public $sPageStart;
     public $sPageHandle;
 
     public $bAddExtensionsForDuplicateEmails = true;
+    public $bGetUserInfoFromIdToken = true;
+    public $sUserIdField = 'identityId';
 
     function __construct($aModule)
     {
@@ -31,6 +32,7 @@ class BxOktaConConfig extends BxBaseModConnectConfig
         $this -> sDomain = getParam('bx_oktacon_domain');
         $this -> sClientID = getParam('bx_oktacon_client_id');
         $this -> sSecret = getParam('bx_oktacon_secret');
+        $this -> sScope = getParam('bx_oktacon_scope');
 
         $this -> sEmailTemplatePasswordGenerated = 'bx_oktacon_password_generated';
         $this -> sDefaultTitleLangKey = '_bx_oktacon';
@@ -42,6 +44,10 @@ class BxOktaConConfig extends BxBaseModConnectConfig
 
         $this -> sPageStart = BX_DOL_URL_ROOT . $this -> getBaseUri() . 'start';
         $this -> sPageHandle = BX_DOL_URL_ROOT . $this -> getBaseUri() . 'handle';
+
+        bx_import('Custom', $aModule);
+        $oCustom = new BxOktaConCustom($aModule);
+        $oCustom->onConfig($this);
     }
 }
 

+ 38 - 0
modules/boonex/okta_connect/classes/BxOktaConCustom.php

@@ -0,0 +1,38 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    OktaConnect Okta Connect
+ * @ingroup     UnaModules
+ *
+ * @{
+ */
+
+class BxOktaConCustom
+{
+    function __construct($aModule)
+    {
+    }
+
+    function onConfig ($oConfig)
+    {
+    }
+
+    function onRegister ($aRemoteProfileInfo)
+    {
+        bx_log('bx_oktacon', $aRemoteProfileInfo);
+    }
+
+    function onLogin ($oProfile, $aRemoteProfileInfo)
+    {
+        bx_log('bx_oktacon', $aRemoteProfileInfo);
+    }
+
+    function onConvertRemoteFields($aProfileInfo, &$aProfileFields)
+    {
+
+    }
+}
+
+/** @} */

+ 30 - 15
modules/boonex/okta_connect/classes/BxOktaConModule.php

@@ -85,20 +85,26 @@ class BxOktaConModule extends BxBaseModConnectModule
         ], 'post', array ('Content-Type: application/x-www-form-urlencoded'));
         $aAuthData = $this->_decodeResponseAndHandleError($s);
 
-        // get the data, especially access_token
-        $sAccessToken = $aAuthData['access_token'];
-        $sExpiresIn = $aAuthData['expires_in'];
-        $sExpiresAt = new \DateTime('+' . $sExpiresIn . ' seconds');
+        $aRemoteProfileInfo = [];
 
-        // request info about profile
-        $s = bx_file_get_contents("https://{$this->_oConfig->sDomain}/oauth2/default/v1/userinfo", array(), 'get', array(
-            'Accept: application/json',
-            'Authorization: Bearer ' . $sAccessToken,
-        ));
-echoDbgLog($s);
-        $aUserData = $this->_decodeResponseAndHandleError($s);
-	    $aRemoteProfileInfo = $aUserData;
-	    $aRemoteProfileInfo['id'] = $aRemoteProfileInfo['accountId'];
+        if ($this->_oConfig->bGetUserInfoFromIdToken) {
+            $aRemoteProfileInfo = json_decode(base64_decode(str_replace('_', '/', str_replace('-','+',explode('.', $aAuthData['id_token'])[1]))), true);
+        }
+        else {
+            // get the data, especially access_token
+            $sAccessToken = $aAuthData['access_token'];
+            $sExpiresIn = $aAuthData['expires_in'];
+            $sExpiresAt = new \DateTime('+' . $sExpiresIn . ' seconds');
+
+            // request info about profile
+            $s = bx_file_get_contents("https://{$this->_oConfig->sDomain}/oauth2/default/v1/userinfo", array(), 'get', array(
+                'Accept: application/json',
+                'Authorization: Bearer ' . $sAccessToken,
+            ));
+            $aRemoteProfileInfo = $this->_decodeResponseAndHandleError($s);
+        }
+
+	    $aRemoteProfileInfo['id'] = $aRemoteProfileInfo[$this->_oConfig->sUserIdField];
 /*
         // request profile photo
         $s = bx_file_get_contents("https://graph.microsoft.com/v1.0/me/photo/", array(), 'get', array(
@@ -111,16 +117,21 @@ echoDbgLog($s);
 */
         if ($aRemoteProfileInfo) {
 
+            bx_import('Custom', $this->_aModule);
+            $oCustom = new BxOktaConCustom($this->_aModule);
+
             // check if user logged in before
             $iLocalProfileId = $this->_oDb->getProfileId($aRemoteProfileInfo['id']);
             
             if ($iLocalProfileId && $oProfile = BxDolProfile::getInstance($iLocalProfileId)) {
                 // user already exists
-                $this->setLogged($oProfile ->id(), '', true, true); // remember user
+                $this->setLogged($oProfile->id(), '', true, getParam('bx_oktacon_remember_session')); // remember user
+                $oCustom->onLogin($oProfile, $aRemoteProfileInfo);
             }             
             else {  
                 // register new user
                 $this->_createProfile($aRemoteProfileInfo);
+                $oCustom->onRegister($aRemoteProfileInfo);
             }
         } 
         else {
@@ -136,7 +147,7 @@ echoDbgLog($s);
     protected function _convertRemoteFields($aProfileInfo, $sAlternativeName = '')
     {
         $aProfileFields = $aProfileInfo;
-	    $sName =  !empty($aProfileInfo['name']) ? $aProfileInfo['name'] : $aProfileInfo['accountId'];
+	    $sName =  !empty($aProfileInfo['name']) ? $aProfileInfo['name'] : $aProfileInfo[$this->_oConfig->sUserIdField];
         $aProfileFields['name'] = !empty($aProfileInfo['preferred_username']) ? $aProfileInfo['preferred_username'] : $sName;
         $aProfileFields['fullname'] = $sName; // !empty($aProfileInfo['given_name']) ? $aProfileInfo['given_name'] : $aProfileFields['name'];
         $aProfileFields['last_name'] = !empty($aProfileInfo['family_name']) ? ' ' . $aProfileInfo['family_name'] : '';
@@ -144,6 +155,10 @@ echoDbgLog($s);
         $aProfileFields['picture'] = '';
         $aProfileFields['allow_view_to'] = getParam('bx_oktacon_privacy');
 
+        bx_import('Custom', $this->_aModule);
+        $oCustom = new BxOktaConCustom($this->_aModule);
+        $oCustom->onConvertRemoteFields($aProfileInfo, $aProfileFields);
+
         return $aProfileFields;
     }
 

+ 2 - 0
modules/boonex/okta_connect/install/langs/en.xml

@@ -13,6 +13,8 @@
     <string name="_bx_oktacon_option_domain"><![CDATA[Okta Domain]]></string>
     <string name="_bx_oktacon_option_client_id"><![CDATA[Application (Client) ID]]></string>
     <string name="_bx_oktacon_option_secret"><![CDATA[Client Secret Value]]></string>
+    <string name="_bx_oktacon_option_scope"><![CDATA[Scope]]></string>
+    <string name="_sys_connect_option_remember_session"><![CDATA[Remember user session]]></string>
 
 	<string name="_bx_oktacon_et_password_generated"><![CDATA[Okta connect temporary password]]></string>
 	<string name="_bx_oktacon_et_password_generated_subject"><![CDATA[Temporary password]]></string>

+ 3 - 1
modules/boonex/okta_connect/install/sql/enable.sql

@@ -35,9 +35,11 @@ INSERT INTO `sys_options` (`name`, `value`, `category_id`, `caption`, `type`, `c
 ('bx_oktacon_domain', '', @iCategId, '_bx_oktacon_option_domain', 'digit', '', '', 20, ''),
 ('bx_oktacon_client_id', '', @iCategId, '_bx_oktacon_option_client_id', 'digit', '', '', 22, ''),
 ('bx_oktacon_secret', '', @iCategId, '_bx_oktacon_option_secret', 'digit', '', '', 24, ''),
+('bx_oktacon_scope', 'openid profile email', @iCategId, '_bx_oktacon_option_scope', 'digit', '', '', 26, ''),
 ('bx_oktacon_redirect_page', 'index', @iCategId, '_sys_connect_option_redirect', 'select', '', '', 40, 'join,settings,dashboard,index'),
 ('bx_oktacon_module', 'bx_persons', @iCategId, '_sys_connect_option_module', 'select', '', '', 50, 'a:2:{s:6:"module";s:10:"bx_oktacon";s:6:"method";s:20:"get_profiles_modules";}'),
 ('bx_oktacon_privacy', '3', @iCategId, '_sys_connect_option_privacy', 'select', '', '', 54, 'a:2:{s:6:"module";s:10:"bx_oktacon";s:6:"method";s:18:"get_privacy_groups";}'),
 ('bx_oktacon_confirm_email', 'on', @iCategId, '_sys_connect_option_confirm_email', 'checkbox', '', '', 70, ''),
-('bx_oktacon_approve', '', @iCategId, '_sys_connect_option_approve', 'checkbox', '', '', 80, '');
+('bx_oktacon_approve', '', @iCategId, '_sys_connect_option_approve', 'checkbox', '', '', 80, ''),
+('bx_oktacon_remember_session', 'on', @iCategId, '_sys_connect_option_remember_session', 'checkbox', '', '', 100, '');