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        $data['grps'] = $result['groups'];
47
48        return $data;
49    }
50
51    /** @inheritdoc */
52    public function getScopes()
53    {
54        return array(Keycloak::SCOPE_OPENID);
55    }
56
57    /** @inheritDoc */
58    public function getLabel()
59    {
60        return $this->getConf('label');
61    }
62
63    /** @inheritDoc */
64    public function getColor()
65    {
66        return $this->getConf('color');
67    }
68}
69