2018-06-18 23:05:44 +02:00
|
|
|
<?php
|
2024-08-24 15:27:00 +02:00
|
|
|
|
|
|
|
// Copyright (C) 2010-2024, the Friendica project
|
|
|
|
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
2020-02-09 15:45:36 +01:00
|
|
|
|
2018-06-18 23:05:44 +02:00
|
|
|
namespace Friendica\Model;
|
|
|
|
|
2018-07-20 08:19:26 -04:00
|
|
|
use Friendica\Database\DBA;
|
2018-06-18 23:05:44 +02:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
|
|
|
|
|
|
|
/**
|
2018-06-20 20:11:26 +02:00
|
|
|
* Methods to deal with entries of the 'openwebauth-token' table.
|
2018-06-18 23:05:44 +02:00
|
|
|
*/
|
2018-06-20 19:24:02 +02:00
|
|
|
class OpenWebAuthToken
|
2018-06-18 23:05:44 +02:00
|
|
|
{
|
|
|
|
/**
|
2018-06-20 20:11:26 +02: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 23:05:44 +02:00
|
|
|
* @param string $token
|
|
|
|
* @param string $meta
|
|
|
|
* @return boolean
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-06-18 23:05:44 +02:00
|
|
|
*/
|
2022-07-29 00:06:49 +02:00
|
|
|
public static function create(string $type, int $uid, string $token, string $meta)
|
2018-06-18 23:05:44 +02:00
|
|
|
{
|
|
|
|
$fields = [
|
2022-07-29 00:06:49 +02:00
|
|
|
'type' => $type,
|
|
|
|
'uid' => $uid,
|
|
|
|
'token' => $token,
|
|
|
|
'meta' => $meta,
|
2022-06-18 18:21:05 +02:00
|
|
|
'created' => DateTimeFormat::utcNow()
|
2018-06-18 23:05:44 +02:00
|
|
|
];
|
2022-06-18 18:21:05 +02:00
|
|
|
return DBA::insert('openwebauth-token', $fields);
|
2018-06-18 23:05:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-20 20:11:26 +02: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 23:05:44 +02:00
|
|
|
* @param string $token
|
2018-07-19 22:15:21 -04:00
|
|
|
*
|
2023-03-26 18:15:44 -04:00
|
|
|
* @return string|boolean The meta entry or false if not found.
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-06-18 23:05:44 +02:00
|
|
|
*/
|
2022-06-18 18:21:05 +02:00
|
|
|
public static function getMeta(string $type, int $uid, string $token)
|
2018-06-18 23:05:44 +02:00
|
|
|
{
|
2022-06-18 18:21:05 +02:00
|
|
|
$condition = ['type' => $type, 'uid' => $uid, 'token' => $token];
|
2018-06-18 23:05:44 +02:00
|
|
|
|
2022-06-18 18:21:05 +02:00
|
|
|
$entry = DBA::selectFirst('openwebauth-token', ['id', 'meta'], $condition);
|
2018-07-21 08:46:04 -04:00
|
|
|
if (DBA::isResult($entry)) {
|
2022-06-18 18:21:05 +02:00
|
|
|
DBA::delete('openwebauth-token', ['id' => $entry['id']]);
|
2018-06-18 23:05:44 +02:00
|
|
|
|
2022-06-18 18:21:05 +02:00
|
|
|
return $entry['meta'];
|
2018-06-18 23:05:44 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Purge entries of a verify-type older than interval.
|
2018-07-19 22:15:21 -04:00
|
|
|
*
|
2018-06-18 23:05:44 +02:00
|
|
|
* @param string $type Verify type.
|
|
|
|
* @param string $interval SQL compatible time interval
|
2022-06-23 10:56:37 +02:00
|
|
|
* @return void
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-06-18 23:05:44 +02:00
|
|
|
*/
|
2022-06-18 18:21:05 +02:00
|
|
|
public static function purge(string $type, string $interval)
|
2018-06-18 23:05:44 +02:00
|
|
|
{
|
2022-06-18 23:30:13 +02:00
|
|
|
$condition = ["`type` = ? AND `created` < ?", $type, DateTimeFormat::utcNow() . ' - INTERVAL ' . $interval];
|
2022-06-18 18:21:05 +02:00
|
|
|
DBA::delete('openwebauth-token', $condition);
|
2018-06-18 23:05:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|