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\AbstractAdapter 19 */ 20 public function loadService(&$servicename) { 21 $id = getID(); // $ID isn't set in trustExternal, yet 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/AbstractAdapter.php'); 28 require_once(__DIR__.'/classes/oAuthHTTPClient.php'); 29 30 $file = __DIR__.'/classes/'.$servicename.'Adapter.php'; 31 if(!file_exists($file)) return null; 32 require_once($file); 33 $class = '\\OAuth\\Plugin\\'.$servicename.'Adapter'; 34 35 /** @var \OAuth\Plugin\AbstractAdapter $service */ 36 $service = new $class($this->redirectURI()); 37 if(!$service->isInitialized()) { 38 msg("Failed to initialize $service authentication service. Check credentials", -1); 39 return null; 40 } 41 42 // The generic service can be externally configured 43 if(is_a($service->oAuth, 'OAuth\\OAuth2\\Service\\Generic')) { 44 $service->oAuth->setAuthorizationEndpoint($this->getAuthEndpoint($servicename)); 45 $service->oAuth->setAccessTokenEndpoint($this->getTokenEndpoint($servicename)); 46 } 47 48 return $service; 49 } 50 51 /** 52 * The redirect URI used in all oAuth requests 53 * 54 * @return string 55 */ 56 public function redirectURI() { 57 return DOKU_URL.DOKU_SCRIPT; 58 } 59 60 /** 61 * List available Services 62 * 63 * @param bool $enabledonly list only enabled services 64 * @return array 65 */ 66 public function listServices($enabledonly = true) { 67 $services = array(); 68 $files = glob(__DIR__.'/classes/*Adapter.php'); 69 70 foreach($files as $file) { 71 $file = basename($file, 'Adapter.php'); 72 if($file == 'Abstract') continue; 73 if($enabledonly && !$this->getKey($file)) continue; 74 $services[] = $file; 75 } 76 77 return $services; 78 } 79 80 /** 81 * Return the configured key for the given service 82 * 83 * @param $service 84 * @return string 85 */ 86 public function getKey($service) { 87 $service = strtolower($service); 88 return $this->getConf($service.'-key'); 89 } 90 91 /** 92 * Return the configured secret for the given service 93 * 94 * @param $service 95 * @return string 96 */ 97 public function getSecret($service) { 98 $service = strtolower($service); 99 return $this->getConf($service.'-secret'); 100 } 101 102 /** 103 * Return the configured Authentication Endpoint URL for the given service 104 * 105 * @param $service 106 * @return string 107 */ 108 public function getAuthEndpoint($service) { 109 $service = strtolower($service); 110 return $this->getConf($service.'-authurl'); 111 } 112 113 /** 114 * Return the configured Access Token Endpoint URL for the given service 115 * 116 * @param $service 117 * @return string 118 */ 119 public function getTokenEndpoint($service) { 120 $service = strtolower($service); 121 return $this->getConf($service.'-tokenurl'); 122 } 123} 124 125// vim:ts=4:sw=4:et: 126