Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge login and login via token, signup and signup via token #54

Open
wants to merge 6 commits into
base: new-validator
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/GraphJS/Controllers/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
abstract class AbstractController extends \Pho\Server\Rest\Controllers\AbstractController
{
protected $validator;

public function __construct()
{
Expand Down
24 changes: 12 additions & 12 deletions src/GraphJS/Controllers/AdministrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ protected function requireAdministrativeRights(Request $request, Response $respo
error_log("founder password is: ".getenv("FOUNDER_PASSWORD"));
error_log("hash is: ".$hash);
$data = $request->getQueryParams();
$this->validator->make($data, [
$validation = $this->validator->validate($data, [
"hash" => "required"
]);
//$v->rule('length', [['hash', 32]]);
//error_log($founder->getEmail().":".$founder->getPassword().":".$hash);
error_log("data hash is: ".$data["hash"]);
if(!$this->validator->validate()||($data["hash"]!=$hash&&$data["hash"]!=$this->superadmin_hash)) {
if($validation->fails()||($data["hash"]!=$hash&&$data["hash"]!=$this->superadmin_hash)) {
return false;
}
return true;
Expand Down Expand Up @@ -96,10 +96,10 @@ public function approvePendingComment(Request $request, Response $response, Kern
if(!$this->requireAdministrativeRights(...\func_get_args()))
return $this->fail($response, "Invalid hash");
$data = $request->getQueryParams();
$this->validator->make($data, [
$validation = $this->validator->validate($data, [
"comment_id" => "required"
]);
if(!$this->validator->validate()) {
if($validation->fails()) {
$this->fail($response, "comment_id required");
return;
}
Expand All @@ -121,11 +121,11 @@ public function setCommentModeration(Request $request, Response $response, Kerne
if(!$this->requireAdministrativeRights(...\func_get_args()))
return $this->fail($response, "Invalid hash");
$data = $request->getQueryParams();
$this->validator->make($data, [
$validation = $this->validator->validate($data, [
"moderator" => "required"
]);
//$v->rule('boolean', ['moderated']);
if(!$this->validator->validate()) {
if($validation->fails()) {
return $this->fail($response, "A boolean 'moderated' field is required");
}
$is_moderated = (bool) $data["moderated"];
Expand Down Expand Up @@ -159,10 +159,10 @@ public function disapprovePendingComment(Request $request, Response $response,Ke
if(!$this->requireAdministrativeRights(...\func_get_args()))
return $this->fail($response, "Invalid hash");
$data = $request->getQueryParams();
$this->validator->make($data, [
$validation = $this->validator->validate($data, [
"comment_id" => "required"
]);
if(!$this->validator->validate()) {
if($validation->fails()) {
$this->fail($response, "comment_id required");
return;
}
Expand All @@ -183,10 +183,10 @@ public function setFounderPassword(Request $request, Response $response,Kernel $
if(!$this->requireAdministrativeRights(...\func_get_args()))
return $this->fail($response, "Invalid hash");
$data = $request->getQueryParams();
$this->validator->make($data, [
$validation = $this->validator->validate($data, [
"password" => "required"
]);
if(!$this->validator->validate()) {
if($validation->fails()) {
$this->fail($response, "password required");
return;
}
Expand All @@ -206,10 +206,10 @@ public function deleteMember(Request $request, Response $response, Kernel $kerne
return $this->fail($response, "Invalid hash");
}
$data = $request->getQueryParams();
$this->validator->make($data, [
$validation = $this->validator->validate($data, [
"id" => "required"
]);
if(!$this->validator->validate()) {
if($validation->fails()) {
return $this->fail($response, "User ID unavailable.");
}
try {
Expand Down
198 changes: 85 additions & 113 deletions src/GraphJS/Controllers/AuthenticationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,19 @@
use CapMousse\ReactRestify\Http\Response;
use CapMousse\ReactRestify\Http\Session;
use Pho\Kernel\Kernel;
use Valitron\Validator;
use PhoNetworksAutogenerated\User;
use Mailgun\Mailgun;
use Defuse\Crypto\Crypto;
use Defuse\Crypto\Key;


/**
/**
* Takes care of Authentication
*
* @author Emre Sokullu <[email protected]>
*/
class AuthenticationController extends AbstractController
{

public function signupViaToken(Request $request, Response $response, Session $session, Kernel $kernel)
{
$token_key = getenv("SINGLE_SIGNON_TOKEN_KEY") ? getenv("SINGLE_SIGNON_TOKEN_KEY") : "";
if(empty($token_key)) {
return $this->fail($response, "Single sign-on not allowed");
}
$token_key = Key::loadFromAsciiSafeString($token_key);
$data = $request->getQueryParams();
$v = new Validator($data);
$v->rule('required', ['username', 'token', 'email']);
$v->rule('email', 'email');
if(!$v->validate()) {
$this->fail($response, "Valid username, email are required.");
return;
}
if(!preg_match("/^[a-zA-Z0-9_]{1,12}$/", $data["username"])) {
$this->fail($response, "Invalid username");
return;
}
try {
$username = Crypto::decrypt($data["token"], $token_key);
}
catch(\Exception $e) {
return $this->fail($response, "Invalid token");
}
if($username!=$data["username"]) {
return $this->fail($response, "Invalid token");
}
$password = substr($data["token"], -8);
$this->actualSignup($request, $response, $session, $kernel, $username, $data["email"], $password);
}

/**
* Sign Up
*
Expand All @@ -77,22 +43,56 @@ public function signupViaToken(Request $request, Response $response, Session $se
public function signup(Request $request, Response $response, Session $session, Kernel $kernel)
{
$data = $request->getQueryParams();
$v = new Validator($data);
$v->rule('required', ['username', 'email', 'password']);
$v->rule('email', 'email');
if(!$v->validate()) {
$this->fail($response, "Valid username, email and password required.");
$rules = [
'username' => 'required',
'email' => 'required|email',
];
if (isset($data['token'])) {
$rules += [
'token' => 'required',
];
}
else {
$rules += [
'password' => 'required',
];
}
$validation = $this->validator->validate($data, $rules);
if($validation->fails()) {
$this->fail($response, "Valid username, email and password or token required.");
return;
}
if(!preg_match("/^[a-zA-Z0-9_]{1,12}$/", $data["username"])) {
$this->fail($response, "Invalid username");
return;
}
if(!preg_match("/[0-9A-Za-z!@#$%_]{5,15}/", $data["password"])) {
$this->fail($response, "Invalid password");
return;
if (isset($data['token'])) {
$token_key = getenv("SINGLE_SIGNON_TOKEN_KEY") ? getenv("SINGLE_SIGNON_TOKEN_KEY") : "";
if(empty($token_key)) {
return $this->fail($response, "Single sign-on not allowed");
}
$token_key = Key::loadFromAsciiSafeString($token_key);

try {
$username = Crypto::decrypt($data["token"], $token_key);
}
catch(\Exception $e) {
return $this->fail($response, "Invalid token");
}
if($username!=$data["username"]) {
return $this->fail($response, "Invalid token");
}
$password = substr($data["token"], -8);
}
$this->actualSignup( $request, $response, $session, $kernel, $data["username"], $data["email"], $data["password"]);
else {
if(!preg_match("/[0-9A-Za-z!@#$%_]{5,15}/", $data["password"])) {
$this->fail($response, "Invalid password");
return;
}
$username = $data['username'];
$password = $data['password'];
}
$this->actualSignup( $request, $response, $session, $kernel, $username, $data["email"], $password);
}

protected function actualSignup(Request $request, Response $response, Session $session, Kernel $kernel, string $username, string $email, string $password): void
Expand Down Expand Up @@ -128,80 +128,51 @@ protected function actualSignup(Request $request, Response $response, Session $s
public function login(Request $request, Response $response, Session $session, Kernel $kernel)
{
$data = $request->getQueryParams();
$v = new Validator($data);
$v->rule('required', ['username', 'password']);
//$v->rule('email', 'email');
if(!$v->validate()) {
$this->fail($response, "Username and password fields are required.");
return;
}

$result = $kernel->index()->query(
"MATCH (n:user {Username: {username}, Password: {password}}) RETURN n",
[
"username" => $data["username"],
"password" => md5($data["password"])
]
);

error_log(print_r($result, true));
$success = (count($result->results()) == 1);
if(!$success) {
$this->fail($response, "Information don't match records");
return;
if (isset($data['token'])) {
$rules = [
'token' => 'required',
];
}
$user = $result->results()[0];
$session->set($request, "id", $user["udid"]);
$this->succeed(
$response, [
"id" => $user["udid"]
]
);
}

/**
* Log In Via Token
*
* [token]
*
* @param Request $request
* @param Response $response
* @param Session $session
* @param Kernel $kernel
*
* @return void
*/
public function loginViatoken(Request $request, Response $response, Session $session, Kernel $kernel)
{
$token_key = getenv("SINGLE_SIGNON_TOKEN_KEY") ? getenv("SINGLE_SIGNON_TOKEN_KEY") : "";
if(empty($token_key)) {
return $this->fail($response, "Single sign-on not allowed");
else {
$rules = [
'username' => 'required',
'password' => 'required',
];
}
$token_key = Key::loadFromAsciiSafeString($token_key);
$data = $request->getQueryParams();
$v = new Validator($data);
$v->rule('required', ['token']);
if(!$v->validate()) {
$this->fail($response, "Token field is required.");
$validation = $this->validator->validate($data, $rules);
if($validation->fails()) {
$this->fail($response, "Either Username and password fields or Token field is required.");
return;
}
try {
$username = Crypto::decrypt($data["token"], $token_key);

if (isset($data['token'])) {
$token_key = getenv("SINGLE_SIGNON_TOKEN_KEY") ? getenv("SINGLE_SIGNON_TOKEN_KEY") : "";
if(empty($token_key)) {
return $this->fail($response, "Single sign-on not allowed");
}
$token_key = Key::loadFromAsciiSafeString($token_key);

try {
$username = Crypto::decrypt($data["token"], $token_key);
}
catch (\Exception $e) {
return $this->fail($response, "Invalid token");
}
$password = substr($data["token"], -8);
}
catch(\Exception $e) {
return $this->fail($response, "Invalid token");
else {
$username = $data["username"];
$password = $data['password'];
}
$password = substr($data["token"], -8);
error_log("username is: ".$username."\npassword is: ".$password);

$result = $kernel->index()->query(
"MATCH (n:user {Username: {username}, Password: {password}}) RETURN n",
[
"username" => $username,
"password" => md5($password)
"password" => md5($password),
]
);

error_log(print_r($result, true));
$success = (count($result->results()) == 1);
if(!$success) {
$this->fail($response, "Information don't match records");
Expand Down Expand Up @@ -248,10 +219,10 @@ public function whoami(Request $request, Response $response, Session $session)
public function reset(Request $request, Response $response)
{
$data = $request->getQueryParams();
$v = new Validator($data);
$v->rule('required', ['email']);
$v->rule('email', 'email');
if(!$v->validate()) {
$validation = $this->validator->validate($data, [
'email' => 'required|email',
]);
if($validation->fails()) {
$this->fail($response, "Valid email required.");
return;
}
Expand All @@ -271,10 +242,11 @@ public function reset(Request $request, Response $response)
public function verify(Request $request, Response $response, Session $session, Kernel $kernel)
{
$data = $request->getQueryParams();
$v = new Validator($data);
$v->rule('required', ['email', 'code']);
$v->rule('email', 'email');
if(!$v->validate()||!preg_match("/^[0-9]{6}$/", $data["code"])) {
$validation = $this->validator->validate($data, [
'email' => 'required|email',
'code' => 'required',
]);
if($validation->fails()||!preg_match("/^[0-9]{6}$/", $data["code"])) {
$this->fail($response, "Valid email and code required.");
return;
}
Expand Down
Loading