zoukankan      html  css  js  c++  java
  • Discuz与第三方应用程序的统一认证

        最近有项目用到Discuz与项目的统一认证.就花大力气去研究Discuz源代码和passport认证.

        刚开始用的是6.1版的.按照说明手册做了好久注册是可以.但统一登录就没办法实现.看了好久的代码.

        看明白一点.但注要的COOKIES,SESSION都没有找到.最好选择放弃用了6.0的版本.注册和统一认证都

    可以简单实现.才松了一口气.

        由于有些地方需要在源代码上更新.所以看了好久的Discuz代码和页面.十再服了Discuz对代码的封装.所有的操作都封装到了函数.十再看的要死.找的也要死.为了知道每一个函数什么意思,就是找这个函数都要好长时间.但不得不佩服这个论坛的代码运行效率的确是很好.现在在用PHP开发.所以这个论坛是值的我去研究和学习.但可能要花超长时间明白这个论坛里的每个函数的意思.才可以正在的看懂代码.

       附上统一认证的代码,有需要的可以学习下.6.1的统一认证没研究出来值的遗憾.

      

    <?php

    /////////////////////////////////////////////////////////////////////////////////
    /**
        * Passport 加密函数
        *
        * @param        string        等待加密的原字串
        * @param        string        私有密匙(用于解密和加密)
        *
        * @return    string        原字串经过私有密匙加密后的结果
        */
        function passport_encrypt($txt, $key) {

            // 使用随机数发生器产生 0~32000 的值并 MD5()
            srand((double)microtime() * 1000000);
            $encrypt_key = md5(rand(0, 32000));

            // 变量初始化
            $ctr = 0;
            $tmp = '';

            // for 循环,$i 为从 0 开始,到小于 $txt 字串长度的整数
            for($i = 0; $i < strlen($txt); $i++) {
                // 如果 $ctr = $encrypt_key 的长度,则 $ctr 清零
                $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
                // $tmp 字串在末尾增加两位,其第一位内容为 $encrypt_key 的第 $ctr 位,
                // 第二位内容为 $txt 的第 $i 位与 $encrypt_key 的 $ctr 位取异或。然后 $ctr = $ctr + 1
                $tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);
            }

            // 返回结果,结果为 passport_key() 函数返回值的 base64 编码结果
            return base64_encode(passport_key($tmp, $key));
        }

        /**
        * Passport 解密函数
        *
        * @param        string        加密后的字串
        * @param        string        私有密匙(用于解密和加密)
        *
        * @return    string        字串经过私有密匙解密后的结果
        */
        function passport_decrypt($txt, $key) {

            // $txt 的结果为加密后的字串经过 base64 解码,然后与私有密匙一起,
            // 经过 passport_key() 函数处理后的返回值
            $txt = passport_key(base64_decode($txt), $key);

            // 变量初始化
            $tmp = '';

            // for 循环,$i 为从 0 开始,到小于 $txt 字串长度的整数
            for ($i = 0; $i < strlen($txt); $i++) {
                // $tmp 字串在末尾增加一位,其内容为 $txt 的第 $i 位,
                // 与 $txt 的第 $i + 1 位取异或。然后 $i = $i + 1
                $tmp .= $txt[$i] ^ $txt[++$i];
            }

            // 返回 $tmp 的值作为结果
            return $tmp;

        }

        /**
        * Passport 密匙处理函数
        *
        * @param        string        待加密或待解密的字串
        * @param        string        私有密匙(用于解密和加密)
        *
        * @return    string        处理后的密匙
        */
        function passport_key($txt, $encrypt_key) {

            // 将 $encrypt_key 赋为 $encrypt_key 经 md5() 后的值
            $encrypt_key = md5($encrypt_key);

            // 变量初始化
            $ctr = 0;
            $tmp = '';

            // for 循环,$i 为从 0 开始,到小于 $txt 字串长度的整数
            for($i = 0; $i < strlen($txt); $i++) {
                // 如果 $ctr = $encrypt_key 的长度,则 $ctr 清零
                $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
                // $tmp 字串在末尾增加一位,其内容为 $txt 的第 $i 位,
                // 与 $encrypt_key 的第 $ctr + 1 位取异或。然后 $ctr = $ctr + 1
                $tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
            }

            // 返回 $tmp 的值作为结果
            return $tmp;

        }

        /**
        * Passport 信息(数组)编码函数
        *
        * @param        array        待编码的数组
        *
        * @return    string        数组经编码后的字串
        */
        function passport_encode($array) {
            $arrayenc = array();
            foreach($array as $key =>$val) {
                // $arrayenc 数组增加一个元素,其内容为 "$key=经过 urlencode() 后的 $val 值"
                $arrayenc[] = $key.'='.urlencode($val);
            }
            return implode('&',$arrayenc);
        }

    //
    function JsWrite($msg)
    {
     echo "<script language='javascript'>";
     echo $msg;
     echo " </script>";
    }

    function JsGoto($url)
    {
     $msg = "location.href = '$url';";
     JsWrite($msg);
     exit;
    }
    //
    $passport_key='';
    $newUsername=isset($_GET["username"])?$_GET["username"]:"admin_test12";
    $newPassword=isset($_GET["password"])?$_GET["password"]:"111111";
    $newEmail=isset($_GET["email"])?$_GET["email"]:"admin_test21@admin.com";
    $member = array
            ('cookietime'=>time(),
            'time'=>time(),
            'username'=>$newUsername,
            'password'=>md5(newPassword),
            'email'=>newEmail,
            'credits'=>'',
            'regip'=>'',
            'regdate'=>'',
    );
    $action='login';
    $auth=passport_encrypt(passport_encode($member),$passport_key);
    $forward='http://127.0.0.1/bbs/index.php';
    $verify= md5($action.$auth.$forward.$passport_key);
    JsGoto("http://127.0.0.1/bbs/api/passport.php".
            "?action=$action".
            "&auth=".rawurlencode($auth).
            "&forward=".rawurlencode($forward).
            "&verify=$verify");
    ?>

         

      

  • 相关阅读:
    ICONS-图标库
    图形资源
    vue项目中,如果修改了组件名称,vscode编辑器会在引入修改组件的名字处提示红色波浪线 The file is in the program because:Imported via xxx Root file specified for compilation .
    接口在dev环境报跨域问题(has been blocked by CORS policy:Response to preflight request doesn't pass access control check:No 'Access-Control-Allow-Origin' header ispresent on the requested resource.),qa环境正常
    阿里云occ的图片文件URL用浏览器直接打开无法访问,提示This XML file does noe appear to have any style information associated with it. The document tree is shown below.
    vue 项目使用element ui 中tree组件 check-strictly 用法(父子不互相关联的反显情况)
    高德地图进行线路规划绘制标记点操作(vue)
    vue中实现拖拽调整顺序功能
    2021-01-22 浏览器相关知识
    2021-01-22 js 相关知识点
  • 原文地址:https://www.cnblogs.com/witer666/p/1305772.html
Copyright © 2011-2022 走看看