trim(substr($authorization, 7))]; $token = DBA::selectFirst('application-view', ['uid', 'id', 'name', 'website', 'created_at', 'read', 'write', 'follow', 'push'], $condition); if (!DBA::isResult($token)) { Logger::notice('Token not found', $condition); return []; } Logger::debug('Token found', $token); $user = User::getById($token['uid'], ['uid', 'parent-uid', 'last-activity', 'login_date']); if (!empty($user)) { User::updateLastActivity($user, false); } // Regularly update suggestions if (Contact\Relation::areSuggestionsOutdated($token['uid'])) { Worker::add(Worker::PRIORITY_MEDIUM, 'UpdateSuggestions', $token['uid']); } return $token; } /** * Get the application record via the provided request header fields * * @param string $client_id * @param string $client_secret * @param string $redirect_uri * @return array application record */ public static function getApplication(string $client_id, string $client_secret, string $redirect_uri) { $condition = ['client_id' => $client_id]; if (!empty($client_secret)) { $condition['client_secret'] = $client_secret; } if (!empty($redirect_uri)) { $redirect_uri = strtok($redirect_uri, '?'); $condition = DBA::mergeConditions($condition, ["`redirect_uri` LIKE ?", '%' . $redirect_uri . '%']); } $application = DBA::selectFirst('application', [], $condition); if (!DBA::isResult($application)) { Logger::warning('Application not found', $condition); return []; } // The redirect_uri could contain several URI that are separated by spaces or new lines. $uris = explode(' ', str_replace(["\n", "\r", "\t"], ' ', $application['redirect_uri'])); if (!in_array($redirect_uri, $uris)) { Logger::warning('Redirection uri does not match', ['redirect_uri' => $redirect_uri, 'application-redirect_uri' => $application['redirect_uri']]); return []; } return $application; } /** * Check if an token for the application and user exists * * @param array $application * @param integer $uid * @return boolean */ public static function existsTokenForUser(array $application, int $uid) { return DBA::exists('application-token', ['application-id' => $application['id'], 'uid' => $uid]); } /** * Fetch the token for the given application and user * * @param array $application * @param integer $uid * @return array application record */ public static function getTokenForUser(array $application, int $uid) { return DBA::selectFirst('application-token', [], ['application-id' => $application['id'], 'uid' => $uid]); } /** * Create and fetch an token for the application and user * * @param array $application * @param integer $uid * @param string $scope * @return array application record */ public static function createTokenForUser(array $application, int $uid, string $scope) { $code = bin2hex(random_bytes(32)); $access_token = bin2hex(random_bytes(32)); $fields = [ 'application-id' => $application['id'], 'uid' => $uid, 'code' => $code, 'access_token' => $access_token, 'scopes' => $scope, 'read' => (stripos($scope, BaseApi::SCOPE_READ) !== false), 'write' => (stripos($scope, BaseApi::SCOPE_WRITE) !== false), 'follow' => (stripos($scope, BaseApi::SCOPE_FOLLOW) !== false), 'push' => (stripos($scope, BaseApi::SCOPE_PUSH) !== false), 'created_at' => DateTimeFormat::utcNow() ]; foreach ([BaseApi::SCOPE_READ, BaseApi::SCOPE_WRITE, BaseApi::SCOPE_WRITE, BaseApi::SCOPE_PUSH] as $scope) { if ($fields[$scope] && !$application[$scope]) { Logger::warning('Requested token scope is not allowed for the application', ['token' => $fields, 'application' => $application]); } } if (!DBA::insert('application-token', $fields, Database::INSERT_UPDATE)) { return []; } return DBA::selectFirst('application-token', [], ['application-id' => $application['id'], 'uid' => $uid]); } }