forked from cosmocode/dokuwiki-plugin-oauth
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.php
219 lines (192 loc) · 5.9 KB
/
helper.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
* DokuWiki Plugin oauth (Helper Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Gohr <[email protected]>
*/
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
class helper_plugin_oauth extends DokuWiki_Plugin {
/**
* Load the needed libraries and initialize the named oAuth service
*
* @param string $servicename
* @return null|\OAuth\Plugin\AbstractAdapter
*/
public function loadService(&$servicename) {
$id = getID(); // $ID isn't set in trustExternal, yet
$servicename = preg_replace('/[^a-zA-Z0-9_]+/', '', $servicename);
if(!$servicename) return null;
require_once(__DIR__.'/phpoauthlib/src/OAuth/bootstrap.php');
require_once(__DIR__.'/classes/AbstractAdapter.php');
require_once(__DIR__.'/classes/AbstractGenericAdapter.php');
require_once(__DIR__.'/classes/oAuthHTTPClient.php');
require_once(__DIR__.'/classes/oAuthStorage.php');
$file = __DIR__.'/classes/'.$servicename.'Adapter.php';
if(!file_exists($file)) return null;
require_once($file);
$class = '\\OAuth\\Plugin\\'.$servicename.'Adapter';
/** @var \OAuth\Plugin\AbstractAdapter $service */
$service = new $class($this->redirectURI());
if(!$service->isInitialized()) {
msg("Failed to initialize $service authentication service. Check credentials", -1);
return null;
}
// The generic service can be externally configured
if(is_a($service->oAuth, 'OAuth\\OAuth2\\Service\\Generic')) {
$service->oAuth->setAuthorizationEndpoint($service->getAuthEndpoint());
$service->oAuth->setAccessTokenEndpoint($service->getTokenEndpoint());
}
return $service;
}
/**
* The redirect URI used in all oAuth requests
*
* @return string
*/
public function redirectURI() {
if ($this->getConf('custom-redirectURI') !== '') {
return $this->getConf('custom-redirectURI');
} else {
return DOKU_URL . DOKU_SCRIPT;
}
}
/**
* List available Services
*
* @param bool $enabledonly list only enabled services
* @return array
*/
public function listServices($enabledonly = true) {
$services = array();
$files = glob(__DIR__.'/classes/*Adapter.php');
foreach($files as $file) {
$file = basename($file, 'Adapter.php');
if($file == 'Abstract') continue;
if($enabledonly && !$this->getKey($file)) continue;
$services[] = $file;
}
return $services;
}
/**
* Return the configured key for the given service
*
* @param $service
* @return string
*/
public function getKey($service) {
$service = strtolower($service);
return $this->getConf($service.'-key');
}
/**
* Return the configured secret for the given service
*
* @param $service
* @return string
*/
public function getSecret($service) {
$service = strtolower($service);
return $this->getConf($service.'-secret');
}
/**
* Return the configured Authentication Endpoint URL for the given service
*
* @param $service
* @return string
*/
public function getUrl($service) {
$service = strtolower($service);
return $this->getConf($service.'-url');
}
/**
* Return the configured Authentication Endpoint URL for the given service
*
* @param $service
* @return string
*/
public function getAuthEndpoint($service) {
$service = strtolower($service);
return $this->getConf($service.'-authurl');
}
/**
* Return the configured Access Token Endpoint URL for the given service
*
* @param $service
* @return string
*/
public function getTokenEndpoint($service) {
$service = strtolower($service);
return $this->getConf($service.'-tokenurl');
}
/**
* Return the configured User Info Endpoint URL for the given service
*
* @param $service
* @return string
*/
public function getUserInfoEndpoint($service) {
$service = strtolower($service);
return $this->getConf($service.'-userinfourl');
}
/**
* @return array
*/
public function getValidDomains() {
if ($this->getConf('mailRestriction') === '') {
return array();
}
$validDomains = explode(',', trim($this->getConf('mailRestriction'), ','));
$validDomains = array_map('trim', $validDomains);
return $validDomains;
}
/**
* @param string $mail
*
* @return bool
*/
public function checkMail($mail) {
$hostedDomains = $this->getValidDomains();
foreach ($hostedDomains as $validDomain) {
if(substr($mail, -strlen($validDomain)) === $validDomain) {
return true;
}
}
return false;
}
/**
* @param array $session cookie auth session
*
* @return bool
*/
public function validBrowserID ($session) {
return $session['buid'] == auth_browseruid();
}
/**
* @param array $session cookie auth session
*
* @return bool
*/
public function isSessionTimedOut ($session) {
global $conf;
return $session['time'] < time() - $conf['auth_security_timeout'];
}
/**
* @return bool
*/
public function isGETRequest () {
global $INPUT;
$result = $INPUT->server->str('REQUEST_METHOD') === 'GET';
return $result;
}
/**
* check if we are handling a request to doku.php. Only doku.php defines $updateVersion
*
* @return bool
*/
public function isDokuPHP() {
global $updateVersion;
return isset($updateVersion);
}
}
// vim:ts=4:sw=4:et: