Skip to content

Commit 9e88e27

Browse files
committed
Extract handling deprecated session layouts into base class
1 parent b54c0ec commit 9e88e27

3 files changed

Lines changed: 20 additions & 14 deletions

File tree

src/main/php/web/auth/oauth/OAuth1Flow.class.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,11 @@ public function authenticate($request, $response, $session) {
9696

9797
// Check whether we are continuing an existing authentication flow based on the
9898
// state given by the server and our session; or if we need to start a new one.
99-
// Handle deprecated session layouts from previous library versions.
100-
$state= $request->param('oauth_token');
101-
$flow= (
102-
$stored['flows'][$state] ??
103-
(isset($stored['flow'][$state]) ? ['uri' => $stored['flow'][$state], 'seed' => []] : null) ??
104-
(isset($stored['target']) ? ['uri' => $stored['target'], 'seed' => []] : null)
105-
);
99+
if (null === ($state= $request->param('oauth_token'))) {
100+
$flow= null;
101+
} else {
102+
$flow= $this->flow($state, $stored);
103+
}
106104

107105
if (null === $flow) {
108106
$state= $this->request('/request_token', null, ['oauth_callback' => $callback])['oauth_token'];

src/main/php/web/auth/oauth/OAuth2Flow.class.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,12 @@ public function authenticate($request, $response, $session) {
9393

9494
// Check whether we are continuing an existing authentication flow based on the
9595
// state given by the server and our session; or if we need to start a new one.
96-
// Handle deprecated session layouts from previous library versions.
97-
sscanf($request->param('state') ?? '', self::STATE, $state, $fragment);
98-
$flow= (
99-
$stored['flows'][$state] ??
100-
(isset($stored['flow'][$state]) ? ['uri' => $stored['flow'][$state], 'seed' => []] : null) ??
101-
(isset($stored['target']) ? ['uri' => $stored['target'], 'seed' => []] : null)
102-
);
96+
if (null === ($server= $request->param('state'))) {
97+
$flow= null;
98+
} else {
99+
sscanf($server, self::STATE, $state, $fragment);
100+
$flow= $this->flow($state, $stored);
101+
}
103102

104103
if (null === $flow) {
105104
$state= bin2hex($this->rand->bytes(16));

src/main/php/web/auth/oauth/OAuthFlow.class.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
abstract class OAuthFlow extends Flow {
77
protected $callback;
88

9+
/** Locate flow stored in session based on a given state, handling deprecated session layouts */
10+
protected function flow($state, $stored) {
11+
return (
12+
$stored['flows'][$state] ??
13+
(isset($stored['flow'][$state]) ? ['uri' => $stored['flow'][$state], 'seed' => []] : null) ??
14+
(isset($stored['target']) ? ['uri' => $stored['target'], 'seed' => []] : null)
15+
);
16+
}
17+
918
/** @return ?util.URI */
1019
public function callback() { return $this->callback; }
1120

0 commit comments

Comments
 (0)