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