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/. */
7 // KaiRo.at authentication utilities PHP class
8 // This class contains helper functions for the authentication system.
10 // function __construct()
14 // The cost parameter for use with PHP password_hash function.
16 // private $pwd_nonces
17 // The array of nonces to use for "peppering" passwords. For new hashes, the last one of those will be used.
19 // function checkPasswordConstraints($new_password, $user_email)
20 // Check password constraints and return an array of error messages (empty if all constraints are met).
22 // function createSessionKey()
23 // Return a random session key.
25 // function createVerificationCode()
26 // Return a random acount/email verification code.
28 // function createTimeCode($session, [$offset], [$validity_minutes])
29 // Return a time-based code based on the key and ID of the given session.
30 // An offset can be given to create a specific code for verification, otherwise and offset will be generated.
31 // Also, an amount of minutes for the code to stay valid can be handed over, by default 10 minutes will be used.
33 // function verifyTimeCode($timecode_to_verify, $session, [$validity_minutes])
34 // Verify a given time-based code and return true if it's valid or false if it's not.
35 // See createTimeCode() documentation for the session and validity paramerters.
37 // function pwdHash($new_password)
38 // Return a hash for the given password.
40 // function pwdVerify($password_to_verify, $user)
41 // Return true if the password verifies against the pwdhash field of the user, false if not.
43 // function pwdNeedsRehash($user)
44 // Return true if the pwdhash field of the user uses an outdated standard and needs to be rehashed.
46 function __construct($settings) {
47 // *** constructor ***
48 if (array_key_exists('pwd_nonces', $settings)) {
49 $this->pwd_nonces = $settings['pwd_nonces'];
51 if (array_key_exists('pwd_cost', $settings)) {
52 $this->pwd_cost = $settings['pwd_cost'];
56 private $pwd_cost = 10;
57 private $pwd_nonces = array();
59 function checkPasswordConstraints($new_password, $user_email) {
61 if ($new_password != trim($new_password)) {
62 $errors[] = _('Password must not start or end with a whitespace character like a space.');
64 if (strlen($new_password) < 8) { $errors[] = sprintf(_('Password too short (min. %s characters).'), 8); }
65 if (strlen($new_password) > 70) { $errors[] = sprintf(_('Password too long (max. %s characters).'), 70); }
66 if ((strtolower($new_password) == strtolower($user_email)) ||
67 in_array(strtolower($new_password), preg_split("/[@\.]+/", strtolower($user_email)))) {
68 $errors[] = _('The passwort can not be equal to your email or any part of it.');
70 if ((strlen($new_password) < 15) && (preg_match('/^[a-zA-Z]+$/', $new_password))) {
71 $errors[] = sprintf(_('Your password must use characters other than normal letters or contain least %s characters.'), 15);
73 if (preg_match('/^\d+$/', $new_password)) {
74 $errors[] = sprintf(_('Your password cannot consist only of numbers.'), 15);
76 if (strlen(count_chars($new_password, 3)) < 5) {
77 $errors[] = sprintf(_('Password does have to contain at least %s different characters.'), 5);
82 function createSessionKey() {
83 return bin2hex(openssl_random_pseudo_bytes(512 / 8)); // Get 512 bits of randomness (128 byte hex string).
86 function createVerificationCode() {
87 return bin2hex(openssl_random_pseudo_bytes(512 / 8)); // Get 512 bits of randomness (128 byte hex string).
90 function createTimeCode($session, $offset = null, $validity_minutes = 10) {
91 // Matches TOTP algorithms, see https://en.wikipedia.org/wiki/Time-based_One-time_Password_Algorithm
92 $valid_seconds = intval($validity_minutes) * 60;
93 if ($valid_seconds < 60) { $valid_seconds = 60; }
96 $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.
97 $counter = floor(($time - $rest) / $valid_seconds);
98 $hmac = mhash(MHASH_SHA1, $counter, $session['id'].$session['sesskey']);
99 $offset = hexdec(substr(bin2hex(substr($hmac, -1)), -1)); // Get the last 4 bits as a number.
100 $totp = hexdec(bin2hex(substr($hmac, $offset, 4))) & 0x7FFFFFFF; // Take 4 bytes at the offset, discard highest bit.
101 $totp_value = sprintf('%0'.$code_digits.'d', substr($totp, -$code_digits));
102 return $rest.'.'.$totp_value;
105 function verifyTimeCode($timecode_to_verify, $session, $validity_minutes = 10) {
106 if (preg_match('/^(\d+)\.\d+$/', $timecode_to_verify, $regs)) {
107 return ($timecode_to_verify === $this->createTimeCode($session, $regs[1], $validity_minutes));
112 function pwdHash($new_password) {
114 if (count($this->pwd_nonces)) {
115 $new_password .= $this->pwd_nonces[count($this->pwd_nonces) - 1];
116 $hash_prefix = (count($this->pwd_nonces) - 1).'|';
118 return $hash_prefix.password_hash($new_password, PASSWORD_DEFAULT, array('cost' => $this->pwd_cost));
121 function pwdVerify($password_to_verify, $userdata) {
122 if (preg_match('/^(\d+)\|/', $userdata['pwdhash'], $regs)) {
123 $password_to_verify .= $this->pwd_nonces[$regs[1]];
125 return password_verify($password_to_verify, $userdata['pwdhash']);
128 function pwdNeedsRehash($userdata) {
130 $pwdhash = $userdata['pwdhash'];
131 if (preg_match('/^(\d+)\|(.+)$/', $userdata['pwdhash'], $regs)) {
135 if ($nonceid == count($this->pwd_nonces) - 1) {
136 return password_needs_rehash($pwdhash, PASSWORD_DEFAULT, array('cost' => $this->pwd_cost));