xref: /plugin/oauth/helper.php (revision a9ed389088d3d78c0f9ad1f5315d8707f36b61b7)
1<?php
2/**
3 * DokuWiki Plugin oauth (Helper Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <andi@splitbrain.org>
7 */
8
9use dokuwiki\Extension\Event;
10use dokuwiki\plugin\oauth\Adapter;
11
12require_once(__DIR__ . '/vendor/autoload.php');
13
14/**
15 * Basic helper methods for the oauth flow
16 */
17class helper_plugin_oauth extends DokuWiki_Plugin
18{
19
20    /**
21     * Load the needed libraries and initialize the named oAuth service
22     *
23     * @param string $servicename
24     * @return null|Adapter
25     */
26    public function loadService($servicename)
27    {
28        $services = $this->listServices(true);
29        if (!isset($services[$servicename])) return null;
30        return $services[$servicename];
31    }
32
33    /**
34     * The redirect URI used in all oAuth requests
35     *
36     * @return string
37     */
38    public function redirectURI()
39    {
40        if ($this->getConf('custom-redirectURI') !== '') {
41            return $this->getConf('custom-redirectURI');
42        } else {
43            return DOKU_URL . DOKU_SCRIPT;
44        }
45    }
46
47    /**
48     * List available Services
49     *
50     * Services returned here, do not have initialized oAuth providers yet!
51     *
52     * @param bool $enabledonly list only services that have been configured
53     * @triggers PLUGIN_OAUTH_BACKEND_REGISTER
54     * @return Adapter[] list of service objects
55     */
56    public function listServices($enabledonly = true)
57    {
58        $services = [];
59        $event = new Event('PLUGIN_OAUTH_BACKEND_REGISTER', $services);
60        $event->advise_before(false);
61        $event->advise_after();
62
63        // filter out unconfigured services
64        if ($enabledonly) {
65            $services = array_filter($services, function ($service) {
66                /** @var Adapter $service */
67                return (bool)$service->getKey();
68            });
69        }
70
71        return $services;
72    }
73
74    /**
75     * @return array
76     */
77    public function getValidDomains()
78    {
79        if ($this->getConf('mailRestriction') === '') {
80            return array();
81        }
82        $validDomains = explode(',', trim($this->getConf('mailRestriction'), ','));
83        $validDomains = array_map('trim', $validDomains);
84        return $validDomains;
85    }
86
87    /**
88     * @param string $mail
89     *
90     * @return bool
91     */
92    public function checkMail($mail)
93    {
94        $hostedDomains = $this->getValidDomains();
95
96        foreach ($hostedDomains as $validDomain) {
97            if (substr($mail, -strlen($validDomain)) === $validDomain) {
98                return true;
99            }
100        }
101        return false;
102    }
103
104    /**
105     * Display an exception to the user
106     *
107     * @param Exception $e
108     * @param string $prefix - user friendly explanation if available
109     */
110    public function showException(\Exception $e, $prefix = '')
111    {
112        global $conf;
113        msg('OAuth: ' . $prefix . ' ' . hsc($e->getMessage()), -1);
114        if ($conf['allowdebug']) {
115            msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
116        }
117    }
118}
119