summaryrefslogtreecommitdiffstats
path: root/lib/oauth/store.php
blob: aa68d38957d4aadd8a5d1adb6a11b6cee8f22737 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
* ownCloud
*
* @author Michael Gapczynski
* @author Tom Needham
* @copyright 2012 Michael Gapczynski mtgap@owncloud.com
* @copyright 2012 Tom Needham tom@owncloud.com 
* 
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either 
* version 3 of the License, or any later version.
* 
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*  
* You should have received a copy of the GNU Affero General Public 
* License along with this library.  If not, see <http://www.gnu.org/licenses/>.
* 
*/

class OC_OAuth_Store extends OAuthDataStore {

	static private $MAX_TIMESTAMP_DIFFERENCE = 300;

	function lookup_consumer($consumer_key) {
		$query = OC_DB::prepare("SELECT `key`, `secret`, `callback_success` FROM `*PREFIX*oauth_consumers` WHERE `key` = ?");
		$results = $query->execute(array($consumer_key));
		if($results->numRows()==0){
			return NULL;
		} else {
			$details = $results->fetchRow();
			$callback = !empty($details['callback_success']) ? $details['callback_success'] : NULL;
			return new OAuthConsumer($details['key'], $details['secret'], $callback);
		}
	}

	function lookup_token($consumer, $token_type, $token) {
		$query = OC_DB::prepare("SELECT `key`, `secret`, `type` FROM `*PREFIX*oauth_tokens` WHERE `consumer_key` = ? AND `key` = ? AND `type` = ?");
		$results = $query->execute(array($consumer->key, $token->key, $token_type));
		if($results->numRows()==0){
			return NULL;
		} else {
			$token = $results->fetchRow();
			return new OAuthToken($token['key'], $token['secret']);
		}
	}

	function lookup_nonce($consumer, $token, $nonce, $timestamp) {
		$query = OC_DB::prepare("INSERT INTO `*PREFIX*oauth_nonce` (`consumer_key`, `token`, `timestamp`, `nonce`) VALUES (?, ?, ?, ?)");
		$affectedrows = $query->execute(array($consumer->key, $token, $timestamp, $nonce));
		// Delete all timestamps older than the one passed
		$query = OC_DB::prepare("DELETE FROM `*PREFIX*oauth_nonce` WHERE `consumer_key` = ? AND `token` = ? AND `timestamp` < ?");
		$result = $query->exec(array($consumer->key, $token, $timestamp - self::$MAX_TIMESTAMP_DIFFERENCE));
		return $result;
	}

	function new_token($consumer, $token_type) {
		$key = md5(time());
		$secret = time() + time();
		$token = new OAuthToken($key, md5(md5($secret)));
		$query = OC_DB::prepare("INSERT INTO `*PREFIX*oauth_tokens` (`consumer_key`, `key`, `secret`, `type`, `timestamp`) VALUES (?, ?, ?, ?, ?, ?)");
		$result = $query->execute(array($consumer->key, $key, $secret, $token_type, time()));
		return $token;
	}

	function new_request_token($consumer, $callback = null) {
		return $this->new_token($consumer, 'request');
	}

	function authorise_request_token($token, $consumer, $uid) {
		$query = OC_DB::prepare("UPDATE `*PREFIX*oauth_tokens` SET uid = ? WHERE `consumer_key` = ? AND `key` = ? AND `type` = ?");
		$query->execute(array($uid, $consumer->key, $token->key, 'request'));
		// TODO Return oauth_verifier
	}

	function new_access_token($token, $consumer, $verifier = null) {
		$query = OC_DB::prepare("SELECT `timestamp`, `scope` FROM `*PREFIX*oauth_tokens` WHERE `consumer_key` = ? AND `key` = ? AND `type` = ?");
		$result = $query->execute(array($consumer->key, $token->key, 'request'))->fetchRow();
		if (isset($result['timestamp'])) {
			if ($timestamp + self::MAX_REQUEST_TOKEN_TTL < time()) {
				return false;
			}
			$accessToken = $this->new_token($consumer, 'access', $result['scope']);
		}
		// Delete request token
		$query = OC_DB::prepare("DELETE FROM `*PREFIX*oauth_tokens` WHERE `key` = ? AND `type` = ?");
		$query->execute(array($token->key, 'request'));
		return $accessToken;
	}

}