Skip to content

Commit 658b56d

Browse files
committed
...
1 parent 715b725 commit 658b56d

File tree

10 files changed

+136
-65
lines changed

10 files changed

+136
-65
lines changed

bootstrap/function.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ function path($root)
3232
if (is_bool($root)) {
3333
array_shift($args);
3434
if ($root) {
35-
$peaces = [\Simples\Core\Kernel\App::$ROOT];
35+
$dir = \Simples\Core\Kernel\App::$ROOT;
36+
if (!$dir) {
37+
$dir = dirname(__DIR__, 4);
38+
}
39+
$peaces = [$dir];
3640
}
3741
}
3842
$path = array_merge($peaces, $args);

src/Helper/Text.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
abstract class Text
1010
{
1111
/**
12-
* @param $string
13-
* @param $search
14-
* @param $replace
15-
* @param null $count
16-
* @return mixed
12+
* @param string $string
13+
* @param string|array $search
14+
* @param string|array $replace
15+
* @param int $count (null)
16+
* @return string
1717
*/
18-
public static function replace($string, $search, $replace, &$count = null)
18+
public static function replace(string $string, $search, $replace, &$count = null): string
1919
{
2020
if ($count) {
2121
str_replace($search, $replace, $string, $count);
@@ -24,14 +24,14 @@ public static function replace($string, $search, $replace, &$count = null)
2424
}
2525

2626
/**
27-
* @param $input
28-
* @param $pad_length
29-
* @param null $pad_string
30-
* @param null $pad_type
27+
* @param string $input
28+
* @param string $length
29+
* @param string $char (null)
30+
* @param int $type (null)
3131
* @return string
3232
*/
33-
public static function pad($input, $pad_length, $pad_string = null, $pad_type = null)
33+
public static function pad($input, $length, $char = null, $type = null): string
3434
{
35-
return str_pad($input, $pad_length, $pad_string, $pad_type);
35+
return str_pad($input, $length, $char, $type);
3636
}
3737
}

src/Http/Controller.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Simples\Core\Http;
44

5+
use Simples\Core\Persistence\Transaction;
56
use Simples\Core\Route\Match;
67
use Simples\Core\Route\Wrapper;
78

@@ -148,6 +149,14 @@ public function input($name, $type = null)
148149
return $input->filter($type);
149150
}
150151

152+
/**
153+
* @param $logging
154+
*/
155+
public function setLog($logging)
156+
{
157+
Transaction::log($logging && env('TEST_MODE'));
158+
}
159+
151160
/**
152161
* @param $name
153162
* @param $arguments

src/Http/Specialty/ApiController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected function answer($content = null, $meta = [], $code = 200): Response
3939
*/
4040
public function post()
4141
{
42-
$this->repository->setLog($this->request()->get('log'));
42+
$this->setLog($this->request()->get('log'));
4343

4444
$fields = $this->repository->getFields(Action::CREATE);
4545

@@ -72,7 +72,7 @@ public function post()
7272
*/
7373
public function get($id = null)
7474
{
75-
$this->repository->setLog($this->request()->get('log'));
75+
$this->setLog($this->request()->get('log'));
7676

7777
$start = null;
7878
$end = null;
@@ -107,7 +107,7 @@ public function get($id = null)
107107
*/
108108
public function put($id)
109109
{
110-
$this->repository->setLog($this->request()->get('log'));
110+
$this->setLog($this->request()->get('log'));
111111

112112
$fields = $this->repository->getFields(Action::UPDATE);
113113

@@ -143,7 +143,7 @@ public function put($id)
143143
*/
144144
public function delete($id)
145145
{
146-
$this->repository->setLog($this->request()->get('log'));
146+
$this->setLog($this->request()->get('log'));
147147

148148
$data = [
149149
$this->repository->getHashKey() => $id

src/Model/DataMapper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ final public function create($record = null)
5050
$fields[] = $timestampsKey;
5151
$values[] = $this->getTimestampValue($type);
5252
}
53-
5453
$created = $this
5554
->source($this->getCollection())
5655
->fields($fields)
@@ -317,7 +316,7 @@ protected function getTimestampValue(string $type)
317316
return Date::create()->now();
318317
break;
319318
case 'by':
320-
return Auth::getEmbedValue();
319+
return Auth::getUser();
321320
break;
322321
}
323322
return null;

src/Model/Repository/ApiRepository.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
namespace Simples\Core\Model\Repository;
44

5-
use Simples\Core\Data\Record;
65
use Simples\Core\Data\Collection;
6+
use Simples\Core\Data\Record;
77
use Simples\Core\Data\Validator;
88
use Simples\Core\Model\AbstractModel;
99
use Simples\Core\Model\Action;
10-
use Simples\Core\Persistence\Transaction;
1110

1211
/**
1312
* Class ApiRepository
@@ -208,14 +207,6 @@ public function getFields($action): array
208207
return $this->model->getFields($action);
209208
}
210209

211-
/**
212-
* @param $logging
213-
*/
214-
public function setLog($logging)
215-
{
216-
Transaction::log($logging && env('TEST_MODE'));
217-
}
218-
219210
/**
220211
* @return string
221212
*/

src/Persistence/Transaction.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ public static function commit()
4949
foreach (self::$connections as $connection) {
5050
/** @var Driver $connection */
5151
if (!$connection->commit()) {
52-
self::rollback();
53-
return false;
52+
return self::rollback();
5453
}
5554
}
5655
return true;

src/Security/Auth.php

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,36 @@
22

33
namespace Simples\Core\Security;
44

5-
use Simples\Core\Helper\Text;
65
use Simples\Core\Kernel\App;
76

87
/**
98
* Class Auth
109
* @package Simples\Core\Security
1110
*/
12-
class Auth
11+
abstract class Auth
1312
{
1413
/**
15-
* @param $password
14+
* @var string
15+
*/
16+
const PAYLOAD_USER = 'user', PAYLOAD_DEVICE = 'device';
17+
18+
/**
19+
* @param string $password
1620
* @return string
1721
*/
18-
public static function crypt($password)
22+
public static function crypt(string $password): string
1923
{
20-
return password_hash($password, PASSWORD_DEFAULT, ['cost' => 12]);
24+
return password_hash($password, PASSWORD_DEFAULT);
2125
}
2226

2327
/**
24-
* @param $password
25-
* @param $hash
28+
* @param string $password
29+
* @param string $candidate
2630
* @return bool
2731
*/
28-
public static function match($password, $hash)
32+
public static function match(string $password, string $candidate): bool
2933
{
30-
return password_verify($password, $hash);
34+
return password_verify($password, $candidate);
3135
}
3236

3337
/**
@@ -39,20 +43,46 @@ public static function getToken()
3943
}
4044

4145
/**
42-
* @param $embed
46+
* @param string $user
47+
* @param string $device
48+
* @param array $options
49+
* @return string
50+
*/
51+
public static function createToken(string $user, string $device, array $options = []): string
52+
{
53+
$data = [
54+
self::PAYLOAD_USER => $user,
55+
self::PAYLOAD_DEVICE => $device
56+
];
57+
return Jwt::create(array_merge($options, $data), env('SECURITY'));
58+
}
59+
60+
/**
61+
* @param string $property
62+
* @return string
63+
*/
64+
public static function getTokenValue(string $property): string
65+
{
66+
$token = self::getToken();
67+
if (!$token) {
68+
return '';
69+
}
70+
return off(Jwt::payload($token, env('SECURITY')), $property);
71+
}
72+
73+
/**
4374
* @return string
4475
*/
45-
public static function createToken($embed)
76+
public static function getUser(): string
4677
{
47-
return guid() . '-' . Text::pad($embed, 10, 'F');
78+
return self::getTokenValue(self::PAYLOAD_USER);
4879
}
4980

5081
/**
51-
* @return array
82+
* @return string
5283
*/
53-
public static function getEmbedValue()
84+
public static function getDevice(): string
5485
{
55-
$peaces = explode('-', self::getToken());
56-
return Text::replace($peaces[count($peaces) - 1], 'F', '');
86+
return self::getTokenValue(self::PAYLOAD_DEVICE);
5787
}
5888
}

src/Security/Encryption.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,31 @@ class Encryption
1111
/**
1212
* @var string
1313
*/
14-
private static $ENCRYPT_MODE = "AES-256-CBC";
14+
const ENCRYPT_MODE = "AES-256-CBC";
1515

1616
/**
17-
* @var string
18-
*/
19-
private static $SECRET_IV = '0df1e73b-812f-9bcf-b07c-e8250e73e748';
20-
21-
/**
22-
* @param $string
23-
* @param null $secretKey
17+
* @param string $string
18+
* @param string $secretKey
2419
* @return string
2520
*/
26-
public static function encode($string, $secretKey = null)
21+
public static function encode($string, $secretKey): string
2722
{
28-
$key = hash('sha256', env('SECURITY', $secretKey));
29-
$iv = substr(hash('sha256', self::$SECRET_IV), 0, 16);
23+
$key = hash('sha256', $secretKey);
24+
$iv = substr(hash('sha256', md5($secretKey)), 0, 16);
3025

31-
return base64_encode(openssl_encrypt($string, self::$ENCRYPT_MODE, $key, 0, $iv));
26+
return base64_encode(openssl_encrypt($string, self::ENCRYPT_MODE, $key, 0, $iv));
3227
}
3328

3429
/**
35-
* @param $string
36-
* @param null $secretKey
30+
* @param string $string
31+
* @param string $secretKey
3732
* @return string
3833
*/
39-
public static function decode($string, $secretKey = null)
34+
public static function decode(string $string, string $secretKey): string
4035
{
41-
$key = hash('sha256', env('SECURITY', $secretKey));
42-
$iv = substr(hash('sha256', self::$SECRET_IV), 0, 16);
36+
$key = hash('sha256', $secretKey);
37+
$iv = substr(hash('sha256', md5($secretKey)), 0, 16);
4338

44-
return openssl_decrypt(base64_decode($string), self::$ENCRYPT_MODE, $key, 0, $iv);
39+
return openssl_decrypt(base64_decode($string), self::ENCRYPT_MODE, $key, 0, $iv);
4540
}
4641
}

src/Security/Jwt.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Simples\Core\Security;
4+
5+
use Simples\Core\Error\RunTimeError;
6+
use Simples\Core\Helper\Json;
7+
8+
/**
9+
* Class Jwt
10+
* @package Simples\Core\Security
11+
*/
12+
abstract class Jwt
13+
{
14+
/**
15+
* @param array $data
16+
* @param string $secret
17+
* @return string
18+
*/
19+
public static function create(array $data, string $secret): string
20+
{
21+
$header = base64_encode(json_encode(['type' => 'JWT', 'alg' => 'HS256']));
22+
23+
$payload = base64_encode(Encryption::encode(Json::encode($data), $secret));
24+
25+
$signature = base64_encode(hash_hmac('sha256', "{$header}.{$payload}", $secret, true));
26+
27+
return "{$header}.{$payload}.{$signature}";
28+
}
29+
30+
/**
31+
* @param string $token
32+
* @param string $secret
33+
* @return array
34+
* @throws RunTimeError
35+
*/
36+
public static function payload(string $token, string $secret): array
37+
{
38+
$peaces = explode('.', $token);
39+
if (count($peaces) !== 3) {
40+
throw new RunTimeError("The token '{$token}' is invalid");
41+
}
42+
return (array)Json::decode(Encryption::decode(base64_decode($peaces[1]), $secret));
43+
}
44+
}

0 commit comments

Comments
 (0)