1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) 2018 Roeland Jago Douma <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @author Roeland Jago Douma <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @license GNU AGPL version 3 or any later version |
9
|
|
|
* |
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
13
|
|
|
* License, or (at your option) any later version. |
14
|
|
|
* |
15
|
|
|
* This program is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
* GNU Affero General Public License for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace OC\Authentication\Token; |
26
|
|
|
|
27
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
28
|
|
|
use OCP\AppFramework\Db\QBMapper; |
29
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
30
|
|
|
use OCP\IDBConnection; |
31
|
|
|
|
32
|
|
|
class PublicKeyTokenMapper extends QBMapper { |
33
|
|
|
|
34
|
|
|
public function __construct(IDBConnection $db) { |
35
|
|
|
parent::__construct($db, 'authtoken'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Invalidate (delete) a given token |
40
|
|
|
* |
41
|
|
|
* @param string $token |
42
|
|
|
*/ |
43
|
|
View Code Duplication |
public function invalidate(string $token) { |
44
|
|
|
/* @var $qb IQueryBuilder */ |
45
|
|
|
$qb = $this->db->getQueryBuilder(); |
46
|
|
|
$qb->delete('authtoken') |
47
|
|
|
->where($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
48
|
|
|
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))) |
49
|
|
|
->execute(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param int $olderThan |
54
|
|
|
* @param int $remember |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function invalidateOld(int $olderThan, int $remember = IToken::DO_NOT_REMEMBER) { |
57
|
|
|
/* @var $qb IQueryBuilder */ |
58
|
|
|
$qb = $this->db->getQueryBuilder(); |
59
|
|
|
$qb->delete('authtoken') |
60
|
|
|
->where($qb->expr()->lt('last_activity', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT))) |
61
|
|
|
->andWhere($qb->expr()->eq('type', $qb->createNamedParameter(IToken::TEMPORARY_TOKEN, IQueryBuilder::PARAM_INT))) |
62
|
|
|
->andWhere($qb->expr()->eq('remember', $qb->createNamedParameter($remember, IQueryBuilder::PARAM_INT))) |
63
|
|
|
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))) |
64
|
|
|
->execute(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the user UID for the given token |
69
|
|
|
* |
70
|
|
|
* @throws DoesNotExistException |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function getToken(string $token): PublicKeyToken { |
73
|
|
|
/* @var $qb IQueryBuilder */ |
74
|
|
|
$qb = $this->db->getQueryBuilder(); |
75
|
|
|
$result = $qb->select('*') |
76
|
|
|
->from('authtoken') |
77
|
|
|
->where($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
78
|
|
|
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))) |
79
|
|
|
->execute(); |
80
|
|
|
|
81
|
|
|
$data = $result->fetch(); |
82
|
|
|
$result->closeCursor(); |
83
|
|
|
if ($data === false) { |
84
|
|
|
throw new DoesNotExistException('token does not exist'); |
85
|
|
|
} |
86
|
|
|
return PublicKeyToken::fromRow($data); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get the token for $id |
91
|
|
|
* |
92
|
|
|
* @throws DoesNotExistException |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function getTokenById(int $id): PublicKeyToken { |
95
|
|
|
/* @var $qb IQueryBuilder */ |
96
|
|
|
$qb = $this->db->getQueryBuilder(); |
97
|
|
|
$result = $qb->select('*') |
98
|
|
|
->from('authtoken') |
99
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
100
|
|
|
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))) |
101
|
|
|
->execute(); |
102
|
|
|
|
103
|
|
|
$data = $result->fetch(); |
104
|
|
|
$result->closeCursor(); |
105
|
|
|
if ($data === false) { |
106
|
|
|
throw new DoesNotExistException('token does not exist'); |
107
|
|
|
} |
108
|
|
|
return PublicKeyToken::fromRow($data); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get all tokens of a user |
113
|
|
|
* |
114
|
|
|
* The provider may limit the number of result rows in case of an abuse |
115
|
|
|
* where a high number of (session) tokens is generated |
116
|
|
|
* |
117
|
|
|
* @param string $uid |
118
|
|
|
* @return PublicKeyToken[] |
119
|
|
|
*/ |
120
|
|
|
public function getTokenByUser(string $uid): array { |
121
|
|
|
/* @var $qb IQueryBuilder */ |
122
|
|
|
$qb = $this->db->getQueryBuilder(); |
123
|
|
|
$qb->select('*') |
124
|
|
|
->from('authtoken') |
125
|
|
|
->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
126
|
|
|
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))) |
127
|
|
|
->setMaxResults(1000); |
128
|
|
|
$result = $qb->execute(); |
129
|
|
|
$data = $result->fetchAll(); |
130
|
|
|
$result->closeCursor(); |
131
|
|
|
|
132
|
|
|
$entities = array_map(function ($row) { |
133
|
|
|
return PublicKeyToken::fromRow($row); |
134
|
|
|
}, $data); |
135
|
|
|
|
136
|
|
|
return $entities; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
View Code Duplication |
public function deleteById(string $uid, int $id) { |
140
|
|
|
/* @var $qb IQueryBuilder */ |
141
|
|
|
$qb = $this->db->getQueryBuilder(); |
142
|
|
|
$qb->delete('authtoken') |
143
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
144
|
|
|
->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
145
|
|
|
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))); |
146
|
|
|
$qb->execute(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* delete all auth token which belong to a specific client if the client was deleted |
151
|
|
|
* |
152
|
|
|
* @param string $name |
153
|
|
|
*/ |
154
|
|
View Code Duplication |
public function deleteByName(string $name) { |
155
|
|
|
$qb = $this->db->getQueryBuilder(); |
156
|
|
|
$qb->delete('authtoken') |
157
|
|
|
->where($qb->expr()->eq('name', $qb->createNamedParameter($name), IQueryBuilder::PARAM_STR)) |
158
|
|
|
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))); |
159
|
|
|
$qb->execute(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function deleteTempToken(PublicKeyToken $except) { |
163
|
|
|
$qb = $this->db->getQueryBuilder(); |
164
|
|
|
|
165
|
|
|
$qb->delete('authtoken') |
166
|
|
|
->where($qb->expr()->eq('type', $qb->createNamedParameter(IToken::TEMPORARY_TOKEN))) |
167
|
|
|
->andWhere($qb->expr()->neq('id', $qb->createNamedParameter($except->getId()))) |
168
|
|
|
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))); |
169
|
|
|
|
170
|
|
|
$qb->execute(); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|