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