Added Yahoo! as another provider

this also adds our own storage class
master
Andreas Gohr 9 years ago
parent 278ded771c
commit 551dc73172

@ -23,7 +23,7 @@ abstract class AbstractAdapter {
public $oAuth = null;
/** @var \helper_plugin_oauth */
protected $hlp = null;
/** @var \OAuth\Common\Storage\Session */
/** @var \OAuth\Plugin\oAuthStorage */
protected $storage = null;
/**
@ -40,7 +40,7 @@ abstract class AbstractAdapter {
$url
);
$this->storage = new Session(); //FIXME build our own
$this->storage = new oAuthStorage();
$serviceFactory = new ServiceFactory();
$serviceFactory->setHttpClient(new oAuthHTTPClient());

@ -0,0 +1,36 @@
<?php
namespace OAuth\Plugin;
class YahooAdapter extends AbstractAdapter {
/**
* Retrieve the user's data
*
* The array needs to contain at least 'user', 'email', 'name' and optional 'grps'
*
* @return array
*/
public function getUser() {
$JSON = new \JSON(JSON_LOOSE_TYPE);
$data = array();
$result = $JSON->decode($this->oAuth->request('me/guid'));
$guid = $result['guid']['value'];
$result = $JSON->decode($this->oAuth->request('users.guid('.$guid.')/profile'));
foreach($result['profiles']['profile'][0]['emails'] as $email) {
if(isset($email['primary'])) {
$data['mail'] = $email['handle'];
break;
}
}
$data['name'] = trim($result['profiles']['profile'][0]['givenName'].' '.$result['profiles']['profile'][0]['familyName']);
if(!$data['name']) $data['name'] = $result['profiles']['profile'][0]['nickname'];
$data['user'] = $data['name'];
return $data;
}
}

@ -0,0 +1,176 @@
<?php
namespace OAuth\Plugin;
use OAuth\Common\Storage\Exception\TokenNotFoundException;
use OAuth\Common\Storage\TokenStorageInterface;
use OAuth\Common\Token\TokenInterface;
/**
* Class oAuthHTTPClient
*
* Implements the client interface using DokuWiki's HTTPClient
*/
class oAuthStorage implements TokenStorageInterface {
/**
* The path to the file where tokens for this service are stored
*
* @param string $service
* @return string
*/
protected function getServiceFile($service) {
return getCacheName($service, '.oauth');
}
/**
* Load the data from disk
*
* @param string $service
* @return array
*/
protected function loadServiceFile($service) {
$file = $this->getServiceFile($service);
if(file_exists($file)) {
return unserialize(io_readFile($file, false));
} else {
return array();
}
}
/**
* Store the data to disk
*
* @param string $service
* @param array $data
*/
protected function saveServiceFile($service, $data) {
$file = $this->getServiceFile($service);
io_saveFile($file, serialize($data));
}
/**
* @param string $service
*
* @return TokenInterface
*
* @throws TokenNotFoundException
*/
public function retrieveAccessToken($service) {
$data = $this->loadServiceFile($service);
if(!isset($data['token'])) {
throw new TokenNotFoundException('No token found in storage');
}
return $data['token'];
}
/**
* @param string $service
* @param TokenInterface $token
*
* @return TokenStorageInterface
*/
public function storeAccessToken($service, TokenInterface $token) {
$data = $this->loadServiceFile($service);
$data['token'] = $token;
$this->saveServiceFile($service, $data);
}
/**
* @param string $service
*
* @return bool
*/
public function hasAccessToken($service) {
$data = $this->loadServiceFile($service);
return isset($data['token']);
}
/**
* Delete the users token. Aka, log out.
*
* @param string $service
*
* @return TokenStorageInterface
*/
public function clearToken($service) {
$data = $this->loadServiceFile($service);
if(isset($data['token'])) unset($data['token']);
$this->saveServiceFile($service, $data);
}
/**
* Delete *ALL* user tokens. Use with care. Most of the time you will likely
* want to use clearToken() instead.
*
* @return TokenStorageInterface
*/
public function clearAllTokens() {
// TODO: Implement clearAllTokens() method.
}
/**
* Store the authorization state related to a given service
*
* @param string $service
* @param string $state
*
* @return TokenStorageInterface
*/
public function storeAuthorizationState($service, $state) {
$data = $this->loadServiceFile($service);
$data['state'] = $state;
$this->saveServiceFile($service, $data);
}
/**
* Check if an authorization state for a given service exists
*
* @param string $service
*
* @return bool
*/
public function hasAuthorizationState($service) {
$data = $this->loadServiceFile($service);
return isset($data['state']);
}
/**
* Retrieve the authorization state for a given service
*
* @param string $service
*
* @throws \OAuth\Common\Storage\Exception\TokenNotFoundException
* @return string
*/
public function retrieveAuthorizationState($service) {
$data = $this->loadServiceFile($service);
if(!isset($data['state'])) {
throw new TokenNotFoundException('No state found in storage');
}
return $data['state'];
}
/**
* Clear the authorization state of a given service
*
* @param string $service
*
* @return TokenStorageInterface
*/
public function clearAuthorizationState($service) {
$data = $this->loadServiceFile($service);
if(isset($data['state'])) unset($data['state']);
$this->saveServiceFile($service, $data);
}
/**
* Delete *ALL* user authorization states. Use with care. Most of the time you will likely
* want to use clearAuthorization() instead.
*
* @return TokenStorageInterface
*/
public function clearAllAuthorizationStates() {
// TODO: Implement clearAllAuthorizationStates() method.
}
}

@ -11,3 +11,7 @@ $conf['github-key'] = '';
$conf['github-secret'] = '';
$conf['google-key'] = '';
$conf['google-secret'] = '';
$conf['google-'] = '';
$conf['google-secret'] = '';
$conf['yahoo-key'] = '';
$conf['yahoo-secret'] = '';

@ -11,3 +11,5 @@ $meta['github-key'] = array('string');
$meta['github-secret'] = array('string');
$meta['google-key'] = array('string');
$meta['google-secret'] = array('string');
$meta['yahoo-key'] = array('string');
$meta['yahoo-secret'] = array('string');

@ -26,6 +26,7 @@ class helper_plugin_oauth extends DokuWiki_Plugin {
require_once(__DIR__.'/phpoauthlib/src/OAuth/bootstrap.php');
require_once(__DIR__.'/classes/AbstractAdapter.php');
require_once(__DIR__.'/classes/oAuthHTTPClient.php');
require_once(__DIR__.'/classes/oAuthStorage.php');
$file = __DIR__.'/classes/'.$servicename.'Adapter.php';
if(!file_exists($file)) return null;

Binary file not shown.

After

Width:  |  Height:  |  Size: 450 B

@ -9,5 +9,7 @@ $lang['facebook-key'] = 'The App ID of your registered <a href="https://devel
$lang['facebook-secret'] = 'The App Secret of your registered <a href="https://developers.facebook.com/apps">Facebook application</a>';
$lang['github-key'] = 'The Client ID of your registered <a href="https://github.com/settings/applications">Github application</a>';
$lang['github-secret'] = 'The Client Secret of your registered <a href="https://github.com/settings/applications">Github application</a>';
$lang['google-key'] = 'The Client ID of your registered registered <a href="https://console.developers.google.com/project">Google Project</a> (see Credentials Screen)';
$lang['google-secret'] = 'The Client Secret of your registered registered <a href="https://console.developers.google.com/project">Google Project</a> (see Credentials Screen)';
$lang['google-key'] = 'The Client ID of your registered <a href="https://console.developers.google.com/project">Google Project</a> (see Credentials Screen)';
$lang['google-secret'] = 'The Client Secret of your registered <a href="https://console.developers.google.com/project">Google Project</a> (see Credentials Screen)';
$lang['yahoo-key'] = 'The Consumer Key of your registered <a href="https://developer.apps.yahoo.com/dashboard/createKey.html">Yahoo Application</a>';
$lang['yahoo-secret'] = 'The Consumer Secret of your registered <a href="https://developer.apps.yahoo.com/dashboard/createKey.html">Yahoo Application</a>';

@ -39,5 +39,11 @@
padding-left: (20px+24px);
}
a.plugin_oauth_Yahoo {
.plugin_oauth_button(#7B0099);
background-image: url(images/yahoo.png);
padding-left: (20px+24px);
}
}
}
Loading…
Cancel
Save