-
Notifications
You must be signed in to change notification settings - Fork 3
/
verifyemail.php
133 lines (121 loc) · 4.43 KB
/
verifyemail.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Script for verifying user's backpack email.
*
* @package local_obf
* @copyright 2013-2020, Open Badge Factory Oy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use classes\obf_backpack;
use classes\obf_user_email;
define('AJAX_SCRIPT', true);
require_once(__DIR__ . '/../../config.php');
require_once(__DIR__ . '/classes/backpack.php');
require_once($CFG->libdir . '/filelib.php');
require_once(__DIR__ . '/classes/user_email.php');
require_login();
$usercontext = context_user::instance($USER->id);
$PAGE->set_context($usercontext);
$assertion = required_param('assertion', PARAM_TEXT);
$action = optional_param('action', 'persona', PARAM_TEXT);
$provider = optional_param('provider', obf_backpack::get_default_provider(), PARAM_INT);
$backpack = obf_backpack::get_instance_by_userid($USER->id, $DB, $provider);
if ($backpack === false) {
$backpack = new obf_backpack();
$backpack->set_user_id($USER->id);
$backpack->set_provider($provider);
}
$return = array('error' => '');
function parse_json_assertion($assertion) {
global $USER;
try {
$object = json_decode($assertion);
$email = property_exists($object, 'email') ? $object->email : '';
$userid = $USER->id;
// TODO: OBF defined capabilities ?
if (!empty($object->userid)) {
$context = context_user::instance($object->userid);
if (has_capability('moodle/user:editprofile', $context)) {
$userid = $object->userid;
}
}
$token = property_exists($object, 'token') ? $object->token : null;
return array($userid, $email, $token);
} catch (Exception $e) {
throw $e;
}
}
switch ($action) {
case 'persona':
try {
$email = $backpack->verify($assertion);
$backpack->connect($email);
} catch (Exception $e) {
$return['error'] = $e->getMessage();
}
break;
case 'check_status':
try {
list($userid, $email) = parse_json_assertion($assertion);
$status = obf_user_email::is_user_email_verified($userid, $email);
$return['status'] = $status;
} catch (Exception $e) {
$return['error'] = $e->getMessage();
}
break;
case 'verify_token':
// TODO: Capability check, is user allowed to verify emails.
try {
list($userid, $email, $token) = parse_json_assertion($assertion);
$status = obf_user_email::is_user_email_verified($userid, $email, $token);
$return['status'] = $status;
} catch (Exception $e) {
$return['error'] = $e->getMessage();
}
break;
case 'create_token':
try {
list($userid, $email) = parse_json_assertion($assertion);
if (!obf_user_email::is_user_email_verified($userid, $email)) {
$status = obf_user_email::create_user_email_token($userid, $email, true);
$return['verified'] = false;
} else {
$status = true;
$return['verified'] = true;
}
$return['status'] = (bool) $status;
} catch (Exception $e) {
$return['error'] = $e->getMessage();
}
break;
case 'connect_email':
try {
list($userid, $email) = parse_json_assertion($assertion);
if (obf_user_email::is_user_email_verified($userid, $email)) {
$backpack->connect($email);
$status = true;
} else {
$status = false;
}
$return['status'] = (bool) $status;
} catch (Exception $e) {
$return['message'] = $e->getMessage();
}
break;
default:
}
echo json_encode($return);