Skip to content

Commit 363607a

Browse files
author
MrAnyx
committed
[feat] Added password service and security csrf
1 parent b42345a commit 363607a

File tree

3 files changed

+94
-17
lines changed

3 files changed

+94
-17
lines changed

src/Foundation/AbstractController.php

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ public function generate(string $name, array $params = [], array $flags = []): s
6161
* @return void
6262
*/
6363
public function createSession(array $params): void {
64-
if($params === null || empty($params) || count($params) === 0){
64+
if($params === null || empty($params) || empty($params)){
6565
throw new SessionException("createSession function requires an array, null given", 1001);
66-
} else if(count($_SESSION) > 0) {
66+
} else if(!empty($_SESSION)) {
6767
throw new SessionException('A session has already been started', 1002);
6868
} else {
6969
$_SESSION["csrf_token"] = CsrfToken::generate();
@@ -79,14 +79,27 @@ public function createSession(array $params): void {
7979
* @return void
8080
*/
8181
public function closeSession(): void {
82-
if(count($_SESSION) === 0){
82+
if(empty($_SESSION) === 0){
8383
throw new SessionException('No session was created. $_SESSION is empty', 1003);
8484
} else {
8585
session_unset();
8686
session_destroy();
8787
}
8888
}
8989

90+
/**
91+
* Return the ucrrent session
92+
*
93+
* @return array|null
94+
*/
95+
public function getSession(): ?array {
96+
if(empty($_SESSION)) {
97+
return null;
98+
} else {
99+
return $_SESSION;
100+
}
101+
}
102+
90103
/**
91104
* Redirect to a url using a specific url
92105
*
@@ -113,4 +126,33 @@ public function redirectRouteName(string $routeName, array $params = [], array $
113126
}
114127
}
115128

129+
130+
/**
131+
* Return a value from $_POST for a specific index
132+
*
133+
* @param string $index
134+
* @return mixed|null
135+
*/
136+
public function post(string $index){
137+
return isset($_POST[$index]) ? $_POST[$index] : null;
138+
}
139+
140+
/**
141+
* Return a value from $_GET for a specific index
142+
*
143+
* @param string $index
144+
* @return mixed|null
145+
*/
146+
public function get(string $index) {
147+
if(isset($_GET[$index])){
148+
if(is_numeric($_GET[$index])){
149+
return (int) $_GET[$index];
150+
} else {
151+
return $_GET[$index];
152+
}
153+
} else {
154+
return null;
155+
}
156+
}
157+
116158
}

src/Foundation/Twig.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
namespace TimePHP\Foundation;
44

55
use Closure;
6+
use Twig\Markup;
67
use Twig\TwigFilter;
78
use Twig\Environment;
89
use Twig\TwigFunction;
910
use Twig\Loader\FilesystemLoader;
1011
use TimePHP\Exception\TwigException;
1112

12-
class Twig {
13+
class Twig
14+
{
1315

1416
/**
1517
* Twig variable
@@ -23,7 +25,8 @@ class Twig {
2325
*
2426
* @param array $options
2527
*/
26-
public function __construct(array $options) {
28+
public function __construct(array $options)
29+
{
2730

2831
$this->twig = new Environment(new FilesystemLoader(__DIR__ . "/../../../../../App/Bundle/Views"));
2932

@@ -36,7 +39,9 @@ public function __construct(array $options) {
3639
$this->twig->addFunction(new TwigFunction('generate', function (string $nameUrl, array $params = [], array $flags = []): string {
3740
return sprintf(Router::generate($nameUrl, $params, $flags));
3841
}));
39-
42+
$this->twig->addFunction(new TwigFunction('provideCsrf', function (string $csrfInputName = "csrf_token"): string {
43+
return !empty($_SESSION) ? new Markup("<input type=\"hidden\" name=\"$csrfInputName\" value\"".$_SESSION["csrf_token"]."\"/>", "utf-8") : "";
44+
}, ['is_safe' => ['html']]));
4045
$this->twig->addFunction(new TwigFunction('dump', function ($object): string {
4146
ob_start();
4247
dump($object);
@@ -48,40 +53,41 @@ public function __construct(array $options) {
4853
$name = $function["name"];
4954

5055
if (array_key_exists("function", $function) && is_object($function["function"])) {
51-
if($function["type"] === "function") {
56+
if ($function["type"] === "function") {
5257
$this->twig->addFunction(new TwigFunction($name, $function["function"]));
53-
} elseif($function["type"] === "filter") {
58+
} elseif ($function["type"] === "filter") {
5459
$this->twig->addFilter(new TwigFilter($name, $function["function"]));
5560
}
5661
} else if (array_key_exists("class", $function) && array_key_exists("function", $function) && is_callable([new $function["class"], $function["function"]])) {
5762
$callable = Closure::fromCallable([new $function["class"], $function["function"]]);
58-
if($function["type"] === "function") {
63+
if ($function["type"] === "function") {
5964
$this->twig->addFunction(new TwigFunction($name, $callable));
60-
} elseif($function["type"] === "filter") {
65+
} elseif ($function["type"] === "filter") {
6166
$this->twig->addFilter(new TwigFilter($name, $callable));
6267
}
6368
} else {
6469
if ($_ENV["APP_ENV"] == 0) {
6570
header('HTTP/1.1 500 Internal Server Error');
6671
} else {
67-
if ($type === "addFunction") {
72+
if ($function["type"] === "function") {
6873
throw new TwigException("Cannot add the custom twig function : $name", 4001);
69-
} elseif ($type === "addFilter") {
70-
throw new TwigException("Cannot add the custom twig filter : $name", 4001);
74+
} elseif ($function["type"] === "filter") {
75+
throw new TwigException("Cannot add the custom twig filter : $name", 4002);
76+
} else {
77+
throw new TwigException("{$function["type"]} is not a valid twig option type. Must be either function or filter.", 4003);
7178
}
7279
}
7380
}
7481
}
75-
7682
}
7783

7884
/**
7985
* Return Twig variable
8086
*
8187
* @return Environment
8288
*/
83-
public function getRenderer(): Environment {
89+
public function getRenderer(): Environment
90+
{
8491
return $this->twig;
8592
}
86-
87-
}
93+
}

src/Security/PasswordService.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace TimePHP\Security;
4+
5+
class PasswordService {
6+
7+
/**
8+
* Hash a password using the Bcrypt default algorithm
9+
*
10+
* @param string $password
11+
* @param string $algo
12+
* @return string
13+
*/
14+
public static function hash(string $password, string $algo = PASSWORD_BCRYPT): string{
15+
return password_hash($password, $algo);
16+
}
17+
18+
/**
19+
* Compare a password and a hash
20+
*
21+
* @param string $password
22+
* @param string $hash
23+
* @return boolean
24+
*/
25+
public static function compare(string $password, string $hash): bool {
26+
return password_verify($password, $hash);
27+
}
28+
29+
}

0 commit comments

Comments
 (0)