2023-06-08 15:10:27 +01:00
|
|
|
<?php
|
2023-10-18 23:58:21 +01:00
|
|
|
|
2023-06-08 15:10:27 +01:00
|
|
|
namespace api\user;
|
2023-10-18 23:58:21 +01:00
|
|
|
require_once __DIR__ . "/../utils/routesInterface.php";
|
2023-06-08 15:10:27 +01:00
|
|
|
require_once "userData.php";
|
|
|
|
|
|
|
|
use api\utils\routesInterface;
|
2024-01-01 13:52:30 +00:00
|
|
|
use OneLogin\Saml2\Auth;
|
|
|
|
use OneLogin\Saml2\Error;
|
2023-06-08 15:10:27 +01:00
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
|
|
use Slim\App;
|
|
|
|
|
|
|
|
class userRoutes implements routesInterface
|
|
|
|
{
|
|
|
|
private userData $user;
|
2024-01-01 13:52:30 +00:00
|
|
|
private Auth $samlAuth;
|
2023-06-09 14:50:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* constructor used to instantiate a base user routes, to be used in the index.php file.
|
|
|
|
* @param App $app - the slim app used to create the routes
|
2024-01-01 13:52:30 +00:00
|
|
|
* @throws Error
|
2023-06-09 14:50:20 +01:00
|
|
|
*/
|
2023-06-08 15:10:27 +01:00
|
|
|
public function __construct(App $app)
|
|
|
|
{
|
|
|
|
$this->user = new userData();
|
2024-01-01 13:52:30 +00:00
|
|
|
$this->samlAuth = new Auth($this->user->getSamlConf());
|
2023-06-08 15:10:27 +01:00
|
|
|
$this->createRoutes($app);
|
|
|
|
}
|
|
|
|
|
2023-06-09 14:50:20 +01:00
|
|
|
/**
|
|
|
|
* creates the routes for the user
|
|
|
|
* @param App $app - the slim app used to create the routes
|
|
|
|
* @return void - returns nothing
|
|
|
|
*/
|
2023-06-08 15:10:27 +01:00
|
|
|
public function createRoutes(App $app): void
|
|
|
|
{
|
2024-01-01 13:52:30 +00:00
|
|
|
$app->get("/user/login", function (Request $request, Response $response)
|
2023-06-08 15:10:27 +01:00
|
|
|
{
|
2024-01-01 13:52:30 +00:00
|
|
|
$this->samlAuth->login();
|
2023-06-08 15:10:27 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$app->get("/user/logout", function (Request $request, Response $response)
|
|
|
|
{
|
|
|
|
session_unset();
|
|
|
|
return $response;
|
|
|
|
});
|
|
|
|
|
|
|
|
$app->get("/user/isLoggedIn", function (Request $request, Response $response)
|
|
|
|
{
|
|
|
|
if (empty($_SESSION["token"]) && empty($_SESSION["username"]))
|
|
|
|
{
|
2023-07-12 03:29:56 +01:00
|
|
|
// uh oh user not logged in
|
|
|
|
return $response->withStatus(401);
|
|
|
|
}
|
|
|
|
|
|
|
|
$inactive = 60 * 60 * 48; // 2 days
|
|
|
|
$sessionLife = time() - $_SESSION["timeout"];
|
|
|
|
if ($sessionLife > $inactive)
|
|
|
|
{
|
|
|
|
// uh oh user session expired
|
|
|
|
session_destroy();
|
2023-06-08 15:10:27 +01:00
|
|
|
return $response->withStatus(401);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($_SESSION["token"]))
|
|
|
|
{
|
2023-07-12 03:29:56 +01:00
|
|
|
// user is logged in but no token was created
|
2023-06-08 15:10:27 +01:00
|
|
|
$_SESSION["token"] = $this->user->createToken($_SESSION["username"]);
|
2023-07-12 03:29:56 +01:00
|
|
|
return $response->withStatus(201);
|
2023-06-08 15:10:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$response->getBody()->write(json_encode(array("token" => $_SESSION["token"])));
|
|
|
|
return $response;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2024-01-01 13:52:30 +00:00
|
|
|
$app->get("/user/metadata", function (Request $request, Response $response)
|
|
|
|
{
|
|
|
|
$settings = $this->samlAuth->getSettings();
|
|
|
|
$metadata = $settings->getSPMetadata();
|
|
|
|
$errors = $settings->validateMetadata($metadata);
|
|
|
|
if (empty($errors))
|
|
|
|
{
|
|
|
|
$response->getBody()->write($metadata);
|
|
|
|
return $response->withHeader("Content-Type", "text/xml");
|
|
|
|
}
|
|
|
|
$response->getBody()->write(json_encode(array("error" => $errors)));
|
|
|
|
return $response->withStatus(500);
|
|
|
|
});
|
|
|
|
|
2023-06-08 15:10:27 +01:00
|
|
|
$app->get("/user/checkResetEmail/{email}", function (Request $request, Response $response, array $args)
|
|
|
|
{
|
|
|
|
if (empty($args["email"]))
|
|
|
|
{
|
|
|
|
// uh oh sent empty data
|
|
|
|
return $response->withStatus(400);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->user->checkEmail($args["email"]))
|
|
|
|
{
|
|
|
|
// yay email does exist
|
|
|
|
$_SESSION["resetToken"] = $this->user->sendResetEmail($args["email"]);
|
|
|
|
$_SESSION["resetEmail"] = $args["email"];
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
return $response->withStatus(404);
|
|
|
|
});
|
|
|
|
|
|
|
|
$app->get("/user/resendEmail", function (Request $request, Response $response)
|
|
|
|
{
|
|
|
|
if (empty($_SESSION["resetToken"]))
|
|
|
|
{
|
|
|
|
// uh oh not authorized to resend email
|
|
|
|
return $response->withStatus(401);
|
|
|
|
}
|
|
|
|
|
|
|
|
$_SESSION["resetToken"] = $this->user->sendResetEmail($_SESSION["resetEmail"]);
|
|
|
|
return $response;
|
|
|
|
});
|
|
|
|
|
|
|
|
$app->get("/user/checkResetCode/{code}", function (Request $request, Response $response, array $args)
|
|
|
|
{
|
|
|
|
if (empty($args["code"]))
|
|
|
|
{
|
|
|
|
// uh oh sent empty data
|
|
|
|
return $response->withStatus(400);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($_SESSION["resetToken"] === $args["code"])
|
|
|
|
{
|
|
|
|
// yay, code code matches
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response->withStatus(401);
|
|
|
|
});
|
|
|
|
|
2024-01-01 13:52:30 +00:00
|
|
|
$app->post("/user/login", function (Request $request, Response $response)
|
|
|
|
{
|
|
|
|
// get request data
|
|
|
|
$data = $request->getParsedBody();
|
|
|
|
|
|
|
|
if (empty($data["username"]) || empty($data["password"]))
|
|
|
|
{
|
|
|
|
// uh oh user sent empty data
|
|
|
|
return $response->withStatus(400);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->user->checkUser($data["username"], $data["password"]))
|
|
|
|
{
|
|
|
|
// yay, user is logged in
|
|
|
|
$_SESSION["token"] = $this->user->createToken($data["username"]);
|
|
|
|
$_SESSION["username"] = $data["username"];
|
|
|
|
|
|
|
|
$inactive = 60 * 60 * 48; // 2 days
|
|
|
|
$_SESSION["timeout"] = time() + $inactive;
|
|
|
|
|
|
|
|
$response->getBody()->write(json_encode(array("token" => $_SESSION["token"])));
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
$response->getBody()->write(json_encode(array("error" => "Unauthorised")));
|
|
|
|
return $response->withStatus(401);
|
|
|
|
});
|
|
|
|
|
|
|
|
$app->post("/user/acs", function (Request $request, Response $response)
|
|
|
|
{
|
|
|
|
$this->samlAuth->processResponse();
|
|
|
|
|
|
|
|
$attributes = $this->samlAuth->getAttributes();
|
|
|
|
$username = $attributes["username"][0];
|
|
|
|
$email = $attributes["email"][0];
|
|
|
|
|
|
|
|
if ($this->user->checkSAMLUser($username, $email))
|
|
|
|
{
|
|
|
|
// yay, user is logged in
|
|
|
|
$_SESSION["token"] = $this->user->createToken($username);
|
|
|
|
$_SESSION["username"] = $username;
|
|
|
|
$_SESSION["email"] = $email;
|
|
|
|
|
|
|
|
$inactive = 60 * 60 * 48; // 2 days
|
|
|
|
$_SESSION["timeout"] = time() + $inactive;
|
|
|
|
|
|
|
|
return $response->withHeader("Location", "https://rohitpai.co.uk/editor/editor.html")->withStatus(302);
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->getBody()->write(json_encode(array("error" => "Unauthorised")));
|
|
|
|
return $response->withStatus(401);
|
|
|
|
});
|
|
|
|
|
2023-06-08 15:10:27 +01:00
|
|
|
$app->post("/user/changePassword", function (Request $request, Response $response)
|
|
|
|
{
|
|
|
|
if (empty($_SESSION["resetToken"]) && empty($_SESSION["resetEmail"]))
|
|
|
|
{
|
|
|
|
// uh oh not authorized to change password
|
|
|
|
return $response->withStatus(401);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = $request->getParsedBody();
|
|
|
|
if (empty($data["password"]))
|
|
|
|
{
|
|
|
|
// uh oh sent empty data
|
|
|
|
return $response->withStatus(400);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->user->changePassword($_SESSION["resetEmail"], $data["password"]))
|
|
|
|
{
|
|
|
|
// yay, password changed
|
|
|
|
unset($_SESSION["resetToken"]);
|
|
|
|
unset($_SESSION["resetEmail"]);
|
2023-06-26 03:54:25 +01:00
|
|
|
return $response->withStatus(201);
|
2023-06-08 15:10:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $response->withStatus(500);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|