来自外部网站 Codeigniter 的 Json 身份验证


Json Authentication from external website Codeigniter

我目前有一个Codeigniter 3框架网站(称为外部系统),我刚刚购买了一个帮助台php脚本,其中包括2个文件Json身份验证,有助于连接外部系统。

的目的是当用户去帮助台登录时,他们需要使用我当前的网站登录信息,这样他们就不必再次注册(这样,帮助台的用户信息和我的Codeigniter脚本可以保持不变)。

帮助台的后端有一些需要提供的字段,例如:URL,站点ID,身份验证密钥,登录时创建用户。

帮助台脚本有 2 个文件,需要将它们上传到外部系统、索引.php和 Authclass.php。Authclass.php只是用于加密的类和函数(无需自定义)。所以我认为 Authclass.php 需要上传到 Codeigniter 应用程序中的库文件夹。索引.php的代码如下。

任何人都可以帮助我或建议将这些文件上传到代码点火器文件夹的位置以及如何修改以使其工作?

非常感谢

索引.php

<?php
header('Cache-Control: no-cache, must-revalidate');
header('Content-Type: application/json; charset=utf-8');
include('authclass.php');
$auth = new auth();
$send_array['success'] = 0;
if (isset($_POST['site_id']) && isset($_POST['data'])) {
   if ($_POST['site_id'] == 1) {
    /*Set your authentication key here*/
    $auth->set('key', '');
    $data = $auth->decrypt($_POST['data']);
    $receive_array = json_decode($data, true);
    if (is_array($receive_array)) {
        if ($receive_array['task'] == 'authenticate') {
            /*This would connect to your external database here.*/
            if ($receive_array['username'] == 'username' && $receive_array['password'] == 'password') {
                $send_array['success']  = 1;
                $send_array['name']     = 'example name';
                $send_array['email']    = 'example@example.com';                
            }
            echo $auth->encrypt(json_encode($send_array));  
        }
    }
}
}
?>

身份验证类.php

class auth {
  private $config = array();
  private $user = array();
  function __construct() {  
    $this->config['key']                = '';
  }
  public function set($name, $value) {
    $this->config[$name] = $value;
  }
  ....

我尝试过的更新 索引.php 下面的索引(在控制器文件夹中)已重命名为帮助台.php

<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
        $this->output->set_header('Cache-Control: no-cache, must-revalidate');
        $this->output->set_header('Content-Type: application/json; charset=utf-8');

    public function helpdesk() { 
        $this->load->library('auth');
        $this -> new auth();
        $send_array['success'] = 0;
        if (isset($_POST['site_id']) && isset($_POST['data'])) {
            if ($_POST['site_id'] == 1) {
    /*
        Set your authentication key here
    */
    $this->auth->set('key', 'test1234');
    $data = $this->auth->decrypt($_POST['data']);
    $receive_array = json_decode($data, true);
    if (is_array($receive_array)) {
        if ($receive_array['task'] == 'authenticate') {
            /*
                This is where your auth function exists (you would connect to your external database here).
            */
            if ($receive_array['username'] == ['email'] && $receive_array['password'] == ['password']) {
                $send_array['success']  = 1;
                $send_array['name']     = ['username'];
                $send_array['username']     = ['email'];    
                $send_array['password']     = ['password'];                 
            }
            echo $this->auth->encrypt(json_encode($send_array));    
        }
    }
}
}
}

?>

Authclass.php被重新保留为Auth.php并上传到库文件夹,我试图修改

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class auth {
  protected $ci; //in case you need to call CI's functions
  function __construct($params = array()) 
  {
    parent::__construct();
    $this->ci = get_instance();
  }
  private $config = array();
  private $user = array();
  function __construct() {  
    $this->config['key']                = '';
  }
  public function set($name, $value) {
    $this->config[$name] = $value;
  }
  ............

(注意:根据身份验证类.php看起来这可能有效,也可能无效。

将 authclass.php 重命名为 Auth.php(区分大小写),确保该文件顶部的类名为"Auth",并将其放在应用程序/库中。

创建具有以下功能的控制器:

function test(){
   $this->load->library('auth');
   $auth = new auth();
   /* rest of index.php code should work */
}

您将他们的身份验证类转换为库是正确的。确保需要调用的函数是公共的。

所以在application/libraries

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class HelpdeskAuth
{
    protected $ci; //in case you need to call CI's functions
    function __construct($params = array()) 
    {
        parent::__construct();
        $this->ci = get_instance();
    }
    //Their code here
}

对于他们的index.php. 您可以创建一个新控制器并将代码粘贴到 index() 函数中,或者在现有控制器中编写一个新函数。

只需要确保你正确地调用了我们创建的库

public function index()
{
    $this->load->library("HelpdeskAuth");
    $send_array['success'] = 0;
    if (isset($_POST['site_id']) && isset($_POST['data'])) 
    {
       if ($_POST['site_id'] == 1) 
       {
            /*Set your authentication key here*/
            $this->HelpdeskAuth->set('key', '');
            $data = $this->HelpdeskAuth->decrypt($_POST['data']);
            $receive_array = json_decode($data, true);
            if (is_array($receive_array)) 
            {
                if ($receive_array['task'] == 'authenticate') 
                {
                    /*This would connect to your external database here.*/
                    if ($receive_array['username'] == 'username' && $receive_array['password'] == 'password') {
                        $send_array['success']  = 1;
                        $send_array['name']     = 'example name';
                        $send_array['email']    = 'example@example.com';                
                    }
                    echo $this->HelpdeskAuth->encrypt(json_encode($send_array));  
                }
            }
        }
    }
}

您还可以将所有$_POST['']转换为$this->input->post('')并使用$this->output->set_output()而不是最终回声,但这不是强制性的