2018-06-18 17:05:44 -04:00
|
|
|
<?php
|
|
|
|
/**
|
2022-01-02 02:27:47 -05:00
|
|
|
* @copyright Copyright (C) 2010-2022, the Friendica project
|
2020-02-09 09:45:36 -05:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
2018-06-18 17:05:44 -04:00
|
|
|
*/
|
2020-02-09 09:45:36 -05:00
|
|
|
|
2018-06-18 17:05:44 -04:00
|
|
|
namespace Friendica\Model;
|
|
|
|
|
2018-07-20 08:19:26 -04:00
|
|
|
use Friendica\Database\DBA;
|
2018-06-18 17:05:44 -04:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
|
|
|
|
|
|
|
/**
|
2018-06-20 14:11:26 -04:00
|
|
|
* Methods to deal with entries of the 'openwebauth-token' table.
|
2018-06-18 17:05:44 -04:00
|
|
|
*/
|
2018-06-20 13:24:02 -04:00
|
|
|
class OpenWebAuthToken
|
2018-06-18 17:05:44 -04:00
|
|
|
{
|
|
|
|
/**
|
2018-06-20 14:11:26 -04:00
|
|
|
* Create an entry in the 'openwebauth-token' table.
|
2018-07-19 22:15:21 -04:00
|
|
|
*
|
2019-01-06 16:06:53 -05:00
|
|
|
* @param string $type Verify type.
|
|
|
|
* @param int $uid The user ID.
|
2018-06-18 17:05:44 -04:00
|
|
|
* @param string $token
|
|
|
|
* @param string $meta
|
|
|
|
* @return boolean
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-06-18 17:05:44 -04:00
|
|
|
*/
|
2022-06-18 12:21:05 -04:00
|
|
|
public static function create(string $type, uid $uid, string $token, string $meta)
|
2018-06-18 17:05:44 -04:00
|
|
|
{
|
|
|
|
$fields = [
|
2022-06-18 12:21:05 -04:00
|
|
|
'type' => $type,
|
|
|
|
'uid' => $uid,
|
|
|
|
'token' => $token,
|
|
|
|
'meta' => $meta,
|
|
|
|
'created' => DateTimeFormat::utcNow()
|
2018-06-18 17:05:44 -04:00
|
|
|
];
|
2022-06-18 12:21:05 -04:00
|
|
|
return DBA::insert('openwebauth-token', $fields);
|
2018-06-18 17:05:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-20 14:11:26 -04:00
|
|
|
* Get the "meta" field of an entry in the openwebauth-token table.
|
2018-07-19 22:15:21 -04:00
|
|
|
*
|
2019-01-06 16:06:53 -05:00
|
|
|
* @param string $type Verify type.
|
|
|
|
* @param int $uid The user ID.
|
2018-06-18 17:05:44 -04:00
|
|
|
* @param string $token
|
2018-07-19 22:15:21 -04:00
|
|
|
*
|
2018-06-18 17:05:44 -04:00
|
|
|
* @return string|boolean The meta enry or false if not found.
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-06-18 17:05:44 -04:00
|
|
|
*/
|
2022-06-18 12:21:05 -04:00
|
|
|
public static function getMeta(string $type, int $uid, string $token)
|
2018-06-18 17:05:44 -04:00
|
|
|
{
|
2022-06-18 12:21:05 -04:00
|
|
|
$condition = ['type' => $type, 'uid' => $uid, 'token' => $token];
|
2018-06-18 17:05:44 -04:00
|
|
|
|
2022-06-18 12:21:05 -04:00
|
|
|
$entry = DBA::selectFirst('openwebauth-token', ['id', 'meta'], $condition);
|
2018-07-21 08:46:04 -04:00
|
|
|
if (DBA::isResult($entry)) {
|
2022-06-18 12:21:05 -04:00
|
|
|
DBA::delete('openwebauth-token', ['id' => $entry['id']]);
|
2018-06-18 17:05:44 -04:00
|
|
|
|
2022-06-18 12:21:05 -04:00
|
|
|
return $entry['meta'];
|
2018-06-18 17:05:44 -04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Purge entries of a verify-type older than interval.
|
2018-07-19 22:15:21 -04:00
|
|
|
*
|
2018-06-18 17:05:44 -04:00
|
|
|
* @param string $type Verify type.
|
|
|
|
* @param string $interval SQL compatible time interval
|
2022-06-23 04:56:37 -04:00
|
|
|
* @return void
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-06-18 17:05:44 -04:00
|
|
|
*/
|
2022-06-18 12:21:05 -04:00
|
|
|
public static function purge(string $type, string $interval)
|
2018-06-18 17:05:44 -04:00
|
|
|
{
|
2022-06-18 17:30:13 -04:00
|
|
|
$condition = ["`type` = ? AND `created` < ?", $type, DateTimeFormat::utcNow() . ' - INTERVAL ' . $interval];
|
2022-06-18 12:21:05 -04:00
|
|
|
DBA::delete('openwebauth-token', $condition);
|
2018-06-18 17:05:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|