1<?php
2
3use dokuwiki\plugin\oauth\Adapter;
4use dokuwiki\plugin\oauthcognito\Cognito;
5
6/**
7 * OAuth Cognito authentication
8 */
9class action_plugin_oauthcognito extends Adapter
10{
11
12    /** @inheritdoc */
13    public function registerServiceClass()
14    {
15        return Cognito::class;
16    }
17
18    /** * @inheritDoc */
19    public function getUser()
20    {
21        $oauth = $this->getOAuthService();
22        $data = array();
23
24        $tokenExtras = $oauth->getStorage()->retrieveAccessToken($oauth->service())->getExtraParams();
25        $idToken = $tokenExtras['id_token'] ?? '';
26
27        $decodedObj = json_decode(base64_decode(str_replace('_', '/', str_replace('-','+',explode('.', $idToken)[1]))));
28        $result = (array)$decodedObj;
29
30        $data['user'] = $result['preferred_username'] ?? $result['cognito:username'];
31        $data['name'] = $result['name'] ?? $data['user'];
32        $data['mail'] = $result['email'];
33
34        if (isset($result['cognito:groups'])) {
35            $data['grps'] = $result['cognito:groups'];
36        }
37
38        return $data;
39    }
40
41    /** @inheritDoc */
42    public function getLabel()
43    {
44        return 'Cognito';
45    }
46
47    /** @inheritDoc */
48    public function getColor()
49    {
50        return '#404041';
51    }
52
53}
54