-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathaction.php
More file actions
123 lines (102 loc) · 3.56 KB
/
Copy pathaction.php
File metadata and controls
123 lines (102 loc) · 3.56 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
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
<?php
use dokuwiki\plugin\oauth\Adapter;
use dokuwiki\plugin\oauthgeneric\DotAccess;
use dokuwiki\plugin\oauthgeneric\Generic;
/**
* Service Implementation for oAuth Doorkeeper authentication
*/
class action_plugin_oauthgeneric extends Adapter
{
/** @inheritdoc */
public function registerServiceClass()
{
return Generic::class;
}
/** * @inheritDoc */
public function getUser()
{
$oauth = $this->getOAuthService();
$data = array();
$url = $this->getConf('userurl');
$raw = $oauth->request($url);
if (!$raw) throw new OAuthException('Failed to fetch data from userurl');
$result = json_decode($raw, true);
if (!$result) throw new OAuthException('Failed to parse data from userurl');
$grpdots = sexplode('[]', $this->getConf('json-grps'), 2);
$user = DotAccess::get($result, $this->getConf('json-user'), '');
$name = DotAccess::get($result, $this->getConf('json-name'), '');
$mail = DotAccess::get($result, $this->getConf('json-mail'), '');
$grps = DotAccess::get($result, $grpdots[0], []);
// use dot notation on each group
if (is_array($grps) && $grpdots[1]) {
$grps = array_map(function ($grp) use ($grpdots) {
return DotAccess::get($grp, $grpdots[1], '');
}, $grps);
}
// type fixes
if (is_array($user)) $user = array_shift($user);
if (is_array($name)) $name = array_shift($name);
if (is_array($mail)) $mail = array_shift($mail);
if (!is_array($grps)) {
$grps = explode(',', $grps);
$grps = array_map('trim', $grps);
}
// fallbacks for user name
if (empty($user)) {
if (!empty($name)) {
$user = $name;
} elseif (!empty($mail)) {
list($user) = explode('@', $mail);
}
}
// fallback for full name
if (empty($name)) {
$name = $user;
}
return compact('user', 'name', 'mail', 'grps');
}
/** @inheritdoc */
public function logout()
{
$url = $this->getConf('logouturl');
if (!$url) {
parent::logout();
return;
}
// add ID token if available
$oauth = $this->getOAuthService();
$token = $oauth->getStorage()->retrieveAccessToken($oauth->service());
$params = $token->getExtraParams();
if (isset($params['id_token'])) {
$url .= (strpos($url, '?') === false ? '?' : '&') . 'id_token_hint=' . urlencode($params['id_token']);
}
// redirect back to dokuwiki after logout
/** @var helper_plugin_oauth $helper */
$helper = plugin_load('helper', 'oauth');
$redir = $helper->redirectURI();
$url .= (strpos($url, '?') === false ? '?' : '&') . 'post_logout_redirect_uri=' . urlencode($redir);
// add state if needed (we don't check it, but some providers require it)
if ($this->getConf('needs-state')) {
$state = bin2hex(random_bytes(16));
$url .= (strpos($url, '?') === false ? '?' : '&') . 'state=' . urlencode($state);
}
parent::logout();
send_redirect($url);
exit;
}
/** @inheritdoc */
public function getScopes()
{
return $this->getConf('scopes');
}
/** @inheritDoc */
public function getLabel()
{
return $this->getConf('label');
}
/** @inheritDoc */
public function getColor()
{
return $this->getConf('color');
}
}