Completed
Push — master ( 59e353...a11bef )
by Roeland
13:46
created
lib/private/Authentication/Token/Manager.php 2 patches
Indentation   +215 added lines, -215 removed lines patch added patch discarded remove patch
@@ -28,221 +28,221 @@
 block discarded – undo
28 28
 
29 29
 class Manager implements IProvider {
30 30
 
31
-	/** @var DefaultTokenProvider */
32
-	private $defaultTokenProvider;
33
-
34
-	/** @var PublicKeyTokenProvider */
35
-	private $publicKeyTokenProvider;
36
-
37
-	public function __construct(DefaultTokenProvider $defaultTokenProvider, PublicKeyTokenProvider $publicKeyTokenProvider) {
38
-		$this->defaultTokenProvider = $defaultTokenProvider;
39
-		$this->publicKeyTokenProvider = $publicKeyTokenProvider;
40
-	}
41
-
42
-	/**
43
-	 * Create and persist a new token
44
-	 *
45
-	 * @param string $token
46
-	 * @param string $uid
47
-	 * @param string $loginName
48
-	 * @param string|null $password
49
-	 * @param string $name
50
-	 * @param int $type token type
51
-	 * @param int $remember whether the session token should be used for remember-me
52
-	 * @return IToken
53
-	 */
54
-	public function generateToken(string $token,
55
-								  string $uid,
56
-								  string $loginName,
57
-								  $password,
58
-								  string $name,
59
-								  int $type = IToken::TEMPORARY_TOKEN,
60
-								  int $remember = IToken::DO_NOT_REMEMBER): IToken {
61
-		return $this->publicKeyTokenProvider->generateToken(
62
-			$token,
63
-			$uid,
64
-			$loginName,
65
-			$password,
66
-			$name,
67
-			$type,
68
-			$remember
69
-		);
70
-	}
71
-
72
-	/**
73
-	 * Save the updated token
74
-	 *
75
-	 * @param IToken $token
76
-	 * @throws InvalidTokenException
77
-	 */
78
-	public function updateToken(IToken $token) {
79
-		$provider = $this->getProvider($token);
80
-		$provider->updateToken($token);
81
-	}
82
-
83
-	/**
84
-	 * Update token activity timestamp
85
-	 *
86
-	 * @throws InvalidTokenException
87
-	 * @param IToken $token
88
-	 */
89
-	public function updateTokenActivity(IToken $token) {
90
-		$provider = $this->getProvider($token);
91
-		$provider->updateTokenActivity($token);
92
-	}
93
-
94
-	/**
95
-	 * @param string $uid
96
-	 * @return IToken[]
97
-	 */
98
-	public function getTokenByUser(string $uid): array {
99
-		$old = $this->defaultTokenProvider->getTokenByUser($uid);
100
-		$new = $this->publicKeyTokenProvider->getTokenByUser($uid);
101
-
102
-		return array_merge($old, $new);
103
-	}
104
-
105
-	/**
106
-	 * Get a token by token
107
-	 *
108
-	 * @param string $tokenId
109
-	 * @throws InvalidTokenException
110
-	 * @return IToken
111
-	 */
112
-	public function getToken(string $tokenId): IToken {
113
-		try {
114
-			return $this->publicKeyTokenProvider->getToken($tokenId);
115
-		} catch (ExpiredTokenException $e) {
116
-			throw $e;
117
-		} catch(InvalidTokenException $e) {
118
-			// No worries we try to convert it to a PublicKey Token
119
-		}
120
-
121
-		//Convert!
122
-		$token = $this->defaultTokenProvider->getToken($tokenId);
123
-
124
-		try {
125
-			$password = $this->defaultTokenProvider->getPassword($token, $tokenId);
126
-		} catch (PasswordlessTokenException $e) {
127
-			$password = null;
128
-		}
129
-
130
-		return $this->publicKeyTokenProvider->convertToken($token, $tokenId, $password);
131
-	}
132
-
133
-	/**
134
-	 * Get a token by token id
135
-	 *
136
-	 * @param int $tokenId
137
-	 * @throws InvalidTokenException
138
-	 * @return IToken
139
-	 */
140
-	public function getTokenById(int $tokenId): IToken {
141
-		try {
142
-			return $this->publicKeyTokenProvider->getTokenById($tokenId);
143
-		} catch (ExpiredTokenException $e) {
144
-			throw $e;
145
-		} catch (InvalidTokenException $e) {
146
-			return $this->defaultTokenProvider->getTokenById($tokenId);
147
-		}
148
-	}
149
-
150
-	/**
151
-	 * @param string $oldSessionId
152
-	 * @param string $sessionId
153
-	 * @throws InvalidTokenException
154
-	 */
155
-	public function renewSessionToken(string $oldSessionId, string $sessionId) {
156
-		try {
157
-			$this->publicKeyTokenProvider->renewSessionToken($oldSessionId, $sessionId);
158
-		} catch (ExpiredTokenException $e) {
159
-			throw $e;
160
-		} catch (InvalidTokenException $e) {
161
-			$this->defaultTokenProvider->renewSessionToken($oldSessionId, $sessionId);
162
-		}
163
-	}
164
-
165
-	/**
166
-	 * @param IToken $savedToken
167
-	 * @param string $tokenId session token
168
-	 * @throws InvalidTokenException
169
-	 * @throws PasswordlessTokenException
170
-	 * @return string
171
-	 */
172
-	public function getPassword(IToken $savedToken, string $tokenId): string {
173
-		$provider = $this->getProvider($savedToken);
174
-		return $provider->getPassword($savedToken, $tokenId);
175
-	}
176
-
177
-	public function setPassword(IToken $token, string $tokenId, string $password) {
178
-		$provider = $this->getProvider($token);
179
-		$provider->setPassword($token, $tokenId, $password);
180
-	}
181
-
182
-	public function invalidateToken(string $token) {
183
-		$this->defaultTokenProvider->invalidateToken($token);
184
-		$this->publicKeyTokenProvider->invalidateToken($token);
185
-	}
186
-
187
-	public function invalidateTokenById(string $uid, int $id) {
188
-		$this->defaultTokenProvider->invalidateTokenById($uid, $id);
189
-		$this->publicKeyTokenProvider->invalidateTokenById($uid, $id);
190
-	}
191
-
192
-	public function invalidateOldTokens() {
193
-		$this->defaultTokenProvider->invalidateOldTokens();
194
-		$this->publicKeyTokenProvider->invalidateOldTokens();
195
-	}
196
-
197
-	/**
198
-	 * @param IToken $token
199
-	 * @param string $oldTokenId
200
-	 * @param string $newTokenId
201
-	 * @return IToken
202
-	 * @throws InvalidTokenException
203
-	 */
204
-	public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken {
205
-		if ($token instanceof DefaultToken) {
206
-			try {
207
-				$password = $this->defaultTokenProvider->getPassword($token, $oldTokenId);
208
-			} catch (PasswordlessTokenException $e) {
209
-				$password = null;
210
-			}
211
-
212
-			return $this->publicKeyTokenProvider->convertToken($token, $newTokenId, $password);
213
-		}
214
-
215
-		if ($token instanceof PublicKeyToken) {
216
-			return $this->publicKeyTokenProvider->rotate($token, $oldTokenId, $newTokenId);
217
-		}
218
-
219
-		throw new InvalidTokenException();
220
-	}
221
-
222
-	/**
223
-	 * @param IToken $token
224
-	 * @return IProvider
225
-	 * @throws InvalidTokenException
226
-	 */
227
-	private function getProvider(IToken $token): IProvider {
228
-		if ($token instanceof DefaultToken) {
229
-			return $this->defaultTokenProvider;
230
-		}
231
-		if ($token instanceof PublicKeyToken) {
232
-			return $this->publicKeyTokenProvider;
233
-		}
234
-		throw new InvalidTokenException();
235
-	}
236
-
237
-
238
-	public function markPasswordInvalid(IToken $token, string $tokenId) {
239
-		$this->getProvider($token)->markPasswordInvalid($token, $tokenId);
240
-	}
241
-
242
-	public function updatePasswords(string $uid, string $password) {
243
-		$this->defaultTokenProvider->updatePasswords($uid, $password);
244
-		$this->publicKeyTokenProvider->updatePasswords($uid, $password);
245
-	}
31
+    /** @var DefaultTokenProvider */
32
+    private $defaultTokenProvider;
33
+
34
+    /** @var PublicKeyTokenProvider */
35
+    private $publicKeyTokenProvider;
36
+
37
+    public function __construct(DefaultTokenProvider $defaultTokenProvider, PublicKeyTokenProvider $publicKeyTokenProvider) {
38
+        $this->defaultTokenProvider = $defaultTokenProvider;
39
+        $this->publicKeyTokenProvider = $publicKeyTokenProvider;
40
+    }
41
+
42
+    /**
43
+     * Create and persist a new token
44
+     *
45
+     * @param string $token
46
+     * @param string $uid
47
+     * @param string $loginName
48
+     * @param string|null $password
49
+     * @param string $name
50
+     * @param int $type token type
51
+     * @param int $remember whether the session token should be used for remember-me
52
+     * @return IToken
53
+     */
54
+    public function generateToken(string $token,
55
+                                    string $uid,
56
+                                    string $loginName,
57
+                                    $password,
58
+                                    string $name,
59
+                                    int $type = IToken::TEMPORARY_TOKEN,
60
+                                    int $remember = IToken::DO_NOT_REMEMBER): IToken {
61
+        return $this->publicKeyTokenProvider->generateToken(
62
+            $token,
63
+            $uid,
64
+            $loginName,
65
+            $password,
66
+            $name,
67
+            $type,
68
+            $remember
69
+        );
70
+    }
71
+
72
+    /**
73
+     * Save the updated token
74
+     *
75
+     * @param IToken $token
76
+     * @throws InvalidTokenException
77
+     */
78
+    public function updateToken(IToken $token) {
79
+        $provider = $this->getProvider($token);
80
+        $provider->updateToken($token);
81
+    }
82
+
83
+    /**
84
+     * Update token activity timestamp
85
+     *
86
+     * @throws InvalidTokenException
87
+     * @param IToken $token
88
+     */
89
+    public function updateTokenActivity(IToken $token) {
90
+        $provider = $this->getProvider($token);
91
+        $provider->updateTokenActivity($token);
92
+    }
93
+
94
+    /**
95
+     * @param string $uid
96
+     * @return IToken[]
97
+     */
98
+    public function getTokenByUser(string $uid): array {
99
+        $old = $this->defaultTokenProvider->getTokenByUser($uid);
100
+        $new = $this->publicKeyTokenProvider->getTokenByUser($uid);
101
+
102
+        return array_merge($old, $new);
103
+    }
104
+
105
+    /**
106
+     * Get a token by token
107
+     *
108
+     * @param string $tokenId
109
+     * @throws InvalidTokenException
110
+     * @return IToken
111
+     */
112
+    public function getToken(string $tokenId): IToken {
113
+        try {
114
+            return $this->publicKeyTokenProvider->getToken($tokenId);
115
+        } catch (ExpiredTokenException $e) {
116
+            throw $e;
117
+        } catch(InvalidTokenException $e) {
118
+            // No worries we try to convert it to a PublicKey Token
119
+        }
120
+
121
+        //Convert!
122
+        $token = $this->defaultTokenProvider->getToken($tokenId);
123
+
124
+        try {
125
+            $password = $this->defaultTokenProvider->getPassword($token, $tokenId);
126
+        } catch (PasswordlessTokenException $e) {
127
+            $password = null;
128
+        }
129
+
130
+        return $this->publicKeyTokenProvider->convertToken($token, $tokenId, $password);
131
+    }
132
+
133
+    /**
134
+     * Get a token by token id
135
+     *
136
+     * @param int $tokenId
137
+     * @throws InvalidTokenException
138
+     * @return IToken
139
+     */
140
+    public function getTokenById(int $tokenId): IToken {
141
+        try {
142
+            return $this->publicKeyTokenProvider->getTokenById($tokenId);
143
+        } catch (ExpiredTokenException $e) {
144
+            throw $e;
145
+        } catch (InvalidTokenException $e) {
146
+            return $this->defaultTokenProvider->getTokenById($tokenId);
147
+        }
148
+    }
149
+
150
+    /**
151
+     * @param string $oldSessionId
152
+     * @param string $sessionId
153
+     * @throws InvalidTokenException
154
+     */
155
+    public function renewSessionToken(string $oldSessionId, string $sessionId) {
156
+        try {
157
+            $this->publicKeyTokenProvider->renewSessionToken($oldSessionId, $sessionId);
158
+        } catch (ExpiredTokenException $e) {
159
+            throw $e;
160
+        } catch (InvalidTokenException $e) {
161
+            $this->defaultTokenProvider->renewSessionToken($oldSessionId, $sessionId);
162
+        }
163
+    }
164
+
165
+    /**
166
+     * @param IToken $savedToken
167
+     * @param string $tokenId session token
168
+     * @throws InvalidTokenException
169
+     * @throws PasswordlessTokenException
170
+     * @return string
171
+     */
172
+    public function getPassword(IToken $savedToken, string $tokenId): string {
173
+        $provider = $this->getProvider($savedToken);
174
+        return $provider->getPassword($savedToken, $tokenId);
175
+    }
176
+
177
+    public function setPassword(IToken $token, string $tokenId, string $password) {
178
+        $provider = $this->getProvider($token);
179
+        $provider->setPassword($token, $tokenId, $password);
180
+    }
181
+
182
+    public function invalidateToken(string $token) {
183
+        $this->defaultTokenProvider->invalidateToken($token);
184
+        $this->publicKeyTokenProvider->invalidateToken($token);
185
+    }
186
+
187
+    public function invalidateTokenById(string $uid, int $id) {
188
+        $this->defaultTokenProvider->invalidateTokenById($uid, $id);
189
+        $this->publicKeyTokenProvider->invalidateTokenById($uid, $id);
190
+    }
191
+
192
+    public function invalidateOldTokens() {
193
+        $this->defaultTokenProvider->invalidateOldTokens();
194
+        $this->publicKeyTokenProvider->invalidateOldTokens();
195
+    }
196
+
197
+    /**
198
+     * @param IToken $token
199
+     * @param string $oldTokenId
200
+     * @param string $newTokenId
201
+     * @return IToken
202
+     * @throws InvalidTokenException
203
+     */
204
+    public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken {
205
+        if ($token instanceof DefaultToken) {
206
+            try {
207
+                $password = $this->defaultTokenProvider->getPassword($token, $oldTokenId);
208
+            } catch (PasswordlessTokenException $e) {
209
+                $password = null;
210
+            }
211
+
212
+            return $this->publicKeyTokenProvider->convertToken($token, $newTokenId, $password);
213
+        }
214
+
215
+        if ($token instanceof PublicKeyToken) {
216
+            return $this->publicKeyTokenProvider->rotate($token, $oldTokenId, $newTokenId);
217
+        }
218
+
219
+        throw new InvalidTokenException();
220
+    }
221
+
222
+    /**
223
+     * @param IToken $token
224
+     * @return IProvider
225
+     * @throws InvalidTokenException
226
+     */
227
+    private function getProvider(IToken $token): IProvider {
228
+        if ($token instanceof DefaultToken) {
229
+            return $this->defaultTokenProvider;
230
+        }
231
+        if ($token instanceof PublicKeyToken) {
232
+            return $this->publicKeyTokenProvider;
233
+        }
234
+        throw new InvalidTokenException();
235
+    }
236
+
237
+
238
+    public function markPasswordInvalid(IToken $token, string $tokenId) {
239
+        $this->getProvider($token)->markPasswordInvalid($token, $tokenId);
240
+    }
241
+
242
+    public function updatePasswords(string $uid, string $password) {
243
+        $this->defaultTokenProvider->updatePasswords($uid, $password);
244
+        $this->publicKeyTokenProvider->updatePasswords($uid, $password);
245
+    }
246 246
 
247 247
 
248 248
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -114,7 +114,7 @@
 block discarded – undo
114 114
 			return $this->publicKeyTokenProvider->getToken($tokenId);
115 115
 		} catch (ExpiredTokenException $e) {
116 116
 			throw $e;
117
-		} catch(InvalidTokenException $e) {
117
+		} catch (InvalidTokenException $e) {
118 118
 			// No worries we try to convert it to a PublicKey Token
119 119
 		}
120 120
 
Please login to merge, or discard this patch.