Completed
Push — master ( b8492c...7ef722 )
by Blizzz
158:19 queued 134:06
created
lib/private/Authentication/Token/Manager.php 1 patch
Indentation   +199 added lines, -199 removed lines patch added patch discarded remove patch
@@ -28,203 +28,203 @@
 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 (InvalidTokenException $e) {
116
-			// No worries we try to convert it to a PublicKey Token
117
-		}
118
-
119
-		//Convert!
120
-		$token = $this->defaultTokenProvider->getToken($tokenId);
121
-
122
-		try {
123
-			$password = $this->defaultTokenProvider->getPassword($token, $tokenId);
124
-		} catch (PasswordlessTokenException $e) {
125
-			$password = null;
126
-		}
127
-
128
-		return $this->publicKeyTokenProvider->convertToken($token, $tokenId, $password);
129
-	}
130
-
131
-	/**
132
-	 * Get a token by token id
133
-	 *
134
-	 * @param int $tokenId
135
-	 * @throws InvalidTokenException
136
-	 * @return IToken
137
-	 */
138
-	public function getTokenById(int $tokenId): IToken {
139
-		try {
140
-			return $this->publicKeyTokenProvider->getTokenById($tokenId);
141
-		} catch (InvalidTokenException $e) {
142
-			return $this->defaultTokenProvider->getTokenById($tokenId);
143
-		}
144
-	}
145
-
146
-	/**
147
-	 * @param string $oldSessionId
148
-	 * @param string $sessionId
149
-	 * @throws InvalidTokenException
150
-	 */
151
-	public function renewSessionToken(string $oldSessionId, string $sessionId) {
152
-		try {
153
-			$this->publicKeyTokenProvider->renewSessionToken($oldSessionId, $sessionId);
154
-		} catch (InvalidTokenException $e) {
155
-			$this->defaultTokenProvider->renewSessionToken($oldSessionId, $sessionId);
156
-		}
157
-	}
158
-
159
-	/**
160
-	 * @param IToken $savedToken
161
-	 * @param string $tokenId session token
162
-	 * @throws InvalidTokenException
163
-	 * @throws PasswordlessTokenException
164
-	 * @return string
165
-	 */
166
-	public function getPassword(IToken $savedToken, string $tokenId): string {
167
-		$provider = $this->getProvider($savedToken);
168
-		return $provider->getPassword($savedToken, $tokenId);
169
-	}
170
-
171
-	public function setPassword(IToken $token, string $tokenId, string $password) {
172
-		$provider = $this->getProvider($token);
173
-		$provider->setPassword($token, $tokenId, $password);
174
-	}
175
-
176
-	public function invalidateToken(string $token) {
177
-		$this->defaultTokenProvider->invalidateToken($token);
178
-		$this->publicKeyTokenProvider->invalidateToken($token);
179
-	}
180
-
181
-	public function invalidateTokenById(string $uid, int $id) {
182
-		$this->defaultTokenProvider->invalidateTokenById($uid, $id);
183
-		$this->publicKeyTokenProvider->invalidateTokenById($uid, $id);
184
-	}
185
-
186
-	public function invalidateOldTokens() {
187
-		$this->defaultTokenProvider->invalidateOldTokens();
188
-		$this->publicKeyTokenProvider->invalidateOldTokens();
189
-	}
190
-
191
-	/**
192
-	 * @param IToken $token
193
-	 * @param string $oldTokenId
194
-	 * @param string $newTokenId
195
-	 * @return IToken
196
-	 * @throws InvalidTokenException
197
-	 */
198
-	public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken {
199
-		if ($token instanceof DefaultToken) {
200
-			try {
201
-				$password = $this->defaultTokenProvider->getPassword($token, $oldTokenId);
202
-			} catch (PasswordlessTokenException $e) {
203
-				$password = null;
204
-			}
205
-
206
-			return $this->publicKeyTokenProvider->convertToken($token, $newTokenId, $password);
207
-		}
208
-
209
-		if ($token instanceof PublicKeyToken) {
210
-			return $this->publicKeyTokenProvider->rotate($token, $oldTokenId, $newTokenId);
211
-		}
212
-
213
-		throw new InvalidTokenException();
214
-	}
215
-
216
-	/**
217
-	 * @param IToken $token
218
-	 * @return IProvider
219
-	 * @throws InvalidTokenException
220
-	 */
221
-	private function getProvider(IToken $token): IProvider {
222
-		if ($token instanceof DefaultToken) {
223
-			return $this->defaultTokenProvider;
224
-		}
225
-		if ($token instanceof PublicKeyToken) {
226
-			return $this->publicKeyTokenProvider;
227
-		}
228
-		throw new InvalidTokenException();
229
-	}
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 (InvalidTokenException $e) {
116
+            // No worries we try to convert it to a PublicKey Token
117
+        }
118
+
119
+        //Convert!
120
+        $token = $this->defaultTokenProvider->getToken($tokenId);
121
+
122
+        try {
123
+            $password = $this->defaultTokenProvider->getPassword($token, $tokenId);
124
+        } catch (PasswordlessTokenException $e) {
125
+            $password = null;
126
+        }
127
+
128
+        return $this->publicKeyTokenProvider->convertToken($token, $tokenId, $password);
129
+    }
130
+
131
+    /**
132
+     * Get a token by token id
133
+     *
134
+     * @param int $tokenId
135
+     * @throws InvalidTokenException
136
+     * @return IToken
137
+     */
138
+    public function getTokenById(int $tokenId): IToken {
139
+        try {
140
+            return $this->publicKeyTokenProvider->getTokenById($tokenId);
141
+        } catch (InvalidTokenException $e) {
142
+            return $this->defaultTokenProvider->getTokenById($tokenId);
143
+        }
144
+    }
145
+
146
+    /**
147
+     * @param string $oldSessionId
148
+     * @param string $sessionId
149
+     * @throws InvalidTokenException
150
+     */
151
+    public function renewSessionToken(string $oldSessionId, string $sessionId) {
152
+        try {
153
+            $this->publicKeyTokenProvider->renewSessionToken($oldSessionId, $sessionId);
154
+        } catch (InvalidTokenException $e) {
155
+            $this->defaultTokenProvider->renewSessionToken($oldSessionId, $sessionId);
156
+        }
157
+    }
158
+
159
+    /**
160
+     * @param IToken $savedToken
161
+     * @param string $tokenId session token
162
+     * @throws InvalidTokenException
163
+     * @throws PasswordlessTokenException
164
+     * @return string
165
+     */
166
+    public function getPassword(IToken $savedToken, string $tokenId): string {
167
+        $provider = $this->getProvider($savedToken);
168
+        return $provider->getPassword($savedToken, $tokenId);
169
+    }
170
+
171
+    public function setPassword(IToken $token, string $tokenId, string $password) {
172
+        $provider = $this->getProvider($token);
173
+        $provider->setPassword($token, $tokenId, $password);
174
+    }
175
+
176
+    public function invalidateToken(string $token) {
177
+        $this->defaultTokenProvider->invalidateToken($token);
178
+        $this->publicKeyTokenProvider->invalidateToken($token);
179
+    }
180
+
181
+    public function invalidateTokenById(string $uid, int $id) {
182
+        $this->defaultTokenProvider->invalidateTokenById($uid, $id);
183
+        $this->publicKeyTokenProvider->invalidateTokenById($uid, $id);
184
+    }
185
+
186
+    public function invalidateOldTokens() {
187
+        $this->defaultTokenProvider->invalidateOldTokens();
188
+        $this->publicKeyTokenProvider->invalidateOldTokens();
189
+    }
190
+
191
+    /**
192
+     * @param IToken $token
193
+     * @param string $oldTokenId
194
+     * @param string $newTokenId
195
+     * @return IToken
196
+     * @throws InvalidTokenException
197
+     */
198
+    public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken {
199
+        if ($token instanceof DefaultToken) {
200
+            try {
201
+                $password = $this->defaultTokenProvider->getPassword($token, $oldTokenId);
202
+            } catch (PasswordlessTokenException $e) {
203
+                $password = null;
204
+            }
205
+
206
+            return $this->publicKeyTokenProvider->convertToken($token, $newTokenId, $password);
207
+        }
208
+
209
+        if ($token instanceof PublicKeyToken) {
210
+            return $this->publicKeyTokenProvider->rotate($token, $oldTokenId, $newTokenId);
211
+        }
212
+
213
+        throw new InvalidTokenException();
214
+    }
215
+
216
+    /**
217
+     * @param IToken $token
218
+     * @return IProvider
219
+     * @throws InvalidTokenException
220
+     */
221
+    private function getProvider(IToken $token): IProvider {
222
+        if ($token instanceof DefaultToken) {
223
+            return $this->defaultTokenProvider;
224
+        }
225
+        if ($token instanceof PublicKeyToken) {
226
+            return $this->publicKeyTokenProvider;
227
+        }
228
+        throw new InvalidTokenException();
229
+    }
230 230
 }
Please login to merge, or discard this patch.