Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Helpers for authenticating mobile users through tokens
19
 *
20
 * @package    mod_hvp
21
 * @copyright  2019 Joubel AS
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace mod_hvp;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
class mobile_auth {
30
 
31
    const VALID_TIME = 60;
32
 
33
    /**
34
     * Generate embed auth token
35
     *
36
     * @param string $secret Secret phrase added to the hash
37
     * @param int $validfor Time factor that determines how long the token is valid
38
     *
39
     * @return array Login token and secret
40
     * @throws \Exception
41
     */
42
    public static function create_embed_auth_token($secret = null, $validfor = null) {
43
        if (!$validfor) {
44
            $validfor = self::get_time_factor();
45
        }
46
 
47
        if (empty($secret)) {
48
            if (function_exists('random_bytes')) {
49
                $secret = base64_encode(random_bytes(15));
50
            } else if (function_exists('openssl_random_pseudo_bytes')) {
51
                $secret = base64_encode(openssl_random_pseudo_bytes(15));
52
            } else {
53
                $secret = uniqid('', true);
54
            }
55
        }
56
 
57
        return [
58
            hash('md5', 'embed_auth' . $validfor . $secret),
59
            $secret
60
        ];
61
    }
62
 
63
    /**
64
     * Validate embed auth token
65
     *
66
     * @param string $token
67
     * @param string $secret
68
     *
69
     * @return bool True if valid token was supplied
70
     * @throws \Exception
71
     */
72
    public static function validate_embed_auth_token($token, $secret) {
73
        $timefactor = self::get_time_factor();
74
        // Splitting into two halves and allowing both allows for fractions roundup in the time factor.
75
        list($generatedtoken) = self::create_embed_auth_token($secret, $timefactor);
76
        list($generatedtoken2) = self::create_embed_auth_token($secret, $timefactor - 1);
77
        return $token === $generatedtoken || $token === $generatedtoken2;
78
    }
79
 
80
    /**
81
     * Check if provided user_id and token are valid for authenticating the user
82
     *
83
     * @param string $userid
84
     * @param string $token
85
     *
86
     * @return bool True if token and user_id is valid
87
     * @throws \dml_exception
88
     */
89
    public static function has_valid_token($userid, $secret) {
90
        global $DB;
91
 
92
        if (!$userid || !$secret) {
93
            return false;
94
        }
95
 
96
        $auth = $DB->get_record('hvp_auth', array(
97
            'user_id' => $userid,
98
        ));
99
        if (!$auth) {
100
            return false;
101
        }
102
 
103
        $isvalid = self::validate_embed_auth_token($auth->secret, $secret);
104
 
105
        // Cleanup user's token when used.
106
        if ($isvalid) {
107
            $DB->delete_records('hvp_auth', array(
108
                'user_id' => $userid
109
            ));
110
        }
111
 
112
        return $isvalid;
113
    }
114
 
115
    /**
116
     * Get time factor for how long the token is valid
117
     *
118
     * @return float
119
     */
120
    public static function get_time_factor() {
121
        return ceil(time() / self::VALID_TIME);
122
    }
123
}