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