Subversion Repositories oidplus

Rev

Rev 1088 | Rev 1099 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1088 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
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
 
20
namespace ViaThinkSoft\OIDplus;
21
 
22
// phpcs:disable PSR1.Files.SideEffects
23
\defined('INSIDE_OIDPLUS') or die;
24
// phpcs:enable PSR1.Files.SideEffects
25
 
26
class OIDplusAuthPluginArgon2 extends OIDplusAuthPlugin {
27
 
28
        public function init($html=true) {
29
                // TODO: Let the admin decide about the memory, iterations, and parallelism options
30
        }
31
 
32
        private function supportedCryptAlgo($authKey) {
33
                return str_starts_with($authKey, '$argon2i$') ||
34
                       str_starts_with($authKey, '$argon2id$');
35
        }
36
 
37
        public function verify(OIDplusRAAuthInfo $authInfo, $check_password) {
38
                $authKey = $authInfo->getAuthKey();
39
 
40
                if (!$this->supportedCryptAlgo($authKey)) {
41
                        // Unsupported algorithm
42
                        return false;
43
                }
44
 
45
                // $argon2i$v=19$m=1024,t=2,p=2$MEhSZkJLQXUxRzljNE5hMw$33pvelMsxqOn/1VV2pnjmKJUECBhilzOZ2+Gq/FxCP4
46
                //  \_____/ \__/ \____________/ \____________________/ \_________________________________________/
47
                //   Algo   Vers  Cost options   Salt                   Hash
48
 
49
                return password_verify($check_password, $authKey);
50
        }
51
 
52
        private function getBestHashAlgo() {
53
                if ($this->supportsArgon2id()) {
54
                        $hashalgo = PASSWORD_ARGON2ID;
55
                } else if ($this->supportsArgon2i()) {
56
                        $hashalgo = PASSWORD_ARGON2I;
57
                } else {
58
                        $hashalgo = false;
59
                }
60
                return $hashalgo;
61
        }
62
 
63
        public function generate($password): OIDplusRAAuthInfo {
64
                $hashalgo = $this->getBestHashAlgo();
65
                assert($hashalgo !== false); // Should not happen if we called available() before!
66
                $calc_authkey = password_hash($password, $hashalgo);
67
                if (!$calc_authkey) throw new OIDplusException(_L('Error creating password hash'));
68
                assert($this->supportedCryptAlgo($calc_authkey));
1090 daniel-mar 69
                return new OIDplusRAAuthInfo($calc_authkey);
1088 daniel-mar 70
        }
71
 
72
        private function supportsArgon2i(): bool {
73
                if (version_compare(PHP_VERSION, '7.4.0') >= 0) {
74
                        return in_array('argon2i', password_algos());
75
                } else {
76
                        return defined('PASSWORD_ARGON2I');
77
                }
78
        }
79
 
80
        private function supportsArgon2id(): bool {
81
                if (version_compare(PHP_VERSION, '7.4.0') >= 0) {
82
                        return in_array('argon2id', password_algos());
83
                } else {
84
                        return defined('PASSWORD_ARGON2ID');
85
                }
86
        }
87
 
88
        public function available(&$reason): bool {
89
                if (!$this->supportsArgon2i() && !$this->supportsArgon2id()) {
90
                        $reason = _L('No fitting hash algorithm found');
91
                        return false;
92
                } else {
93
                        return true;
94
                }
95
        }
96
 
97
}