Skip to content

Commit 69a2b83

Browse files
committed
Login: store token in session as array [Ref #15]
When stored as a Token object, can be unserialized as an __PHP_Incomplete_Class when session starts before the library is loaded.
1 parent 3be6166 commit 69a2b83

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

src/Github/OAuth/Login.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function obtainToken($code, $state)
122122
}
123123

124124
$token = new Token($json->access_token, $json->token_type, strlen($json->scope) ? explode(',', $json->scope) : []);
125-
$this->storage->set('auth.token', $token);
125+
$this->storage->set('auth.token', $token->toArray());
126126
$this->storage->remove('auth.state');
127127

128128
return $token;
@@ -148,9 +148,14 @@ public function getToken()
148148
$token = $this->storage->get('auth.token');
149149
if ($token === NULL) {
150150
throw new Github\LogicException('Token has not been obtained yet.');
151+
152+
} elseif ($token instanceof Token) {
153+
/** @deprecated */
154+
$token = $token->toArray();
155+
$this->storage->set('auth.token', $token);
151156
}
152157

153-
return $token;
158+
return Token::createFromArray($token);
154159
}
155160

156161

src/Github/OAuth/Token.php

+18
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,22 @@ public function hasScope($scope)
8787
return FALSE;
8888
}
8989

90+
91+
/** @internal */
92+
public function toArray()
93+
{
94+
return [
95+
'value' => $this->value,
96+
'type' => $this->type,
97+
'scopes' => $this->scopes,
98+
];
99+
}
100+
101+
102+
/** @internal */
103+
public static function createFromArray(array $data)
104+
{
105+
return new static($data['value'], $data['type'], $data['scopes']);
106+
}
107+
90108
}

tests/Github/OAuth/Login.phpt

+15
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,21 @@ class LoginTestCase extends Tester\TestCase
170170
Assert::null($e->getPrevious());
171171
}
172172

173+
174+
/** @deprecated */
175+
public function testTokenObjectInSession()
176+
{
177+
$token = new Milo\Github\OAuth\Token('a', 'b', ['c', 'd']);
178+
$this->storage->set('auth.token', $token);
179+
180+
Assert::type('Milo\Github\OAuth\Token', $this->login->getToken());
181+
Assert::same([
182+
'value' => 'a',
183+
'type' => 'b',
184+
'scopes' => ['c', 'd'],
185+
], $this->storage->get('auth.token'));
186+
}
187+
173188
}
174189

175190
(new LoginTestCase())->run();

tests/Github/OAuth/Token.phpt

+10
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,13 @@ Assert::true($token->hasScope('user'));
2323
Assert::true($token->hasScope('user:email'));
2424
Assert::false($token->hasScope('user:foo'));
2525
Assert::false($token->hasScope('foo'));
26+
27+
$asArray = [
28+
'value' => 'hash2',
29+
'type' => 'type',
30+
'scopes' => ['user'],
31+
];
32+
Assert::same($asArray, $token->toArray());
33+
34+
$fromArray = Milo\Github\OAuth\Token::createFromArray($asArray);
35+
Assert::equal($token, $fromArray);

0 commit comments

Comments
 (0)