22
33namespace Simples \Core \Security ;
44
5- use Simples \Core \Helper \Text ;
65use 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}
0 commit comments