From eb5c81dea5a60bc65d3ec607daf5ad81fd709928 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Tue, 30 Sep 2014 23:13:49 +0200 Subject: store client name and last use time for tokens. show them in token management --- lib/client.php | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/oauth.php | 8 +++++++- lib/token.php | 14 +++++++++++++ lib/tokenstorage.php | 29 ++++++++++++++++++++++++--- 4 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 lib/client.php (limited to 'lib') diff --git a/lib/client.php b/lib/client.php new file mode 100644 index 0000000..358e60b --- /dev/null +++ b/lib/client.php @@ -0,0 +1,55 @@ + + * @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; + +/** + * Client identification helper + * + * @category Tools + * @package Grauphel + * @author Christian Weiske + * @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 Client +{ + public function getClient() + { + if (isset($_SERVER['HTTP_X_TOMBOY_CLIENT'])) { + $client = $_SERVER['HTTP_X_TOMBOY_CLIENT']; + $doublepos = strpos($client, ', org.tomdroid'); + if ($doublepos !== false) { + //https://bugs.launchpad.net/tomdroid/+bug/1375436 + //X-Tomboy-Client header is sent twice + $client = substr($client, 0, $doublepos); + } + return $client; + } + + return false; + } + + public function getNiceName($client) + { + if (substr($client, 0, 12) == 'org.tomdroid') { + //org.tomdroid v0.7.5, build 14, Android v4.4.2, innotek GmbH/VirtualBox + return 'Tomdroid'; + } + return $client; + } + +} +?> \ No newline at end of file diff --git a/lib/oauth.php b/lib/oauth.php index 5f84e7e..231a177 100644 --- a/lib/oauth.php +++ b/lib/oauth.php @@ -111,6 +111,12 @@ class OAuth } throw $e; } + + if (time() - $token->lastuse > 60) { + //time to update lastuse after at least a minute + $this->tokens->updateLastUse($token->tokenKey); + } + $provider->token_secret = $token->secret; return OAUTH_OK; } @@ -147,7 +153,7 @@ class OAuth /** * Get a new oauth provider instance. * Used to work around the fastcgi bug in oauthprovider. - * + * * @return \OAuthProvider */ public static function getProvider() diff --git a/lib/token.php b/lib/token.php index ebb0783..2cf6580 100644 --- a/lib/token.php +++ b/lib/token.php @@ -69,6 +69,20 @@ class Token */ public $callback; + /** + * Client name/identifier (user agent) + * + * @var string + */ + public $client; + + /** + * Unix timestamp when the token was used last + * + * @var integer + */ + public $lastuse; + public function __construct($type = null) { $this->type = $type; diff --git a/lib/tokenstorage.php b/lib/tokenstorage.php index b9689ab..cdbce11 100644 --- a/lib/tokenstorage.php +++ b/lib/tokenstorage.php @@ -37,15 +37,17 @@ class TokenStorage { \OC_DB::executeAudited( 'INSERT INTO `*PREFIX*grauphel_oauth_tokens`' - . '(`token_user`, `token_type`, `token_key`, `token_secret`, `token_verifier`, `token_callback`)' - . ' VALUES(?, ?, ?, ?, ?, ?)', + . '(`token_user`, `token_type`, `token_key`, `token_secret`, `token_verifier`, `token_callback`, `token_client`, `token_lastuse`)' + . ' VALUES(?, ?, ?, ?, ?, ?, ?, ?)', array( $token->user, $token->type, $token->tokenKey, (string) $token->secret, (string) $token->verifier, - (string) $token->callback + (string) $token->callback, + (string) $token->client, + (string) date('c'), ) ); } @@ -133,6 +135,25 @@ class TokenStorage return $tokens; } + /** + * Update the "last use" field of a token + * + * @param string $tokenKey Random token string to load + * + * @return void + */ + public function updateLastUse($tokenKey) + { + \OC_DB::executeAudited( + 'UPDATE `*PREFIX*grauphel_oauth_tokens`' + . ' SET `token_lastuse` = ? WHERE `token_key` = ?', + array( + (string) date('c'), + $tokenKey, + ) + ); + } + protected function fromDb($tokenRow) { $token = new Token(); @@ -142,6 +163,8 @@ class TokenStorage $token->user = $tokenRow['token_user']; $token->verifier = $tokenRow['token_verifier']; $token->callback = $tokenRow['token_callback']; + $token->client = $tokenRow['token_client']; + $token->lastuse = \strtotime($tokenRow['token_lastuse']); return $token; } } -- cgit v1.2.3