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