forked from rabser/moodle-auth_googleoauth2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
165 lines (139 loc) · 5.79 KB
/
lib.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
// This file is part of Oauth2 authentication plugin for Moodle.
//
// Oauth2 authentication plugin for 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.
//
// Oauth2 authentication plugin for 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 Oauth2 authentication plugin for Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains lib functions for the Oauth2 authentication plugin.
*
* @package auth_googleoauth2
* @copyright 2013 Jerome Mouneyrac {@link http://jerome.mouneyrac.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/auth/googleoauth2/vendor/autoload.php');
function googleoauth2_html_button($authurl, $providerdisplaystyle, $provider) {
return '<a class="singinprovider" href="' . $authurl . '" style="' . $providerdisplaystyle .'">
<div class="social-button ' . $provider->sskstyle . '">' .
get_string('signinwithanaccount', 'auth_googleoauth2', $provider->readablename) .
'</div>
</a>';
}
/**
* Return list of supported provider.
*
* @return array
*/
function provider_list() {
return array('vp','google', 'facebook', 'battlenet', 'github', 'linkedin', 'messenger', 'microsoft', 'vk', 'dropbox');
}
/**
* oauth_add_to_log is a quick hack to avoid add_to_log debugging
*/
function oauth_add_to_log($courseid, $module, $action, $url='', $info='', $cm=0, $user=0) {
if (function_exists('get_log_manager')) {
$manager = get_log_manager();
$manager->legacy_add_to_log($courseid, $module, $action, $url, $info, $cm, $user);
} else if (function_exists('add_to_log')) {
add_to_log($courseid, $module, $action, $url, $info, $cm, $user);
}
}
function googleoauth2_provider_redirect($providername) {
global $CFG;
$code = optional_param('code', '', PARAM_TEXT);
if (empty($code)) {
throw new moodle_exception($providername . '_failure', 'auth_googleoauth2');
}
$loginurl = '/login/index.php';
if (!empty($CFG->alternateloginurl)) {
$loginurl = $CFG->alternateloginurl;
}
$url = new moodle_url($loginurl, array('code' => $code, 'authprovider' => $providername));
redirect($url);
}
/**
* Get (generate) session state token.
*
* @return string the state token.
*/
function auth_googleoauth2_get_state_token() {
// Create a state token to prevent request forgery.
// Store it in the session for later validation.
if (empty($_SESSION['STATETOKEN'])) {
$state = md5(rand());
$_SESSION['STATETOKEN'] = $state;
}
return $_SESSION['STATETOKEN'];
}
function set_state_token($providername, $providerstate) {
$_SESSION['oauth2state_' . $providername] = $providerstate;
}
/**
* For backwards compatibility only: this echoes the html created in auth_googleoauth2_render_buttons
*/
function auth_googleoauth2_display_buttons($echo = true) {
$html = auth_googleoauth2_render_buttons();
if ($echo) {
echo $html;
}
return $html;
}
/**
* The very ugly code to render the html buttons.
* TODO remove ugly html like center-tag and inline styles, implement a moodle renderer
* @return string returns the html for buttons and some JavaScript
*/
function auth_googleoauth2_render_buttons() {
global $CFG;
$html = '';
if (!is_enabled_auth('googleoauth2')) {
return $html;
}
// Get previous auth provider.
$allauthproviders = optional_param('allauthproviders', false, PARAM_BOOL);
$cookiename = 'MOODLEGOOGLEOAUTH2_'.$CFG->sessioncookie;
$authprovider = '';
if (!empty($_COOKIE[$cookiename])) {
$authprovider = $_COOKIE[$cookiename];
}
$html .= "<div>";
$providerscount = 0;
// TODO get the list from the provider folder instead to hard code it here.
$providers = provider_list();
foreach ($providers as $providername) {
require_once($CFG->dirroot . '/auth/googleoauth2/classes/provider/'.$providername.'.php');
// Load the provider plugin.
$providerclassname = 'provideroauth2' . $providername;
$provider = new $providerclassname();
$authurl = $provider->getAuthorizationUrl();
set_state_token($providername, $provider->state);
// Check if we should display the button.
$providerisenabled = $provider->isenabled();
$providerscount = $providerisenabled ? $providerscount + 1 : $providerscount;
$displayprovider = ((empty($authprovider) || $authprovider == $providername || $allauthproviders) && $providerisenabled);
$providerdisplaystyle = $displayprovider ? 'display:inline-block;padding:10px;' : 'display:none;';
// The button html code.
$html .= $provider->html_button($authurl, $providerdisplaystyle);
}
if (!$allauthproviders && !empty($authprovider) && $providerscount > 1) {
$html .= '<br /><br />
<div class="moreproviderlink">
<a href="'. $CFG->wwwroot . (!empty($CFG->alternateloginurl) ? $CFG->alternateloginurl : '/login/index.php')
. '?allauthproviders=true' .'" onclick="changecss(\\\'singinprovider\\\',\\\'display\\\',\\\'inline-block\\\');">
'. get_string('moreproviderlink', 'auth_googleoauth2').'
</a>
</div>';
}
$html .= "</div>";
return $html;
}