2022-04-26 19:48:03 +00:00
|
|
|
<?php
|
|
|
|
namespace fox\auth;
|
|
|
|
|
|
|
|
use fox\externalCallable;
|
|
|
|
use fox\request;
|
|
|
|
use fox\oAuthProfile;
|
|
|
|
use fox\config;
|
|
|
|
use fox\common;
|
|
|
|
use fox\foxException;
|
|
|
|
use fox\user;
|
|
|
|
use fox\authToken;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Class fox\auth\oauth
|
|
|
|
*
|
|
|
|
* @copyright MX STAR LLC 2018-2022
|
|
|
|
* @version 4.0.0
|
|
|
|
* @author Pavel Dmitriev
|
|
|
|
* @license GPLv3
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
|
|
|
|
class oauth implements externalCallable
|
|
|
|
{
|
|
|
|
public static function APICall(request $request) {
|
|
|
|
switch ($request->method) {
|
|
|
|
case "GET":
|
|
|
|
try {
|
|
|
|
$profile = new oAuthProfile(common::clearInput($request->function,"0-9"));
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
if ($e->getCode()==691) {
|
|
|
|
throw new foxException("Not found",404);
|
|
|
|
} else {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($profile->deleted || !$profile->enabled) {
|
|
|
|
throw new foxException("Not found",404);
|
|
|
|
}
|
|
|
|
return [
|
|
|
|
"id"=>$profile->id,
|
|
|
|
"name"=>$profile->name,
|
|
|
|
"url"=>$profile->getClient(config::get("SITEPREFIX")."/auth/oauth")->getAuthURL(),
|
|
|
|
"icon"=>$profile->getClient(config::get("SITEPREFIX"))->getAuthIcon(),
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "POST":
|
|
|
|
$profile = oAuthProfile::getByHash(common::clearInput($request->requestBody->hash));
|
|
|
|
$oac = $profile->getClient(config::get("SITEPREFIX")."/auth/oauth");
|
2022-05-12 13:59:48 +00:00
|
|
|
$oac->getTokenByCode(common::clearInput($request->requestBody->code));
|
2022-04-26 19:48:03 +00:00
|
|
|
$userInfo=$oac->getUserInfo();
|
|
|
|
$userRefId=$profile->id.":".$userInfo->sub;
|
|
|
|
$u=user::getByRefID("oauth",$userRefId);
|
|
|
|
|
|
|
|
if (!$u) {
|
|
|
|
foxException::throw("ERR", "User not registered",401,"UNR");
|
|
|
|
}
|
|
|
|
|
|
|
|
$t = authToken::issue($u, "WEB");
|
|
|
|
return [
|
|
|
|
"token" => $t->token,
|
|
|
|
"expire" => $t->expireStamp->isNull() ? "Never" : $t->expireStamp
|
|
|
|
];
|
|
|
|
|
|
|
|
break;
|
2022-05-12 13:59:48 +00:00
|
|
|
default:
|
|
|
|
throw new foxException("Method not implemented",405);
|
2022-04-26 19:48:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|