xref: /plugin/oauthgeneric/action.php (revision 7c8a6248c74873ba6a3f8ca359eb06b2139ca8a0)
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
32cf22df2fSAndreas Gohr        $user = DotAccess::get($result, $this->getConf('json-user'), '');
33cf22df2fSAndreas Gohr        $name = DotAccess::get($result, $this->getConf('json-name'), '');
34cf22df2fSAndreas Gohr        $mail = DotAccess::get($result, $this->getConf('json-mail'), '');
35cf22df2fSAndreas Gohr        $grps = DotAccess::get($result, $this->getConf('json-grps'), []);
36cf22df2fSAndreas Gohr
37cf22df2fSAndreas Gohr        // type fixes
38cf22df2fSAndreas Gohr        if (is_array($user)) $user = array_shift($user);
39*7c8a6248SMoon Sungjoon        if (is_array($name)) $name = array_shift($name);
40*7c8a6248SMoon Sungjoon        if (is_array($mail)) $mail = array_shift($mail);
41cf22df2fSAndreas Gohr        if (!is_array($grps)) {
42cf22df2fSAndreas Gohr            $grps = explode(',', $grps);
43cf22df2fSAndreas Gohr            $grps = array_map('trim', $grps);
44cf22df2fSAndreas Gohr        }
45cf22df2fSAndreas Gohr
46cf22df2fSAndreas Gohr        // fallbacks for user name
47cf22df2fSAndreas Gohr        if (empty($user)) {
48cf22df2fSAndreas Gohr            if (!empty($name)) {
49cf22df2fSAndreas Gohr                $user = $name;
50cf22df2fSAndreas Gohr            } elseif (!empty($mail)) {
51cf22df2fSAndreas Gohr                list($user) = explode('@', $mail);
52cf22df2fSAndreas Gohr            }
53cf22df2fSAndreas Gohr        }
54cf22df2fSAndreas Gohr
55cf22df2fSAndreas Gohr        // fallback for full name
56cf22df2fSAndreas Gohr        if (empty($name)) {
57cf22df2fSAndreas Gohr            $name = $user;
58cf22df2fSAndreas Gohr        }
59cf22df2fSAndreas Gohr
60cf22df2fSAndreas Gohr        return compact('user', 'name', 'mail', 'grps');
61cf22df2fSAndreas Gohr    }
62cf22df2fSAndreas Gohr
63cf22df2fSAndreas Gohr    /** @inheritdoc */
64cf22df2fSAndreas Gohr    public function getScopes()
65cf22df2fSAndreas Gohr    {
66cf22df2fSAndreas Gohr        return $this->getConf('scopes');
67cf22df2fSAndreas Gohr    }
68cf22df2fSAndreas Gohr
69cf22df2fSAndreas Gohr    /** @inheritDoc */
70cf22df2fSAndreas Gohr    public function getLabel()
71cf22df2fSAndreas Gohr    {
72cf22df2fSAndreas Gohr        return $this->getConf('label');
73cf22df2fSAndreas Gohr    }
74cf22df2fSAndreas Gohr
75cf22df2fSAndreas Gohr    /** @inheritDoc */
76cf22df2fSAndreas Gohr    public function getColor()
77cf22df2fSAndreas Gohr    {
78cf22df2fSAndreas Gohr        return $this->getConf('color');
79cf22df2fSAndreas Gohr    }
80cf22df2fSAndreas Gohr}
81