diff options
| author | Christian Weiske <cweiske@cweiske.de> | 2014-08-18 23:54:32 +0200 |
|---|---|---|
| committer | Christian Weiske <cweiske@cweiske.de> | 2014-08-18 23:54:32 +0200 |
| commit | db2f09d46ce2f3a46be1b6f6e031492966242025 (patch) | |
| tree | 4e22eba650c022936a4071afd9b2b0ee417ad34b /lib/oauth.php | |
| parent | 3780cf15a59c48b3d71e8ec27e3bdacd8a119460 (diff) | |
| download | grauphel-db2f09d46ce2f3a46be1b6f6e031492966242025.tar.gz grauphel-db2f09d46ce2f3a46be1b6f6e031492966242025.zip | |
oauth dance works
Diffstat (limited to 'lib/oauth.php')
| -rw-r--r-- | lib/oauth.php | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/lib/oauth.php b/lib/oauth.php new file mode 100644 index 0000000..7cfd4e0 --- /dev/null +++ b/lib/oauth.php @@ -0,0 +1,135 @@ +<?php +/** + * Part of grauphel + * + * PHP version 5 + * + * @category Tools + * @package Grauphel + * @author Christian Weiske <cweiske@cweiske.de> + * @copyright 2014 Christian Weiske + * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3 + * @link http://cweiske.de/grauphel.htm + */ +namespace OCA\Grauphel\Lib; + +/** + * Storage base class that implements note updating + * + * @category Tools + * @package Grauphel + * @author Christian Weiske <cweiske@cweiske.de> + * @copyright 2014 Christian Weiske + * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3 + * @version Release: @package_version@ + * @link http://cweiske.de/grauphel.htm + */ +class OAuth +{ + /** + * Token data store + * + * @var Token_Storage + */ + protected $tokens; + + public function setDeps(Dependencies $deps) + { + $this->tokens = $deps->tokens; + } + + /** + * Register callbacks for the oauth dance. + */ + public function registerHandler(\OAuthProvider $provider) + { + $provider->consumerHandler(array($this, 'lookupConsumer')); + $provider->timestampNonceHandler(array($this, 'timestampNonceChecker')); + return $this; + } + + public function registerVerificationTokenHandler(\OAuthProvider $provider) + { + $provider->tokenHandler(array($this, 'verifyTokenHandler')); + return $this; + } + + public function registerAccessTokenHandler(\OAuthProvider $provider) + { + $provider->tokenHandler(array($this, 'accessTokenHandler')); + return $this; + } + + public function validateToken($tokenKey) + { + return (bool) preg_match('#^[a-z0-9]+$#', $tokenKey); + } + + public function lookupConsumer(\OAuthProvider $provider) + { + //tomboy assumes secret==key=="anyone" + $provider->consumer_secret = $provider->consumer_key;//'anyone'; + $provider->addRequiredParameter('oauth_callback'); + + return OAUTH_OK; + } + + public function timestampNonceChecker(\OAuthProvider $provider) + { + //var_dump($provider->nonce, $provider->timestamp); + //OAUTH_BAD_NONCE + //OAUTH_BAD_TIMESTAMP + return OAUTH_OK; + } + + public function verifyTokenHandler(\OAuthProvider $provider) + { + $token = $this->tokens->load('verify', $provider->token); + if ($provider->verifier == '') { + return OAUTH_VERIFIER_INVALID; + } + if ($provider->verifier != $token->verifier) { + return OAUTH_VERIFIER_INVALID; + } + + $provider->token_secret = $token->secret; + return OAUTH_OK; + } + + public function accessTokenHandler(\OAuthProvider $provider) + { + $token = $this->tokens->load('access', $provider->token); + $provider->token_secret = $token->secret; + return OAUTH_OK; + } + + public function verifyOAuthUser($username, $url) + { + try { + $provider = new \OAuthProvider(); + $this->registerHandler($provider); + $this->registerAccessTokenHandler($provider); + //do not use "user" in signature + $provider->setParam('user', null); + + $provider->checkOAuthRequest($url); + + $token = $this->tokens->load('access', $provider->token); + if ($token->user != $username) { + errorOut('Invalid user'); + } + } catch (\OAuthException $e) { + $this->error($e); + } + } + + public function error(\OAuthException $e) + { + header('HTTP/1.0 400 Bad Request'); + //header('Content-type: application/x-www-form-urlencoded'); + echo \OAuthProvider::reportProblem($e); + //var_dump($e); + exit(1); + } +} +?> |
