PHP_SMART_HOME_V3/app/views/Oauth.php

87 lines
3.0 KiB
PHP
Raw Normal View History

2020-05-16 23:27:06 +00:00
<?php
//https://developers.google.com/oauthplayground/
//https://developer.okta.com/blog/2018/04/10/oauth-authorization-code-grant-type
class Oauth
2020-05-16 23:27:06 +00:00
{
function default()
2020-05-16 23:27:06 +00:00
{
//Log
$logManager = new LogManager(__DIR__ . '/../../logs/auth/' . date("Y-m-d") . '.log');
$logManager->setLevel(LOGLEVEL);
$logManager->write("[OAUTH] GET " . json_encode($_GET), LogRecordTypes::WARNING);
$logManager->write("[OAUTH] DATA " . file_get_contents('php://input'), LogRecordTypes::WARNING);
$logManager->write("[OAUTH] URL " . $_SERVER['REQUEST_URI'], LogRecordTypes::WARNING);
$userManager = new UserManager();
2020-07-23 13:16:44 +00:00
$langMng = new LanguageManager('en');
2020-05-16 23:27:06 +00:00
$template = new Template('oauth');
$template->prepare('baseDir', BASEDIR);
2020-12-09 11:19:19 +00:00
$template->prepare('baseUrl', BASEURL);
2020-05-17 12:24:56 +00:00
$template->prepare('title', 'Simple Home - Oauth');
2020-05-16 23:27:06 +00:00
if (isset($_GET['response_type']) && $_GET['response_type'] == 'code') {
2020-05-16 23:27:06 +00:00
$template->prepare('responseType', $_GET['response_type']);
$template->prepare('redirectUrl', $_GET['redirect_uri']);
$template->prepare('clientId', $_GET['client_id']);
$template->prepare('scope', $_GET['scope']);
2020-05-16 23:27:06 +00:00
$template->prepare('state', $_GET['state']);
} else {
if (isset($_GET['redirect_uri'])) {
$template->prepare('responseType', $_GET['response_type']);
$template->prepare('redirectUrl', $_GET['redirect_uri']);
$template->prepare('clientId', $_GET['client_id']);
$template->prepare('state', $_GET['state']);
} else {
$template->prepare('responseType', $_POST['responseType']);
$template->prepare('redirectUrl', $_POST['redirectUrl']);
$template->prepare('clientId', $_POST['clientId']);
$template->prepare('state', $_POST['state']);
}
2020-05-16 23:27:06 +00:00
}
$template->render();
}
function token()
{
//Log
$logManager = new LogManager(__DIR__ . '/../../logs/auth/' . date("Y-m-d") . '.log');
$logManager->setLevel(LOGLEVEL);
$logManager->write("[OAUTH] GET " . json_encode($_GET), LogRecordTypes::WARNING);
$logManager->write("[OAUTH] POST " . json_encode($_POST), LogRecordTypes::WARNING);
$logManager->write("[OAUTH] DATA " . file_get_contents('php://input'), LogRecordTypes::WARNING);
$logManager->write("[OAUTH] URL " . $_SERVER['REQUEST_URI'], LogRecordTypes::WARNING);
// $template = new Template('oauth');
// $template->prepare('baseDir', BASEDIR);
// $template->prepare('baseUrl', BASEURL);
// $template->prepare('title', 'Simple Home - Oauth');
// $template->render();
$token = $_POST["code"];
$get = [
"access_token" => $token,
"token_type" => "bearer",
"refresh_token" => $token,
"scope" => 'user',
];
$logManager->write("[OAUTH] Response " . json_encode($get), LogRecordTypes::WARNING);
echo json_encode($get);
die();
}
function httpPost($url, $data)
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
2020-05-16 23:27:06 +00:00
}