Skip to content

Add ability to upgrade to a revocable session #368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Parse/ParseSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,32 @@ public static function _isRevocable($token)
return strpos($token, 'r:') === 0;
}

/**
* Upgrades the current session to a revocable one
*
* @throws ParseException
*/
public static function upgradeToRevocableSession()
{
$user = ParseUser::getCurrentUser();
if ($user) {
$token = $user->getSessionToken();
$response = ParseClient::_request(
'POST',
'upgradeToRevocableSession',
$token,
null,
false
);
$session = new self();
$session->_mergeAfterFetch($response);
$session->handleSaveResult();
ParseUser::become($session->getSessionToken());
} else {
throw new ParseException('No session to upgrade.');
}
}

/**
* After a save, perform Session object specific logic.
*/
Expand Down
33 changes: 33 additions & 0 deletions tests/Parse/ParseSessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,37 @@ public function testRevocableSession()
$this->setExpectedException('Parse\ParseException', 'invalid session token');
ParseUser::become($sessionToken);
}

/**
* @group upgrade-to-revocable-session
*/
public function testUpgradeToRevocableSession()
{
$user = new ParseUser();
$user->setUsername('revocable_username');
$user->setPassword('revocable_password');
$user->signUp();

$session = ParseSession::getCurrentSession();
$this->assertEquals($user->getSessionToken(), $session->getSessionToken());

// upgrade the current session (changes our session as well)
ParseSession::upgradeToRevocableSession();

// verify that our session has changed, and our updated current user matches it
$session = ParseSession::getCurrentSession();
$user = ParseUser::getCurrentUser();
$this->assertEquals($user->getSessionToken(), $session->getSessionToken());
$this->assertTrue($session->isCurrentSessionRevocable());
}

/**
* @group upgrade-to-revocable-session
*/
public function testBadUpgradeToRevocableSession()
{
// upgrade the current session (changes our session as well)
$this->setExpectedException('Parse\ParseException', 'No session to upgrade.');
ParseSession::upgradeToRevocableSession();
}
}