xref: /plugin/oauth/helper.php (revision f10e09e2973ae496de9f18a40de02b630286307d)
180852c15SAndreas Gohr<?php
280852c15SAndreas Gohr/**
380852c15SAndreas Gohr * DokuWiki Plugin oauth (Helper Component)
480852c15SAndreas Gohr *
580852c15SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
680852c15SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
780852c15SAndreas Gohr */
880852c15SAndreas Gohr
980852c15SAndreas Gohr// must be run within Dokuwiki
1080852c15SAndreas Gohrif(!defined('DOKU_INC')) die();
1180852c15SAndreas Gohr
1280852c15SAndreas Gohrclass helper_plugin_oauth extends DokuWiki_Plugin {
1380852c15SAndreas Gohr
1480852c15SAndreas Gohr    /**
15*f10e09e2SAndreas Gohr     * Load the needed libraries and initialize the named oAuth service
1680852c15SAndreas Gohr     *
17*f10e09e2SAndreas Gohr     * @param string $servicename
18*f10e09e2SAndreas Gohr     * @return null|\OAuth\Plugin\AbstractAuthService
1980852c15SAndreas Gohr     */
20*f10e09e2SAndreas Gohr    public function loadService($servicename) {
21*f10e09e2SAndreas Gohr        global $ID;
22*f10e09e2SAndreas Gohr
23*f10e09e2SAndreas Gohr        $servicename = preg_replace('/[^a-zA-Z_]+/', '', $servicename);
24*f10e09e2SAndreas Gohr        if(!$servicename) return null;
25*f10e09e2SAndreas Gohr
26*f10e09e2SAndreas Gohr        require_once(__DIR__.'/phpoauthlib/src/OAuth/bootstrap.php');
27*f10e09e2SAndreas Gohr        require_once(__DIR__.'/classes/AbstractAuthService.php');
28*f10e09e2SAndreas Gohr        require_once(__DIR__.'/classes/oAuthHTTPClient.php');
29*f10e09e2SAndreas Gohr
30*f10e09e2SAndreas Gohr        $file = __DIR__.'/classes/'.$servicename.'AuthService.php';
31*f10e09e2SAndreas Gohr        if(!file_exists($file)) return null;
32*f10e09e2SAndreas Gohr        require_once($file);
33*f10e09e2SAndreas Gohr        $class = '\\OAuth\\Plugin\\'.$servicename.'AuthService';
34*f10e09e2SAndreas Gohr
35*f10e09e2SAndreas Gohr        /** @var \OAuth\Plugin\AbstractAuthService $service */
36*f10e09e2SAndreas Gohr        $service = new $class(wl($ID, array('oa' => $servicename), true, '&'));
37*f10e09e2SAndreas Gohr        if(!$service->isInitialized()){
38*f10e09e2SAndreas Gohr            msg("Failed to initialize $service authentication service. Check credentials", -1);
39*f10e09e2SAndreas Gohr            return null;
40*f10e09e2SAndreas Gohr        }
41*f10e09e2SAndreas Gohr
42*f10e09e2SAndreas Gohr        return $service;
43*f10e09e2SAndreas Gohr    }
44*f10e09e2SAndreas Gohr
45*f10e09e2SAndreas Gohr
46*f10e09e2SAndreas Gohr    /**
47*f10e09e2SAndreas Gohr     * Return the configured key for the given service
48*f10e09e2SAndreas Gohr     *
49*f10e09e2SAndreas Gohr     * @param $service
50*f10e09e2SAndreas Gohr     * @return string
51*f10e09e2SAndreas Gohr     */
52*f10e09e2SAndreas Gohr    public function getKey($service) {
53*f10e09e2SAndreas Gohr        $service = strtolower($service);
54*f10e09e2SAndreas Gohr        return $this->getConf($service.'-key');
55*f10e09e2SAndreas Gohr    }
56*f10e09e2SAndreas Gohr
57*f10e09e2SAndreas Gohr    /**
58*f10e09e2SAndreas Gohr     * Return the configured secret for the given service
59*f10e09e2SAndreas Gohr     *
60*f10e09e2SAndreas Gohr     * @param $service
61*f10e09e2SAndreas Gohr     * @return string
62*f10e09e2SAndreas Gohr     */
63*f10e09e2SAndreas Gohr    public function getSecret($service) {
64*f10e09e2SAndreas Gohr        $service = strtolower($service);
65*f10e09e2SAndreas Gohr        return $this->getConf($service.'-secret');
6680852c15SAndreas Gohr    }
6780852c15SAndreas Gohr
6880852c15SAndreas Gohr}
6980852c15SAndreas Gohr
7080852c15SAndreas Gohr// vim:ts=4:sw=4:et:
71