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