Skip to content

Commit cbb3802

Browse files
author
Fosco Marotto
committed
Added ParseSession
1 parent 7212c0c commit cbb3802

File tree

4 files changed

+121
-4
lines changed

4 files changed

+121
-4
lines changed

src/Parse/ParseClient.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ final class ParseClient
4444
*/
4545
private static $storage;
4646

47+
/**
48+
* @var - Boolean for enabling revocable sessions.
49+
*/
50+
private static $forceRevocableSession = false;
51+
4752
/**
4853
* Constant for version string to include with requests.
4954
* @ignore
@@ -56,7 +61,7 @@ final class ParseClient
5661
* @param string $app_id Parse Application ID
5762
* @param string $rest_key Parse REST API Key
5863
* @param string $master_key Parse Master Key
59-
* @param string $enableCurlExceptions Enable or disable Parse curl exceptions
64+
* @param boolean $enableCurlExceptions Enable or disable Parse curl exceptions
6065
*
6166
* @return null
6267
*/
@@ -65,6 +70,7 @@ public static function initialize($app_id, $rest_key, $master_key, $enableCurlEx
6570
ParseUser::registerSubclass();
6671
ParseRole::registerSubclass();
6772
ParseInstallation::registerSubclass();
73+
ParseSession::registerSubclass();
6874
self::$applicationId = $app_id;
6975
self::$restKey = $rest_key;
7076
self::$masterKey = $master_key;
@@ -341,6 +347,9 @@ public static function _getRequestHeaders($sessionToken, $useMasterKey)
341347
} else {
342348
$headers[] = 'X-Parse-REST-API-Key: ' . self::$restKey;
343349
}
350+
if (self::$forceRevocableSession) {
351+
$headers[] = 'X-Parse-Revocable-Session: 1';
352+
}
344353
/**
345354
* Set an empty Expect header to stop the 100-continue behavior for post
346355
* data greater than 1024 bytes.
@@ -384,4 +393,16 @@ public static function getLocalPushDateFormat($value)
384393
$date = date_format($value, $dateFormatString);
385394
return $date;
386395
}
396+
397+
/**
398+
* Allows an existing application to start using revocable sessions, without forcing
399+
* all requests for the app to use them. After calling this method, login & signup requests
400+
* will be returned a unique and revocable session token.
401+
*
402+
*/
403+
public static function enableRevocableSessions()
404+
{
405+
self::$forceRevocableSession = true;
406+
}
407+
387408
}

src/Parse/ParseObject.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ public function _mergeAfterFetchWithSelectedKeys($result, $selectedKeys)
532532
private function mergeFromServer($data, $completeData = true)
533533
{
534534
$this->hasBeenFetched = ($this->hasBeenFetched || $completeData) ? true : false;
535-
$this->mergeMagicFields($data);
535+
$this->_mergeMagicFields($data);
536536
foreach ($data as $key => $value) {
537537
if ($key === '__type' && $value === 'className') {
538538
continue;
@@ -567,7 +567,7 @@ private function mergeFromServer($data, $completeData = true)
567567
*
568568
* @return null
569569
*/
570-
private function mergeMagicFields(&$data)
570+
public function _mergeMagicFields(&$data)
571571
{
572572
if (isset($data['objectId'])) {
573573
$this->objectId = $data['objectId'];

src/Parse/ParseSession.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace Parse;
4+
5+
use Parse\ParseObject;
6+
use Parse\ParseUser;
7+
8+
/**
9+
* ParseSession - Representation of an expiring user session
10+
*
11+
* @package Parse
12+
* @author Fosco Marotto <fjm@fb.com>
13+
*/
14+
class ParseSession extends ParseObject
15+
{
16+
17+
public static $parseClassName = "_Session";
18+
19+
private $_sessionToken = null;
20+
21+
/**
22+
* Returns the session token string.
23+
*
24+
* @return string
25+
*/
26+
public function getSessionToken()
27+
{
28+
return $this->_sessionToken;
29+
}
30+
31+
/**
32+
* Retrieves the Session object for the currently logged in user.
33+
*
34+
* @param boolean $useMasterKey If the Master Key should be used to override security
35+
*
36+
* @return ParseSession
37+
*/
38+
public static function getCurrentSession($useMasterKey = false)
39+
{
40+
$session = new ParseSession();
41+
$token = ParseUser::getCurrentUser()->getSessionToken();
42+
$response = ParseClient::_request('GET', '/1/sessions/me', $token, null, $useMasterKey);
43+
$session->_mergeAfterFetch($response);
44+
$session->handleSaveResult();
45+
return $session;
46+
}
47+
48+
49+
/**
50+
* Determines whether the current session token is revocable.
51+
* This method is useful for migrating an existing app to use
52+
* revocable sessions.
53+
*
54+
* @return boolean
55+
*/
56+
public static function isCurrentSessionRevocable()
57+
{
58+
$user = ParseUser::getCurrentUser();
59+
if ($user) {
60+
return self::_isRevocable($user->getSessionToken());
61+
}
62+
}
63+
64+
/**
65+
* Determines whether a session token is revocable.
66+
*
67+
* @param string $token The session token to check
68+
*
69+
* @return boolean
70+
*/
71+
public static function _isRevocable($token)
72+
{
73+
return strpos($token, "r:") === 0;
74+
}
75+
76+
/**
77+
* After a save, perform Session object specific logic.
78+
*
79+
* @return null
80+
*/
81+
private function handleSaveResult()
82+
{
83+
if (isset($this->serverData['sessionToken'])) {
84+
$this->_sessionToken = $this->serverData['sessionToken'];
85+
unset($this->serverData['sessionToken']);
86+
}
87+
$this->rebuildEstimatedData();
88+
}
89+
90+
}

src/Parse/ParseUser.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,19 @@ public static function become($sessionToken)
162162

163163
/**
164164
* Log out the current user. This will clear the storage and future calls
165-
* to current will return null
165+
* to current will return null.
166+
* This will make a network request to /1/logout to invalidate the session
166167
*
167168
* @return null
168169
*/
169170
public static function logOut()
170171
{
171172
if (ParseUser::getCurrentUser()) {
173+
try {
174+
$result = ParseClient::_request('GET', '/1/logout');
175+
} catch (ParseException $ex) {
176+
// If this fails, we're going to ignore it.
177+
}
172178
static::$currentUser = null;
173179
}
174180
ParseClient::getStorage()->remove('user');

0 commit comments

Comments
 (0)