@@ -42,168 +42,168 @@ |
||
42 | 42 | |
43 | 43 | class AuthSettingsController extends Controller { |
44 | 44 | |
45 | - /** @var IProvider */ |
|
46 | - private $tokenProvider; |
|
47 | - |
|
48 | - /** @var IUserManager */ |
|
49 | - private $userManager; |
|
50 | - |
|
51 | - /** @var ISession */ |
|
52 | - private $session; |
|
53 | - |
|
54 | - /** @var string */ |
|
55 | - private $uid; |
|
56 | - |
|
57 | - /** @var ISecureRandom */ |
|
58 | - private $random; |
|
59 | - |
|
60 | - /** |
|
61 | - * @param string $appName |
|
62 | - * @param IRequest $request |
|
63 | - * @param IProvider $tokenProvider |
|
64 | - * @param IUserManager $userManager |
|
65 | - * @param ISession $session |
|
66 | - * @param ISecureRandom $random |
|
67 | - * @param string $userId |
|
68 | - */ |
|
69 | - public function __construct($appName, IRequest $request, IProvider $tokenProvider, IUserManager $userManager, |
|
70 | - ISession $session, ISecureRandom $random, $userId) { |
|
71 | - parent::__construct($appName, $request); |
|
72 | - $this->tokenProvider = $tokenProvider; |
|
73 | - $this->userManager = $userManager; |
|
74 | - $this->uid = $userId; |
|
75 | - $this->session = $session; |
|
76 | - $this->random = $random; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * @NoAdminRequired |
|
81 | - * @NoSubadminRequired |
|
82 | - * |
|
83 | - * @return JSONResponse|array |
|
84 | - */ |
|
85 | - public function index() { |
|
86 | - $tokens = $this->tokenProvider->getTokenByUser($this->uid); |
|
45 | + /** @var IProvider */ |
|
46 | + private $tokenProvider; |
|
47 | + |
|
48 | + /** @var IUserManager */ |
|
49 | + private $userManager; |
|
50 | + |
|
51 | + /** @var ISession */ |
|
52 | + private $session; |
|
53 | + |
|
54 | + /** @var string */ |
|
55 | + private $uid; |
|
56 | + |
|
57 | + /** @var ISecureRandom */ |
|
58 | + private $random; |
|
59 | + |
|
60 | + /** |
|
61 | + * @param string $appName |
|
62 | + * @param IRequest $request |
|
63 | + * @param IProvider $tokenProvider |
|
64 | + * @param IUserManager $userManager |
|
65 | + * @param ISession $session |
|
66 | + * @param ISecureRandom $random |
|
67 | + * @param string $userId |
|
68 | + */ |
|
69 | + public function __construct($appName, IRequest $request, IProvider $tokenProvider, IUserManager $userManager, |
|
70 | + ISession $session, ISecureRandom $random, $userId) { |
|
71 | + parent::__construct($appName, $request); |
|
72 | + $this->tokenProvider = $tokenProvider; |
|
73 | + $this->userManager = $userManager; |
|
74 | + $this->uid = $userId; |
|
75 | + $this->session = $session; |
|
76 | + $this->random = $random; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * @NoAdminRequired |
|
81 | + * @NoSubadminRequired |
|
82 | + * |
|
83 | + * @return JSONResponse|array |
|
84 | + */ |
|
85 | + public function index() { |
|
86 | + $tokens = $this->tokenProvider->getTokenByUser($this->uid); |
|
87 | 87 | |
88 | - try { |
|
89 | - $sessionId = $this->session->getId(); |
|
90 | - } catch (SessionNotAvailableException $ex) { |
|
91 | - return $this->getServiceNotAvailableResponse(); |
|
92 | - } |
|
93 | - try { |
|
94 | - $sessionToken = $this->tokenProvider->getToken($sessionId); |
|
95 | - } catch (InvalidTokenException $ex) { |
|
96 | - return $this->getServiceNotAvailableResponse(); |
|
97 | - } |
|
98 | - |
|
99 | - return array_map(function(IToken $token) use ($sessionToken) { |
|
100 | - $data = $token->jsonSerialize(); |
|
101 | - if ($sessionToken->getId() === $token->getId()) { |
|
102 | - $data['canDelete'] = false; |
|
103 | - $data['current'] = true; |
|
104 | - } else { |
|
105 | - $data['canDelete'] = true; |
|
106 | - } |
|
107 | - return $data; |
|
108 | - }, $tokens); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * @NoAdminRequired |
|
113 | - * @NoSubadminRequired |
|
114 | - * @PasswordConfirmationRequired |
|
115 | - * |
|
116 | - * @param string $name |
|
117 | - * @return JSONResponse |
|
118 | - */ |
|
119 | - public function create($name) { |
|
120 | - try { |
|
121 | - $sessionId = $this->session->getId(); |
|
122 | - } catch (SessionNotAvailableException $ex) { |
|
123 | - return $this->getServiceNotAvailableResponse(); |
|
124 | - } |
|
125 | - |
|
126 | - try { |
|
127 | - $sessionToken = $this->tokenProvider->getToken($sessionId); |
|
128 | - $loginName = $sessionToken->getLoginName(); |
|
129 | - try { |
|
130 | - $password = $this->tokenProvider->getPassword($sessionToken, $sessionId); |
|
131 | - } catch (PasswordlessTokenException $ex) { |
|
132 | - $password = null; |
|
133 | - } |
|
134 | - } catch (InvalidTokenException $ex) { |
|
135 | - return $this->getServiceNotAvailableResponse(); |
|
136 | - } |
|
137 | - |
|
138 | - $token = $this->generateRandomDeviceToken(); |
|
139 | - $deviceToken = $this->tokenProvider->generateToken($token, $this->uid, $loginName, $password, $name, IToken::PERMANENT_TOKEN); |
|
140 | - $tokenData = $deviceToken->jsonSerialize(); |
|
141 | - $tokenData['canDelete'] = true; |
|
142 | - |
|
143 | - return new JSONResponse([ |
|
144 | - 'token' => $token, |
|
145 | - 'loginName' => $loginName, |
|
146 | - 'deviceToken' => $tokenData, |
|
147 | - ]); |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * @return JSONResponse |
|
152 | - */ |
|
153 | - private function getServiceNotAvailableResponse() { |
|
154 | - $resp = new JSONResponse(); |
|
155 | - $resp->setStatus(Http::STATUS_SERVICE_UNAVAILABLE); |
|
156 | - return $resp; |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Return a 25 digit device password |
|
161 | - * |
|
162 | - * Example: AbCdE-fGhJk-MnPqR-sTwXy-23456 |
|
163 | - * |
|
164 | - * @return string |
|
165 | - */ |
|
166 | - private function generateRandomDeviceToken() { |
|
167 | - $groups = []; |
|
168 | - for ($i = 0; $i < 5; $i++) { |
|
169 | - $groups[] = $this->random->generate(5, ISecureRandom::CHAR_HUMAN_READABLE); |
|
170 | - } |
|
171 | - return implode('-', $groups); |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * @NoAdminRequired |
|
176 | - * @NoSubadminRequired |
|
177 | - * |
|
178 | - * @return array |
|
179 | - */ |
|
180 | - public function destroy($id) { |
|
181 | - $this->tokenProvider->invalidateTokenById($this->uid, $id); |
|
182 | - return []; |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * @NoAdminRequired |
|
187 | - * @NoSubadminRequired |
|
188 | - * |
|
189 | - * @param int $id |
|
190 | - * @param array $scope |
|
191 | - * @return array|JSONResponse |
|
192 | - */ |
|
193 | - public function update($id, array $scope) { |
|
194 | - try { |
|
195 | - $token = $this->tokenProvider->getTokenById((string)$id); |
|
196 | - if ($token->getUID() !== $this->uid) { |
|
197 | - throw new InvalidTokenException('User mismatch'); |
|
198 | - } |
|
199 | - } catch (InvalidTokenException $e) { |
|
200 | - return new JSONResponse([], Http::STATUS_NOT_FOUND); |
|
201 | - } |
|
202 | - |
|
203 | - $token->setScope([ |
|
204 | - 'filesystem' => $scope['filesystem'] |
|
205 | - ]); |
|
206 | - $this->tokenProvider->updateToken($token); |
|
207 | - return []; |
|
208 | - } |
|
88 | + try { |
|
89 | + $sessionId = $this->session->getId(); |
|
90 | + } catch (SessionNotAvailableException $ex) { |
|
91 | + return $this->getServiceNotAvailableResponse(); |
|
92 | + } |
|
93 | + try { |
|
94 | + $sessionToken = $this->tokenProvider->getToken($sessionId); |
|
95 | + } catch (InvalidTokenException $ex) { |
|
96 | + return $this->getServiceNotAvailableResponse(); |
|
97 | + } |
|
98 | + |
|
99 | + return array_map(function(IToken $token) use ($sessionToken) { |
|
100 | + $data = $token->jsonSerialize(); |
|
101 | + if ($sessionToken->getId() === $token->getId()) { |
|
102 | + $data['canDelete'] = false; |
|
103 | + $data['current'] = true; |
|
104 | + } else { |
|
105 | + $data['canDelete'] = true; |
|
106 | + } |
|
107 | + return $data; |
|
108 | + }, $tokens); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * @NoAdminRequired |
|
113 | + * @NoSubadminRequired |
|
114 | + * @PasswordConfirmationRequired |
|
115 | + * |
|
116 | + * @param string $name |
|
117 | + * @return JSONResponse |
|
118 | + */ |
|
119 | + public function create($name) { |
|
120 | + try { |
|
121 | + $sessionId = $this->session->getId(); |
|
122 | + } catch (SessionNotAvailableException $ex) { |
|
123 | + return $this->getServiceNotAvailableResponse(); |
|
124 | + } |
|
125 | + |
|
126 | + try { |
|
127 | + $sessionToken = $this->tokenProvider->getToken($sessionId); |
|
128 | + $loginName = $sessionToken->getLoginName(); |
|
129 | + try { |
|
130 | + $password = $this->tokenProvider->getPassword($sessionToken, $sessionId); |
|
131 | + } catch (PasswordlessTokenException $ex) { |
|
132 | + $password = null; |
|
133 | + } |
|
134 | + } catch (InvalidTokenException $ex) { |
|
135 | + return $this->getServiceNotAvailableResponse(); |
|
136 | + } |
|
137 | + |
|
138 | + $token = $this->generateRandomDeviceToken(); |
|
139 | + $deviceToken = $this->tokenProvider->generateToken($token, $this->uid, $loginName, $password, $name, IToken::PERMANENT_TOKEN); |
|
140 | + $tokenData = $deviceToken->jsonSerialize(); |
|
141 | + $tokenData['canDelete'] = true; |
|
142 | + |
|
143 | + return new JSONResponse([ |
|
144 | + 'token' => $token, |
|
145 | + 'loginName' => $loginName, |
|
146 | + 'deviceToken' => $tokenData, |
|
147 | + ]); |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * @return JSONResponse |
|
152 | + */ |
|
153 | + private function getServiceNotAvailableResponse() { |
|
154 | + $resp = new JSONResponse(); |
|
155 | + $resp->setStatus(Http::STATUS_SERVICE_UNAVAILABLE); |
|
156 | + return $resp; |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Return a 25 digit device password |
|
161 | + * |
|
162 | + * Example: AbCdE-fGhJk-MnPqR-sTwXy-23456 |
|
163 | + * |
|
164 | + * @return string |
|
165 | + */ |
|
166 | + private function generateRandomDeviceToken() { |
|
167 | + $groups = []; |
|
168 | + for ($i = 0; $i < 5; $i++) { |
|
169 | + $groups[] = $this->random->generate(5, ISecureRandom::CHAR_HUMAN_READABLE); |
|
170 | + } |
|
171 | + return implode('-', $groups); |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * @NoAdminRequired |
|
176 | + * @NoSubadminRequired |
|
177 | + * |
|
178 | + * @return array |
|
179 | + */ |
|
180 | + public function destroy($id) { |
|
181 | + $this->tokenProvider->invalidateTokenById($this->uid, $id); |
|
182 | + return []; |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * @NoAdminRequired |
|
187 | + * @NoSubadminRequired |
|
188 | + * |
|
189 | + * @param int $id |
|
190 | + * @param array $scope |
|
191 | + * @return array|JSONResponse |
|
192 | + */ |
|
193 | + public function update($id, array $scope) { |
|
194 | + try { |
|
195 | + $token = $this->tokenProvider->getTokenById((string)$id); |
|
196 | + if ($token->getUID() !== $this->uid) { |
|
197 | + throw new InvalidTokenException('User mismatch'); |
|
198 | + } |
|
199 | + } catch (InvalidTokenException $e) { |
|
200 | + return new JSONResponse([], Http::STATUS_NOT_FOUND); |
|
201 | + } |
|
202 | + |
|
203 | + $token->setScope([ |
|
204 | + 'filesystem' => $scope['filesystem'] |
|
205 | + ]); |
|
206 | + $this->tokenProvider->updateToken($token); |
|
207 | + return []; |
|
208 | + } |
|
209 | 209 | } |
@@ -32,128 +32,128 @@ |
||
32 | 32 | interface IProvider { |
33 | 33 | |
34 | 34 | |
35 | - /** |
|
36 | - * Create and persist a new token |
|
37 | - * |
|
38 | - * @param string $token |
|
39 | - * @param string $uid |
|
40 | - * @param string $loginName |
|
41 | - * @param string|null $password |
|
42 | - * @param string $name |
|
43 | - * @param int $type token type |
|
44 | - * @param int $remember whether the session token should be used for remember-me |
|
45 | - * @return IToken |
|
46 | - */ |
|
47 | - public function generateToken(string $token, |
|
48 | - string $uid, |
|
49 | - string $loginName, |
|
50 | - $password, |
|
51 | - string $name, |
|
52 | - int $type = IToken::TEMPORARY_TOKEN, |
|
53 | - int $remember = IToken::DO_NOT_REMEMBER): IToken; |
|
54 | - |
|
55 | - /** |
|
56 | - * Get a token by token id |
|
57 | - * |
|
58 | - * @param string $tokenId |
|
59 | - * @throws InvalidTokenException |
|
60 | - * @throws ExpiredTokenException |
|
61 | - * @return IToken |
|
62 | - */ |
|
63 | - public function getToken(string $tokenId): IToken; |
|
64 | - |
|
65 | - /** |
|
66 | - * Get a token by token id |
|
67 | - * |
|
68 | - * @param int $tokenId |
|
69 | - * @throws InvalidTokenException |
|
70 | - * @throws ExpiredTokenException |
|
71 | - * @return IToken |
|
72 | - */ |
|
73 | - public function getTokenById(int $tokenId): IToken; |
|
74 | - |
|
75 | - /** |
|
76 | - * Duplicate an existing session token |
|
77 | - * |
|
78 | - * @param string $oldSessionId |
|
79 | - * @param string $sessionId |
|
80 | - * @throws InvalidTokenException |
|
81 | - */ |
|
82 | - public function renewSessionToken(string $oldSessionId, string $sessionId); |
|
83 | - |
|
84 | - /** |
|
85 | - * Invalidate (delete) the given session token |
|
86 | - * |
|
87 | - * @param string $token |
|
88 | - */ |
|
89 | - public function invalidateToken(string $token); |
|
90 | - |
|
91 | - /** |
|
92 | - * Invalidate (delete) the given token |
|
93 | - * |
|
94 | - * @param string $uid |
|
95 | - * @param int $id |
|
96 | - */ |
|
97 | - public function invalidateTokenById(string $uid, int $id); |
|
98 | - |
|
99 | - /** |
|
100 | - * Invalidate (delete) old session tokens |
|
101 | - */ |
|
102 | - public function invalidateOldTokens(); |
|
103 | - |
|
104 | - /** |
|
105 | - * Save the updated token |
|
106 | - * |
|
107 | - * @param IToken $token |
|
108 | - */ |
|
109 | - public function updateToken(IToken $token); |
|
110 | - |
|
111 | - /** |
|
112 | - * Update token activity timestamp |
|
113 | - * |
|
114 | - * @param IToken $token |
|
115 | - */ |
|
116 | - public function updateTokenActivity(IToken $token); |
|
117 | - |
|
118 | - /** |
|
119 | - * Get all tokens of a user |
|
120 | - * |
|
121 | - * The provider may limit the number of result rows in case of an abuse |
|
122 | - * where a high number of (session) tokens is generated |
|
123 | - * |
|
124 | - * @param string $uid |
|
125 | - * @return IToken[] |
|
126 | - */ |
|
127 | - public function getTokenByUser(string $uid): array; |
|
128 | - |
|
129 | - /** |
|
130 | - * Get the (unencrypted) password of the given token |
|
131 | - * |
|
132 | - * @param IToken $token |
|
133 | - * @param string $tokenId |
|
134 | - * @throws InvalidTokenException |
|
135 | - * @throws PasswordlessTokenException |
|
136 | - * @return string |
|
137 | - */ |
|
138 | - public function getPassword(IToken $token, string $tokenId): string; |
|
139 | - |
|
140 | - /** |
|
141 | - * Encrypt and set the password of the given token |
|
142 | - * |
|
143 | - * @param IToken $token |
|
144 | - * @param string $tokenId |
|
145 | - * @param string $password |
|
146 | - * @throws InvalidTokenException |
|
147 | - */ |
|
148 | - public function setPassword(IToken $token, string $tokenId, string $password); |
|
149 | - |
|
150 | - /** |
|
151 | - * Rotate the token. Usefull for for example oauth tokens |
|
152 | - * |
|
153 | - * @param IToken $token |
|
154 | - * @param string $oldTokenId |
|
155 | - * @param string $newTokenId |
|
156 | - * @return IToken |
|
157 | - */ |
|
158 | - public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken; |
|
35 | + /** |
|
36 | + * Create and persist a new token |
|
37 | + * |
|
38 | + * @param string $token |
|
39 | + * @param string $uid |
|
40 | + * @param string $loginName |
|
41 | + * @param string|null $password |
|
42 | + * @param string $name |
|
43 | + * @param int $type token type |
|
44 | + * @param int $remember whether the session token should be used for remember-me |
|
45 | + * @return IToken |
|
46 | + */ |
|
47 | + public function generateToken(string $token, |
|
48 | + string $uid, |
|
49 | + string $loginName, |
|
50 | + $password, |
|
51 | + string $name, |
|
52 | + int $type = IToken::TEMPORARY_TOKEN, |
|
53 | + int $remember = IToken::DO_NOT_REMEMBER): IToken; |
|
54 | + |
|
55 | + /** |
|
56 | + * Get a token by token id |
|
57 | + * |
|
58 | + * @param string $tokenId |
|
59 | + * @throws InvalidTokenException |
|
60 | + * @throws ExpiredTokenException |
|
61 | + * @return IToken |
|
62 | + */ |
|
63 | + public function getToken(string $tokenId): IToken; |
|
64 | + |
|
65 | + /** |
|
66 | + * Get a token by token id |
|
67 | + * |
|
68 | + * @param int $tokenId |
|
69 | + * @throws InvalidTokenException |
|
70 | + * @throws ExpiredTokenException |
|
71 | + * @return IToken |
|
72 | + */ |
|
73 | + public function getTokenById(int $tokenId): IToken; |
|
74 | + |
|
75 | + /** |
|
76 | + * Duplicate an existing session token |
|
77 | + * |
|
78 | + * @param string $oldSessionId |
|
79 | + * @param string $sessionId |
|
80 | + * @throws InvalidTokenException |
|
81 | + */ |
|
82 | + public function renewSessionToken(string $oldSessionId, string $sessionId); |
|
83 | + |
|
84 | + /** |
|
85 | + * Invalidate (delete) the given session token |
|
86 | + * |
|
87 | + * @param string $token |
|
88 | + */ |
|
89 | + public function invalidateToken(string $token); |
|
90 | + |
|
91 | + /** |
|
92 | + * Invalidate (delete) the given token |
|
93 | + * |
|
94 | + * @param string $uid |
|
95 | + * @param int $id |
|
96 | + */ |
|
97 | + public function invalidateTokenById(string $uid, int $id); |
|
98 | + |
|
99 | + /** |
|
100 | + * Invalidate (delete) old session tokens |
|
101 | + */ |
|
102 | + public function invalidateOldTokens(); |
|
103 | + |
|
104 | + /** |
|
105 | + * Save the updated token |
|
106 | + * |
|
107 | + * @param IToken $token |
|
108 | + */ |
|
109 | + public function updateToken(IToken $token); |
|
110 | + |
|
111 | + /** |
|
112 | + * Update token activity timestamp |
|
113 | + * |
|
114 | + * @param IToken $token |
|
115 | + */ |
|
116 | + public function updateTokenActivity(IToken $token); |
|
117 | + |
|
118 | + /** |
|
119 | + * Get all tokens of a user |
|
120 | + * |
|
121 | + * The provider may limit the number of result rows in case of an abuse |
|
122 | + * where a high number of (session) tokens is generated |
|
123 | + * |
|
124 | + * @param string $uid |
|
125 | + * @return IToken[] |
|
126 | + */ |
|
127 | + public function getTokenByUser(string $uid): array; |
|
128 | + |
|
129 | + /** |
|
130 | + * Get the (unencrypted) password of the given token |
|
131 | + * |
|
132 | + * @param IToken $token |
|
133 | + * @param string $tokenId |
|
134 | + * @throws InvalidTokenException |
|
135 | + * @throws PasswordlessTokenException |
|
136 | + * @return string |
|
137 | + */ |
|
138 | + public function getPassword(IToken $token, string $tokenId): string; |
|
139 | + |
|
140 | + /** |
|
141 | + * Encrypt and set the password of the given token |
|
142 | + * |
|
143 | + * @param IToken $token |
|
144 | + * @param string $tokenId |
|
145 | + * @param string $password |
|
146 | + * @throws InvalidTokenException |
|
147 | + */ |
|
148 | + public function setPassword(IToken $token, string $tokenId, string $password); |
|
149 | + |
|
150 | + /** |
|
151 | + * Rotate the token. Usefull for for example oauth tokens |
|
152 | + * |
|
153 | + * @param IToken $token |
|
154 | + * @param string $oldTokenId |
|
155 | + * @param string $newTokenId |
|
156 | + * @return IToken |
|
157 | + */ |
|
158 | + public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken; |
|
159 | 159 | } |
@@ -30,25 +30,25 @@ |
||
30 | 30 | |
31 | 31 | class Version14000Date20180518120534 extends SimpleMigrationStep { |
32 | 32 | |
33 | - public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { |
|
34 | - /** @var ISchemaWrapper $schema */ |
|
35 | - $schema = $schemaClosure(); |
|
33 | + public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { |
|
34 | + /** @var ISchemaWrapper $schema */ |
|
35 | + $schema = $schemaClosure(); |
|
36 | 36 | |
37 | - $table = $schema->getTable('authtoken'); |
|
38 | - $table->addColumn('private_key', 'text', [ |
|
39 | - 'notnull' => false, |
|
40 | - ]); |
|
41 | - $table->addColumn('public_key', 'text', [ |
|
42 | - 'notnull' => false, |
|
43 | - ]); |
|
44 | - $table->addColumn('version', 'smallint', [ |
|
45 | - 'notnull' => true, |
|
46 | - 'default' => 1, |
|
47 | - 'unsigned' => true, |
|
48 | - ]); |
|
49 | - $table->addIndex(['uid'], 'authtoken_uid_index'); |
|
50 | - $table->addIndex(['version'], 'authtoken_version_index'); |
|
37 | + $table = $schema->getTable('authtoken'); |
|
38 | + $table->addColumn('private_key', 'text', [ |
|
39 | + 'notnull' => false, |
|
40 | + ]); |
|
41 | + $table->addColumn('public_key', 'text', [ |
|
42 | + 'notnull' => false, |
|
43 | + ]); |
|
44 | + $table->addColumn('version', 'smallint', [ |
|
45 | + 'notnull' => true, |
|
46 | + 'default' => 1, |
|
47 | + 'unsigned' => true, |
|
48 | + ]); |
|
49 | + $table->addIndex(['uid'], 'authtoken_uid_index'); |
|
50 | + $table->addIndex(['version'], 'authtoken_version_index'); |
|
51 | 51 | |
52 | - return $schema; |
|
53 | - } |
|
52 | + return $schema; |
|
53 | + } |
|
54 | 54 | } |
@@ -36,136 +36,136 @@ |
||
36 | 36 | |
37 | 37 | class DefaultTokenMapper extends QBMapper { |
38 | 38 | |
39 | - public function __construct(IDBConnection $db) { |
|
40 | - parent::__construct($db, 'authtoken'); |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * Invalidate (delete) a given token |
|
45 | - * |
|
46 | - * @param string $token |
|
47 | - */ |
|
48 | - public function invalidate(string $token) { |
|
49 | - /* @var $qb IQueryBuilder */ |
|
50 | - $qb = $this->db->getQueryBuilder(); |
|
51 | - $qb->delete('authtoken') |
|
52 | - ->where($qb->expr()->eq('token', $qb->createNamedParameter($token, IQueryBuilder::PARAM_STR))) |
|
53 | - ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
54 | - ->execute(); |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * @param int $olderThan |
|
59 | - * @param int $remember |
|
60 | - */ |
|
61 | - public function invalidateOld(int $olderThan, int $remember = IToken::DO_NOT_REMEMBER) { |
|
62 | - /* @var $qb IQueryBuilder */ |
|
63 | - $qb = $this->db->getQueryBuilder(); |
|
64 | - $qb->delete('authtoken') |
|
65 | - ->where($qb->expr()->lt('last_activity', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT))) |
|
66 | - ->andWhere($qb->expr()->eq('type', $qb->createNamedParameter(IToken::TEMPORARY_TOKEN, IQueryBuilder::PARAM_INT))) |
|
67 | - ->andWhere($qb->expr()->eq('remember', $qb->createNamedParameter($remember, IQueryBuilder::PARAM_INT))) |
|
68 | - ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
69 | - ->execute(); |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * Get the user UID for the given token |
|
74 | - * |
|
75 | - * @param string $token |
|
76 | - * @throws DoesNotExistException |
|
77 | - * @return DefaultToken |
|
78 | - */ |
|
79 | - public function getToken(string $token): DefaultToken { |
|
80 | - /* @var $qb IQueryBuilder */ |
|
81 | - $qb = $this->db->getQueryBuilder(); |
|
82 | - $result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'token', 'type', 'remember', 'last_activity', 'last_check', 'scope', 'expires', 'version') |
|
83 | - ->from('authtoken') |
|
84 | - ->where($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
|
85 | - ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
86 | - ->execute(); |
|
87 | - |
|
88 | - $data = $result->fetch(); |
|
89 | - $result->closeCursor(); |
|
90 | - if ($data === false) { |
|
91 | - throw new DoesNotExistException('token does not exist'); |
|
92 | - } |
|
93 | - return DefaultToken::fromRow($data); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Get the token for $id |
|
98 | - * |
|
99 | - * @param int $id |
|
100 | - * @throws DoesNotExistException |
|
101 | - * @return DefaultToken |
|
102 | - */ |
|
103 | - public function getTokenById(int $id): DefaultToken { |
|
104 | - /* @var $qb IQueryBuilder */ |
|
105 | - $qb = $this->db->getQueryBuilder(); |
|
106 | - $result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'token', 'type', 'remember', 'last_activity', 'last_check', 'scope', 'expires', 'version') |
|
107 | - ->from('authtoken') |
|
108 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
109 | - ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
110 | - ->execute(); |
|
111 | - |
|
112 | - $data = $result->fetch(); |
|
113 | - $result->closeCursor(); |
|
114 | - if ($data === false) { |
|
115 | - throw new DoesNotExistException('token does not exist'); |
|
116 | - } |
|
117 | - return DefaultToken::fromRow($data); |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Get all tokens of a user |
|
122 | - * |
|
123 | - * The provider may limit the number of result rows in case of an abuse |
|
124 | - * where a high number of (session) tokens is generated |
|
125 | - * |
|
126 | - * @param string $uid |
|
127 | - * @return DefaultToken[] |
|
128 | - */ |
|
129 | - public function getTokenByUser(string $uid): array { |
|
130 | - /* @var $qb IQueryBuilder */ |
|
131 | - $qb = $this->db->getQueryBuilder(); |
|
132 | - $qb->select('id', 'uid', 'login_name', 'password', 'name', 'token', 'type', 'remember', 'last_activity', 'last_check', 'scope', 'expires', 'version') |
|
133 | - ->from('authtoken') |
|
134 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
135 | - ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
136 | - ->setMaxResults(1000); |
|
137 | - $result = $qb->execute(); |
|
138 | - $data = $result->fetchAll(); |
|
139 | - $result->closeCursor(); |
|
140 | - |
|
141 | - $entities = array_map(function ($row) { |
|
142 | - return DefaultToken::fromRow($row); |
|
143 | - }, $data); |
|
144 | - |
|
145 | - return $entities; |
|
146 | - } |
|
147 | - |
|
148 | - public function deleteById(string $uid, int $id) { |
|
149 | - /* @var $qb IQueryBuilder */ |
|
150 | - $qb = $this->db->getQueryBuilder(); |
|
151 | - $qb->delete('authtoken') |
|
152 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
153 | - ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
154 | - ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT))); |
|
155 | - $qb->execute(); |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * delete all auth token which belong to a specific client if the client was deleted |
|
160 | - * |
|
161 | - * @param string $name |
|
162 | - */ |
|
163 | - public function deleteByName(string $name) { |
|
164 | - $qb = $this->db->getQueryBuilder(); |
|
165 | - $qb->delete('authtoken') |
|
166 | - ->where($qb->expr()->eq('name', $qb->createNamedParameter($name), IQueryBuilder::PARAM_STR)) |
|
167 | - ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT))); |
|
168 | - $qb->execute(); |
|
169 | - } |
|
39 | + public function __construct(IDBConnection $db) { |
|
40 | + parent::__construct($db, 'authtoken'); |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * Invalidate (delete) a given token |
|
45 | + * |
|
46 | + * @param string $token |
|
47 | + */ |
|
48 | + public function invalidate(string $token) { |
|
49 | + /* @var $qb IQueryBuilder */ |
|
50 | + $qb = $this->db->getQueryBuilder(); |
|
51 | + $qb->delete('authtoken') |
|
52 | + ->where($qb->expr()->eq('token', $qb->createNamedParameter($token, IQueryBuilder::PARAM_STR))) |
|
53 | + ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
54 | + ->execute(); |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * @param int $olderThan |
|
59 | + * @param int $remember |
|
60 | + */ |
|
61 | + public function invalidateOld(int $olderThan, int $remember = IToken::DO_NOT_REMEMBER) { |
|
62 | + /* @var $qb IQueryBuilder */ |
|
63 | + $qb = $this->db->getQueryBuilder(); |
|
64 | + $qb->delete('authtoken') |
|
65 | + ->where($qb->expr()->lt('last_activity', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT))) |
|
66 | + ->andWhere($qb->expr()->eq('type', $qb->createNamedParameter(IToken::TEMPORARY_TOKEN, IQueryBuilder::PARAM_INT))) |
|
67 | + ->andWhere($qb->expr()->eq('remember', $qb->createNamedParameter($remember, IQueryBuilder::PARAM_INT))) |
|
68 | + ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
69 | + ->execute(); |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * Get the user UID for the given token |
|
74 | + * |
|
75 | + * @param string $token |
|
76 | + * @throws DoesNotExistException |
|
77 | + * @return DefaultToken |
|
78 | + */ |
|
79 | + public function getToken(string $token): DefaultToken { |
|
80 | + /* @var $qb IQueryBuilder */ |
|
81 | + $qb = $this->db->getQueryBuilder(); |
|
82 | + $result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'token', 'type', 'remember', 'last_activity', 'last_check', 'scope', 'expires', 'version') |
|
83 | + ->from('authtoken') |
|
84 | + ->where($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
|
85 | + ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
86 | + ->execute(); |
|
87 | + |
|
88 | + $data = $result->fetch(); |
|
89 | + $result->closeCursor(); |
|
90 | + if ($data === false) { |
|
91 | + throw new DoesNotExistException('token does not exist'); |
|
92 | + } |
|
93 | + return DefaultToken::fromRow($data); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Get the token for $id |
|
98 | + * |
|
99 | + * @param int $id |
|
100 | + * @throws DoesNotExistException |
|
101 | + * @return DefaultToken |
|
102 | + */ |
|
103 | + public function getTokenById(int $id): DefaultToken { |
|
104 | + /* @var $qb IQueryBuilder */ |
|
105 | + $qb = $this->db->getQueryBuilder(); |
|
106 | + $result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'token', 'type', 'remember', 'last_activity', 'last_check', 'scope', 'expires', 'version') |
|
107 | + ->from('authtoken') |
|
108 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
109 | + ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
110 | + ->execute(); |
|
111 | + |
|
112 | + $data = $result->fetch(); |
|
113 | + $result->closeCursor(); |
|
114 | + if ($data === false) { |
|
115 | + throw new DoesNotExistException('token does not exist'); |
|
116 | + } |
|
117 | + return DefaultToken::fromRow($data); |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Get all tokens of a user |
|
122 | + * |
|
123 | + * The provider may limit the number of result rows in case of an abuse |
|
124 | + * where a high number of (session) tokens is generated |
|
125 | + * |
|
126 | + * @param string $uid |
|
127 | + * @return DefaultToken[] |
|
128 | + */ |
|
129 | + public function getTokenByUser(string $uid): array { |
|
130 | + /* @var $qb IQueryBuilder */ |
|
131 | + $qb = $this->db->getQueryBuilder(); |
|
132 | + $qb->select('id', 'uid', 'login_name', 'password', 'name', 'token', 'type', 'remember', 'last_activity', 'last_check', 'scope', 'expires', 'version') |
|
133 | + ->from('authtoken') |
|
134 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
135 | + ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
136 | + ->setMaxResults(1000); |
|
137 | + $result = $qb->execute(); |
|
138 | + $data = $result->fetchAll(); |
|
139 | + $result->closeCursor(); |
|
140 | + |
|
141 | + $entities = array_map(function ($row) { |
|
142 | + return DefaultToken::fromRow($row); |
|
143 | + }, $data); |
|
144 | + |
|
145 | + return $entities; |
|
146 | + } |
|
147 | + |
|
148 | + public function deleteById(string $uid, int $id) { |
|
149 | + /* @var $qb IQueryBuilder */ |
|
150 | + $qb = $this->db->getQueryBuilder(); |
|
151 | + $qb->delete('authtoken') |
|
152 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
153 | + ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
154 | + ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT))); |
|
155 | + $qb->execute(); |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * delete all auth token which belong to a specific client if the client was deleted |
|
160 | + * |
|
161 | + * @param string $name |
|
162 | + */ |
|
163 | + public function deleteByName(string $name) { |
|
164 | + $qb = $this->db->getQueryBuilder(); |
|
165 | + $qb->delete('authtoken') |
|
166 | + ->where($qb->expr()->eq('name', $qb->createNamedParameter($name), IQueryBuilder::PARAM_STR)) |
|
167 | + ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT))); |
|
168 | + $qb->execute(); |
|
169 | + } |
|
170 | 170 | |
171 | 171 | } |
@@ -41,164 +41,164 @@ |
||
41 | 41 | */ |
42 | 42 | class DefaultToken extends Entity implements IToken { |
43 | 43 | |
44 | - const VERSION = 1; |
|
45 | - |
|
46 | - /** @var string user UID */ |
|
47 | - protected $uid; |
|
48 | - |
|
49 | - /** @var string login name used for generating the token */ |
|
50 | - protected $loginName; |
|
51 | - |
|
52 | - /** @var string encrypted user password */ |
|
53 | - protected $password; |
|
54 | - |
|
55 | - /** @var string token name (e.g. browser/OS) */ |
|
56 | - protected $name; |
|
57 | - |
|
58 | - /** @var string */ |
|
59 | - protected $token; |
|
60 | - |
|
61 | - /** @var int */ |
|
62 | - protected $type; |
|
63 | - |
|
64 | - /** @var int */ |
|
65 | - protected $remember; |
|
66 | - |
|
67 | - /** @var int */ |
|
68 | - protected $lastActivity; |
|
69 | - |
|
70 | - /** @var int */ |
|
71 | - protected $lastCheck; |
|
72 | - |
|
73 | - /** @var string */ |
|
74 | - protected $scope; |
|
75 | - |
|
76 | - /** @var int */ |
|
77 | - protected $expires; |
|
78 | - |
|
79 | - /** @var int */ |
|
80 | - protected $version; |
|
81 | - |
|
82 | - public function __construct() { |
|
83 | - $this->addType('uid', 'string'); |
|
84 | - $this->addType('loginName', 'string'); |
|
85 | - $this->addType('password', 'string'); |
|
86 | - $this->addType('name', 'string'); |
|
87 | - $this->addType('token', 'string'); |
|
88 | - $this->addType('type', 'int'); |
|
89 | - $this->addType('remember', 'int'); |
|
90 | - $this->addType('lastActivity', 'int'); |
|
91 | - $this->addType('lastCheck', 'int'); |
|
92 | - $this->addType('scope', 'string'); |
|
93 | - $this->addType('expires', 'int'); |
|
94 | - $this->addType('version', 'int'); |
|
95 | - } |
|
96 | - |
|
97 | - public function getId(): int { |
|
98 | - return $this->id; |
|
99 | - } |
|
100 | - |
|
101 | - public function getUID(): string { |
|
102 | - return $this->uid; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Get the login name used when generating the token |
|
107 | - * |
|
108 | - * @return string |
|
109 | - */ |
|
110 | - public function getLoginName(): string { |
|
111 | - return parent::getLoginName(); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Get the (encrypted) login password |
|
116 | - * |
|
117 | - * @return string|null |
|
118 | - */ |
|
119 | - public function getPassword() { |
|
120 | - return parent::getPassword(); |
|
121 | - } |
|
122 | - |
|
123 | - public function jsonSerialize() { |
|
124 | - return [ |
|
125 | - 'id' => $this->id, |
|
126 | - 'name' => $this->name, |
|
127 | - 'lastActivity' => $this->lastActivity, |
|
128 | - 'type' => $this->type, |
|
129 | - 'scope' => $this->getScopeAsArray() |
|
130 | - ]; |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Get the timestamp of the last password check |
|
135 | - * |
|
136 | - * @return int |
|
137 | - */ |
|
138 | - public function getLastCheck(): int { |
|
139 | - return parent::getLastCheck(); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Get the timestamp of the last password check |
|
144 | - * |
|
145 | - * @param int $time |
|
146 | - */ |
|
147 | - public function setLastCheck(int $time) { |
|
148 | - parent::setLastCheck($time); |
|
149 | - } |
|
150 | - |
|
151 | - public function getScope(): string { |
|
152 | - $scope = parent::getScope(); |
|
153 | - if ($scope === null) { |
|
154 | - return ''; |
|
155 | - } |
|
156 | - |
|
157 | - return $scope; |
|
158 | - } |
|
159 | - |
|
160 | - public function getScopeAsArray(): array { |
|
161 | - $scope = json_decode($this->getScope(), true); |
|
162 | - if (!$scope) { |
|
163 | - return [ |
|
164 | - 'filesystem'=> true |
|
165 | - ]; |
|
166 | - } |
|
167 | - return $scope; |
|
168 | - } |
|
169 | - |
|
170 | - public function setScope($scope) { |
|
171 | - if (\is_array($scope)) { |
|
172 | - parent::setScope(json_encode($scope)); |
|
173 | - } else { |
|
174 | - parent::setScope((string)$scope); |
|
175 | - } |
|
176 | - } |
|
177 | - |
|
178 | - public function getName(): string { |
|
179 | - return parent::getName(); |
|
180 | - } |
|
181 | - |
|
182 | - public function getRemember(): int { |
|
183 | - return parent::getRemember(); |
|
184 | - } |
|
185 | - |
|
186 | - public function setToken(string $token) { |
|
187 | - parent::setToken($token); |
|
188 | - } |
|
189 | - |
|
190 | - public function setPassword(string $password = null) { |
|
191 | - parent::setPassword($password); |
|
192 | - } |
|
193 | - |
|
194 | - public function setExpires($expires) { |
|
195 | - parent::setExpires($expires); |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * @return int|null |
|
200 | - */ |
|
201 | - public function getExpires() { |
|
202 | - return parent::getExpires(); |
|
203 | - } |
|
44 | + const VERSION = 1; |
|
45 | + |
|
46 | + /** @var string user UID */ |
|
47 | + protected $uid; |
|
48 | + |
|
49 | + /** @var string login name used for generating the token */ |
|
50 | + protected $loginName; |
|
51 | + |
|
52 | + /** @var string encrypted user password */ |
|
53 | + protected $password; |
|
54 | + |
|
55 | + /** @var string token name (e.g. browser/OS) */ |
|
56 | + protected $name; |
|
57 | + |
|
58 | + /** @var string */ |
|
59 | + protected $token; |
|
60 | + |
|
61 | + /** @var int */ |
|
62 | + protected $type; |
|
63 | + |
|
64 | + /** @var int */ |
|
65 | + protected $remember; |
|
66 | + |
|
67 | + /** @var int */ |
|
68 | + protected $lastActivity; |
|
69 | + |
|
70 | + /** @var int */ |
|
71 | + protected $lastCheck; |
|
72 | + |
|
73 | + /** @var string */ |
|
74 | + protected $scope; |
|
75 | + |
|
76 | + /** @var int */ |
|
77 | + protected $expires; |
|
78 | + |
|
79 | + /** @var int */ |
|
80 | + protected $version; |
|
81 | + |
|
82 | + public function __construct() { |
|
83 | + $this->addType('uid', 'string'); |
|
84 | + $this->addType('loginName', 'string'); |
|
85 | + $this->addType('password', 'string'); |
|
86 | + $this->addType('name', 'string'); |
|
87 | + $this->addType('token', 'string'); |
|
88 | + $this->addType('type', 'int'); |
|
89 | + $this->addType('remember', 'int'); |
|
90 | + $this->addType('lastActivity', 'int'); |
|
91 | + $this->addType('lastCheck', 'int'); |
|
92 | + $this->addType('scope', 'string'); |
|
93 | + $this->addType('expires', 'int'); |
|
94 | + $this->addType('version', 'int'); |
|
95 | + } |
|
96 | + |
|
97 | + public function getId(): int { |
|
98 | + return $this->id; |
|
99 | + } |
|
100 | + |
|
101 | + public function getUID(): string { |
|
102 | + return $this->uid; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Get the login name used when generating the token |
|
107 | + * |
|
108 | + * @return string |
|
109 | + */ |
|
110 | + public function getLoginName(): string { |
|
111 | + return parent::getLoginName(); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Get the (encrypted) login password |
|
116 | + * |
|
117 | + * @return string|null |
|
118 | + */ |
|
119 | + public function getPassword() { |
|
120 | + return parent::getPassword(); |
|
121 | + } |
|
122 | + |
|
123 | + public function jsonSerialize() { |
|
124 | + return [ |
|
125 | + 'id' => $this->id, |
|
126 | + 'name' => $this->name, |
|
127 | + 'lastActivity' => $this->lastActivity, |
|
128 | + 'type' => $this->type, |
|
129 | + 'scope' => $this->getScopeAsArray() |
|
130 | + ]; |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Get the timestamp of the last password check |
|
135 | + * |
|
136 | + * @return int |
|
137 | + */ |
|
138 | + public function getLastCheck(): int { |
|
139 | + return parent::getLastCheck(); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Get the timestamp of the last password check |
|
144 | + * |
|
145 | + * @param int $time |
|
146 | + */ |
|
147 | + public function setLastCheck(int $time) { |
|
148 | + parent::setLastCheck($time); |
|
149 | + } |
|
150 | + |
|
151 | + public function getScope(): string { |
|
152 | + $scope = parent::getScope(); |
|
153 | + if ($scope === null) { |
|
154 | + return ''; |
|
155 | + } |
|
156 | + |
|
157 | + return $scope; |
|
158 | + } |
|
159 | + |
|
160 | + public function getScopeAsArray(): array { |
|
161 | + $scope = json_decode($this->getScope(), true); |
|
162 | + if (!$scope) { |
|
163 | + return [ |
|
164 | + 'filesystem'=> true |
|
165 | + ]; |
|
166 | + } |
|
167 | + return $scope; |
|
168 | + } |
|
169 | + |
|
170 | + public function setScope($scope) { |
|
171 | + if (\is_array($scope)) { |
|
172 | + parent::setScope(json_encode($scope)); |
|
173 | + } else { |
|
174 | + parent::setScope((string)$scope); |
|
175 | + } |
|
176 | + } |
|
177 | + |
|
178 | + public function getName(): string { |
|
179 | + return parent::getName(); |
|
180 | + } |
|
181 | + |
|
182 | + public function getRemember(): int { |
|
183 | + return parent::getRemember(); |
|
184 | + } |
|
185 | + |
|
186 | + public function setToken(string $token) { |
|
187 | + parent::setToken($token); |
|
188 | + } |
|
189 | + |
|
190 | + public function setPassword(string $password = null) { |
|
191 | + parent::setPassword($password); |
|
192 | + } |
|
193 | + |
|
194 | + public function setExpires($expires) { |
|
195 | + parent::setExpires($expires); |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * @return int|null |
|
200 | + */ |
|
201 | + public function getExpires() { |
|
202 | + return parent::getExpires(); |
|
203 | + } |
|
204 | 204 | } |
@@ -33,289 +33,289 @@ |
||
33 | 33 | use OCP\Security\ICrypto; |
34 | 34 | |
35 | 35 | class PublicKeyTokenProvider implements IProvider { |
36 | - /** @var PublicKeyTokenMapper */ |
|
37 | - private $mapper; |
|
38 | - |
|
39 | - /** @var ICrypto */ |
|
40 | - private $crypto; |
|
41 | - |
|
42 | - /** @var IConfig */ |
|
43 | - private $config; |
|
44 | - |
|
45 | - /** @var ILogger $logger */ |
|
46 | - private $logger; |
|
47 | - |
|
48 | - /** @var ITimeFactory $time */ |
|
49 | - private $time; |
|
50 | - |
|
51 | - public function __construct(PublicKeyTokenMapper $mapper, |
|
52 | - ICrypto $crypto, |
|
53 | - IConfig $config, |
|
54 | - ILogger $logger, |
|
55 | - ITimeFactory $time) { |
|
56 | - $this->mapper = $mapper; |
|
57 | - $this->crypto = $crypto; |
|
58 | - $this->config = $config; |
|
59 | - $this->logger = $logger; |
|
60 | - $this->time = $time; |
|
61 | - } |
|
62 | - |
|
63 | - public function generateToken(string $token, |
|
64 | - string $uid, |
|
65 | - string $loginName, |
|
66 | - $password, |
|
67 | - string $name, |
|
68 | - int $type = IToken::TEMPORARY_TOKEN, |
|
69 | - int $remember = IToken::DO_NOT_REMEMBER): IToken { |
|
70 | - $dbToken = $this->newToken($token, $uid, $loginName, $password, $name, $type, $remember); |
|
71 | - |
|
72 | - $this->mapper->insert($dbToken); |
|
73 | - |
|
74 | - return $dbToken; |
|
75 | - } |
|
76 | - |
|
77 | - public function getToken(string $tokenId): IToken { |
|
78 | - try { |
|
79 | - $token = $this->mapper->getToken($this->hashToken($tokenId)); |
|
80 | - } catch (DoesNotExistException $ex) { |
|
81 | - throw new InvalidTokenException(); |
|
82 | - } |
|
83 | - |
|
84 | - if ($token->getExpires() !== null && $token->getExpires() < $this->time->getTime()) { |
|
85 | - throw new ExpiredTokenException($token); |
|
86 | - } |
|
87 | - |
|
88 | - return $token; |
|
89 | - } |
|
90 | - |
|
91 | - public function getTokenById(int $tokenId): IToken { |
|
92 | - try { |
|
93 | - $token = $this->mapper->getTokenById($tokenId); |
|
94 | - } catch (DoesNotExistException $ex) { |
|
95 | - throw new InvalidTokenException(); |
|
96 | - } |
|
97 | - |
|
98 | - if ($token->getExpires() !== null && $token->getExpires() < $this->time->getTime()) { |
|
99 | - throw new ExpiredTokenException($token); |
|
100 | - } |
|
101 | - |
|
102 | - return $token; |
|
103 | - } |
|
104 | - |
|
105 | - public function renewSessionToken(string $oldSessionId, string $sessionId) { |
|
106 | - $token = $this->getToken($oldSessionId); |
|
107 | - |
|
108 | - if (!($token instanceof PublicKeyToken)) { |
|
109 | - throw new InvalidTokenException(); |
|
110 | - } |
|
111 | - |
|
112 | - $password = null; |
|
113 | - if (!is_null($token->getPassword())) { |
|
114 | - $privateKey = $this->decrypt($token->getPrivateKey(), $oldSessionId); |
|
115 | - $password = $this->decryptPassword($token->getPassword(), $privateKey); |
|
116 | - } |
|
117 | - |
|
118 | - $this->generateToken( |
|
119 | - $sessionId, |
|
120 | - $token->getUID(), |
|
121 | - $token->getLoginName(), |
|
122 | - $password, |
|
123 | - $token->getName(), |
|
124 | - IToken::TEMPORARY_TOKEN, |
|
125 | - $token->getRemember() |
|
126 | - ); |
|
127 | - |
|
128 | - $this->mapper->delete($token); |
|
129 | - } |
|
130 | - |
|
131 | - public function invalidateToken(string $token) { |
|
132 | - $this->mapper->invalidate($this->hashToken($token)); |
|
133 | - } |
|
134 | - |
|
135 | - public function invalidateTokenById(string $uid, int $id) { |
|
136 | - $this->mapper->deleteById($uid, $id); |
|
137 | - } |
|
138 | - |
|
139 | - public function invalidateOldTokens() { |
|
140 | - $olderThan = $this->time->getTime() - (int) $this->config->getSystemValue('session_lifetime', 60 * 60 * 24); |
|
141 | - $this->logger->debug('Invalidating session tokens older than ' . date('c', $olderThan), ['app' => 'cron']); |
|
142 | - $this->mapper->invalidateOld($olderThan, IToken::DO_NOT_REMEMBER); |
|
143 | - $rememberThreshold = $this->time->getTime() - (int) $this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15); |
|
144 | - $this->logger->debug('Invalidating remembered session tokens older than ' . date('c', $rememberThreshold), ['app' => 'cron']); |
|
145 | - $this->mapper->invalidateOld($rememberThreshold, IToken::REMEMBER); |
|
146 | - } |
|
147 | - |
|
148 | - public function updateToken(IToken $token) { |
|
149 | - if (!($token instanceof PublicKeyToken)) { |
|
150 | - throw new InvalidTokenException(); |
|
151 | - } |
|
152 | - $this->mapper->update($token); |
|
153 | - } |
|
154 | - |
|
155 | - public function updateTokenActivity(IToken $token) { |
|
156 | - if (!($token instanceof PublicKeyToken)) { |
|
157 | - throw new InvalidTokenException(); |
|
158 | - } |
|
159 | - /** @var DefaultToken $token */ |
|
160 | - $now = $this->time->getTime(); |
|
161 | - if ($token->getLastActivity() < ($now - 60)) { |
|
162 | - // Update token only once per minute |
|
163 | - $token->setLastActivity($now); |
|
164 | - $this->mapper->update($token); |
|
165 | - } |
|
166 | - } |
|
167 | - |
|
168 | - public function getTokenByUser(string $uid): array { |
|
169 | - return $this->mapper->getTokenByUser($uid); |
|
170 | - } |
|
171 | - |
|
172 | - public function getPassword(IToken $token, string $tokenId): string { |
|
173 | - if (!($token instanceof PublicKeyToken)) { |
|
174 | - throw new InvalidTokenException(); |
|
175 | - } |
|
176 | - |
|
177 | - if ($token->getPassword() === null) { |
|
178 | - throw new PasswordlessTokenException(); |
|
179 | - } |
|
180 | - |
|
181 | - // Decrypt private key with tokenId |
|
182 | - $privateKey = $this->decrypt($token->getPrivateKey(), $tokenId); |
|
183 | - |
|
184 | - // Decrypt password with private key |
|
185 | - return $this->decryptPassword($token->getPassword(), $privateKey); |
|
186 | - } |
|
187 | - |
|
188 | - public function setPassword(IToken $token, string $tokenId, string $password) { |
|
189 | - if (!($token instanceof PublicKeyToken)) { |
|
190 | - throw new InvalidTokenException(); |
|
191 | - } |
|
192 | - |
|
193 | - // When changing passwords all temp tokens are deleted |
|
194 | - $this->mapper->deleteTempToken($token); |
|
195 | - |
|
196 | - // Update the password for all tokens |
|
197 | - $tokens = $this->mapper->getTokenByUser($token->getUID()); |
|
198 | - foreach ($tokens as $t) { |
|
199 | - $publicKey = $t->getPublicKey(); |
|
200 | - $t->setPassword($this->encryptPassword($password, $publicKey)); |
|
201 | - $this->updateToken($t); |
|
202 | - } |
|
203 | - } |
|
204 | - |
|
205 | - public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken { |
|
206 | - if (!($token instanceof PublicKeyToken)) { |
|
207 | - throw new InvalidTokenException(); |
|
208 | - } |
|
209 | - |
|
210 | - // Decrypt private key with oldTokenId |
|
211 | - $privateKey = $this->decrypt($token->getPrivateKey(), $oldTokenId); |
|
212 | - // Encrypt with the new token |
|
213 | - $token->setPrivateKey($this->encrypt($privateKey, $newTokenId)); |
|
214 | - |
|
215 | - $token->setToken($this->hashToken($newTokenId)); |
|
216 | - $this->updateToken($token); |
|
217 | - |
|
218 | - return $token; |
|
219 | - } |
|
220 | - |
|
221 | - private function encrypt(string $plaintext, string $token): string { |
|
222 | - $secret = $this->config->getSystemValue('secret'); |
|
223 | - return $this->crypto->encrypt($plaintext, $token . $secret); |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * @throws InvalidTokenException |
|
228 | - */ |
|
229 | - private function decrypt(string $cipherText, string $token): string { |
|
230 | - $secret = $this->config->getSystemValue('secret'); |
|
231 | - try { |
|
232 | - return $this->crypto->decrypt($cipherText, $token . $secret); |
|
233 | - } catch (\Exception $ex) { |
|
234 | - // Delete the invalid token |
|
235 | - $this->invalidateToken($token); |
|
236 | - throw new InvalidTokenException(); |
|
237 | - } |
|
238 | - } |
|
239 | - |
|
240 | - private function encryptPassword(string $password, string $publicKey): string { |
|
241 | - openssl_public_encrypt($password, $encryptedPassword, $publicKey, OPENSSL_PKCS1_OAEP_PADDING); |
|
242 | - $encryptedPassword = base64_encode($encryptedPassword); |
|
243 | - |
|
244 | - return $encryptedPassword; |
|
245 | - } |
|
246 | - |
|
247 | - private function decryptPassword(string $encryptedPassword, string $privateKey): string { |
|
248 | - $encryptedPassword = base64_decode($encryptedPassword); |
|
249 | - openssl_private_decrypt($encryptedPassword, $password, $privateKey, OPENSSL_PKCS1_OAEP_PADDING); |
|
250 | - |
|
251 | - return $password; |
|
252 | - } |
|
253 | - |
|
254 | - private function hashToken(string $token): string { |
|
255 | - $secret = $this->config->getSystemValue('secret'); |
|
256 | - return hash('sha512', $token . $secret); |
|
257 | - } |
|
258 | - |
|
259 | - /** |
|
260 | - * Convert a DefaultToken to a publicKeyToken |
|
261 | - * This will also be updated directly in the Database |
|
262 | - */ |
|
263 | - public function convertToken(DefaultToken $defaultToken, string $token, $password): PublicKeyToken { |
|
264 | - $pkToken = $this->newToken( |
|
265 | - $token, |
|
266 | - $defaultToken->getUID(), |
|
267 | - $defaultToken->getLoginName(), |
|
268 | - $password, |
|
269 | - $defaultToken->getName(), |
|
270 | - $defaultToken->getType(), |
|
271 | - $defaultToken->getRemember() |
|
272 | - ); |
|
273 | - |
|
274 | - $pkToken->setExpires($defaultToken->getExpires()); |
|
275 | - $pkToken->setId($defaultToken->getId()); |
|
276 | - |
|
277 | - return $this->mapper->update($pkToken); |
|
278 | - } |
|
279 | - |
|
280 | - private function newToken(string $token, |
|
281 | - string $uid, |
|
282 | - string $loginName, |
|
283 | - $password, |
|
284 | - string $name, |
|
285 | - int $type, |
|
286 | - int $remember): PublicKeyToken { |
|
287 | - $dbToken = new PublicKeyToken(); |
|
288 | - $dbToken->setUid($uid); |
|
289 | - $dbToken->setLoginName($loginName); |
|
290 | - |
|
291 | - $config = [ |
|
292 | - 'digest_alg' => 'sha512', |
|
293 | - 'private_key_bits' => 2048, |
|
294 | - ]; |
|
295 | - |
|
296 | - // Generate new key |
|
297 | - $res = openssl_pkey_new($config); |
|
298 | - openssl_pkey_export($res, $privateKey); |
|
299 | - |
|
300 | - // Extract the public key from $res to $pubKey |
|
301 | - $publicKey = openssl_pkey_get_details($res); |
|
302 | - $publicKey = $publicKey['key']; |
|
303 | - |
|
304 | - $dbToken->setPublicKey($publicKey); |
|
305 | - $dbToken->setPrivateKey($this->encrypt($privateKey, $token)); |
|
306 | - |
|
307 | - if (!is_null($password)) { |
|
308 | - $dbToken->setPassword($this->encryptPassword($password, $publicKey)); |
|
309 | - } |
|
310 | - |
|
311 | - $dbToken->setName($name); |
|
312 | - $dbToken->setToken($this->hashToken($token)); |
|
313 | - $dbToken->setType($type); |
|
314 | - $dbToken->setRemember($remember); |
|
315 | - $dbToken->setLastActivity($this->time->getTime()); |
|
316 | - $dbToken->setLastCheck($this->time->getTime()); |
|
317 | - $dbToken->setVersion(PublicKeyToken::VERSION); |
|
318 | - |
|
319 | - return $dbToken; |
|
320 | - } |
|
36 | + /** @var PublicKeyTokenMapper */ |
|
37 | + private $mapper; |
|
38 | + |
|
39 | + /** @var ICrypto */ |
|
40 | + private $crypto; |
|
41 | + |
|
42 | + /** @var IConfig */ |
|
43 | + private $config; |
|
44 | + |
|
45 | + /** @var ILogger $logger */ |
|
46 | + private $logger; |
|
47 | + |
|
48 | + /** @var ITimeFactory $time */ |
|
49 | + private $time; |
|
50 | + |
|
51 | + public function __construct(PublicKeyTokenMapper $mapper, |
|
52 | + ICrypto $crypto, |
|
53 | + IConfig $config, |
|
54 | + ILogger $logger, |
|
55 | + ITimeFactory $time) { |
|
56 | + $this->mapper = $mapper; |
|
57 | + $this->crypto = $crypto; |
|
58 | + $this->config = $config; |
|
59 | + $this->logger = $logger; |
|
60 | + $this->time = $time; |
|
61 | + } |
|
62 | + |
|
63 | + public function generateToken(string $token, |
|
64 | + string $uid, |
|
65 | + string $loginName, |
|
66 | + $password, |
|
67 | + string $name, |
|
68 | + int $type = IToken::TEMPORARY_TOKEN, |
|
69 | + int $remember = IToken::DO_NOT_REMEMBER): IToken { |
|
70 | + $dbToken = $this->newToken($token, $uid, $loginName, $password, $name, $type, $remember); |
|
71 | + |
|
72 | + $this->mapper->insert($dbToken); |
|
73 | + |
|
74 | + return $dbToken; |
|
75 | + } |
|
76 | + |
|
77 | + public function getToken(string $tokenId): IToken { |
|
78 | + try { |
|
79 | + $token = $this->mapper->getToken($this->hashToken($tokenId)); |
|
80 | + } catch (DoesNotExistException $ex) { |
|
81 | + throw new InvalidTokenException(); |
|
82 | + } |
|
83 | + |
|
84 | + if ($token->getExpires() !== null && $token->getExpires() < $this->time->getTime()) { |
|
85 | + throw new ExpiredTokenException($token); |
|
86 | + } |
|
87 | + |
|
88 | + return $token; |
|
89 | + } |
|
90 | + |
|
91 | + public function getTokenById(int $tokenId): IToken { |
|
92 | + try { |
|
93 | + $token = $this->mapper->getTokenById($tokenId); |
|
94 | + } catch (DoesNotExistException $ex) { |
|
95 | + throw new InvalidTokenException(); |
|
96 | + } |
|
97 | + |
|
98 | + if ($token->getExpires() !== null && $token->getExpires() < $this->time->getTime()) { |
|
99 | + throw new ExpiredTokenException($token); |
|
100 | + } |
|
101 | + |
|
102 | + return $token; |
|
103 | + } |
|
104 | + |
|
105 | + public function renewSessionToken(string $oldSessionId, string $sessionId) { |
|
106 | + $token = $this->getToken($oldSessionId); |
|
107 | + |
|
108 | + if (!($token instanceof PublicKeyToken)) { |
|
109 | + throw new InvalidTokenException(); |
|
110 | + } |
|
111 | + |
|
112 | + $password = null; |
|
113 | + if (!is_null($token->getPassword())) { |
|
114 | + $privateKey = $this->decrypt($token->getPrivateKey(), $oldSessionId); |
|
115 | + $password = $this->decryptPassword($token->getPassword(), $privateKey); |
|
116 | + } |
|
117 | + |
|
118 | + $this->generateToken( |
|
119 | + $sessionId, |
|
120 | + $token->getUID(), |
|
121 | + $token->getLoginName(), |
|
122 | + $password, |
|
123 | + $token->getName(), |
|
124 | + IToken::TEMPORARY_TOKEN, |
|
125 | + $token->getRemember() |
|
126 | + ); |
|
127 | + |
|
128 | + $this->mapper->delete($token); |
|
129 | + } |
|
130 | + |
|
131 | + public function invalidateToken(string $token) { |
|
132 | + $this->mapper->invalidate($this->hashToken($token)); |
|
133 | + } |
|
134 | + |
|
135 | + public function invalidateTokenById(string $uid, int $id) { |
|
136 | + $this->mapper->deleteById($uid, $id); |
|
137 | + } |
|
138 | + |
|
139 | + public function invalidateOldTokens() { |
|
140 | + $olderThan = $this->time->getTime() - (int) $this->config->getSystemValue('session_lifetime', 60 * 60 * 24); |
|
141 | + $this->logger->debug('Invalidating session tokens older than ' . date('c', $olderThan), ['app' => 'cron']); |
|
142 | + $this->mapper->invalidateOld($olderThan, IToken::DO_NOT_REMEMBER); |
|
143 | + $rememberThreshold = $this->time->getTime() - (int) $this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15); |
|
144 | + $this->logger->debug('Invalidating remembered session tokens older than ' . date('c', $rememberThreshold), ['app' => 'cron']); |
|
145 | + $this->mapper->invalidateOld($rememberThreshold, IToken::REMEMBER); |
|
146 | + } |
|
147 | + |
|
148 | + public function updateToken(IToken $token) { |
|
149 | + if (!($token instanceof PublicKeyToken)) { |
|
150 | + throw new InvalidTokenException(); |
|
151 | + } |
|
152 | + $this->mapper->update($token); |
|
153 | + } |
|
154 | + |
|
155 | + public function updateTokenActivity(IToken $token) { |
|
156 | + if (!($token instanceof PublicKeyToken)) { |
|
157 | + throw new InvalidTokenException(); |
|
158 | + } |
|
159 | + /** @var DefaultToken $token */ |
|
160 | + $now = $this->time->getTime(); |
|
161 | + if ($token->getLastActivity() < ($now - 60)) { |
|
162 | + // Update token only once per minute |
|
163 | + $token->setLastActivity($now); |
|
164 | + $this->mapper->update($token); |
|
165 | + } |
|
166 | + } |
|
167 | + |
|
168 | + public function getTokenByUser(string $uid): array { |
|
169 | + return $this->mapper->getTokenByUser($uid); |
|
170 | + } |
|
171 | + |
|
172 | + public function getPassword(IToken $token, string $tokenId): string { |
|
173 | + if (!($token instanceof PublicKeyToken)) { |
|
174 | + throw new InvalidTokenException(); |
|
175 | + } |
|
176 | + |
|
177 | + if ($token->getPassword() === null) { |
|
178 | + throw new PasswordlessTokenException(); |
|
179 | + } |
|
180 | + |
|
181 | + // Decrypt private key with tokenId |
|
182 | + $privateKey = $this->decrypt($token->getPrivateKey(), $tokenId); |
|
183 | + |
|
184 | + // Decrypt password with private key |
|
185 | + return $this->decryptPassword($token->getPassword(), $privateKey); |
|
186 | + } |
|
187 | + |
|
188 | + public function setPassword(IToken $token, string $tokenId, string $password) { |
|
189 | + if (!($token instanceof PublicKeyToken)) { |
|
190 | + throw new InvalidTokenException(); |
|
191 | + } |
|
192 | + |
|
193 | + // When changing passwords all temp tokens are deleted |
|
194 | + $this->mapper->deleteTempToken($token); |
|
195 | + |
|
196 | + // Update the password for all tokens |
|
197 | + $tokens = $this->mapper->getTokenByUser($token->getUID()); |
|
198 | + foreach ($tokens as $t) { |
|
199 | + $publicKey = $t->getPublicKey(); |
|
200 | + $t->setPassword($this->encryptPassword($password, $publicKey)); |
|
201 | + $this->updateToken($t); |
|
202 | + } |
|
203 | + } |
|
204 | + |
|
205 | + public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken { |
|
206 | + if (!($token instanceof PublicKeyToken)) { |
|
207 | + throw new InvalidTokenException(); |
|
208 | + } |
|
209 | + |
|
210 | + // Decrypt private key with oldTokenId |
|
211 | + $privateKey = $this->decrypt($token->getPrivateKey(), $oldTokenId); |
|
212 | + // Encrypt with the new token |
|
213 | + $token->setPrivateKey($this->encrypt($privateKey, $newTokenId)); |
|
214 | + |
|
215 | + $token->setToken($this->hashToken($newTokenId)); |
|
216 | + $this->updateToken($token); |
|
217 | + |
|
218 | + return $token; |
|
219 | + } |
|
220 | + |
|
221 | + private function encrypt(string $plaintext, string $token): string { |
|
222 | + $secret = $this->config->getSystemValue('secret'); |
|
223 | + return $this->crypto->encrypt($plaintext, $token . $secret); |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * @throws InvalidTokenException |
|
228 | + */ |
|
229 | + private function decrypt(string $cipherText, string $token): string { |
|
230 | + $secret = $this->config->getSystemValue('secret'); |
|
231 | + try { |
|
232 | + return $this->crypto->decrypt($cipherText, $token . $secret); |
|
233 | + } catch (\Exception $ex) { |
|
234 | + // Delete the invalid token |
|
235 | + $this->invalidateToken($token); |
|
236 | + throw new InvalidTokenException(); |
|
237 | + } |
|
238 | + } |
|
239 | + |
|
240 | + private function encryptPassword(string $password, string $publicKey): string { |
|
241 | + openssl_public_encrypt($password, $encryptedPassword, $publicKey, OPENSSL_PKCS1_OAEP_PADDING); |
|
242 | + $encryptedPassword = base64_encode($encryptedPassword); |
|
243 | + |
|
244 | + return $encryptedPassword; |
|
245 | + } |
|
246 | + |
|
247 | + private function decryptPassword(string $encryptedPassword, string $privateKey): string { |
|
248 | + $encryptedPassword = base64_decode($encryptedPassword); |
|
249 | + openssl_private_decrypt($encryptedPassword, $password, $privateKey, OPENSSL_PKCS1_OAEP_PADDING); |
|
250 | + |
|
251 | + return $password; |
|
252 | + } |
|
253 | + |
|
254 | + private function hashToken(string $token): string { |
|
255 | + $secret = $this->config->getSystemValue('secret'); |
|
256 | + return hash('sha512', $token . $secret); |
|
257 | + } |
|
258 | + |
|
259 | + /** |
|
260 | + * Convert a DefaultToken to a publicKeyToken |
|
261 | + * This will also be updated directly in the Database |
|
262 | + */ |
|
263 | + public function convertToken(DefaultToken $defaultToken, string $token, $password): PublicKeyToken { |
|
264 | + $pkToken = $this->newToken( |
|
265 | + $token, |
|
266 | + $defaultToken->getUID(), |
|
267 | + $defaultToken->getLoginName(), |
|
268 | + $password, |
|
269 | + $defaultToken->getName(), |
|
270 | + $defaultToken->getType(), |
|
271 | + $defaultToken->getRemember() |
|
272 | + ); |
|
273 | + |
|
274 | + $pkToken->setExpires($defaultToken->getExpires()); |
|
275 | + $pkToken->setId($defaultToken->getId()); |
|
276 | + |
|
277 | + return $this->mapper->update($pkToken); |
|
278 | + } |
|
279 | + |
|
280 | + private function newToken(string $token, |
|
281 | + string $uid, |
|
282 | + string $loginName, |
|
283 | + $password, |
|
284 | + string $name, |
|
285 | + int $type, |
|
286 | + int $remember): PublicKeyToken { |
|
287 | + $dbToken = new PublicKeyToken(); |
|
288 | + $dbToken->setUid($uid); |
|
289 | + $dbToken->setLoginName($loginName); |
|
290 | + |
|
291 | + $config = [ |
|
292 | + 'digest_alg' => 'sha512', |
|
293 | + 'private_key_bits' => 2048, |
|
294 | + ]; |
|
295 | + |
|
296 | + // Generate new key |
|
297 | + $res = openssl_pkey_new($config); |
|
298 | + openssl_pkey_export($res, $privateKey); |
|
299 | + |
|
300 | + // Extract the public key from $res to $pubKey |
|
301 | + $publicKey = openssl_pkey_get_details($res); |
|
302 | + $publicKey = $publicKey['key']; |
|
303 | + |
|
304 | + $dbToken->setPublicKey($publicKey); |
|
305 | + $dbToken->setPrivateKey($this->encrypt($privateKey, $token)); |
|
306 | + |
|
307 | + if (!is_null($password)) { |
|
308 | + $dbToken->setPassword($this->encryptPassword($password, $publicKey)); |
|
309 | + } |
|
310 | + |
|
311 | + $dbToken->setName($name); |
|
312 | + $dbToken->setToken($this->hashToken($token)); |
|
313 | + $dbToken->setType($type); |
|
314 | + $dbToken->setRemember($remember); |
|
315 | + $dbToken->setLastActivity($this->time->getTime()); |
|
316 | + $dbToken->setLastCheck($this->time->getTime()); |
|
317 | + $dbToken->setVersion(PublicKeyToken::VERSION); |
|
318 | + |
|
319 | + return $dbToken; |
|
320 | + } |
|
321 | 321 | } |
@@ -46,172 +46,172 @@ |
||
46 | 46 | */ |
47 | 47 | class PublicKeyToken extends Entity implements IToken { |
48 | 48 | |
49 | - const VERSION = 2; |
|
50 | - |
|
51 | - /** @var string user UID */ |
|
52 | - protected $uid; |
|
53 | - |
|
54 | - /** @var string login name used for generating the token */ |
|
55 | - protected $loginName; |
|
56 | - |
|
57 | - /** @var string encrypted user password */ |
|
58 | - protected $password; |
|
59 | - |
|
60 | - /** @var string token name (e.g. browser/OS) */ |
|
61 | - protected $name; |
|
62 | - |
|
63 | - /** @var string */ |
|
64 | - protected $token; |
|
65 | - |
|
66 | - /** @var int */ |
|
67 | - protected $type; |
|
68 | - |
|
69 | - /** @var int */ |
|
70 | - protected $remember; |
|
71 | - |
|
72 | - /** @var int */ |
|
73 | - protected $lastActivity; |
|
74 | - |
|
75 | - /** @var int */ |
|
76 | - protected $lastCheck; |
|
77 | - |
|
78 | - /** @var string */ |
|
79 | - protected $scope; |
|
80 | - |
|
81 | - /** @var int */ |
|
82 | - protected $expires; |
|
83 | - |
|
84 | - /** @var string */ |
|
85 | - protected $privateKey; |
|
86 | - |
|
87 | - /** @var string */ |
|
88 | - protected $publicKey; |
|
89 | - |
|
90 | - /** @var int */ |
|
91 | - protected $version; |
|
92 | - |
|
93 | - public function __construct() { |
|
94 | - $this->addType('uid', 'string'); |
|
95 | - $this->addType('loginName', 'string'); |
|
96 | - $this->addType('password', 'string'); |
|
97 | - $this->addType('name', 'string'); |
|
98 | - $this->addType('token', 'string'); |
|
99 | - $this->addType('type', 'int'); |
|
100 | - $this->addType('remember', 'int'); |
|
101 | - $this->addType('lastActivity', 'int'); |
|
102 | - $this->addType('lastCheck', 'int'); |
|
103 | - $this->addType('scope', 'string'); |
|
104 | - $this->addType('expires', 'int'); |
|
105 | - $this->addType('publicKey', 'string'); |
|
106 | - $this->addType('privateKey', 'string'); |
|
107 | - $this->addType('version', 'int'); |
|
108 | - } |
|
109 | - |
|
110 | - public function getId(): int { |
|
111 | - return $this->id; |
|
112 | - } |
|
113 | - |
|
114 | - public function getUID(): string { |
|
115 | - return $this->uid; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Get the login name used when generating the token |
|
120 | - * |
|
121 | - * @return string |
|
122 | - */ |
|
123 | - public function getLoginName(): string { |
|
124 | - return parent::getLoginName(); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Get the (encrypted) login password |
|
129 | - * |
|
130 | - * @return string|null |
|
131 | - */ |
|
132 | - public function getPassword() { |
|
133 | - return parent::getPassword(); |
|
134 | - } |
|
135 | - |
|
136 | - public function jsonSerialize() { |
|
137 | - return [ |
|
138 | - 'id' => $this->id, |
|
139 | - 'name' => $this->name, |
|
140 | - 'lastActivity' => $this->lastActivity, |
|
141 | - 'type' => $this->type, |
|
142 | - 'scope' => $this->getScopeAsArray() |
|
143 | - ]; |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Get the timestamp of the last password check |
|
148 | - * |
|
149 | - * @return int |
|
150 | - */ |
|
151 | - public function getLastCheck(): int { |
|
152 | - return parent::getLastCheck(); |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * Get the timestamp of the last password check |
|
157 | - * |
|
158 | - * @param int $time |
|
159 | - */ |
|
160 | - public function setLastCheck(int $time) { |
|
161 | - parent::setLastCheck($time); |
|
162 | - } |
|
163 | - |
|
164 | - public function getScope(): string { |
|
165 | - $scope = parent::getScope(); |
|
166 | - if ($scope === null) { |
|
167 | - return ''; |
|
168 | - } |
|
169 | - |
|
170 | - return $scope; |
|
171 | - } |
|
172 | - |
|
173 | - public function getScopeAsArray(): array { |
|
174 | - $scope = json_decode($this->getScope(), true); |
|
175 | - if (!$scope) { |
|
176 | - return [ |
|
177 | - 'filesystem'=> true |
|
178 | - ]; |
|
179 | - } |
|
180 | - return $scope; |
|
181 | - } |
|
182 | - |
|
183 | - public function setScope($scope) { |
|
184 | - if (is_array($scope)) { |
|
185 | - parent::setScope(json_encode($scope)); |
|
186 | - } else { |
|
187 | - parent::setScope((string)$scope); |
|
188 | - } |
|
189 | - } |
|
190 | - |
|
191 | - public function getName(): string { |
|
192 | - return parent::getName(); |
|
193 | - } |
|
194 | - |
|
195 | - public function getRemember(): int { |
|
196 | - return parent::getRemember(); |
|
197 | - } |
|
198 | - |
|
199 | - public function setToken(string $token) { |
|
200 | - parent::setToken($token); |
|
201 | - } |
|
202 | - |
|
203 | - public function setPassword(string $password = null) { |
|
204 | - parent::setPassword($password); |
|
205 | - } |
|
206 | - |
|
207 | - public function setExpires($expires) { |
|
208 | - parent::setExpires($expires); |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * @return int|null |
|
213 | - */ |
|
214 | - public function getExpires() { |
|
215 | - return parent::getExpires(); |
|
216 | - } |
|
49 | + const VERSION = 2; |
|
50 | + |
|
51 | + /** @var string user UID */ |
|
52 | + protected $uid; |
|
53 | + |
|
54 | + /** @var string login name used for generating the token */ |
|
55 | + protected $loginName; |
|
56 | + |
|
57 | + /** @var string encrypted user password */ |
|
58 | + protected $password; |
|
59 | + |
|
60 | + /** @var string token name (e.g. browser/OS) */ |
|
61 | + protected $name; |
|
62 | + |
|
63 | + /** @var string */ |
|
64 | + protected $token; |
|
65 | + |
|
66 | + /** @var int */ |
|
67 | + protected $type; |
|
68 | + |
|
69 | + /** @var int */ |
|
70 | + protected $remember; |
|
71 | + |
|
72 | + /** @var int */ |
|
73 | + protected $lastActivity; |
|
74 | + |
|
75 | + /** @var int */ |
|
76 | + protected $lastCheck; |
|
77 | + |
|
78 | + /** @var string */ |
|
79 | + protected $scope; |
|
80 | + |
|
81 | + /** @var int */ |
|
82 | + protected $expires; |
|
83 | + |
|
84 | + /** @var string */ |
|
85 | + protected $privateKey; |
|
86 | + |
|
87 | + /** @var string */ |
|
88 | + protected $publicKey; |
|
89 | + |
|
90 | + /** @var int */ |
|
91 | + protected $version; |
|
92 | + |
|
93 | + public function __construct() { |
|
94 | + $this->addType('uid', 'string'); |
|
95 | + $this->addType('loginName', 'string'); |
|
96 | + $this->addType('password', 'string'); |
|
97 | + $this->addType('name', 'string'); |
|
98 | + $this->addType('token', 'string'); |
|
99 | + $this->addType('type', 'int'); |
|
100 | + $this->addType('remember', 'int'); |
|
101 | + $this->addType('lastActivity', 'int'); |
|
102 | + $this->addType('lastCheck', 'int'); |
|
103 | + $this->addType('scope', 'string'); |
|
104 | + $this->addType('expires', 'int'); |
|
105 | + $this->addType('publicKey', 'string'); |
|
106 | + $this->addType('privateKey', 'string'); |
|
107 | + $this->addType('version', 'int'); |
|
108 | + } |
|
109 | + |
|
110 | + public function getId(): int { |
|
111 | + return $this->id; |
|
112 | + } |
|
113 | + |
|
114 | + public function getUID(): string { |
|
115 | + return $this->uid; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Get the login name used when generating the token |
|
120 | + * |
|
121 | + * @return string |
|
122 | + */ |
|
123 | + public function getLoginName(): string { |
|
124 | + return parent::getLoginName(); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Get the (encrypted) login password |
|
129 | + * |
|
130 | + * @return string|null |
|
131 | + */ |
|
132 | + public function getPassword() { |
|
133 | + return parent::getPassword(); |
|
134 | + } |
|
135 | + |
|
136 | + public function jsonSerialize() { |
|
137 | + return [ |
|
138 | + 'id' => $this->id, |
|
139 | + 'name' => $this->name, |
|
140 | + 'lastActivity' => $this->lastActivity, |
|
141 | + 'type' => $this->type, |
|
142 | + 'scope' => $this->getScopeAsArray() |
|
143 | + ]; |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Get the timestamp of the last password check |
|
148 | + * |
|
149 | + * @return int |
|
150 | + */ |
|
151 | + public function getLastCheck(): int { |
|
152 | + return parent::getLastCheck(); |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * Get the timestamp of the last password check |
|
157 | + * |
|
158 | + * @param int $time |
|
159 | + */ |
|
160 | + public function setLastCheck(int $time) { |
|
161 | + parent::setLastCheck($time); |
|
162 | + } |
|
163 | + |
|
164 | + public function getScope(): string { |
|
165 | + $scope = parent::getScope(); |
|
166 | + if ($scope === null) { |
|
167 | + return ''; |
|
168 | + } |
|
169 | + |
|
170 | + return $scope; |
|
171 | + } |
|
172 | + |
|
173 | + public function getScopeAsArray(): array { |
|
174 | + $scope = json_decode($this->getScope(), true); |
|
175 | + if (!$scope) { |
|
176 | + return [ |
|
177 | + 'filesystem'=> true |
|
178 | + ]; |
|
179 | + } |
|
180 | + return $scope; |
|
181 | + } |
|
182 | + |
|
183 | + public function setScope($scope) { |
|
184 | + if (is_array($scope)) { |
|
185 | + parent::setScope(json_encode($scope)); |
|
186 | + } else { |
|
187 | + parent::setScope((string)$scope); |
|
188 | + } |
|
189 | + } |
|
190 | + |
|
191 | + public function getName(): string { |
|
192 | + return parent::getName(); |
|
193 | + } |
|
194 | + |
|
195 | + public function getRemember(): int { |
|
196 | + return parent::getRemember(); |
|
197 | + } |
|
198 | + |
|
199 | + public function setToken(string $token) { |
|
200 | + parent::setToken($token); |
|
201 | + } |
|
202 | + |
|
203 | + public function setPassword(string $password = null) { |
|
204 | + parent::setPassword($password); |
|
205 | + } |
|
206 | + |
|
207 | + public function setExpires($expires) { |
|
208 | + parent::setExpires($expires); |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * @return int|null |
|
213 | + */ |
|
214 | + public function getExpires() { |
|
215 | + return parent::getExpires(); |
|
216 | + } |
|
217 | 217 | } |
@@ -39,303 +39,303 @@ |
||
39 | 39 | |
40 | 40 | class DefaultTokenProvider implements IProvider { |
41 | 41 | |
42 | - /** @var DefaultTokenMapper */ |
|
43 | - private $mapper; |
|
44 | - |
|
45 | - /** @var ICrypto */ |
|
46 | - private $crypto; |
|
47 | - |
|
48 | - /** @var IConfig */ |
|
49 | - private $config; |
|
50 | - |
|
51 | - /** @var ILogger $logger */ |
|
52 | - private $logger; |
|
53 | - |
|
54 | - /** @var ITimeFactory $time */ |
|
55 | - private $time; |
|
56 | - |
|
57 | - /** |
|
58 | - * @param DefaultTokenMapper $mapper |
|
59 | - * @param ICrypto $crypto |
|
60 | - * @param IConfig $config |
|
61 | - * @param ILogger $logger |
|
62 | - * @param ITimeFactory $time |
|
63 | - */ |
|
64 | - public function __construct(DefaultTokenMapper $mapper, |
|
65 | - ICrypto $crypto, |
|
66 | - IConfig $config, |
|
67 | - ILogger $logger, |
|
68 | - ITimeFactory $time) { |
|
69 | - $this->mapper = $mapper; |
|
70 | - $this->crypto = $crypto; |
|
71 | - $this->config = $config; |
|
72 | - $this->logger = $logger; |
|
73 | - $this->time = $time; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Create and persist a new token |
|
78 | - * |
|
79 | - * @param string $token |
|
80 | - * @param string $uid |
|
81 | - * @param string $loginName |
|
82 | - * @param string|null $password |
|
83 | - * @param string $name |
|
84 | - * @param int $type token type |
|
85 | - * @param int $remember whether the session token should be used for remember-me |
|
86 | - * @return IToken |
|
87 | - */ |
|
88 | - public function generateToken(string $token, |
|
89 | - string $uid, |
|
90 | - string $loginName, |
|
91 | - $password, |
|
92 | - string $name, |
|
93 | - int $type = IToken::TEMPORARY_TOKEN, |
|
94 | - int $remember = IToken::DO_NOT_REMEMBER): IToken { |
|
95 | - $dbToken = new DefaultToken(); |
|
96 | - $dbToken->setUid($uid); |
|
97 | - $dbToken->setLoginName($loginName); |
|
98 | - if (!is_null($password)) { |
|
99 | - $dbToken->setPassword($this->encryptPassword($password, $token)); |
|
100 | - } |
|
101 | - $dbToken->setName($name); |
|
102 | - $dbToken->setToken($this->hashToken($token)); |
|
103 | - $dbToken->setType($type); |
|
104 | - $dbToken->setRemember($remember); |
|
105 | - $dbToken->setLastActivity($this->time->getTime()); |
|
106 | - $dbToken->setLastCheck($this->time->getTime()); |
|
107 | - $dbToken->setVersion(DefaultToken::VERSION); |
|
108 | - |
|
109 | - $this->mapper->insert($dbToken); |
|
110 | - |
|
111 | - return $dbToken; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Save the updated token |
|
116 | - * |
|
117 | - * @param IToken $token |
|
118 | - * @throws InvalidTokenException |
|
119 | - */ |
|
120 | - public function updateToken(IToken $token) { |
|
121 | - if (!($token instanceof DefaultToken)) { |
|
122 | - throw new InvalidTokenException(); |
|
123 | - } |
|
124 | - $this->mapper->update($token); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Update token activity timestamp |
|
129 | - * |
|
130 | - * @throws InvalidTokenException |
|
131 | - * @param IToken $token |
|
132 | - */ |
|
133 | - public function updateTokenActivity(IToken $token) { |
|
134 | - if (!($token instanceof DefaultToken)) { |
|
135 | - throw new InvalidTokenException(); |
|
136 | - } |
|
137 | - /** @var DefaultToken $token */ |
|
138 | - $now = $this->time->getTime(); |
|
139 | - if ($token->getLastActivity() < ($now - 60)) { |
|
140 | - // Update token only once per minute |
|
141 | - $token->setLastActivity($now); |
|
142 | - $this->mapper->update($token); |
|
143 | - } |
|
144 | - } |
|
145 | - |
|
146 | - public function getTokenByUser(string $uid): array { |
|
147 | - return $this->mapper->getTokenByUser($uid); |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Get a token by token |
|
152 | - * |
|
153 | - * @param string $tokenId |
|
154 | - * @throws InvalidTokenException |
|
155 | - * @throws ExpiredTokenException |
|
156 | - * @return IToken |
|
157 | - */ |
|
158 | - public function getToken(string $tokenId): IToken { |
|
159 | - try { |
|
160 | - $token = $this->mapper->getToken($this->hashToken($tokenId)); |
|
161 | - } catch (DoesNotExistException $ex) { |
|
162 | - throw new InvalidTokenException(); |
|
163 | - } |
|
164 | - |
|
165 | - if ((int)$token->getExpires() !== 0 && $token->getExpires() < $this->time->getTime()) { |
|
166 | - throw new ExpiredTokenException($token); |
|
167 | - } |
|
168 | - |
|
169 | - return $token; |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Get a token by token id |
|
174 | - * |
|
175 | - * @param int $tokenId |
|
176 | - * @throws InvalidTokenException |
|
177 | - * @throws ExpiredTokenException |
|
178 | - * @return IToken |
|
179 | - */ |
|
180 | - public function getTokenById(int $tokenId): IToken { |
|
181 | - try { |
|
182 | - $token = $this->mapper->getTokenById($tokenId); |
|
183 | - } catch (DoesNotExistException $ex) { |
|
184 | - throw new InvalidTokenException(); |
|
185 | - } |
|
186 | - |
|
187 | - if ($token->getExpires() !== null && $token->getExpires() < $this->time->getTime()) { |
|
188 | - throw new ExpiredTokenException($token); |
|
189 | - } |
|
190 | - |
|
191 | - return $token; |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * @param string $oldSessionId |
|
196 | - * @param string $sessionId |
|
197 | - * @throws InvalidTokenException |
|
198 | - */ |
|
199 | - public function renewSessionToken(string $oldSessionId, string $sessionId) { |
|
200 | - $token = $this->getToken($oldSessionId); |
|
201 | - |
|
202 | - $newToken = new DefaultToken(); |
|
203 | - $newToken->setUid($token->getUID()); |
|
204 | - $newToken->setLoginName($token->getLoginName()); |
|
205 | - if (!is_null($token->getPassword())) { |
|
206 | - $password = $this->decryptPassword($token->getPassword(), $oldSessionId); |
|
207 | - $newToken->setPassword($this->encryptPassword($password, $sessionId)); |
|
208 | - } |
|
209 | - $newToken->setName($token->getName()); |
|
210 | - $newToken->setToken($this->hashToken($sessionId)); |
|
211 | - $newToken->setType(IToken::TEMPORARY_TOKEN); |
|
212 | - $newToken->setRemember($token->getRemember()); |
|
213 | - $newToken->setLastActivity($this->time->getTime()); |
|
214 | - $this->mapper->insert($newToken); |
|
215 | - $this->mapper->delete($token); |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * @param IToken $savedToken |
|
220 | - * @param string $tokenId session token |
|
221 | - * @throws InvalidTokenException |
|
222 | - * @throws PasswordlessTokenException |
|
223 | - * @return string |
|
224 | - */ |
|
225 | - public function getPassword(IToken $savedToken, string $tokenId): string { |
|
226 | - $password = $savedToken->getPassword(); |
|
227 | - if (is_null($password)) { |
|
228 | - throw new PasswordlessTokenException(); |
|
229 | - } |
|
230 | - return $this->decryptPassword($password, $tokenId); |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Encrypt and set the password of the given token |
|
235 | - * |
|
236 | - * @param IToken $token |
|
237 | - * @param string $tokenId |
|
238 | - * @param string $password |
|
239 | - * @throws InvalidTokenException |
|
240 | - */ |
|
241 | - public function setPassword(IToken $token, string $tokenId, string $password) { |
|
242 | - if (!($token instanceof DefaultToken)) { |
|
243 | - throw new InvalidTokenException(); |
|
244 | - } |
|
245 | - /** @var DefaultToken $token */ |
|
246 | - $token->setPassword($this->encryptPassword($password, $tokenId)); |
|
247 | - $this->mapper->update($token); |
|
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * Invalidate (delete) the given session token |
|
252 | - * |
|
253 | - * @param string $token |
|
254 | - */ |
|
255 | - public function invalidateToken(string $token) { |
|
256 | - $this->mapper->invalidate($this->hashToken($token)); |
|
257 | - } |
|
258 | - |
|
259 | - public function invalidateTokenById(string $uid, int $id) { |
|
260 | - $this->mapper->deleteById($uid, $id); |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * Invalidate (delete) old session tokens |
|
265 | - */ |
|
266 | - public function invalidateOldTokens() { |
|
267 | - $olderThan = $this->time->getTime() - (int) $this->config->getSystemValue('session_lifetime', 60 * 60 * 24); |
|
268 | - $this->logger->debug('Invalidating session tokens older than ' . date('c', $olderThan), ['app' => 'cron']); |
|
269 | - $this->mapper->invalidateOld($olderThan, IToken::DO_NOT_REMEMBER); |
|
270 | - $rememberThreshold = $this->time->getTime() - (int) $this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15); |
|
271 | - $this->logger->debug('Invalidating remembered session tokens older than ' . date('c', $rememberThreshold), ['app' => 'cron']); |
|
272 | - $this->mapper->invalidateOld($rememberThreshold, IToken::REMEMBER); |
|
273 | - } |
|
274 | - |
|
275 | - /** |
|
276 | - * Rotate the token. Usefull for for example oauth tokens |
|
277 | - * |
|
278 | - * @param IToken $token |
|
279 | - * @param string $oldTokenId |
|
280 | - * @param string $newTokenId |
|
281 | - * @return IToken |
|
282 | - */ |
|
283 | - public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken { |
|
284 | - try { |
|
285 | - $password = $this->getPassword($token, $oldTokenId); |
|
286 | - $token->setPassword($this->encryptPassword($password, $newTokenId)); |
|
287 | - } catch (PasswordlessTokenException $e) { |
|
288 | - |
|
289 | - } |
|
290 | - |
|
291 | - $token->setToken($this->hashToken($newTokenId)); |
|
292 | - $this->updateToken($token); |
|
293 | - |
|
294 | - return $token; |
|
295 | - } |
|
296 | - |
|
297 | - /** |
|
298 | - * @param string $token |
|
299 | - * @return string |
|
300 | - */ |
|
301 | - private function hashToken(string $token): string { |
|
302 | - $secret = $this->config->getSystemValue('secret'); |
|
303 | - return hash('sha512', $token . $secret); |
|
304 | - } |
|
305 | - |
|
306 | - /** |
|
307 | - * Encrypt the given password |
|
308 | - * |
|
309 | - * The token is used as key |
|
310 | - * |
|
311 | - * @param string $password |
|
312 | - * @param string $token |
|
313 | - * @return string encrypted password |
|
314 | - */ |
|
315 | - private function encryptPassword(string $password, string $token): string { |
|
316 | - $secret = $this->config->getSystemValue('secret'); |
|
317 | - return $this->crypto->encrypt($password, $token . $secret); |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * Decrypt the given password |
|
322 | - * |
|
323 | - * The token is used as key |
|
324 | - * |
|
325 | - * @param string $password |
|
326 | - * @param string $token |
|
327 | - * @throws InvalidTokenException |
|
328 | - * @return string the decrypted key |
|
329 | - */ |
|
330 | - private function decryptPassword(string $password, string $token): string { |
|
331 | - $secret = $this->config->getSystemValue('secret'); |
|
332 | - try { |
|
333 | - return $this->crypto->decrypt($password, $token . $secret); |
|
334 | - } catch (Exception $ex) { |
|
335 | - // Delete the invalid token |
|
336 | - $this->invalidateToken($token); |
|
337 | - throw new InvalidTokenException(); |
|
338 | - } |
|
339 | - } |
|
42 | + /** @var DefaultTokenMapper */ |
|
43 | + private $mapper; |
|
44 | + |
|
45 | + /** @var ICrypto */ |
|
46 | + private $crypto; |
|
47 | + |
|
48 | + /** @var IConfig */ |
|
49 | + private $config; |
|
50 | + |
|
51 | + /** @var ILogger $logger */ |
|
52 | + private $logger; |
|
53 | + |
|
54 | + /** @var ITimeFactory $time */ |
|
55 | + private $time; |
|
56 | + |
|
57 | + /** |
|
58 | + * @param DefaultTokenMapper $mapper |
|
59 | + * @param ICrypto $crypto |
|
60 | + * @param IConfig $config |
|
61 | + * @param ILogger $logger |
|
62 | + * @param ITimeFactory $time |
|
63 | + */ |
|
64 | + public function __construct(DefaultTokenMapper $mapper, |
|
65 | + ICrypto $crypto, |
|
66 | + IConfig $config, |
|
67 | + ILogger $logger, |
|
68 | + ITimeFactory $time) { |
|
69 | + $this->mapper = $mapper; |
|
70 | + $this->crypto = $crypto; |
|
71 | + $this->config = $config; |
|
72 | + $this->logger = $logger; |
|
73 | + $this->time = $time; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Create and persist a new token |
|
78 | + * |
|
79 | + * @param string $token |
|
80 | + * @param string $uid |
|
81 | + * @param string $loginName |
|
82 | + * @param string|null $password |
|
83 | + * @param string $name |
|
84 | + * @param int $type token type |
|
85 | + * @param int $remember whether the session token should be used for remember-me |
|
86 | + * @return IToken |
|
87 | + */ |
|
88 | + public function generateToken(string $token, |
|
89 | + string $uid, |
|
90 | + string $loginName, |
|
91 | + $password, |
|
92 | + string $name, |
|
93 | + int $type = IToken::TEMPORARY_TOKEN, |
|
94 | + int $remember = IToken::DO_NOT_REMEMBER): IToken { |
|
95 | + $dbToken = new DefaultToken(); |
|
96 | + $dbToken->setUid($uid); |
|
97 | + $dbToken->setLoginName($loginName); |
|
98 | + if (!is_null($password)) { |
|
99 | + $dbToken->setPassword($this->encryptPassword($password, $token)); |
|
100 | + } |
|
101 | + $dbToken->setName($name); |
|
102 | + $dbToken->setToken($this->hashToken($token)); |
|
103 | + $dbToken->setType($type); |
|
104 | + $dbToken->setRemember($remember); |
|
105 | + $dbToken->setLastActivity($this->time->getTime()); |
|
106 | + $dbToken->setLastCheck($this->time->getTime()); |
|
107 | + $dbToken->setVersion(DefaultToken::VERSION); |
|
108 | + |
|
109 | + $this->mapper->insert($dbToken); |
|
110 | + |
|
111 | + return $dbToken; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Save the updated token |
|
116 | + * |
|
117 | + * @param IToken $token |
|
118 | + * @throws InvalidTokenException |
|
119 | + */ |
|
120 | + public function updateToken(IToken $token) { |
|
121 | + if (!($token instanceof DefaultToken)) { |
|
122 | + throw new InvalidTokenException(); |
|
123 | + } |
|
124 | + $this->mapper->update($token); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Update token activity timestamp |
|
129 | + * |
|
130 | + * @throws InvalidTokenException |
|
131 | + * @param IToken $token |
|
132 | + */ |
|
133 | + public function updateTokenActivity(IToken $token) { |
|
134 | + if (!($token instanceof DefaultToken)) { |
|
135 | + throw new InvalidTokenException(); |
|
136 | + } |
|
137 | + /** @var DefaultToken $token */ |
|
138 | + $now = $this->time->getTime(); |
|
139 | + if ($token->getLastActivity() < ($now - 60)) { |
|
140 | + // Update token only once per minute |
|
141 | + $token->setLastActivity($now); |
|
142 | + $this->mapper->update($token); |
|
143 | + } |
|
144 | + } |
|
145 | + |
|
146 | + public function getTokenByUser(string $uid): array { |
|
147 | + return $this->mapper->getTokenByUser($uid); |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Get a token by token |
|
152 | + * |
|
153 | + * @param string $tokenId |
|
154 | + * @throws InvalidTokenException |
|
155 | + * @throws ExpiredTokenException |
|
156 | + * @return IToken |
|
157 | + */ |
|
158 | + public function getToken(string $tokenId): IToken { |
|
159 | + try { |
|
160 | + $token = $this->mapper->getToken($this->hashToken($tokenId)); |
|
161 | + } catch (DoesNotExistException $ex) { |
|
162 | + throw new InvalidTokenException(); |
|
163 | + } |
|
164 | + |
|
165 | + if ((int)$token->getExpires() !== 0 && $token->getExpires() < $this->time->getTime()) { |
|
166 | + throw new ExpiredTokenException($token); |
|
167 | + } |
|
168 | + |
|
169 | + return $token; |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Get a token by token id |
|
174 | + * |
|
175 | + * @param int $tokenId |
|
176 | + * @throws InvalidTokenException |
|
177 | + * @throws ExpiredTokenException |
|
178 | + * @return IToken |
|
179 | + */ |
|
180 | + public function getTokenById(int $tokenId): IToken { |
|
181 | + try { |
|
182 | + $token = $this->mapper->getTokenById($tokenId); |
|
183 | + } catch (DoesNotExistException $ex) { |
|
184 | + throw new InvalidTokenException(); |
|
185 | + } |
|
186 | + |
|
187 | + if ($token->getExpires() !== null && $token->getExpires() < $this->time->getTime()) { |
|
188 | + throw new ExpiredTokenException($token); |
|
189 | + } |
|
190 | + |
|
191 | + return $token; |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * @param string $oldSessionId |
|
196 | + * @param string $sessionId |
|
197 | + * @throws InvalidTokenException |
|
198 | + */ |
|
199 | + public function renewSessionToken(string $oldSessionId, string $sessionId) { |
|
200 | + $token = $this->getToken($oldSessionId); |
|
201 | + |
|
202 | + $newToken = new DefaultToken(); |
|
203 | + $newToken->setUid($token->getUID()); |
|
204 | + $newToken->setLoginName($token->getLoginName()); |
|
205 | + if (!is_null($token->getPassword())) { |
|
206 | + $password = $this->decryptPassword($token->getPassword(), $oldSessionId); |
|
207 | + $newToken->setPassword($this->encryptPassword($password, $sessionId)); |
|
208 | + } |
|
209 | + $newToken->setName($token->getName()); |
|
210 | + $newToken->setToken($this->hashToken($sessionId)); |
|
211 | + $newToken->setType(IToken::TEMPORARY_TOKEN); |
|
212 | + $newToken->setRemember($token->getRemember()); |
|
213 | + $newToken->setLastActivity($this->time->getTime()); |
|
214 | + $this->mapper->insert($newToken); |
|
215 | + $this->mapper->delete($token); |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * @param IToken $savedToken |
|
220 | + * @param string $tokenId session token |
|
221 | + * @throws InvalidTokenException |
|
222 | + * @throws PasswordlessTokenException |
|
223 | + * @return string |
|
224 | + */ |
|
225 | + public function getPassword(IToken $savedToken, string $tokenId): string { |
|
226 | + $password = $savedToken->getPassword(); |
|
227 | + if (is_null($password)) { |
|
228 | + throw new PasswordlessTokenException(); |
|
229 | + } |
|
230 | + return $this->decryptPassword($password, $tokenId); |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Encrypt and set the password of the given token |
|
235 | + * |
|
236 | + * @param IToken $token |
|
237 | + * @param string $tokenId |
|
238 | + * @param string $password |
|
239 | + * @throws InvalidTokenException |
|
240 | + */ |
|
241 | + public function setPassword(IToken $token, string $tokenId, string $password) { |
|
242 | + if (!($token instanceof DefaultToken)) { |
|
243 | + throw new InvalidTokenException(); |
|
244 | + } |
|
245 | + /** @var DefaultToken $token */ |
|
246 | + $token->setPassword($this->encryptPassword($password, $tokenId)); |
|
247 | + $this->mapper->update($token); |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * Invalidate (delete) the given session token |
|
252 | + * |
|
253 | + * @param string $token |
|
254 | + */ |
|
255 | + public function invalidateToken(string $token) { |
|
256 | + $this->mapper->invalidate($this->hashToken($token)); |
|
257 | + } |
|
258 | + |
|
259 | + public function invalidateTokenById(string $uid, int $id) { |
|
260 | + $this->mapper->deleteById($uid, $id); |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * Invalidate (delete) old session tokens |
|
265 | + */ |
|
266 | + public function invalidateOldTokens() { |
|
267 | + $olderThan = $this->time->getTime() - (int) $this->config->getSystemValue('session_lifetime', 60 * 60 * 24); |
|
268 | + $this->logger->debug('Invalidating session tokens older than ' . date('c', $olderThan), ['app' => 'cron']); |
|
269 | + $this->mapper->invalidateOld($olderThan, IToken::DO_NOT_REMEMBER); |
|
270 | + $rememberThreshold = $this->time->getTime() - (int) $this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15); |
|
271 | + $this->logger->debug('Invalidating remembered session tokens older than ' . date('c', $rememberThreshold), ['app' => 'cron']); |
|
272 | + $this->mapper->invalidateOld($rememberThreshold, IToken::REMEMBER); |
|
273 | + } |
|
274 | + |
|
275 | + /** |
|
276 | + * Rotate the token. Usefull for for example oauth tokens |
|
277 | + * |
|
278 | + * @param IToken $token |
|
279 | + * @param string $oldTokenId |
|
280 | + * @param string $newTokenId |
|
281 | + * @return IToken |
|
282 | + */ |
|
283 | + public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken { |
|
284 | + try { |
|
285 | + $password = $this->getPassword($token, $oldTokenId); |
|
286 | + $token->setPassword($this->encryptPassword($password, $newTokenId)); |
|
287 | + } catch (PasswordlessTokenException $e) { |
|
288 | + |
|
289 | + } |
|
290 | + |
|
291 | + $token->setToken($this->hashToken($newTokenId)); |
|
292 | + $this->updateToken($token); |
|
293 | + |
|
294 | + return $token; |
|
295 | + } |
|
296 | + |
|
297 | + /** |
|
298 | + * @param string $token |
|
299 | + * @return string |
|
300 | + */ |
|
301 | + private function hashToken(string $token): string { |
|
302 | + $secret = $this->config->getSystemValue('secret'); |
|
303 | + return hash('sha512', $token . $secret); |
|
304 | + } |
|
305 | + |
|
306 | + /** |
|
307 | + * Encrypt the given password |
|
308 | + * |
|
309 | + * The token is used as key |
|
310 | + * |
|
311 | + * @param string $password |
|
312 | + * @param string $token |
|
313 | + * @return string encrypted password |
|
314 | + */ |
|
315 | + private function encryptPassword(string $password, string $token): string { |
|
316 | + $secret = $this->config->getSystemValue('secret'); |
|
317 | + return $this->crypto->encrypt($password, $token . $secret); |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * Decrypt the given password |
|
322 | + * |
|
323 | + * The token is used as key |
|
324 | + * |
|
325 | + * @param string $password |
|
326 | + * @param string $token |
|
327 | + * @throws InvalidTokenException |
|
328 | + * @return string the decrypted key |
|
329 | + */ |
|
330 | + private function decryptPassword(string $password, string $token): string { |
|
331 | + $secret = $this->config->getSystemValue('secret'); |
|
332 | + try { |
|
333 | + return $this->crypto->decrypt($password, $token . $secret); |
|
334 | + } catch (Exception $ex) { |
|
335 | + // Delete the invalid token |
|
336 | + $this->invalidateToken($token); |
|
337 | + throw new InvalidTokenException(); |
|
338 | + } |
|
339 | + } |
|
340 | 340 | |
341 | 341 | } |
@@ -31,142 +31,142 @@ |
||
31 | 31 | |
32 | 32 | class PublicKeyTokenMapper extends QBMapper { |
33 | 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 | - 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 | - 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 | - 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 | - 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 | - 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 | - 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 | - } |
|
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 | + 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 | + 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 | + 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 | + 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 | + 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 | + 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 | 172 | } |