Rev 1086 | Rev 1090 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
635 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
1086 | daniel-mar | 5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
635 | daniel-mar | 6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
8 | * you may not use this file except in compliance with the License. |
||
9 | * You may obtain a copy of the License at |
||
10 | * |
||
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
12 | * |
||
13 | * Unless required by applicable law or agreed to in writing, software |
||
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
16 | * See the License for the specific language governing permissions and |
||
17 | * limitations under the License. |
||
18 | */ |
||
19 | |||
1050 | daniel-mar | 20 | namespace ViaThinkSoft\OIDplus; |
635 | daniel-mar | 21 | |
1086 | daniel-mar | 22 | // phpcs:disable PSR1.Files.SideEffects |
23 | \defined('INSIDE_OIDPLUS') or die; |
||
24 | // phpcs:enable PSR1.Files.SideEffects |
||
25 | |||
635 | daniel-mar | 26 | class OIDplusAuthPluginBCrypt extends OIDplusAuthPlugin { |
27 | |||
28 | public function init($html=true) { |
||
1088 | daniel-mar | 29 | // Note: Example #3 here https://www.php.net/manual/en/function.password-hash.php can help you with finding a good cost value |
635 | daniel-mar | 30 | OIDplus::config()->prepareConfigKey('ra_bcrypt_cost', 'How complex should the BCrypt hash of RA passwords be? (Only for plugin A3_bcrypt; values 4-31, default 10)', 10, OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
31 | if (empty($value) || !is_int($value) || ($value<4) || ($value>31)) { |
||
32 | throw new OIDplusException(_L('Invalid value for "cost" (must be 4-31, default 10).')); |
||
33 | } |
||
34 | }); |
||
35 | } |
||
36 | |||
1088 | daniel-mar | 37 | private function supportedCryptAlgo($authKey) { |
38 | return str_starts_with($authKey, '$2$') || |
||
39 | str_starts_with($authKey, '$2a$') || |
||
40 | str_starts_with($authKey, '$2b$') || |
||
41 | str_starts_with($authKey, '$2x$') || |
||
42 | str_starts_with($authKey, '$2y$'); |
||
43 | } |
||
44 | |||
635 | daniel-mar | 45 | public function verify(OIDplusRAAuthInfo $authInfo, $check_password) { |
46 | $authKey = $authInfo->getAuthKey(); |
||
47 | $salt = $authInfo->getSalt(); |
||
48 | |||
1088 | daniel-mar | 49 | if (str_starts_with($authKey, 'A3#')) { |
50 | $authKey = substr($authKey, strlen('A3#')); // Backwards compatibility: Remove "A3#" prefix. |
||
51 | } |
||
52 | |||
53 | if (!$this->supportedCryptAlgo($authKey)) { |
||
54 | // Unsupported algorithm |
||
635 | daniel-mar | 55 | return false; |
56 | } |
||
1088 | daniel-mar | 57 | |
58 | // $2a$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy |
||
59 | // \/ \/ \____________________/\_____________________________/ |
||
60 | // Alg Cost Salt (128 bit) Hash |
||
61 | |||
62 | if ($salt != '') { |
||
63 | throw new OIDplusException(_L('This function does not accept an explicit salt')); |
||
64 | } |
||
65 | |||
66 | return password_verify($check_password, $authKey); |
||
635 | daniel-mar | 67 | } |
68 | |||
69 | public function generate($password): OIDplusRAAuthInfo { |
||
1088 | daniel-mar | 70 | if (strlen($password) > 72) throw new OIDplusException(_L('Password is too long (max %1 bytes)',72)); |
635 | daniel-mar | 71 | $s_salt = ''; // BCrypt automatically generates a salt |
72 | $cost = OIDplus::config()->getValue('ra_bcrypt_cost', 10); |
||
1088 | daniel-mar | 73 | $calc_authkey = password_hash($password, PASSWORD_BCRYPT, array("cost" => $cost)); |
74 | if (!$calc_authkey) throw new OIDplusException(_L('Error creating password hash')); |
||
75 | assert($this->supportedCryptAlgo($calc_authkey)); |
||
635 | daniel-mar | 76 | return new OIDplusRAAuthInfo($s_salt, $calc_authkey); |
77 | } |
||
78 | |||
1088 | daniel-mar | 79 | public function available(&$reason): bool { |
80 | if (version_compare(PHP_VERSION, '7.4.0') >= 0) { |
||
81 | $ok = in_array('2', password_algos()) || |
||
82 | in_array('2a', password_algos()) || |
||
83 | in_array('2b', password_algos()) || |
||
84 | in_array('2x', password_algos()) || |
||
85 | in_array('2y', password_algos()); |
||
86 | |||
87 | } else { |
||
88 | $ok = defined('PASSWORD_BCRYPT'); |
||
89 | } |
||
90 | |||
91 | if ($ok) return true; |
||
92 | |||
93 | $reason = _L('No fitting hash algorithm found'); |
||
94 | return false; |
||
95 | } |
||
96 | |||
635 | daniel-mar | 97 | } |