tilde-oauth/auth/api/index.php

108 lines
4.2 KiB
PHP
Raw Normal View History

2023-10-04 05:26:33 +00:00
<?php
$config = json_decode(file_get_contents("/var/www/usergen/secret/config.json", true));
require_once("/var/www/usergen/secret/helpers.php");
require_once("/var/www/usergen/secret/oauth.php");
require_once("/var/www/usergen/secret/rsa.php");
if (isset($_REQUEST["act"])) {
// internal functions such as id request
switch($_REQUEST["act"]){
case "id":
// return OAUTH app ID
header('Content-type: application/json');
echo json_encode(array("id" => $config->oauth->key));
exit();
break;
case "login":
if (isset($_REQUEST["code"])){
// Mastodon callback (Authorization Code from /oauth/authorize)
$authCode = $_REQUEST["code"];
$Auth = oauthToken($authCode, $config);
if(isset($Auth->token_type)){
// Valid Auth?
$User = verifyCredentials($Auth->access_token);
if (gettype($User) == "object" && isset($User->id)) {
// Congrats!
returnSuccess($User, buildEncToken($Auth->access_token, $User->id, $_SERVER["REMOTE_ADDR"], $_SERVER["HTTP_USER_AGENT"]));
}else{
// invalid auth
// $User contains error string
returnError($User);
}
}else{
// invalid auth
returnError($Auth);
}
}else{
returnError("Incorrect Login Query");
}
break;
case "verify":
if (isset($_REQUEST["token"])){
// Verify Encrypted Token
$EncTokenData = $_REQUEST["token"];
$TokenData = verifyEncToken($EncTokenData);
if (gettype($TokenData) == "string") {
// Invalid Token
returnError($TokenData);
}else{
// Valid Token
returnSuccess($TokenData["MastodonData"],
buildEncToken($TokenData["AuthToken"],
$TokenData["UserID"],
$_SERVER["REMOTE_ADDR"],
$_SERVER["HTTP_USER_AGENT"]
)
);
}
}else{
returnError("Incorrect Verify Query");
}
break;
2023-10-05 22:19:12 +00:00
case "gemproxy":
if (isset($_REQUEST["token"])){
// Verify Encrypted Token
$EncTokenData = $_REQUEST["token"];
$TokenData = verifyEncToken($EncTokenData);
if (gettype($TokenData) == "string") {
// Invalid Token
returnError($TokenData);
}else{
// Valid Token
if (isset($_REQUEST["enable"])){
$userHomeDir = shell_exec("eval echo ~".$TokenData["MastodonData"]->username);
if (!file_exists($userHomeDir)){
returnError("User Home Directory Not Found, try making a new SSH key.");
} else {
if (file_exists($userHomeDir."/public_gemini")){
mkdir($userHomeDir."/public_gemini", 0755);
}
$EnableFile = $userHomeDir."/public_gemini/.serve_ok";
if ($_REQUEST["enable"] == 1){
if (!file_exists($EnableFile)){
file_put_contents($EnableFile, "web_gen");
}
}else{
if (file_exists($EnableFile)){
unlink($EnableFile);
}
}
}
}else {
returnError("Incorrect Gemini Proxy Query");
}
}
}
break;
2023-10-04 05:26:33 +00:00
default:
returnError("Incorrect Action Query");
break;
}
}else {
returnError("Incorrect Empty Query");
}
?>