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\Model;
24 use Friendica\Database\DBA;
25 use Friendica\Util\DateTimeFormat;
28 * Methods to deal with entries of the 'openwebauth-token' table.
30 class OpenWebAuthToken
33 * Create an entry in the 'openwebauth-token' table.
35 * @param string $type Verify type.
36 * @param int $uid The user ID.
37 * @param string $token
42 public static function create(string $type, int $uid, string $token, string $meta)
49 'created' => DateTimeFormat::utcNow()
51 return DBA::insert('openwebauth-token', $fields);
55 * Get the "meta" field of an entry in the openwebauth-token table.
57 * @param string $type Verify type.
58 * @param int $uid The user ID.
59 * @param string $token
61 * @return string|boolean The meta entry or false if not found.
64 public static function getMeta(string $type, int $uid, string $token)
66 $condition = ['type' => $type, 'uid' => $uid, 'token' => $token];
68 $entry = DBA::selectFirst('openwebauth-token', ['id', 'meta'], $condition);
69 if (DBA::isResult($entry)) {
70 DBA::delete('openwebauth-token', ['id' => $entry['id']]);
72 return $entry['meta'];
78 * Purge entries of a verify-type older than interval.
80 * @param string $type Verify type.
81 * @param string $interval SQL compatible time interval
85 public static function purge(string $type, string $interval)
87 $condition = ["`type` = ? AND `created` < ?", $type, DateTimeFormat::utcNow() . ' - INTERVAL ' . $interval];
88 DBA::delete('openwebauth-token', $condition);