]> git.mxchange.org Git - friendica.git/blob - src/Model/OpenWebAuthToken.php
Merge pull request #13238 from annando/issue-13221
[friendica.git] / src / Model / OpenWebAuthToken.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
24 use Friendica\Database\DBA;
25 use Friendica\Util\DateTimeFormat;
26
27 /**
28  * Methods to deal with entries of the 'openwebauth-token' table.
29  */
30 class OpenWebAuthToken
31 {
32         /**
33          * Create an entry in the 'openwebauth-token' table.
34          *
35          * @param string $type Verify type.
36          * @param int    $uid  The user ID.
37          * @param string $token
38          * @param string $meta
39          * @return boolean
40          * @throws \Exception
41          */
42         public static function create(string $type, int $uid, string $token, string $meta)
43         {
44                 $fields = [
45                         'type'    => $type,
46                         'uid'     => $uid,
47                         'token'   => $token,
48                         'meta'    => $meta,
49                         'created' => DateTimeFormat::utcNow()
50                 ];
51                 return DBA::insert('openwebauth-token', $fields);
52         }
53
54         /**
55          * Get the "meta" field of an entry in the openwebauth-token table.
56          *
57          * @param string $type Verify type.
58          * @param int    $uid  The user ID.
59          * @param string $token
60          *
61          * @return string|boolean The meta entry or false if not found.
62          * @throws \Exception
63          */
64         public static function getMeta(string $type, int $uid, string $token)
65         {
66                 $condition = ['type' => $type, 'uid' => $uid, 'token' => $token];
67
68                 $entry = DBA::selectFirst('openwebauth-token', ['id', 'meta'], $condition);
69                 if (DBA::isResult($entry)) {
70                         DBA::delete('openwebauth-token', ['id' => $entry['id']]);
71
72                         return $entry['meta'];
73                 }
74                 return false;
75         }
76
77         /**
78          * Purge entries of a verify-type older than interval.
79          *
80          * @param string $type     Verify type.
81          * @param string $interval SQL compatible time interval
82          * @return void
83          * @throws \Exception
84          */
85         public static function purge(string $type, string $interval)
86         {
87                 $condition = ["`type` = ? AND `created` < ?", $type, DateTimeFormat::utcNow() . ' - INTERVAL ' . $interval];
88                 DBA::delete('openwebauth-token', $condition);
89         }
90
91 }