forked from cosmocode/dokuwiki-plugin-oauthgeneric
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.php
More file actions
157 lines (134 loc) · 5.35 KB
/
Copy pathaction.php
File metadata and controls
157 lines (134 loc) · 5.35 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
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
<?php
use dokuwiki\plugin\oauth\Adapter;
use dokuwiki\plugin\oauthauthsch\DotAccess;
use dokuwiki\plugin\oauthauthsch\Generic;
/**
* Service Implementation for oAuth Doorkeeper authentication
*/
class action_plugin_oauthauthsch extends Adapter
{
/** @inheritdoc */
public function registerServiceClass()
{
return Generic::class;
}
/** * @inheritDoc */
public function getUser()
{
$oauth = $this->getOAuthService();
$data = array();
$url = 'https://auth.sch.bme.hu/api/profile/'; //$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');
$user = DotAccess::get($result, $this->getConf('authsch_username'), '');
$name = DotAccess::get($result, 'displayName', '');
$mail = DotAccess::get($result, $this->getConf('authsch_mail'), '').($this->getConf('authsch_mail')=='linkedAccounts.schacc'?'@sch.bme.hu':'');
// $grps = DotAccess::get($result, '', []);
if($this->getConf('authsch_circles')){
$circles2groups = json_decode($this->getConf('authsch_circles'), true);
$roles2groups = json_decode($this->getConf('authsch_roles'), true);
$grps = array();
$combine = $this->getConf('authsch_combine_circles_roles');
foreach($result['eduPersonEntitlement'] as $circle){
if(isset($circles2groups[$circle['id']])){
$circle_groupname = $circles2groups[$circle['id']];
if($circle['status']=='körvezető' || $circle['status']=='tag' || $circle['status']=='öregtag'){
$grps[]=$circle_groupname;
foreach($roles2groups as $rol => $role_groupname){
if(in_array($rol,$circle['title'])){
$grps[]=$role_groupname;
if($combine){
$grps[]=$circle_groupname.'-'.$role_groupname;
}
}
}
if($circle['status']=='körvezető'){
$role_x_groupname=$this->getConf('authsch_korvez_role');
}else if($circle['status']=='öregtag'){
$role_x_groupname=$this->getConf('authsch_oreg_role');
}else{
$role_x_groupname=$this->getConf('authsch_tag_role');
}
if(! in_array($role_x_groupname, $grps)){
$grps[]=$role_x_groupname;
}
if($combine){
$grps[]=$circle_groupname.'-'.$role_x_groupname;
}
}
}
}
if($this->getConf('authsch_allow_outside_circles')){
if(count($result['eduPersonEntitlement'])>0)$grps[]='user';
}
if(count($grps)==0)return null;
}else{
$grps[]='user';
}
// type fixes
if (is_array($user)) $user = array_shift($user);
if (is_array($name)) $user = array_shift($name);
if (is_array($mail)) $user = 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 checkToken()
{
global $INPUT;
$oauth = $this->getOAuthService();
/** @var Abstract2Service $oauth */
if (!$INPUT->get->has('code')) return false;
$state = $INPUT->get->str('state', null);
if(!$state)$state=null;
$accessToken = $oauth->requestAccessToken($INPUT->get->str('code'), $state);
if (
$accessToken->getEndOfLife() !== $accessToken::EOL_NEVER_EXPIRES &&
!$accessToken->getRefreshToken()) {
msg('Service did not provide a Refresh Token. You will be logged out when the session expires.');
}
return true;
}
/** @inheritdoc */
public function getScopes()
{
$scopes = array('basic', 'displayName');
if ($this->getConf('authsch_mail')=='linkedAccounts.schacc' || $this->getConf('authsch_username')=='linkedAccounts.schacc'){
$scopes[] = 'linkedAccounts';
}
if ($this->getConf('authsch_mail')=='mail'){
$scopes[] = 'mail';
}
if($this->getConf('authsch_circles')){
$scopes[] = 'eduPersonEntitlement';
}
return $scopes; // $this->getConf('scopes');
}
/** @inheritDoc */
public function getLabel()
{
return $this->getConf('label');
}
/** @inheritDoc */
public function getColor()
{
return $this->getConf('color');
}
}