3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Security;
24 use Friendica\Core\Logger;
25 use Friendica\Database\Database;
26 use Friendica\Database\DBA;
27 use Friendica\Util\DateTimeFormat;
34 const SCOPE_READ = 'read';
35 const SCOPE_WRITE = 'write';
36 const SCOPE_FOLLOW = 'follow';
37 const SCOPE_PUSH = 'push';
42 protected static $current_user_id = 0;
46 protected static $current_token = [];
49 * Check if the provided scope does exist
51 * @param string $scope the requested scope (read, write, follow, push)
53 * @return bool "true" if the scope is allowed
55 public static function isAllowedScope(string $scope)
57 $token = self::getCurrentApplicationToken();
60 Logger::notice('Empty application token');
64 if (!isset($token[$scope])) {
65 Logger::warning('The requested scope does not exist', ['scope' => $scope, 'application' => $token]);
69 if (empty($token[$scope])) {
70 Logger::warning('The requested scope is not allowed', ['scope' => $scope, 'application' => $token]);
78 * Get current application token
82 public static function getCurrentApplicationToken()
84 if (empty(self::$current_token)) {
85 self::$current_token = self::getTokenByBearer();
88 return self::$current_token;
92 * Get current user id, returns 0 if not logged in
96 public static function getCurrentUserID()
98 if (empty(self::$current_user_id)) {
99 $token = self::getCurrentApplicationToken();
100 if (!empty($token['uid'])) {
101 self::$current_user_id = $token['uid'];
103 self::$current_user_id = 0;
107 return (int)self::$current_user_id;
111 * Get the user token via the Bearer token
113 * @return array User Token
115 private static function getTokenByBearer()
117 $authorization = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
119 if (substr($authorization, 0, 7) != 'Bearer ') {
123 $bearer = trim(substr($authorization, 7));
125 $condition = ['access_token' => $bearer];
127 $token = DBA::selectFirst('application-view', ['uid', 'id', 'name', 'website', 'created_at', 'read', 'write', 'follow', 'push'], $condition);
128 if (!DBA::isResult($token)) {
129 Logger::warning('Token not found', $condition);
132 Logger::debug('Token found', $token);
137 * Get the application record via the provided request header fields
139 * @param string $client_id
140 * @param string $client_secret
141 * @param string $redirect_uri
142 * @return array application record
144 public static function getApplication(string $client_id, string $client_secret, string $redirect_uri)
146 $condition = ['client_id' => $client_id];
147 if (!empty($client_secret)) {
148 $condition['client_secret'] = $client_secret;
150 if (!empty($redirect_uri)) {
151 $condition['redirect_uri'] = $redirect_uri;
154 $application = DBA::selectFirst('application', [], $condition);
155 if (!DBA::isResult($application)) {
156 Logger::warning('Application not found', $condition);
163 * Check if an token for the application and user exists
165 * @param array $application
166 * @param integer $uid
169 public static function existsTokenForUser(array $application, int $uid)
171 return DBA::exists('application-token', ['application-id' => $application['id'], 'uid' => $uid]);
175 * Fetch the token for the given application and user
177 * @param array $application
178 * @param integer $uid
179 * @return array application record
181 public static function getTokenForUser(array $application, int $uid)
183 return DBA::selectFirst('application-token', [], ['application-id' => $application['id'], 'uid' => $uid]);
187 * Create and fetch an token for the application and user
189 * @param array $application
190 * @param integer $uid
191 * @param string $scope
192 * @return array application record
194 public static function createTokenForUser(array $application, int $uid, string $scope)
196 $code = bin2hex(random_bytes(32));
197 $access_token = bin2hex(random_bytes(32));
200 'application-id' => $application['id'],
203 'access_token' => $access_token,
205 'read' => (stripos($scope, self::SCOPE_READ) !== false),
206 'write' => (stripos($scope, self::SCOPE_WRITE) !== false),
207 'follow' => (stripos($scope, self::SCOPE_FOLLOW) !== false),
208 'push' => (stripos($scope, self::SCOPE_PUSH) !== false),
209 'created_at' => DateTimeFormat::utcNow(DateTimeFormat::MYSQL)];
211 foreach ([self::SCOPE_READ, self::SCOPE_WRITE, self::SCOPE_WRITE, self::SCOPE_PUSH] as $scope) {
212 if ($fields[$scope] && !$application[$scope]) {
213 Logger::warning('Requested token scope is not allowed for the application', ['token' => $fields, 'application' => $application]);
217 if (!DBA::insert('application-token', $fields, Database::INSERT_UPDATE)) {
221 return DBA::selectFirst('application-token', [], ['application-id' => $application['id'], 'uid' => $uid]);