Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Twitter.php « Validator « lib « server - github.com/nextcloud/lookup-server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7cd3724813181dfd8d6ba1d76f8ec717d1f7fd14 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
 * @copyright Copyright (c) 2017 Bjoern Schiessle <bjoern@schiessle.org>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program 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 program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


namespace LookupServer\Validator;


use Abraham\TwitterOAuth\TwitterOAuth;
use LookupServer\SignatureHandler;

class Twitter {

	/** @var TwitterOAuth */
	private $twitterOAuth;

	/** @var SignatureHandler */
	private $signatureHandler;

	/** @var \PDO */
	private $db;

	/**
	 * Twitter constructor.
	 *
	 * @param TwitterOAuth $twitterOAuth
	 * @param SignatureHandler $signatureHandler
	 * @param \PDO $db
	 */
	public function __construct(TwitterOAuth $twitterOAuth, SignatureHandler $signatureHandler, \PDO $db) {
		$this->twitterOAuth = $twitterOAuth;
		$this->signatureHandler = $signatureHandler;
		$this->db = $db;
	}

	/**
	 * verify Twitter proof
	 *
	 * @param array $verificationData from toVerify table
	 * @param array $userData stored user data
	 * @return bool
	 */
	public function verify(array $verificationData, array $userData) {
		$twitterHandle = $verificationData['location'];
		$isValid = $this->isValidTwitterHandle($twitterHandle);
		$result = false;

		if ($isValid === false) {
			return $result;
		}

		try {
			$userName = substr($twitterHandle, 1);
			list($tweetId, $text) = $this->getTweet($userName);
			if ($text !== null) {
				$cloudId = $userData['federationId'];
				list($message, $md5signature) = $this->splitMessageSignature($text);
				$signature = $userData['twitter_signature']['value'];
				$result = $this->signatureHandler->verify($cloudId, $message, $signature);
				$result = $result && md5($signature) === $md5signature;
			}
		} catch (\Exception $e) {
			// do nothing, just return false;
		}

		if ($result === true) {
			$this->storeReference((int)$verificationData['userId'], $tweetId);
		}

		return $result;
	}

	/**
	 * get tweet text and id
	 *
	 * @param string $userName user name without the '@'
	 * @return array
	 */
	private function getTweet($userName) {

		try {
			$search = 'from:' . $userName . ' Use my Federated Cloud ID to share with me';
			$statuses = $this->twitterOAuth->get('search/tweets', ['q' => $search]);

			$id = $statuses->statuses[0]->id;
			$text = $statuses->statuses[0]->text;
		} catch (\Exception $e) {
			return [null, null];
		}

		return [$id, $text];
	}

	/**
	 * check if we have a correct twitter Handle
	 *
	 * @param $twitterHandle
	 * @return bool
	 */
	private function isValidTwitterHandle($twitterHandle) {
		$result = preg_match('/^@[A-Za-z0-9_]+$/', $twitterHandle);
		return $result === 1;
	}

	/**
	 * split message and signature
	 *
	 * @param string $proof
	 * @return array
	 */
	private function splitMessageSignature($proof) {
		$signature = substr($proof, -32);
		$message = substr($proof, 0, -32);

		return [trim($message), trim($signature)];
	}

	/**
	 * store reference to tweet
	 *
	 * @param $userId
	 * @param $tweetId
	 */
	private function storeReference($userId, $tweetId) {

		$key = 'tweet_id';

		// delete old value, if exists
		$stmt = $this->db->prepare('DELETE FROM store WHERE userId = :userId AND k = :k');
		$stmt->bindParam(':userId', $userId, \PDO::PARAM_INT);
		$stmt->bindParam(':k', $key, \PDO::PARAM_STR);
		$stmt->execute();
		$stmt->closeCursor();

		// add new value
		$stmt = $this->db->prepare('INSERT INTO store (userId, k, v) VALUES (:userId, :k, :v)');
		$stmt->bindParam(':userId', $userId, \PDO::PARAM_INT);
		$stmt->bindParam(':k', $key, \PDO::PARAM_STR);
		$stmt->bindParam(':v', $tweetId, \PDO::PARAM_STR);
		$stmt->execute();
		$stmt->closeCursor();
	}

}