Completed
Push — master ( 908097...292a70 )
by Morris
71:58 queued 58:06
created
lib/private/Authentication/Token/DefaultTokenProvider.php 1 patch
Indentation   +246 added lines, -246 removed lines patch added patch discarded remove patch
@@ -35,274 +35,274 @@
 block discarded – undo
35 35
 
36 36
 class DefaultTokenProvider implements IProvider {
37 37
 
38
-	/** @var DefaultTokenMapper */
39
-	private $mapper;
38
+    /** @var DefaultTokenMapper */
39
+    private $mapper;
40 40
 
41
-	/** @var ICrypto */
42
-	private $crypto;
41
+    /** @var ICrypto */
42
+    private $crypto;
43 43
 
44
-	/** @var IConfig */
45
-	private $config;
44
+    /** @var IConfig */
45
+    private $config;
46 46
 
47
-	/** @var ILogger $logger */
48
-	private $logger;
47
+    /** @var ILogger $logger */
48
+    private $logger;
49 49
 
50
-	/** @var ITimeFactory $time */
51
-	private $time;
50
+    /** @var ITimeFactory $time */
51
+    private $time;
52 52
 
53
-	/**
54
-	 * @param DefaultTokenMapper $mapper
55
-	 * @param ICrypto $crypto
56
-	 * @param IConfig $config
57
-	 * @param ILogger $logger
58
-	 * @param ITimeFactory $time
59
-	 */
60
-	public function __construct(DefaultTokenMapper $mapper,
61
-								ICrypto $crypto,
62
-								IConfig $config,
63
-								ILogger $logger,
64
-								ITimeFactory $time) {
65
-		$this->mapper = $mapper;
66
-		$this->crypto = $crypto;
67
-		$this->config = $config;
68
-		$this->logger = $logger;
69
-		$this->time = $time;
70
-	}
53
+    /**
54
+     * @param DefaultTokenMapper $mapper
55
+     * @param ICrypto $crypto
56
+     * @param IConfig $config
57
+     * @param ILogger $logger
58
+     * @param ITimeFactory $time
59
+     */
60
+    public function __construct(DefaultTokenMapper $mapper,
61
+                                ICrypto $crypto,
62
+                                IConfig $config,
63
+                                ILogger $logger,
64
+                                ITimeFactory $time) {
65
+        $this->mapper = $mapper;
66
+        $this->crypto = $crypto;
67
+        $this->config = $config;
68
+        $this->logger = $logger;
69
+        $this->time = $time;
70
+    }
71 71
 
72
-	/**
73
-	 * Create and persist a new token
74
-	 *
75
-	 * @param string $token
76
-	 * @param string $uid
77
-	 * @param string $loginName
78
-	 * @param string|null $password
79
-	 * @param string $name
80
-	 * @param int $type token type
81
-	 * @param int $remember whether the session token should be used for remember-me
82
-	 * @return IToken
83
-	 */
84
-	public function generateToken($token, $uid, $loginName, $password, $name, $type = IToken::TEMPORARY_TOKEN, $remember = IToken::DO_NOT_REMEMBER) {
85
-		$dbToken = new DefaultToken();
86
-		$dbToken->setUid($uid);
87
-		$dbToken->setLoginName($loginName);
88
-		if (!is_null($password)) {
89
-			$dbToken->setPassword($this->encryptPassword($password, $token));
90
-		}
91
-		$dbToken->setName($name);
92
-		$dbToken->setToken($this->hashToken($token));
93
-		$dbToken->setType($type);
94
-		$dbToken->setRemember($remember);
95
-		$dbToken->setLastActivity($this->time->getTime());
72
+    /**
73
+     * Create and persist a new token
74
+     *
75
+     * @param string $token
76
+     * @param string $uid
77
+     * @param string $loginName
78
+     * @param string|null $password
79
+     * @param string $name
80
+     * @param int $type token type
81
+     * @param int $remember whether the session token should be used for remember-me
82
+     * @return IToken
83
+     */
84
+    public function generateToken($token, $uid, $loginName, $password, $name, $type = IToken::TEMPORARY_TOKEN, $remember = IToken::DO_NOT_REMEMBER) {
85
+        $dbToken = new DefaultToken();
86
+        $dbToken->setUid($uid);
87
+        $dbToken->setLoginName($loginName);
88
+        if (!is_null($password)) {
89
+            $dbToken->setPassword($this->encryptPassword($password, $token));
90
+        }
91
+        $dbToken->setName($name);
92
+        $dbToken->setToken($this->hashToken($token));
93
+        $dbToken->setType($type);
94
+        $dbToken->setRemember($remember);
95
+        $dbToken->setLastActivity($this->time->getTime());
96 96
 
97
-		$this->mapper->insert($dbToken);
97
+        $this->mapper->insert($dbToken);
98 98
 
99
-		return $dbToken;
100
-	}
99
+        return $dbToken;
100
+    }
101 101
 
102
-	/**
103
-	 * Save the updated token
104
-	 *
105
-	 * @param IToken $token
106
-	 * @throws InvalidTokenException
107
-	 */
108
-	public function updateToken(IToken $token) {
109
-		if (!($token instanceof DefaultToken)) {
110
-			throw new InvalidTokenException();
111
-		}
112
-		$this->mapper->update($token);
113
-	}
102
+    /**
103
+     * Save the updated token
104
+     *
105
+     * @param IToken $token
106
+     * @throws InvalidTokenException
107
+     */
108
+    public function updateToken(IToken $token) {
109
+        if (!($token instanceof DefaultToken)) {
110
+            throw new InvalidTokenException();
111
+        }
112
+        $this->mapper->update($token);
113
+    }
114 114
 
115
-	/**
116
-	 * Update token activity timestamp
117
-	 *
118
-	 * @throws InvalidTokenException
119
-	 * @param IToken $token
120
-	 */
121
-	public function updateTokenActivity(IToken $token) {
122
-		if (!($token instanceof DefaultToken)) {
123
-			throw new InvalidTokenException();
124
-		}
125
-		/** @var DefaultToken $token */
126
-		$now = $this->time->getTime();
127
-		if ($token->getLastActivity() < ($now - 60)) {
128
-			// Update token only once per minute
129
-			$token->setLastActivity($now);
130
-			$this->mapper->update($token);
131
-		}
132
-	}
115
+    /**
116
+     * Update token activity timestamp
117
+     *
118
+     * @throws InvalidTokenException
119
+     * @param IToken $token
120
+     */
121
+    public function updateTokenActivity(IToken $token) {
122
+        if (!($token instanceof DefaultToken)) {
123
+            throw new InvalidTokenException();
124
+        }
125
+        /** @var DefaultToken $token */
126
+        $now = $this->time->getTime();
127
+        if ($token->getLastActivity() < ($now - 60)) {
128
+            // Update token only once per minute
129
+            $token->setLastActivity($now);
130
+            $this->mapper->update($token);
131
+        }
132
+    }
133 133
 
134
-	/**
135
-	 * Get all tokens of a user
136
-	 *
137
-	 * The provider may limit the number of result rows in case of an abuse
138
-	 * where a high number of (session) tokens is generated
139
-	 *
140
-	 * @param IUser $user
141
-	 * @return IToken[]
142
-	 */
143
-	public function getTokenByUser(IUser $user) {
144
-		return $this->mapper->getTokenByUser($user);
145
-	}
134
+    /**
135
+     * Get all tokens of a user
136
+     *
137
+     * The provider may limit the number of result rows in case of an abuse
138
+     * where a high number of (session) tokens is generated
139
+     *
140
+     * @param IUser $user
141
+     * @return IToken[]
142
+     */
143
+    public function getTokenByUser(IUser $user) {
144
+        return $this->mapper->getTokenByUser($user);
145
+    }
146 146
 
147
-	/**
148
-	 * Get a token by token
149
-	 *
150
-	 * @param string $tokenId
151
-	 * @throws InvalidTokenException
152
-	 * @return DefaultToken
153
-	 */
154
-	public function getToken($tokenId) {
155
-		try {
156
-			return $this->mapper->getToken($this->hashToken($tokenId));
157
-		} catch (DoesNotExistException $ex) {
158
-			throw new InvalidTokenException();
159
-		}
160
-	}
147
+    /**
148
+     * Get a token by token
149
+     *
150
+     * @param string $tokenId
151
+     * @throws InvalidTokenException
152
+     * @return DefaultToken
153
+     */
154
+    public function getToken($tokenId) {
155
+        try {
156
+            return $this->mapper->getToken($this->hashToken($tokenId));
157
+        } catch (DoesNotExistException $ex) {
158
+            throw new InvalidTokenException();
159
+        }
160
+    }
161 161
 
162
-	/**
163
-	 * Get a token by token id
164
-	 *
165
-	 * @param string $tokenId
166
-	 * @throws InvalidTokenException
167
-	 * @return DefaultToken
168
-	 */
169
-	public function getTokenById($tokenId) {
170
-		try {
171
-			return $this->mapper->getTokenById($tokenId);
172
-		} catch (DoesNotExistException $ex) {
173
-			throw new InvalidTokenException();
174
-		}
175
-	}
162
+    /**
163
+     * Get a token by token id
164
+     *
165
+     * @param string $tokenId
166
+     * @throws InvalidTokenException
167
+     * @return DefaultToken
168
+     */
169
+    public function getTokenById($tokenId) {
170
+        try {
171
+            return $this->mapper->getTokenById($tokenId);
172
+        } catch (DoesNotExistException $ex) {
173
+            throw new InvalidTokenException();
174
+        }
175
+    }
176 176
 
177
-	/**
178
-	 * @param string $oldSessionId
179
-	 * @param string $sessionId
180
-	 * @throws InvalidTokenException
181
-	 */
182
-	public function renewSessionToken($oldSessionId, $sessionId) {
183
-		$token = $this->getToken($oldSessionId);
177
+    /**
178
+     * @param string $oldSessionId
179
+     * @param string $sessionId
180
+     * @throws InvalidTokenException
181
+     */
182
+    public function renewSessionToken($oldSessionId, $sessionId) {
183
+        $token = $this->getToken($oldSessionId);
184 184
 
185
-		$newToken = new DefaultToken();
186
-		$newToken->setUid($token->getUID());
187
-		$newToken->setLoginName($token->getLoginName());
188
-		if (!is_null($token->getPassword())) {
189
-			$password = $this->decryptPassword($token->getPassword(), $oldSessionId);
190
-			$newToken->setPassword($this->encryptPassword($password, $sessionId));
191
-		}
192
-		$newToken->setName($token->getName());
193
-		$newToken->setToken($this->hashToken($sessionId));
194
-		$newToken->setType(IToken::TEMPORARY_TOKEN);
195
-		$newToken->setRemember($token->getRemember());
196
-		$newToken->setLastActivity($this->time->getTime());
197
-		$this->mapper->insert($newToken);
198
-		$this->mapper->delete($token);
199
-	}
185
+        $newToken = new DefaultToken();
186
+        $newToken->setUid($token->getUID());
187
+        $newToken->setLoginName($token->getLoginName());
188
+        if (!is_null($token->getPassword())) {
189
+            $password = $this->decryptPassword($token->getPassword(), $oldSessionId);
190
+            $newToken->setPassword($this->encryptPassword($password, $sessionId));
191
+        }
192
+        $newToken->setName($token->getName());
193
+        $newToken->setToken($this->hashToken($sessionId));
194
+        $newToken->setType(IToken::TEMPORARY_TOKEN);
195
+        $newToken->setRemember($token->getRemember());
196
+        $newToken->setLastActivity($this->time->getTime());
197
+        $this->mapper->insert($newToken);
198
+        $this->mapper->delete($token);
199
+    }
200 200
 
201
-	/**
202
-	 * @param IToken $savedToken
203
-	 * @param string $tokenId session token
204
-	 * @throws InvalidTokenException
205
-	 * @throws PasswordlessTokenException
206
-	 * @return string
207
-	 */
208
-	public function getPassword(IToken $savedToken, $tokenId) {
209
-		$password = $savedToken->getPassword();
210
-		if (is_null($password)) {
211
-			throw new PasswordlessTokenException();
212
-		}
213
-		return $this->decryptPassword($password, $tokenId);
214
-	}
201
+    /**
202
+     * @param IToken $savedToken
203
+     * @param string $tokenId session token
204
+     * @throws InvalidTokenException
205
+     * @throws PasswordlessTokenException
206
+     * @return string
207
+     */
208
+    public function getPassword(IToken $savedToken, $tokenId) {
209
+        $password = $savedToken->getPassword();
210
+        if (is_null($password)) {
211
+            throw new PasswordlessTokenException();
212
+        }
213
+        return $this->decryptPassword($password, $tokenId);
214
+    }
215 215
 
216
-	/**
217
-	 * Encrypt and set the password of the given token
218
-	 *
219
-	 * @param IToken $token
220
-	 * @param string $tokenId
221
-	 * @param string $password
222
-	 * @throws InvalidTokenException
223
-	 */
224
-	public function setPassword(IToken $token, $tokenId, $password) {
225
-		if (!($token instanceof DefaultToken)) {
226
-			throw new InvalidTokenException();
227
-		}
228
-		/** @var DefaultToken $token */
229
-		$token->setPassword($this->encryptPassword($password, $tokenId));
230
-		$this->mapper->update($token);
231
-	}
216
+    /**
217
+     * Encrypt and set the password of the given token
218
+     *
219
+     * @param IToken $token
220
+     * @param string $tokenId
221
+     * @param string $password
222
+     * @throws InvalidTokenException
223
+     */
224
+    public function setPassword(IToken $token, $tokenId, $password) {
225
+        if (!($token instanceof DefaultToken)) {
226
+            throw new InvalidTokenException();
227
+        }
228
+        /** @var DefaultToken $token */
229
+        $token->setPassword($this->encryptPassword($password, $tokenId));
230
+        $this->mapper->update($token);
231
+    }
232 232
 
233
-	/**
234
-	 * Invalidate (delete) the given session token
235
-	 *
236
-	 * @param string $token
237
-	 */
238
-	public function invalidateToken($token) {
239
-		$this->mapper->invalidate($this->hashToken($token));
240
-	}
233
+    /**
234
+     * Invalidate (delete) the given session token
235
+     *
236
+     * @param string $token
237
+     */
238
+    public function invalidateToken($token) {
239
+        $this->mapper->invalidate($this->hashToken($token));
240
+    }
241 241
 
242
-	/**
243
-	 * Invalidate (delete) the given token
244
-	 *
245
-	 * @param IUser $user
246
-	 * @param int $id
247
-	 */
248
-	public function invalidateTokenById(IUser $user, $id) {
249
-		$this->mapper->deleteById($user, $id);
250
-	}
242
+    /**
243
+     * Invalidate (delete) the given token
244
+     *
245
+     * @param IUser $user
246
+     * @param int $id
247
+     */
248
+    public function invalidateTokenById(IUser $user, $id) {
249
+        $this->mapper->deleteById($user, $id);
250
+    }
251 251
 
252
-	/**
253
-	 * Invalidate (delete) old session tokens
254
-	 */
255
-	public function invalidateOldTokens() {
256
-		$olderThan = $this->time->getTime() - (int) $this->config->getSystemValue('session_lifetime', 60 * 60 * 24);
257
-		$this->logger->debug('Invalidating session tokens older than ' . date('c', $olderThan), ['app' => 'cron']);
258
-		$this->mapper->invalidateOld($olderThan, IToken::DO_NOT_REMEMBER);
259
-		$rememberThreshold = $this->time->getTime() - (int) $this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15);
260
-		$this->logger->debug('Invalidating remembered session tokens older than ' . date('c', $rememberThreshold), ['app' => 'cron']);
261
-		$this->mapper->invalidateOld($rememberThreshold, IToken::REMEMBER);
262
-	}
252
+    /**
253
+     * Invalidate (delete) old session tokens
254
+     */
255
+    public function invalidateOldTokens() {
256
+        $olderThan = $this->time->getTime() - (int) $this->config->getSystemValue('session_lifetime', 60 * 60 * 24);
257
+        $this->logger->debug('Invalidating session tokens older than ' . date('c', $olderThan), ['app' => 'cron']);
258
+        $this->mapper->invalidateOld($olderThan, IToken::DO_NOT_REMEMBER);
259
+        $rememberThreshold = $this->time->getTime() - (int) $this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15);
260
+        $this->logger->debug('Invalidating remembered session tokens older than ' . date('c', $rememberThreshold), ['app' => 'cron']);
261
+        $this->mapper->invalidateOld($rememberThreshold, IToken::REMEMBER);
262
+    }
263 263
 
264
-	/**
265
-	 * @param string $token
266
-	 * @return string
267
-	 */
268
-	private function hashToken($token) {
269
-		$secret = $this->config->getSystemValue('secret');
270
-		return hash('sha512', $token . $secret);
271
-	}
264
+    /**
265
+     * @param string $token
266
+     * @return string
267
+     */
268
+    private function hashToken($token) {
269
+        $secret = $this->config->getSystemValue('secret');
270
+        return hash('sha512', $token . $secret);
271
+    }
272 272
 
273
-	/**
274
-	 * Encrypt the given password
275
-	 *
276
-	 * The token is used as key
277
-	 *
278
-	 * @param string $password
279
-	 * @param string $token
280
-	 * @return string encrypted password
281
-	 */
282
-	private function encryptPassword($password, $token) {
283
-		$secret = $this->config->getSystemValue('secret');
284
-		return $this->crypto->encrypt($password, $token . $secret);
285
-	}
273
+    /**
274
+     * Encrypt the given password
275
+     *
276
+     * The token is used as key
277
+     *
278
+     * @param string $password
279
+     * @param string $token
280
+     * @return string encrypted password
281
+     */
282
+    private function encryptPassword($password, $token) {
283
+        $secret = $this->config->getSystemValue('secret');
284
+        return $this->crypto->encrypt($password, $token . $secret);
285
+    }
286 286
 
287
-	/**
288
-	 * Decrypt the given password
289
-	 *
290
-	 * The token is used as key
291
-	 *
292
-	 * @param string $password
293
-	 * @param string $token
294
-	 * @throws InvalidTokenException
295
-	 * @return string the decrypted key
296
-	 */
297
-	private function decryptPassword($password, $token) {
298
-		$secret = $this->config->getSystemValue('secret');
299
-		try {
300
-			return $this->crypto->decrypt($password, $token . $secret);
301
-		} catch (Exception $ex) {
302
-			// Delete the invalid token
303
-			$this->invalidateToken($token);
304
-			throw new InvalidTokenException();
305
-		}
306
-	}
287
+    /**
288
+     * Decrypt the given password
289
+     *
290
+     * The token is used as key
291
+     *
292
+     * @param string $password
293
+     * @param string $token
294
+     * @throws InvalidTokenException
295
+     * @return string the decrypted key
296
+     */
297
+    private function decryptPassword($password, $token) {
298
+        $secret = $this->config->getSystemValue('secret');
299
+        try {
300
+            return $this->crypto->decrypt($password, $token . $secret);
301
+        } catch (Exception $ex) {
302
+            // Delete the invalid token
303
+            $this->invalidateToken($token);
304
+            throw new InvalidTokenException();
305
+        }
306
+    }
307 307
 
308 308
 }
Please login to merge, or discard this patch.