/** * 控制器执行主逻辑函数 */ public function invoke($uri = null) { // 调用父类初始化函数,注册自定义的异常和错误处理逻辑 parent::init(); $path = explode('?', $uri); $parts = array_slice(explode('/', $path[0]), 2); if ($parts[0] === "authorize") { $oauth = new PDOOAuth2(); if ($_POST) { $oauth->finishClientAuthorization($_POST["accept"] == "Yep", $_POST); } $auth_params = $oauth->getAuthorizeParams(); $inputs = ""; foreach ($auth_params as $k => $v) { $inputs = $inputs . '<input type="hidden" name="' . $k . '" value="' . $v . '" />'; } $content = '<html>' . '<head>Authorize</head>' . '<body>' . '<form method="post" action="http://web.miniyun.cn/miniyun_oauth2/api.php/1/oauth2/authorize">' . $inputs . 'Do you authorize the app to do its thing?' . '<p>' . ' <input type="submit" name="accept" value="Yep" />' . '<input type="submit" name="accept" value="Nope" />' . ' </p>' . '</form>' . '</body>' . ' </html>'; echo $content; } elseif ($parts[0] === "token") { $oauth = new PDOOAuth2(); $token = $oauth->grantAccessToken(); #添加登陆日志 $deviceId = $oauth->getVariable("device_id"); MiniLog::getInstance()->createLogin($deviceId); #返回site_id,便于与cloud.miniyun.cn通信 $token["site_id"] = MiniSiteUtils::getSiteID(); echo json_encode($token); } }
<?php /** * @file * Sample token endpoint. * * Obviously not production-ready code, just simple and to the point. * * In reality, you'd probably use a nifty framework to handle most of the crud for you. */ require "lib/PDOOAuth2.php"; $oauth = new PDOOAuth2(); $oauth->grantAccessToken();
/** * This is a callback for authorized token * @method requesting authorize_token * If the device has been approved, we can read the authorize_token necessarily * Callback: * [offiria]/index.php/component/oauth/?view=oauth&task=authenticate&response_type=code&client_id=[clientId]&client_secret=[clientSecret] * * @method requesting access_token with authorize token * If the device have the authorize_token, exchange for access_token to use access * Callback: * [offiria]/index.php/component/oauth/?view=oauth&task=authenticate&grant_type=authorization_code&client_id=[clientId]&client_secret=[clientSecret]&code=[code] * * @method requesting access_token with password (skipping authorization) * Callback: * [offiria]/index.php/component/oauth/?view=oauth&task=authenticate&grant_type=password &client_id=[clientId]&client_secret=[clientSecret]&username=[username]&pass=[pass]&redirect_uri=[redirect_uri] */ public function authenticate() { $oauth = new PDOOAuth2(); $responseType = JRequest::getVar('response_type'); $clientId = JRequest::getVar('client_id'); $clientSecret = JRequest::getVar('client_secret'); $table = JTable::getInstance('Token', 'OAuthTable'); if ($responseType == 'code') { header("Content-Type: application/json"); header("Cache-Control: no-store"); $code = $table->getParam('code', array('client_id' => $clientId, 'client_secret' => $clientSecret)); echo json_encode(array('code' => $code)); exit; break; } else { switch (JRequest::getVar('grant_type')) { case 'password': $app = JFactory::getApplication(); // Get the log in credentials. $credentials = array(); $credentials['username'] = JRequest::getVar('username'); $credentials['password'] = JRequest::getVar('pass'); // Get the log in options. $options = array(); $options['remember'] = false; $options['return'] = ''; header("Content-Type: application/json"); header("Cache-Control: no-store"); // error handler if (!JRequest::getVar('redirect_uri')) { echo json_encode(array('error' => 'invalid_redirect_uri')); exit; } // Perform the log in. if (true === $app->login($credentials, $options)) { // Success $url = JRoute::_('/index.php/component/profile/?view=edit&task=applications&client_id=' . JRequest::getVar('client_id') . '&client_secret=' . JRequest::getVar('client_secret') . '&redirect_uri=' . JRequest::getVar('redirect_uri') . '&silent=true'); $mainframe = JFactory::getApplication(); $mainframe->redirect($url); } break; default: $oauth->grantAccessToken(); } exit; } exit; }
/** * 用户登录 * @return array|bool * @throws */ public function oauth2() { $isExtend = apply_filters("license_expired"); if ($isExtend === 1) { $userName = MiniHttp::getParam("username", ""); if ($userName !== "admin") { throw new MiniException(440); } } $oauth = new PDOOAuth2(); $token = $oauth->grantAccessToken(); #添加登陆日志 $deviceId = $oauth->getVariable("device_id"); MiniLog::getInstance()->createLogin($deviceId); #返回site_id,便于与cloud.miniyun.cn通信 $token["site_id"] = MiniSiteUtils::getSiteID(); return $token; }
<?php /** * @file * Sample token endpoint. * * Obviously not production-ready code, just simple and to the point. * * In reality, you'd probably use a nifty framework to handle most of the crud for you. */ session_start(); require "lib/PDOOAuth2.inc"; $oauth = new PDOOAuth2(); $actoken = $oauth->grantAccessToken(); $host = 'localhost'; $username = '******'; $password = '******'; $database = 'mydb'; $dbc = mysqli_connect($host, $username, $password, $database); if (!$dbc) { die('Could not connect: ' . mysql_error()); } $token = $actoken['access_token']; $uid = $_POST['uid']; $query = "INSERT INTO tokenuid VALUES ('{$token}','{$uid}')"; mysqli_query($dbc, $query) or die("Error!!"); mysqli_close($dbc);