Commit | Line | Data |
---|---|---|
d46a42f1 RK |
1 | <?php |
2 | /* This Source Code Form is subject to the terms of the Mozilla Public | |
3 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, | |
4 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
5 | ||
6 | class AuthUtils { | |
7 | // KaiRo.at authentication utilities PHP class | |
8 | // This class contains helper functions for the authentication system. | |
9 | // | |
558e9862 | 10 | // function __construct($settings, $db) |
ac442755 | 11 | // CONSTRUCTOR |
558e9862 RK |
12 | // Settings are an associative array with a numeric pwd_cost field and an array pwd_nonces field. |
13 | // The DB is a PDO object. | |
14 | // | |
15 | // public $db | |
16 | // A PDO database object for interaction. | |
be1082a6 | 17 | // |
ac442755 RK |
18 | // private $pwd_cost |
19 | // The cost parameter for use with PHP password_hash function. | |
20 | // | |
21 | // private $pwd_nonces | |
22 | // The array of nonces to use for "peppering" passwords. For new hashes, the last one of those will be used. | |
087085d6 | 23 | // Generate a nonce with this command: |openssl rand -base64 48| |
ac442755 | 24 | // |
558e9862 RK |
25 | // function log($code, $additional_info) |
26 | // Log an entry for admin purposes, with a code and some additional info. | |
27 | // | |
ac442755 | 28 | // function checkPasswordConstraints($new_password, $user_email) |
d46a42f1 RK |
29 | // Check password constraints and return an array of error messages (empty if all constraints are met). |
30 | // | |
ac442755 | 31 | // function createSessionKey() |
d46a42f1 RK |
32 | // Return a random session key. |
33 | // | |
ac442755 | 34 | // function createVerificationCode() |
d46a42f1 RK |
35 | // Return a random acount/email verification code. |
36 | // | |
ac442755 | 37 | // function createTimeCode($session, [$offset], [$validity_minutes]) |
d46a42f1 RK |
38 | // Return a time-based code based on the key and ID of the given session. |
39 | // An offset can be given to create a specific code for verification, otherwise and offset will be generated. | |
40 | // Also, an amount of minutes for the code to stay valid can be handed over, by default 10 minutes will be used. | |
41 | // | |
ac442755 | 42 | // function verifyTimeCode($timecode_to_verify, $session, [$validity_minutes]) |
d46a42f1 RK |
43 | // Verify a given time-based code and return true if it's valid or false if it's not. |
44 | // See createTimeCode() documentation for the session and validity paramerters. | |
be1082a6 | 45 | // |
ac442755 | 46 | // function pwdHash($new_password) |
be1082a6 RK |
47 | // Return a hash for the given password. |
48 | // | |
ac442755 | 49 | // function pwdVerify($password_to_verify, $user) |
be1082a6 RK |
50 | // Return true if the password verifies against the pwdhash field of the user, false if not. |
51 | // | |
ac442755 | 52 | // function pwdNeedsRehash($user) |
be1082a6 RK |
53 | // Return true if the pwdhash field of the user uses an outdated standard and needs to be rehashed. |
54 | ||
558e9862 | 55 | function __construct($settings, $db) { |
ac442755 | 56 | // *** constructor *** |
558e9862 | 57 | $this->db = $db; |
ac442755 RK |
58 | if (array_key_exists('pwd_cost', $settings)) { |
59 | $this->pwd_cost = $settings['pwd_cost']; | |
60 | } | |
558e9862 RK |
61 | if (array_key_exists('pwd_nonces', $settings)) { |
62 | $this->pwd_nonces = $settings['pwd_nonces']; | |
63 | } | |
ac442755 RK |
64 | } |
65 | ||
558e9862 | 66 | public $db = null; |
ac442755 RK |
67 | private $pwd_cost = 10; |
68 | private $pwd_nonces = array(); | |
d46a42f1 | 69 | |
558e9862 RK |
70 | function log($code, $info) { |
71 | $result = $this->db->prepare('INSERT INTO `auth_log` (`code`, `info`, `ip_addr`) VALUES (:code, :info, :ipaddr);'); | |
72 | if (!$result->execute(array(':code' => $code, ':info' => $info, ':ipaddr' => $_SERVER['REMOTE_ADDR']))) { | |
73 | // print($result->errorInfo()[2]); | |
74 | } | |
75 | } | |
76 | ||
ac442755 | 77 | function checkPasswordConstraints($new_password, $user_email) { |
d46a42f1 RK |
78 | $errors = array(); |
79 | if ($new_password != trim($new_password)) { | |
80 | $errors[] = _('Password must not start or end with a whitespace character like a space.'); | |
81 | } | |
82 | if (strlen($new_password) < 8) { $errors[] = sprintf(_('Password too short (min. %s characters).'), 8); } | |
83 | if (strlen($new_password) > 70) { $errors[] = sprintf(_('Password too long (max. %s characters).'), 70); } | |
84 | if ((strtolower($new_password) == strtolower($user_email)) || | |
85 | in_array(strtolower($new_password), preg_split("/[@\.]+/", strtolower($user_email)))) { | |
86 | $errors[] = _('The passwort can not be equal to your email or any part of it.'); | |
87 | } | |
88 | if ((strlen($new_password) < 15) && (preg_match('/^[a-zA-Z]+$/', $new_password))) { | |
89 | $errors[] = sprintf(_('Your password must use characters other than normal letters or contain least %s characters.'), 15); | |
90 | } | |
91 | if (preg_match('/^\d+$/', $new_password)) { | |
92 | $errors[] = sprintf(_('Your password cannot consist only of numbers.'), 15); | |
93 | } | |
94 | if (strlen(count_chars($new_password, 3)) < 5) { | |
95 | $errors[] = sprintf(_('Password does have to contain at least %s different characters.'), 5); | |
96 | } | |
97 | return $errors; | |
98 | } | |
99 | ||
ac442755 | 100 | function createSessionKey() { |
d46a42f1 RK |
101 | return bin2hex(openssl_random_pseudo_bytes(512 / 8)); // Get 512 bits of randomness (128 byte hex string). |
102 | } | |
103 | ||
ac442755 | 104 | function createVerificationCode() { |
d46a42f1 RK |
105 | return bin2hex(openssl_random_pseudo_bytes(512 / 8)); // Get 512 bits of randomness (128 byte hex string). |
106 | } | |
107 | ||
ac442755 | 108 | function createTimeCode($session, $offset = null, $validity_minutes = 10) { |
d46a42f1 RK |
109 | // Matches TOTP algorithms, see https://en.wikipedia.org/wiki/Time-based_One-time_Password_Algorithm |
110 | $valid_seconds = intval($validity_minutes) * 60; | |
111 | if ($valid_seconds < 60) { $valid_seconds = 60; } | |
112 | $code_digits = 8; | |
113 | $time = time(); | |
114 | $rest = is_null($offset)?($time % $valid_seconds):intval($offset); // T0, will be sent as part of code to make it valid for the full duration. | |
115 | $counter = floor(($time - $rest) / $valid_seconds); | |
116 | $hmac = mhash(MHASH_SHA1, $counter, $session['id'].$session['sesskey']); | |
117 | $offset = hexdec(substr(bin2hex(substr($hmac, -1)), -1)); // Get the last 4 bits as a number. | |
118 | $totp = hexdec(bin2hex(substr($hmac, $offset, 4))) & 0x7FFFFFFF; // Take 4 bytes at the offset, discard highest bit. | |
119 | $totp_value = sprintf('%0'.$code_digits.'d', substr($totp, -$code_digits)); | |
120 | return $rest.'.'.$totp_value; | |
121 | } | |
122 | ||
ac442755 | 123 | function verifyTimeCode($timecode_to_verify, $session, $validity_minutes = 10) { |
d46a42f1 | 124 | if (preg_match('/^(\d+)\.\d+$/', $timecode_to_verify, $regs)) { |
ac442755 | 125 | return ($timecode_to_verify === $this->createTimeCode($session, $regs[1], $validity_minutes)); |
d46a42f1 RK |
126 | } |
127 | return false; | |
128 | } | |
be1082a6 | 129 | |
ac442755 RK |
130 | function pwdHash($new_password) { |
131 | $hash_prefix = ''; | |
132 | if (count($this->pwd_nonces)) { | |
133 | $new_password .= $this->pwd_nonces[count($this->pwd_nonces) - 1]; | |
134 | $hash_prefix = (count($this->pwd_nonces) - 1).'|'; | |
135 | } | |
136 | return $hash_prefix.password_hash($new_password, PASSWORD_DEFAULT, array('cost' => $this->pwd_cost)); | |
be1082a6 RK |
137 | } |
138 | ||
ac442755 | 139 | function pwdVerify($password_to_verify, $userdata) { |
087085d6 RK |
140 | $pwdhash = $userdata['pwdhash']; |
141 | if (preg_match('/^(\d+)\|(.+)$/', $userdata['pwdhash'], $regs)) { | |
ac442755 | 142 | $password_to_verify .= $this->pwd_nonces[$regs[1]]; |
087085d6 | 143 | $pwdhash = $regs[2]; |
ac442755 | 144 | } |
087085d6 | 145 | return password_verify($password_to_verify, $pwdhash); |
be1082a6 RK |
146 | } |
147 | ||
ac442755 RK |
148 | function pwdNeedsRehash($userdata) { |
149 | $nonceid = -1; | |
150 | $pwdhash = $userdata['pwdhash']; | |
151 | if (preg_match('/^(\d+)\|(.+)$/', $userdata['pwdhash'], $regs)) { | |
152 | $nonceid = $regs[1]; | |
153 | $pwdhash = $regs[2]; | |
154 | } | |
155 | if ($nonceid == count($this->pwd_nonces) - 1) { | |
156 | return password_needs_rehash($pwdhash, PASSWORD_DEFAULT, array('cost' => $this->pwd_cost)); | |
157 | } | |
158 | else { | |
159 | return true; | |
160 | } | |
be1082a6 | 161 | } |
d46a42f1 RK |
162 | } |
163 | ?> |