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\Module\BaseApi;
28 use Friendica\Util\DateTimeFormat;
38 protected static $current_user_id = 0;
42 protected static $current_token = [];
45 * Get current user id, returns 0 if not logged in
49 public static function getCurrentUserID()
51 if (empty(self::$current_user_id)) {
52 $token = self::getCurrentApplicationToken();
53 if (!empty($token['uid'])) {
54 self::$current_user_id = $token['uid'];
56 self::$current_user_id = 0;
60 return (int)self::$current_user_id;
64 * Get current application token
68 public static function getCurrentApplicationToken()
70 if (empty(self::$current_token)) {
71 self::$current_token = self::getTokenByBearer();
74 return self::$current_token;
78 * Get the user token via the Bearer token
80 * @return array User Token
82 private static function getTokenByBearer()
84 $authorization = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
86 if (substr($authorization, 0, 7) != 'Bearer ') {
90 $condition = ['access_token' => trim(substr($authorization, 7))];
92 $token = DBA::selectFirst('application-view', ['uid', 'id', 'name', 'website', 'created_at', 'read', 'write', 'follow', 'push'], $condition);
93 if (!DBA::isResult($token)) {
94 Logger::warning('Token not found', $condition);
97 Logger::debug('Token found', $token);
102 * Get the application record via the provided request header fields
104 * @param string $client_id
105 * @param string $client_secret
106 * @param string $redirect_uri
107 * @return array application record
109 public static function getApplication(string $client_id, string $client_secret, string $redirect_uri)
111 $condition = ['client_id' => $client_id];
112 if (!empty($client_secret)) {
113 $condition['client_secret'] = $client_secret;
115 if (!empty($redirect_uri)) {
116 $condition['redirect_uri'] = $redirect_uri;
119 $application = DBA::selectFirst('application', [], $condition);
120 if (!DBA::isResult($application)) {
121 Logger::warning('Application not found', $condition);
128 * Check if an token for the application and user exists
130 * @param array $application
131 * @param integer $uid
134 public static function existsTokenForUser(array $application, int $uid)
136 return DBA::exists('application-token', ['application-id' => $application['id'], 'uid' => $uid]);
140 * Fetch the token for the given application and user
142 * @param array $application
143 * @param integer $uid
144 * @return array application record
146 public static function getTokenForUser(array $application, int $uid)
148 return DBA::selectFirst('application-token', [], ['application-id' => $application['id'], 'uid' => $uid]);
152 * Create and fetch an token for the application and user
154 * @param array $application
155 * @param integer $uid
156 * @param string $scope
157 * @return array application record
159 public static function createTokenForUser(array $application, int $uid, string $scope)
161 $code = bin2hex(random_bytes(32));
162 $access_token = bin2hex(random_bytes(32));
165 'application-id' => $application['id'],
168 'access_token' => $access_token,
170 'read' => (stripos($scope, BaseApi::SCOPE_READ) !== false),
171 'write' => (stripos($scope, BaseApi::SCOPE_WRITE) !== false),
172 'follow' => (stripos($scope, BaseApi::SCOPE_FOLLOW) !== false),
173 'push' => (stripos($scope, BaseApi::SCOPE_PUSH) !== false),
174 'created_at' => DateTimeFormat::utcNow(DateTimeFormat::MYSQL)];
176 foreach ([BaseApi::SCOPE_READ, BaseApi::SCOPE_WRITE, BaseApi::SCOPE_WRITE, BaseApi::SCOPE_PUSH] as $scope) {
177 if ($fields[$scope] && !$application[$scope]) {
178 Logger::warning('Requested token scope is not allowed for the application', ['token' => $fields, 'application' => $application]);
182 if (!DBA::insert('application-token', $fields, Database::INSERT_UPDATE)) {
186 return DBA::selectFirst('application-token', [], ['application-id' => $application['id'], 'uid' => $uid]);