-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.php
More file actions
69 lines (55 loc) · 2.13 KB
/
Copy pathauth.php
File metadata and controls
69 lines (55 loc) · 2.13 KB
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
<?php
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/../../config.php';
$PAGE->set_url($page_url = new moodle_url('/local/jwt_auth/auth.php'));
$PAGE->set_context(context_system::instance());
$PAGE->set_title('Authorize oLab');
if ( ! ( $USER->id ?? null ) ) { // no user logged in, redirect to login screen
$SESSION->wantsurl = new moodle_url(strval($page_url), $_GET);
return redirect(new moodle_url('/login/index.php'));
}
if ( isset( $_POST['cancel'] ) ) // auth cancelled
return redirect(new moodle_url('/'));
// where to redirect next, with the bearer appended
$next_url = 'http://' . OLAB_AUTH_REDIRECT_ALLOW_HOSTS[0];
// validate url against whitelisted hosts
if ( $redirect_to = urldecode($_GET['callback_url'] ?? '') ) {
$url = parse_url($redirect_to);
$whitelisted = false;
foreach ( OLAB_AUTH_REDIRECT_ALLOW_HOSTS as $host ) {
if ( strtolower($url['host']) == strtolower($host) ) {
$whitelisted = true;
break;
} else if ( ($url['port'] ?? '') && strtolower(join(':', [$url['host'], $url['port']])) == strtolower($host) ) {
$whitelisted = true;
break;
}
}
if ( $whitelisted ) {
$next_url = $redirect_to;
}
}
if ( ! isset( $_POST['authorize_olab'] ) ) { // consent screen
ob_start(); ?>
<?php echo $OUTPUT->header(); ?>
<form method="post">
<h1>Authorize oLab</h1>
<p>oLab is requesting access to your user information:</p>
<ul>
<li>First and last name</li>
<li>Email address</li>
<li>Account role</li>
</ul>
<p class="mt-4">
<button type="submit" class="btn btn-primary" name="authorize_olab">Continue to <?php echo htmlentities(parse_url($next_url, PHP_URL_HOST)); ?></button>
<button type="submit" class="btn" name="cancel">Cancel</button>
</p>
</form>
<?php echo $OUTPUT->footer(); ?>
<?php echo ob_get_clean();
return;
}
return redirect(new moodle_url($next_url, [
'bearer' => jwt_auth_get_current_user_session_jwt(jwt_auth_get_current_user_jwt_payload())
]));