xref: /plugin/oauth/helper.php (revision f866280e4c6c2430bc9eb6ea7dffcc074e6f0bd9)
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
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class helper_plugin_oauth extends DokuWiki_Plugin {
13
14    /**
15     * Load the needed libraries and initialize the named oAuth service
16     *
17     * @param string $servicename
18     * @return null|\OAuth\Plugin\AbstractAuthService
19     */
20    public function loadService(&$servicename) {
21        global $ID;
22
23        $servicename = preg_replace('/[^a-zA-Z_]+/', '', $servicename);
24        if(!$servicename) return null;
25
26        require_once(__DIR__.'/phpoauthlib/src/OAuth/bootstrap.php');
27        require_once(__DIR__.'/classes/AbstractAuthService.php');
28        require_once(__DIR__.'/classes/oAuthHTTPClient.php');
29
30        $file = __DIR__.'/classes/'.$servicename.'AuthService.php';
31        if(!file_exists($file)) return null;
32        require_once($file);
33        $class = '\\OAuth\\Plugin\\'.$servicename.'AuthService';
34
35        /** @var \OAuth\Plugin\AbstractAuthService $service */
36        $service = new $class(wl($ID, array('oa' => $servicename), true, '&'));
37        if(!$service->isInitialized()){
38            msg("Failed to initialize $service authentication service. Check credentials", -1);
39            return null;
40        }
41
42        return $service;
43    }
44
45
46    /**
47     * Return the configured key for the given service
48     *
49     * @param $service
50     * @return string
51     */
52    public function getKey($service) {
53        $service = strtolower($service);
54        return $this->getConf($service.'-key');
55    }
56
57    /**
58     * Return the configured secret for the given service
59     *
60     * @param $service
61     * @return string
62     */
63    public function getSecret($service) {
64        $service = strtolower($service);
65        return $this->getConf($service.'-secret');
66    }
67
68}
69
70// vim:ts=4:sw=4:et:
71