Skip to content

Commit 89f2f32

Browse files
authored
Add ability to upgrade to a revocable session (#368)
* Adds ParseSession::upgradeToRevocableSession * added exception & exception test
1 parent 9f3dffe commit 89f2f32

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/Parse/ParseSession.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,32 @@ public static function _isRevocable($token)
8686
return strpos($token, 'r:') === 0;
8787
}
8888

89+
/**
90+
* Upgrades the current session to a revocable one
91+
*
92+
* @throws ParseException
93+
*/
94+
public static function upgradeToRevocableSession()
95+
{
96+
$user = ParseUser::getCurrentUser();
97+
if ($user) {
98+
$token = $user->getSessionToken();
99+
$response = ParseClient::_request(
100+
'POST',
101+
'upgradeToRevocableSession',
102+
$token,
103+
null,
104+
false
105+
);
106+
$session = new self();
107+
$session->_mergeAfterFetch($response);
108+
$session->handleSaveResult();
109+
ParseUser::become($session->getSessionToken());
110+
} else {
111+
throw new ParseException('No session to upgrade.');
112+
}
113+
}
114+
89115
/**
90116
* After a save, perform Session object specific logic.
91117
*/

tests/Parse/ParseSessionTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,37 @@ public function testRevocableSession()
5656
$this->setExpectedException('Parse\ParseException', 'invalid session token');
5757
ParseUser::become($sessionToken);
5858
}
59+
60+
/**
61+
* @group upgrade-to-revocable-session
62+
*/
63+
public function testUpgradeToRevocableSession()
64+
{
65+
$user = new ParseUser();
66+
$user->setUsername('revocable_username');
67+
$user->setPassword('revocable_password');
68+
$user->signUp();
69+
70+
$session = ParseSession::getCurrentSession();
71+
$this->assertEquals($user->getSessionToken(), $session->getSessionToken());
72+
73+
// upgrade the current session (changes our session as well)
74+
ParseSession::upgradeToRevocableSession();
75+
76+
// verify that our session has changed, and our updated current user matches it
77+
$session = ParseSession::getCurrentSession();
78+
$user = ParseUser::getCurrentUser();
79+
$this->assertEquals($user->getSessionToken(), $session->getSessionToken());
80+
$this->assertTrue($session->isCurrentSessionRevocable());
81+
}
82+
83+
/**
84+
* @group upgrade-to-revocable-session
85+
*/
86+
public function testBadUpgradeToRevocableSession()
87+
{
88+
// upgrade the current session (changes our session as well)
89+
$this->setExpectedException('Parse\ParseException', 'No session to upgrade.');
90+
ParseSession::upgradeToRevocableSession();
91+
}
5992
}

0 commit comments

Comments
 (0)