oauth2实现php codeigniter

时间:2018-07-16 09:19:25

标签: javascript php oauth-2.0 codeigniter-3

我正在使用oauth2进行Web应用程序以进行用户身份验证。

我已将标头设置为授权承载,但出现错误

  

{“错误”:“ invalid_request”,“错误说明”:“一次只能使用一种方法进行身份验证(Auth标头,GET或POST)”}

我尝试了如下代码 Javscript函数

$.ajaxSetup({
  beforeSend: function(xhr) {
    xhr.setRequestHeader('Authorization', 'Bearer' + localStorage["access_token"]);
  }
});

function passDataToController(msg) {
  var fData = new FormData();
  fData.append('access_token', msg['access_token']);
  fData.append('refresh_token', msg['refresh_token']);
  fData.append('token_type', msg['token_type']);
  fData.append('scope', msg['scope']);
  $.ajax({
    type: "POST",
    url: site_url + "Dashboard/init",
    data: fData,
    cache: false,
    contentType: false,
    processData: false,
    success: function(msg) {
      console.log(msg);
      var html = JSON.parse(msg);
      if (html.status === true) {
        $(".se-pre-con").hide();

        window.location.href = site_url + 'Dashboard';
      } else
      if (html.status === false) {
        $(".se-pre-con").hide();
        console.log(html.message);
        window.location.href = site_url + 'Signin';
      }
    },
    error: function(jqXHR, textStatus, errorThrown) {
      modalShow(errorThrown);
    }
  }, "json");
}

仪表板控制器

class Dashboard extends MY_Controller {
  public function __construct() {
    parent::__construct();
    $this->server_response = new stdClass();
    $this->load->helper('url');
    $this->load->database();
    $this->load->library('session');
  }

  public function init() {
    $this->load->view('Dashboard_view', '');
  }
}

MY_controller

<?php

class MY_Controller extends CI_Controller {

    function __construct() {

        //@session_start();
        parent::__construct();
        $this->load->library("Server", "server");
        $this->load->database();
        $this->server->require_scope("userinfo"); //you can require scope here 
    }
    public function isAllMandatoryFieldsPresent($MANDATORY_FIELDS,$receivedFields) {
        $flag = 1;
        //$MANDATORY_FIELDS = array('doctorcontactnumber', 'doctorname', 'subscriptionlimittype');
        $MANDATORY_FIELDS_COUNT = count($MANDATORY_FIELDS);
        for ($i = 0; $i < $MANDATORY_FIELDS_COUNT; $i++) {
            if (!array_key_exists($MANDATORY_FIELDS[$i], $receivedFields)) {
                $flag = 0;
                break 1;
            }
        }
        if ($flag == 0) {
            return FALSE;
        } else {
            return TRUE;
        }
    }

    public function mp_isexist($data, $key) {
        if (array_key_exists($key, $data)) {
            if (!empty($data[$key])) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function mp_isemail($data, $key) {
        if ($this->mp_isexist($data, $key)) {
            if (valid_email($data[$key])) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function mp_ismobilenumber($data, $key) {
        if ($this->mp_isexist($data, $key)) {
            if (preg_match('/^\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/', $data[$key])) {
                return true;
            } else {
                return FALSE;
            }
        } else {
            return false;
        }
    }

    public function mp_ispassword($data, $key) {
        if ($this->mp_isexist($data, $key)) {
            if (strlen($data[$key]) >= 8) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function mp_isconfirmpassword($data, $key, $preg) {
        if ($this->mp_isexist($data, $key)) {
            if ($data[$key] == $data[$preg]) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function mp_isalpha($data, $key) {
        if ($this->mp_isexist($data, $key)) {
            if (ctype_alpha($data[$key])) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function mp_isdigit($data, $key) {
        if ($this->mp_isexist($data, $key)) {
            if (ctype_digit($data[$key])) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function mp_isdate($data, $key) {
        if ($this->mp_isexist($data, $key)) {
            if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $data[$key])) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function mp_encrypt($data) {
        $encoded_string = trim($this->encrypt->encode($data));
        $encoded_pattern = str_replace(array('+', '/', '='), array('-', '_', '~'), $encoded_string);
        return $encoded_pattern;
    }

    public function mp_decrypt($data) {
        $decoded_string = str_replace(array('-', '_', '~'), array('+', '/', '='), $data);
        $decoded_pattern = trim($this->encrypt->decode($decoded_string));
        return $decoded_pattern;
    }

    public function mp_isclinicdetails($data, $key) {
        $array_count = count($data);
        if ($array_count != 0) {
            if ($array_count % 3 == 0) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function displaydata($key) {
        $message = array(
            'invalid' => 'Invalid request',
            'true' => 'TRUE',
            'false' => 'FALSE',
            'email' => 'Enter valid email',
            'emailexist' => 'Email already exist',
            'emailnotexist' => 'Email not exist',
            'mobilenumber' => 'Enter valid mobile number',
            'password' => 'Enter valid 8 character password',
            'confirmpassword' => 'Password and confirm password does not match',
            'signupsuccess' => 'Successfully sign up',
            'signuperror' => 'Invalid sign up',
            'signinsuccess' => 'Successfully sign in',
            'signinerror' => 'Invalid sign in',
            'notactivate' => 'Account not activate',
            'doctor_id' => 'Enter valid doctor id',
            'firstname' => 'Enter valid first name',
            'lastname' => 'Enter valid last name',
            'dateofbirth' => 'Enter valid date of birth',
            'medicalcouncil' => 'Enter valid medical council',
            'year' => 'Enter valid year',
            'registrationnumber' => 'Enter valid registration number',
            'degree' => 'Enter valid degree',
            'degreecertificate' => 'Upload degree certificate',
            'profilephoto' => 'Upload profile photo',
            'editprofilesuccess' => 'Profile successfully update',
            'editprofileerror' => 'Profile not update',
            'addpatientsuccess' => 'Patient insert successfully',
            'addpatienterror' => 'Failed to insert patient',
            'clinicdetails' => 'Enter valid clinic details',
            'degreecertificate_error' => 'Degree certificate not upload',
            'profilephoto_error' => 'Profile photo not upload'
        );
        return $message[$key];
    }

    public function setStatusCode($code) {
        $statuscodetext = array(
            100 => 'Continue',
            101 => 'Switching Protocols',
            200 => 'OK',
            201 => 'Created',
            202 => 'Accepted',
            203 => 'Non-Authoritative Information',
            204 => 'No Content',
            205 => 'Reset Content',
            206 => 'Partial Content',
            300 => 'Multiple Choices',
            301 => 'Moved Permanently',
            302 => 'Found',
            303 => 'See Other',
            304 => 'Not Modified',
            305 => 'Use Proxy',
            307 => 'Temporary Redirect',
            400 => 'Bad Request',
            401 => 'Unauthorized',
            402 => 'Payment Required',
            403 => 'Forbidden',
            404 => 'Not Found',
            405 => 'Method Not Allowed',
            406 => 'Not Acceptable',
            407 => 'Proxy Authentication Required',
            408 => 'Request Time-out',
            409 => 'Conflict',
            410 => 'Gone',
            411 => 'Length Required',
            412 => 'Precondition Failed',
            413 => 'Request Entity Too Large',
            414 => 'Request-URI Too Large',
            415 => 'Unsupported Media Type',
            416 => 'Requested range not satisfiable',
            417 => 'Expectation Failed',
            500 => 'Internal Server Error',
            501 => 'Not Implemented',
            502 => 'Bad Gateway',
            503 => 'Service Unavailable',
            504 => 'Gateway Time-out'
        );
        return $statuscodetext[$code];
    }

}

0 个答案:

没有答案