Skip to content

Commit

Permalink
Cleaner implementation of getting auth
Browse files Browse the repository at this point in the history
  • Loading branch information
flovilmart committed Aug 9, 2018
1 parent d8e3cf4 commit 5b477c8
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/LiveQuery/ParseLiveQueryServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SchemaController from '../Controllers/SchemaController';
import _ from 'lodash';
import uuid from 'uuid';
import { runLiveQueryEventHandlers } from '../triggers';
import { getAuthForSessionToken } from '../Auth';
import { getAuthForSessionToken, Auth } from '../Auth';
import { getCacheController } from '../Controllers';

class ParseLiveQueryServer {
Expand Down Expand Up @@ -333,11 +333,12 @@ class ParseLiveQueryServer {
return matchesQuery(parseObject, subscription.query);
}

async getUserId(sessionToken: ?string): ?string {
async getAuthForSessionToken(sessionToken: ?string): { auth: ?Auth, userId: ?string } {
try {
const auth = await getAuthForSessionToken({ cacheController: this.cacheController, sessionToken: sessionToken });
return auth && auth.user && auth.user.id; // return the ID of the found user
return { auth, userId: auth && auth.user && auth.user.id }// return the ID of the found user
} catch(e) { /* ignore errors */ }
return {};
}

async _matchesCLP(classLevelPermissions: ?any, object: any, client: any, requestId: number, op: string): any {
Expand All @@ -348,7 +349,7 @@ class ParseLiveQueryServer {
}
const subscriptionSessionToken = subscriptionInfo.sessionToken;
const aclGroup = ['*'];
const userId = await this.getUserId(subscriptionSessionToken);
const { userId } = await this.getAuthForSessionToken(subscriptionSessionToken);
if (userId) {
aclGroup.push(userId);
}
Expand Down Expand Up @@ -391,7 +392,7 @@ class ParseLiveQueryServer {

const subscriptionSessionToken = subscriptionInfo.sessionToken;
// TODO: get auth there and de-duplicate code below to work with the same Auth obj.
const userId = await this.getUserId(subscriptionSessionToken);
const { auth, userId } = await this.getAuthForSessionToken(subscriptionSessionToken);
const isSubscriptionSessionTokenMatched = acl.getReadAccess(userId);
if (isSubscriptionSessionTokenMatched) {
return Promise.resolve(true);
Expand All @@ -406,7 +407,6 @@ class ParseLiveQueryServer {
return false;
}

const auth = await getAuthForSessionToken({ cacheController: this.cacheController, sessionToken: subscriptionSessionToken });
const roleNames = await auth.getUserRoles();
// Finally, see if any of the user's roles allow them read access
for (const role of roleNames) {
Expand All @@ -425,7 +425,7 @@ class ParseLiveQueryServer {
// Check client sessionToken matches ACL
const clientSessionToken = client.sessionToken;
if (clientSessionToken) {
const userId = await this.getUserId(clientSessionToken);
const { userId } = await this.getAuthForSessionToken(clientSessionToken);
return acl.getReadAccess(userId);
} else {
return isRoleMatched;
Expand Down

0 comments on commit 5b477c8

Please sign in to comment.