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