1<?php
2
3use dokuwiki\plugin\oauth\Adapter;
4use dokuwiki\plugin\oauthkeycloak\Keycloak;
5
6/**
7 * Service Implementation for Keycloak authentication
8 */
9class action_plugin_oauthkeycloak extends Adapter
10{
11    /** @inheritdoc */
12    public function registerServiceClass()
13    {
14        return Keycloak::class;
15    }
16
17    /**
18     * @inheritdoc
19     * @throws \OAuth\Common\Exception\Exception
20     */
21    public function logout()
22    {
23        /** @var Keycloak */
24        $oauth = $this->getOAuthService();
25        $oauth->logout();
26    }
27
28    /** * @inheritDoc */
29    public function getUser()
30    {
31        /** @var Keycloak */
32        $oauth = $this->getOAuthService();
33        $data = array();
34
35        $url = $oauth->getEndpoint(Keycloak::ENDPOINT_USERINFO);
36        $raw = $oauth->request($url);
37
38        if (!$raw) throw new OAuthException('Failed to fetch data from userinfo endpoint');
39        $result = json_decode($raw, true);
40        if (!$result) throw new OAuthException('Failed to parse data from userinfo endpoint');
41
42        $data = array();
43        $data['user'] = $result['preferred_username'];
44        $data['name'] = $result['name'];
45        $data['mail'] = $result['email'];
46        if (array_key_exists('groups', $result)) {
47            $data['grps'] = $result['groups'];
48        } else {
49            $data['grps'] = [];
50        }
51
52        return $data;
53    }
54
55    /** @inheritdoc */
56    public function getScopes()
57    {
58        return array(Keycloak::SCOPE_OPENID);
59    }
60
61    /** @inheritDoc */
62    public function getLabel()
63    {
64        return $this->getConf('label');
65    }
66
67    /** @inheritDoc */
68    public function getColor()
69    {
70        return $this->getConf('color');
71    }
72}
73