1<?php
2
3use dokuwiki\plugin\oauth\Adapter;
4use dokuwiki\plugin\oauthgeneric\DotAccess;
5use dokuwiki\plugin\oauthgeneric\Generic;
6
7/**
8 * Service Implementation for oAuth Doorkeeper authentication
9 */
10class action_plugin_oauthgeneric extends Adapter
11{
12
13    /** @inheritdoc */
14    public function registerServiceClass()
15    {
16        return Generic::class;
17    }
18
19    /** * @inheritDoc */
20    public function getUser()
21    {
22        $oauth = $this->getOAuthService();
23        $data = array();
24
25        $url = $this->getConf('userurl');
26        $raw = $oauth->request($url);
27
28        if (!$raw) throw new OAuthException('Failed to fetch data from userurl');
29        $result = json_decode($raw, true);
30        if (!$result) throw new OAuthException('Failed to parse data from userurl');
31
32        $user = DotAccess::get($result, $this->getConf('json-user'), '');
33        $name = DotAccess::get($result, $this->getConf('json-name'), '');
34        $mail = DotAccess::get($result, $this->getConf('json-mail'), '');
35        $grps = DotAccess::get($result, $this->getConf('json-grps'), []);
36
37        // type fixes
38        if (is_array($user)) $user = array_shift($user);
39        if (is_array($name)) $user = array_shift($name);
40        if (is_array($mail)) $user = array_shift($mail);
41        if (!is_array($grps)) {
42            $grps = explode(',', $grps);
43            $grps = array_map('trim', $grps);
44        }
45
46        // fallbacks for user name
47        if (empty($user)) {
48            if (!empty($name)) {
49                $user = $name;
50            } elseif (!empty($mail)) {
51                list($user) = explode('@', $mail);
52            }
53        }
54
55        // fallback for full name
56        if (empty($name)) {
57            $name = $user;
58        }
59
60        return compact('user', 'name', 'mail', 'grps');
61    }
62
63    /** @inheritdoc */
64    public function getScopes()
65    {
66        return $this->getConf('scopes');
67    }
68
69    /** @inheritDoc */
70    public function getLabel()
71    {
72        return $this->getConf('label');
73    }
74
75    /** @inheritDoc */
76    public function getColor()
77    {
78        return $this->getConf('color');
79    }
80}
81