Make info.xml valid
[grauphel.git] / lib / tokenstorage.php
index 4b5f42094978a139c156a41aff6b8f3e1c47d5ac..8b266f1d5352df9cc6b495deeb6934512eb2c864 100644 (file)
@@ -14,7 +14,7 @@
 namespace OCA\Grauphel\Lib;
 
 /**
 namespace OCA\Grauphel\Lib;
 
 /**
- * Token store
+ * OAuth token store
  *
  * @category  Tools
  * @package   Grauphel
  *
  * @category  Tools
  * @package   Grauphel
@@ -26,26 +26,57 @@ namespace OCA\Grauphel\Lib;
  */
 class TokenStorage
 {
  */
 class TokenStorage
 {
+    /**
+     * @var \OCP\IDBConnection
+     */
+    protected $db;
+
+    public function __construct()
+    {
+        $this->db = \OC::$server->getDatabaseConnection();
+    }
+
+    /**
+     * Delete token
+     *
+     * @param string $type     Token type: temp, access, verify
+     * @param string $tokenKey Random token string to load
+     *
+     * @return void
+     *
+     * @throws OAuthException When token does not exist
+     */
+    public function delete($type, $tokenKey)
+    {
+        $this->db->executeQuery(
+            'DELETE FROM `*PREFIX*grauphel_oauth_tokens`'
+            . ' WHERE `token_key` = ? AND `token_type` = ?',
+            array($tokenKey, $type)
+        );
+    }
+
     /**
      * Store the given token
      *
     /**
      * Store the given token
      *
-     * @param OAuth_Token $token Token object to store
+     * @param Token $token Token object to store
      *
      * @return void
      */
      *
      * @return void
      */
-    public function store(OAuth_Token $token)
+    public function store(Token $token)
     {
     {
-        \OC_DB::executeAudited(
+        $this->db->executeQuery(
             'INSERT INTO `*PREFIX*grauphel_oauth_tokens`'
             '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,
             array(
                 $token->user,
                 $token->type,
                 $token->tokenKey,
                 (string) $token->secret,
                 (string) $token->verifier,
-                (string) $token->callback
+                (string) $token->callback,
+                (string) $token->client,
+                date('Y-m-d H:i:s'),
             )
         );
     }
             )
         );
     }
@@ -64,11 +95,7 @@ class TokenStorage
     {
         try {
             $token = $this->load($type, $tokenKey);
     {
         try {
             $token = $this->load($type, $tokenKey);
-            \OC_DB::executeAudited(
-                'DELETE FROM `*PREFIX*grauphel_oauth_tokens`'
-                . ' WHERE `token_key` = ? AND `token_type` = ?',
-                array($tokenKey, $type)
-            );
+            $this->delete($type, $tokenKey);
             return $token;
         } catch (OAuthException $e) {
             throw $e;
             return $token;
         } catch (OAuthException $e) {
             throw $e;
@@ -88,11 +115,11 @@ class TokenStorage
      */
     public function load($type, $tokenKey)
     {
      */
     public function load($type, $tokenKey)
     {
-        $tokenRow = \OC_DB::executeAudited(
+        $tokenRow = $this->db->executeQuery(
             'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`'
             . ' WHERE `token_key` = ? AND `token_type` = ?',
             array($tokenKey, $type)
             'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`'
             . ' WHERE `token_key` = ? AND `token_type` = ?',
             array($tokenKey, $type)
-        )->fetchRow();
+        )->fetch();
 
         if ($tokenRow === false) {
             throw new OAuthException(
 
         if ($tokenRow === false) {
             throw new OAuthException(
@@ -119,20 +146,39 @@ class TokenStorage
      */
     public function loadForUser($username, $type)
     {
      */
     public function loadForUser($username, $type)
     {
-        $result = \OC_DB::executeAudited(
+        $result = $this->db->executeQuery(
             'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`'
             . ' WHERE `token_user` = ? AND `token_type` = ?',
             array($username, $type)
         );
 
         $tokens = array();
             'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`'
             . ' WHERE `token_user` = ? AND `token_type` = ?',
             array($username, $type)
         );
 
         $tokens = array();
-        while ($tokenRow = $result->fetchRow()) {
+        while ($tokenRow = $result->fetch()) {
             $tokens[] = $this->fromDb($tokenRow);
         }
 
         return $tokens;
     }
 
             $tokens[] = $this->fromDb($tokenRow);
         }
 
         return $tokens;
     }
 
+    /**
+     * Update the "last use" field of a token
+     *
+     * @param string $tokenKey Random token string to load
+     *
+     * @return void
+     */
+    public function updateLastUse($tokenKey)
+    {
+        $this->db->executeQuery(
+            'UPDATE `*PREFIX*grauphel_oauth_tokens`'
+            . ' SET `token_lastuse` = ? WHERE `token_key` = ?',
+            array(
+                date('Y-m-d H:i:s'),
+                $tokenKey,
+            )
+        );
+    }
+
     protected function fromDb($tokenRow)
     {
         $token = new Token();
     protected function fromDb($tokenRow)
     {
         $token = new Token();
@@ -142,6 +188,8 @@ class TokenStorage
         $token->user     = $tokenRow['token_user'];
         $token->verifier = $tokenRow['token_verifier'];
         $token->callback = $tokenRow['token_callback'];
         $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;
     }
 }
         return $token;
     }
 }