xref: /plugin/oauth/helper.php (revision 3c0138db6331751d1ea0948f418a981582962f18)
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    /**
15f10e09e2SAndreas Gohr     * Load the needed libraries and initialize the named oAuth service
1680852c15SAndreas Gohr     *
17f10e09e2SAndreas Gohr     * @param string $servicename
1863b91737SAndreas Gohr     * @return null|\OAuth\Plugin\AbstractAdapter
1980852c15SAndreas Gohr     */
20a7a8f46aSAndreas Gohr    public function loadService(&$servicename) {
2167e2b52dSAndreas Gohr        $id = getID(); // $ID isn't set in trustExternal, yet
22f10e09e2SAndreas Gohr
23f10e09e2SAndreas Gohr        $servicename = preg_replace('/[^a-zA-Z_]+/', '', $servicename);
24f10e09e2SAndreas Gohr        if(!$servicename) return null;
25f10e09e2SAndreas Gohr
26f10e09e2SAndreas Gohr        require_once(__DIR__.'/phpoauthlib/src/OAuth/bootstrap.php');
2763b91737SAndreas Gohr        require_once(__DIR__.'/classes/AbstractAdapter.php');
28f10e09e2SAndreas Gohr        require_once(__DIR__.'/classes/oAuthHTTPClient.php');
29f10e09e2SAndreas Gohr
3063b91737SAndreas Gohr        $file = __DIR__.'/classes/'.$servicename.'Adapter.php';
31f10e09e2SAndreas Gohr        if(!file_exists($file)) return null;
32f10e09e2SAndreas Gohr        require_once($file);
3363b91737SAndreas Gohr        $class = '\\OAuth\\Plugin\\'.$servicename.'Adapter';
34f10e09e2SAndreas Gohr
3563b91737SAndreas Gohr        /** @var \OAuth\Plugin\AbstractAdapter $service */
3667e2b52dSAndreas Gohr        $rdurl = wl($id, array('oa' => $servicename), true, '&');
3767e2b52dSAndreas Gohr        dbglog($rdurl);
3867e2b52dSAndreas Gohr        $service = new $class($rdurl);
39f10e09e2SAndreas Gohr        if(!$service->isInitialized()) {
40f10e09e2SAndreas Gohr            msg("Failed to initialize $service authentication service. Check credentials", -1);
41f10e09e2SAndreas Gohr            return null;
42f10e09e2SAndreas Gohr        }
43f10e09e2SAndreas Gohr
44f10e09e2SAndreas Gohr        return $service;
45f10e09e2SAndreas Gohr    }
46f10e09e2SAndreas Gohr
47dfbdd519SAndreas Gohr    /**
48dfbdd519SAndreas Gohr     * List available Services
49dfbdd519SAndreas Gohr     *
50*3c0138dbSAndreas Gohr     * @param bool $enabledonly list only enabled services
51dfbdd519SAndreas Gohr     * @return array
52dfbdd519SAndreas Gohr     */
53*3c0138dbSAndreas Gohr    public function listServices($enabledonly = true) {
54dfbdd519SAndreas Gohr        $services = array();
5563b91737SAndreas Gohr        $files    = glob(__DIR__.'/classes/*Adapter.php');
56dfbdd519SAndreas Gohr
57dfbdd519SAndreas Gohr        foreach($files as $file) {
5863b91737SAndreas Gohr            $file = basename($file, 'Adapter.php');
59dfbdd519SAndreas Gohr            if($file == 'Abstract') continue;
60*3c0138dbSAndreas Gohr            if($enabledonly && !$this->getKey($file)) continue;
61dfbdd519SAndreas Gohr            $services[] = $file;
62dfbdd519SAndreas Gohr        }
63dfbdd519SAndreas Gohr
64dfbdd519SAndreas Gohr        return $services;
65dfbdd519SAndreas Gohr    }
66f10e09e2SAndreas Gohr
67f10e09e2SAndreas Gohr    /**
68f10e09e2SAndreas Gohr     * Return the configured key for the given service
69f10e09e2SAndreas Gohr     *
70f10e09e2SAndreas Gohr     * @param $service
71f10e09e2SAndreas Gohr     * @return string
72f10e09e2SAndreas Gohr     */
73f10e09e2SAndreas Gohr    public function getKey($service) {
74f10e09e2SAndreas Gohr        $service = strtolower($service);
75f10e09e2SAndreas Gohr        return $this->getConf($service.'-key');
76f10e09e2SAndreas Gohr    }
77f10e09e2SAndreas Gohr
78f10e09e2SAndreas Gohr    /**
79f10e09e2SAndreas Gohr     * Return the configured secret for the given service
80f10e09e2SAndreas Gohr     *
81f10e09e2SAndreas Gohr     * @param $service
82f10e09e2SAndreas Gohr     * @return string
83f10e09e2SAndreas Gohr     */
84f10e09e2SAndreas Gohr    public function getSecret($service) {
85f10e09e2SAndreas Gohr        $service = strtolower($service);
86f10e09e2SAndreas Gohr        return $this->getConf($service.'-secret');
8780852c15SAndreas Gohr    }
8880852c15SAndreas Gohr
8980852c15SAndreas Gohr}
9080852c15SAndreas Gohr
9180852c15SAndreas Gohr// vim:ts=4:sw=4:et:
92