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 | // | |
be1082a6 RK |
10 | // private static $pwd_cost |
11 | // Store cost parameter for use with PHP password_hash function. | |
12 | // | |
d46a42f1 RK |
13 | // static function checkPasswordConstraints($new_password, $user_email) |
14 | // Check password constraints and return an array of error messages (empty if all constraints are met). | |
15 | // | |
16 | // static function createSessionKey() | |
17 | // Return a random session key. | |
18 | // | |
19 | // static function createVerificationCode() | |
20 | // Return a random acount/email verification code. | |
21 | // | |
22 | // static function createTimeCode($session, [$offset], [$validity_minutes]) | |
23 | // Return a time-based code based on the key and ID of the given session. | |
24 | // An offset can be given to create a specific code for verification, otherwise and offset will be generated. | |
25 | // Also, an amount of minutes for the code to stay valid can be handed over, by default 10 minutes will be used. | |
26 | // | |
27 | // static function verifyTimeCode($timecode_to_verify, $session, [$validity_minutes]) | |
28 | // Verify a given time-based code and return true if it's valid or false if it's not. | |
29 | // See createTimeCode() documentation for the session and validity paramerters. | |
be1082a6 RK |
30 | // |
31 | // static function pwdHash($new_password) | |
32 | // Return a hash for the given password. | |
33 | // | |
34 | // static function pwdVerify($password_to_verify, $user) | |
35 | // Return true if the password verifies against the pwdhash field of the user, false if not. | |
36 | // | |
37 | // static function pwdNeedsRehash($user) | |
38 | // Return true if the pwdhash field of the user uses an outdated standard and needs to be rehashed. | |
39 | ||
40 | private static $pwd_cost = 10; | |
d46a42f1 RK |
41 | |
42 | static function checkPasswordConstraints($new_password, $user_email) { | |
43 | $errors = array(); | |
44 | if ($new_password != trim($new_password)) { | |
45 | $errors[] = _('Password must not start or end with a whitespace character like a space.'); | |
46 | } | |
47 | if (strlen($new_password) < 8) { $errors[] = sprintf(_('Password too short (min. %s characters).'), 8); } | |
48 | if (strlen($new_password) > 70) { $errors[] = sprintf(_('Password too long (max. %s characters).'), 70); } | |
49 | if ((strtolower($new_password) == strtolower($user_email)) || | |
50 | in_array(strtolower($new_password), preg_split("/[@\.]+/", strtolower($user_email)))) { | |
51 | $errors[] = _('The passwort can not be equal to your email or any part of it.'); | |
52 | } | |
53 | if ((strlen($new_password) < 15) && (preg_match('/^[a-zA-Z]+$/', $new_password))) { | |
54 | $errors[] = sprintf(_('Your password must use characters other than normal letters or contain least %s characters.'), 15); | |
55 | } | |
56 | if (preg_match('/^\d+$/', $new_password)) { | |
57 | $errors[] = sprintf(_('Your password cannot consist only of numbers.'), 15); | |
58 | } | |
59 | if (strlen(count_chars($new_password, 3)) < 5) { | |
60 | $errors[] = sprintf(_('Password does have to contain at least %s different characters.'), 5); | |
61 | } | |
62 | return $errors; | |
63 | } | |
64 | ||
65 | static function createSessionKey() { | |
66 | return bin2hex(openssl_random_pseudo_bytes(512 / 8)); // Get 512 bits of randomness (128 byte hex string). | |
67 | } | |
68 | ||
69 | static function createVerificationCode() { | |
70 | return bin2hex(openssl_random_pseudo_bytes(512 / 8)); // Get 512 bits of randomness (128 byte hex string). | |
71 | } | |
72 | ||
73 | static function createTimeCode($session, $offset = null, $validity_minutes = 10) { | |
74 | // Matches TOTP algorithms, see https://en.wikipedia.org/wiki/Time-based_One-time_Password_Algorithm | |
75 | $valid_seconds = intval($validity_minutes) * 60; | |
76 | if ($valid_seconds < 60) { $valid_seconds = 60; } | |
77 | $code_digits = 8; | |
78 | $time = time(); | |
79 | $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. | |
80 | $counter = floor(($time - $rest) / $valid_seconds); | |
81 | $hmac = mhash(MHASH_SHA1, $counter, $session['id'].$session['sesskey']); | |
82 | $offset = hexdec(substr(bin2hex(substr($hmac, -1)), -1)); // Get the last 4 bits as a number. | |
83 | $totp = hexdec(bin2hex(substr($hmac, $offset, 4))) & 0x7FFFFFFF; // Take 4 bytes at the offset, discard highest bit. | |
84 | $totp_value = sprintf('%0'.$code_digits.'d', substr($totp, -$code_digits)); | |
85 | return $rest.'.'.$totp_value; | |
86 | } | |
87 | ||
88 | static function verifyTimeCode($timecode_to_verify, $session, $validity_minutes = 10) { | |
89 | if (preg_match('/^(\d+)\.\d+$/', $timecode_to_verify, $regs)) { | |
be1082a6 | 90 | return ($timecode_to_verify === self::createTimeCode($session, $regs[1], $validity_minutes)); |
d46a42f1 RK |
91 | } |
92 | return false; | |
93 | } | |
be1082a6 RK |
94 | |
95 | static function pwdHash($new_password) { | |
96 | return password_hash($new_password, PASSWORD_DEFAULT, array('cost' => self::$pwd_cost)); | |
97 | } | |
98 | ||
99 | static function pwdVerify($password_to_verify, $userdata) { | |
100 | return password_verify($password_to_verify, $userdata['pwdhash'])); | |
101 | } | |
102 | ||
103 | static function pwdNeedsRehash($userdata) { | |
104 | return password_needs_rehash($userdata['pwdhash'], PASSWORD_DEFAULT, array('cost' => self::$pwd_cost)); | |
105 | } | |
d46a42f1 RK |
106 | } |
107 | ?> |