Send HTTP 401 on invalid token
[grauphel.git] / lib / tokenstorage.php
index c40ab3395f841918bf8b4518628256c0e4a4f1ea..4b5f42094978a139c156a41aff6b8f3e1c47d5ac 100644 (file)
@@ -58,7 +58,7 @@ class TokenStorage
      *
      * @return OAuth_Token Stored token
      *
-     * @throws OAuth_Exception When token does not exist
+     * @throws OAuthException When token does not exist
      */
     public function loadAndDelete($type, $tokenKey)
     {
@@ -84,7 +84,7 @@ class TokenStorage
      *
      * @return OAuth_Token Stored token
      *
-     * @throws OAuth_Exception When token does not exist or 
+     * @throws OAuthException When token does not exist or it is invalid
      */
     public function load($type, $tokenKey)
     {
@@ -95,17 +95,44 @@ class TokenStorage
         )->fetchRow();
 
         if ($tokenRow === false) {
-            throw new OAuthException('Unknown token: ' . $type . ' / ' . $tokenKey);
+            throw new OAuthException(
+                'Unknown token: ' . $type . ' / ' . $tokenKey,
+                OAUTH_TOKEN_REJECTED
+            );
         }
 
         $token = $this->fromDb($tokenRow);
         if ($token->tokenKey != $tokenKey) {
-            throw new OAuthException('Invalid token');
+            throw new OAuthException('Invalid token', OAUTH_TOKEN_REJECTED);
         }
 
         return $token;
     }
 
+    /**
+     * Load multiple tokens
+     *
+     * @param string $username User name
+     * @param string $type     Token type: temp, access, verify
+     *
+     * @return array Array of Token objects
+     */
+    public function loadForUser($username, $type)
+    {
+        $result = \OC_DB::executeAudited(
+            'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`'
+            . ' WHERE `token_user` = ? AND `token_type` = ?',
+            array($username, $type)
+        );
+
+        $tokens = array();
+        while ($tokenRow = $result->fetchRow()) {
+            $tokens[] = $this->fromDb($tokenRow);
+        }
+
+        return $tokens;
+    }
+
     protected function fromDb($tokenRow)
     {
         $token = new Token();