Skip to content

Fix sessionId handling to allow authentication with sessionId #80

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 6 commits into from
Feb 16, 2019
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
2 changes: 0 additions & 2 deletions lib/parse_server_sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ class Parse {
sessionId: sessionId,
securityContext: securityContext);

ParseCoreData().initStorage();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to confirm, the ParseCoreData class is a singleton, initStorage method creates an instance of ParseCoreData with all the necessary data. If this method is not called here, where else is it called? / Or is not needed to be called?

Copy link
Contributor Author

@chrbayer chrbayer Feb 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The init functionality was and still is in getStore(), It is initialized at the first usage:

Future<SharedPreferences> getStore() async {
  return storage ?? (storage = await SharedPreferences.getInstance());
}


_hasBeenInitialized = true;

return Parse();
Expand Down
6 changes: 1 addition & 5 deletions lib/src/data/parse_core_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,8 @@ class ParseCoreData {
this.sessionId = sessionId;
}

void initStorage() async {
storage = await SharedPreferences.getInstance();
}

Future<SharedPreferences> getStore() async {
return storage != null ? storage : await SharedPreferences.getInstance();
return storage ?? (storage = await SharedPreferences.getInstance());
}

@override
Expand Down
13 changes: 9 additions & 4 deletions lib/src/network/parse_http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ class ParseHTTPClient extends BaseClient {
Future<StreamedResponse> send(BaseRequest request) {
request.headers[keyHeaderUserAgent] = _userAgent;
request.headers[keyHeaderApplicationId] = data.applicationId;
if ((data.sessionId != null) &&
(request.headers[keyHeaderSessionToken] == null))
request.headers[keyHeaderSessionToken] = data.sessionId;

if (data.clientKey != null) request.headers[keyHeaderClientKey] = data.clientKey;
if (data.masterKey != null) request.headers[keyHeaderMasterKey] = data.masterKey;
if (data.clientKey != null)
request.headers[keyHeaderClientKey] = data.clientKey;
if (data.masterKey != null)
request.headers[keyHeaderMasterKey] = data.masterKey;

/// If developer wants to add custom headers, extend this class and add headers needed.
if (additionalHeaders != null && additionalHeaders.length > 0){
additionalHeaders.forEach((k,v) => request.headers[k] = v);
if (additionalHeaders != null && additionalHeaders.length > 0) {
additionalHeaders.forEach((k, v) => request.headers[k] = v);
}

return _client.send(request);
Expand Down
12 changes: 3 additions & 9 deletions lib/src/objects/parse_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ abstract class ParseBase {

/// Returns the objects variables
@protected
Map getObjectData() => _objectData != null ? _objectData : Map();
Map getObjectData() => _objectData ?? Map();

/// Saves in storage
@protected
Expand Down Expand Up @@ -149,7 +149,7 @@ abstract class ParseBase {
/// Replicates Android SDK pin process and saves object to storage
Future<bool> unpin({String key}) async {
if (objectId != null) {
await SharedPreferences.getInstance()
await ParseCoreData().getStore()
..remove(key ?? objectId);
return true;
}
Expand All @@ -165,13 +165,7 @@ abstract class ParseBase {
var itemFromStore =
(await ParseCoreData().getStore()).getString(objectId);

if (itemFromStore != null) {
var map = json.decode(itemFromStore);

if (map != null) {
return fromJson(map);
}
}
if (itemFromStore != null) return fromJson(json.decode(itemFromStore));
}
return null;
}
Expand Down
19 changes: 8 additions & 11 deletions lib/src/objects/parse_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,13 @@ class ParseUser extends ParseObject implements ParseCloneable {
client ?? ParseHTTPClient(ParseCoreData().securityContext);

// We can't get the current user and session without a sessionId
if (token == null && ParseCoreData().sessionId == null) {
if ((ParseCoreData().sessionId == null) && (token == null)) {
return null;
}

if (token == null) {
token = ParseCoreData().sessionId;
final Map<String, String> headers = {};
if (token != null) {
headers[keyHeaderSessionToken] = token;
}

try {
Expand All @@ -85,8 +86,7 @@ class ParseUser extends ParseObject implements ParseCloneable {
host: tempUri.host,
path: "${tempUri.path}$keyEndPointUserName");

final response =
await _client.get(uri, headers: {keyHeaderSessionToken: token});
final response = await _client.get(uri, headers: headers);
return _handleResponse(_getEmptyUser(), response, ParseApiRQ.currentUser,
_debug, _getEmptyUser().className);
} on Exception catch (e) {
Expand Down Expand Up @@ -242,9 +242,7 @@ class ParseUser extends ParseObject implements ParseCloneable {
var uri = _client.data.serverUrl + "$path/$objectId";
var body =
json.encode(toJson(forApiRQ: true), toEncodable: dateTimeEncoder);
final response = await _client.put(uri,
headers: {keyHeaderSessionToken: _client.data.sessionId},
body: body);
final response = await _client.put(uri, body: body);
return _handleResponse(
this, response, ParseApiRQ.save, _debug, className);
} on Exception catch (e) {
Expand All @@ -257,9 +255,8 @@ class ParseUser extends ParseObject implements ParseCloneable {
Future<ParseResponse> destroy() async {
if (objectId != null) {
try {
final response = await _client.delete(
_client.data.serverUrl + "$path/$objectId",
headers: {keyHeaderSessionToken: _client.data.sessionId});
final response =
await _client.delete(_client.data.serverUrl + "$path/$objectId");
return _handleResponse(
this, response, ParseApiRQ.destroy, _debug, className);
} on Exception catch (e) {
Expand Down