3 * @copyright Copyright (C) 2010-2023, 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\Core\Worker;
26 use Friendica\Database\Database;
27 use Friendica\Database\DBA;
28 use Friendica\Model\Contact;
29 use Friendica\Model\User;
30 use Friendica\Module\BaseApi;
31 use Friendica\Util\DateTimeFormat;
32 use GuzzleHttp\Psr7\Uri;
42 protected static $current_user_id = 0;
46 protected static $current_token = [];
49 * Get current user id, returns 0 if not logged in
53 public static function getCurrentUserID()
55 if (empty(self::$current_user_id)) {
56 $token = self::getCurrentApplicationToken();
57 if (!empty($token['uid'])) {
58 self::$current_user_id = $token['uid'];
60 self::$current_user_id = 0;
64 return (int)self::$current_user_id;
68 * Get current application token
72 public static function getCurrentApplicationToken()
74 if (empty(self::$current_token)) {
75 self::$current_token = self::getTokenByBearer();
78 return self::$current_token;
82 * Get the user token via the Bearer token
84 * @return array User Token
86 private static function getTokenByBearer()
88 $authorization = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
90 if (empty($authorization)) {
91 // workaround for HTTP-auth in CGI mode
92 $authorization = $_SERVER['REDIRECT_REMOTE_USER'] ?? '';
95 if (substr($authorization, 0, 7) != 'Bearer ') {
99 $condition = ['access_token' => trim(substr($authorization, 7))];
101 $token = DBA::selectFirst('application-view', ['uid', 'id', 'name', 'website', 'created_at', 'read', 'write', 'follow', 'push'], $condition);
102 if (!DBA::isResult($token)) {
103 Logger::notice('Token not found', $condition);
106 Logger::debug('Token found', $token);
108 User::updateLastActivity($token['uid']);
110 // Regularly update suggestions
111 if (Contact\Relation::areSuggestionsOutdated($token['uid'])) {
112 Worker::add(Worker::PRIORITY_MEDIUM, 'UpdateSuggestions', $token['uid']);
119 * Get the application record via the provided request header fields
121 * @param string $client_id
122 * @param string $client_secret
123 * @param string $redirect_uri
124 * @return array application record
126 public static function getApplication(string $client_id, string $client_secret, string $redirect_uri)
128 $condition = ['client_id' => $client_id];
129 if (!empty($client_secret)) {
130 $condition['client_secret'] = $client_secret;
133 if (!empty($redirect_uri)) {
134 $redirect_uri = strtok($redirect_uri, '?');
135 $condition = DBA::mergeConditions($condition, ["`redirect_uri` LIKE ?", '%' . $redirect_uri . '%']);
138 $application = DBA::selectFirst('application', [], $condition);
139 if (!DBA::isResult($application)) {
140 Logger::warning('Application not found', $condition);
144 // The redirect_uri could contain several URI that are separated by spaces.
145 if (($application['redirect_uri'] != $redirect_uri) && !in_array($redirect_uri, explode(' ', $application['redirect_uri']))) {
153 * Check if an token for the application and user exists
155 * @param array $application
156 * @param integer $uid
159 public static function existsTokenForUser(array $application, int $uid)
161 return DBA::exists('application-token', ['application-id' => $application['id'], 'uid' => $uid]);
165 * Fetch the token for the given application and user
167 * @param array $application
168 * @param integer $uid
169 * @return array application record
171 public static function getTokenForUser(array $application, int $uid)
173 return DBA::selectFirst('application-token', [], ['application-id' => $application['id'], 'uid' => $uid]);
177 * Create and fetch an token for the application and user
179 * @param array $application
180 * @param integer $uid
181 * @param string $scope
182 * @return array application record
184 public static function createTokenForUser(array $application, int $uid, string $scope)
186 $code = bin2hex(random_bytes(32));
187 $access_token = bin2hex(random_bytes(32));
190 'application-id' => $application['id'],
193 'access_token' => $access_token,
195 'read' => (stripos($scope, BaseApi::SCOPE_READ) !== false),
196 'write' => (stripos($scope, BaseApi::SCOPE_WRITE) !== false),
197 'follow' => (stripos($scope, BaseApi::SCOPE_FOLLOW) !== false),
198 'push' => (stripos($scope, BaseApi::SCOPE_PUSH) !== false),
199 'created_at' => DateTimeFormat::utcNow()
202 foreach ([BaseApi::SCOPE_READ, BaseApi::SCOPE_WRITE, BaseApi::SCOPE_WRITE, BaseApi::SCOPE_PUSH] as $scope) {
203 if ($fields[$scope] && !$application[$scope]) {
204 Logger::warning('Requested token scope is not allowed for the application', ['token' => $fields, 'application' => $application]);
208 if (!DBA::insert('application-token', $fields, Database::INSERT_UPDATE)) {
212 return DBA::selectFirst('application-token', [], ['application-id' => $application['id'], 'uid' => $uid]);