1 | <?php |
||
2 | /** |
||
3 | * Created by PhpStorm. |
||
4 | * User: Alexandre |
||
5 | * Date: 10/03/2018 |
||
6 | * Time: 15:59 |
||
7 | */ |
||
8 | |||
9 | namespace OAuth2; |
||
10 | |||
11 | |||
12 | abstract class Helper |
||
13 | { |
||
14 | const CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; // [A-Z] / [a-z] / [0-9] |
||
0 ignored issues
–
show
|
|||
15 | const LENGTH = 22; |
||
16 | |||
17 | public static function containsNotAsciiChar(string $string) |
||
18 | { |
||
19 | return preg_match('/[^\x20-\x7e]/', $string); |
||
20 | } |
||
21 | |||
22 | /** |
||
23 | * @param int $length |
||
24 | * @return string |
||
25 | * @throws \Exception |
||
26 | */ |
||
27 | public static function generateToken($length = self::LENGTH) |
||
28 | { |
||
29 | $token = ''; |
||
30 | for ($i = 0; $i < $length; ++$i) { |
||
31 | $token .= self::CHARS[random_int(0, strlen(self::CHARS) - 1)]; |
||
32 | } |
||
33 | return $token; |
||
34 | } |
||
35 | |||
36 | // public static function pemToInline($pem) |
||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
52% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. ![]() |
|||
37 | // { |
||
38 | // $pem = str_replace('-----BEGIN PUBLIC KEY-----', '', $pem); |
||
39 | // $pem = str_replace('-----END PUBLIC KEY-----', '', $pem); |
||
40 | // $pem = str_replace("\n", '', $pem); |
||
41 | // return $pem; |
||
42 | // } |
||
43 | |||
44 | // public static function certToArray($cert) |
||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
53% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. ![]() |
|||
45 | // { |
||
46 | // return [ |
||
47 | // 'kty' => 'RSA', |
||
48 | // 'alg' => 'RSA256', |
||
49 | // 'use' => 'sig', |
||
50 | // 'kid' => $cert->getKid(), |
||
51 | // 'n' => $cert->getN(), |
||
52 | // 'e' => $cert->getE(), |
||
53 | // 'x5c' => self::pemToInline($cert->getPublicKey()) |
||
54 | // ]; |
||
55 | // } |
||
56 | |||
57 | // public static function generateRSAKeys() |
||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
48% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. ![]() |
|||
58 | // { |
||
59 | // $config = array( |
||
60 | // "digest_alg" => "sha512", |
||
61 | // "private_key_bits" => 4096, |
||
62 | // "private_key_type" => OPENSSL_KEYTYPE_RSA, |
||
63 | // ); |
||
64 | // // Create the private and public key |
||
65 | // $res = openssl_pkey_new($config); |
||
66 | // |
||
67 | // // Extract the private key from $res to $privKey |
||
68 | // openssl_pkey_export($res, $privKey); |
||
69 | // |
||
70 | // // Extract the public key from $res to $pubKey |
||
71 | // $details = openssl_pkey_get_details($res); |
||
72 | // |
||
73 | // $pubKey = $details["key"]; |
||
74 | // return ['privKey' => $privKey, 'pubKey' => $pubKey, 'rsa' => $details['rsa']]; |
||
75 | // } |
||
76 | |||
77 | /** |
||
78 | * @param $data |
||
79 | * @return string |
||
80 | * @src https://gist.github.com/nathggns/6652997 |
||
81 | */ |
||
82 | public static function base64url_encode($data) |
||
83 | { |
||
84 | return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param $data |
||
89 | * @param bool $pad |
||
90 | * @return bool|string |
||
91 | * @src https://gist.github.com/nathggns/6652997 |
||
92 | */ |
||
93 | public static function base64url_decode($data, $pad = false) |
||
94 | { |
||
95 | $data = strtr($data, '-_', '+/'); |
||
96 | if ($pad) { |
||
97 | $data = str_pad($data, strlen($data) + (4 - strlen($data) % 4) % 4); |
||
98 | } |
||
99 | return base64_decode($data); |
||
100 | } |
||
101 | |||
102 | public static function array_equals($a, $b) { |
||
103 | return ( |
||
104 | is_array($a) |
||
105 | && is_array($b) |
||
106 | && count($a) == count($b) |
||
107 | && array_diff($a, $b) === array_diff($b, $a) |
||
108 | ); |
||
109 | } |
||
110 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.