Skip to content

Commit

Permalink
Merge pull request #9659 from Expensify/yuwen-authenticatePusher
Browse files Browse the repository at this point in the history
Use new AuthenticatePusher NewDot Optimized API commands
  • Loading branch information
luacmartins authored Jul 11, 2022
2 parents 83f0094 + ae3dc67 commit da22ec6
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 50 deletions.
6 changes: 3 additions & 3 deletions src/libs/Middleware/Logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ function Logging(response, request) {
// This error seems to only throw on dev when localhost:8080 tries to access the production web server. It's unclear whether this can happen on production or if
// it's a sign that the web server is down.
Log.hmmm('[Network] Error: Gateway Timeout error', {message: error.message, status: error.status});
} else if (request.command === 'Push_Authenticate') {
// Push_Authenticate requests can return with fetch errors and no message. It happens because we return a non 200 header like 403 Forbidden.
} else if (request.command === 'AuthenticatePusher') {
// AuthenticatePusher requests can return with fetch errors and no message. It happens because we return a non 200 header like 403 Forbidden.
// This is common to see if we are subscribing to a bad channel related to something the user shouldn't be able to access. There's no additional information
// we can get about these requests.
Log.hmmm('[Network] Error: Push_Authenticate', {message: error.message, status: error.status});
Log.hmmm('[Network] Error: AuthenticatePusher', {message: error.message, status: error.status});
} else if (error.message === CONST.ERROR.EXPENSIFY_SERVICE_INTERRUPTED) {
// Expensify site is down completely OR
// Auth (database connection) is down / bedrock has timed out while making a request. We currently can't tell the difference between Auth down and bedrock timing out.
Expand Down
2 changes: 1 addition & 1 deletion src/libs/Navigation/AppNavigator/AuthScreens.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class AuthScreens extends React.Component {
Pusher.init({
appKey: CONFIG.PUSHER.APP_KEY,
cluster: CONFIG.PUSHER.CLUSTER,
authEndpoint: `${CONFIG.EXPENSIFY.URL_API_ROOT}api?command=Push_Authenticate`,
authEndpoint: `${CONFIG.EXPENSIFY.URL_API_ROOT}api?command=AuthenticatePusher`,
}).then(() => {
Report.subscribeToUserEvents();
User.subscribeToUserEvents();
Expand Down
2 changes: 1 addition & 1 deletion src/libs/Pusher/pusher.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function init(args, params) {
// If we want to pass params in our requests to api.php we'll need to add it to socket.config.auth.params
// as per the documentation
// (https://pusher.com/docs/channels/using_channels/connection#channels-options-parameter).
// Any param mentioned here will show up in $_REQUEST when we call "Push_Authenticate". Params passed here need
// Any param mentioned here will show up in $_REQUEST when we call "AuthenticatePusher". Params passed here need
// to pass our inputRules to show up in the request.
if (params) {
socket.config.auth = {};
Expand Down
54 changes: 27 additions & 27 deletions src/libs/actions/Session/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ function validateEmail(accountID, validateCode) {
// subscribe to a bunch of channels at once we will only reauthenticate and force reconnect Pusher once.
const reauthenticatePusher = _.throttle(() => {
Log.info('[Pusher] Re-authenticating and then reconnecting');
Authentication.reauthenticate('Push_Authenticate')
Authentication.reauthenticate('AuthenticatePusher')
.then(Pusher.reconnect)
.catch(() => {
console.debug(
Expand All @@ -459,39 +459,39 @@ const reauthenticatePusher = _.throttle(() => {
function authenticatePusher(socketID, channelName, callback) {
Log.info('[PusherAuthorizer] Attempting to authorize Pusher', false, {channelName});

DeprecatedAPI.Push_Authenticate({
// We use makeRequestWithSideEffects here because we need to authorize to Pusher (an external service) each time a user connects to any channel.
// eslint-disable-next-line rulesdir/no-api-side-effects-method
API.makeRequestWithSideEffects('AuthenticatePusher', {
socket_id: socketID,
channel_name: channelName,
shouldRetry: false,
forceNetworkRequest: true,
})
.then((response) => {
if (response.jsonCode === CONST.JSON_CODE.NOT_AUTHENTICATED) {
Log.hmmm('[PusherAuthorizer] Unable to authenticate Pusher because authToken is expired');
callback(new Error('Pusher failed to authenticate because authToken is expired'), {auth: ''});
}).then((response) => {
if (response.jsonCode === CONST.JSON_CODE.NOT_AUTHENTICATED) {
Log.hmmm('[PusherAuthorizer] Unable to authenticate Pusher because authToken is expired');
callback(new Error('Pusher failed to authenticate because authToken is expired'), {auth: ''});

// Attempt to refresh the authToken then reconnect to Pusher
reauthenticatePusher();
return;
}
// Attempt to refresh the authToken then reconnect to Pusher
reauthenticatePusher();
return;
}

if (response.jsonCode !== CONST.JSON_CODE.SUCCESS) {
Log.hmmm('[PusherAuthorizer] Unable to authenticate Pusher for reason other than expired session');
callback(new Error(`Pusher failed to authenticate because code: ${response.jsonCode} message: ${response.message}`), {auth: ''});
return;
}
if (response.jsonCode !== CONST.JSON_CODE.SUCCESS) {
Log.hmmm('[PusherAuthorizer] Unable to authenticate Pusher for reason other than expired session');
callback(new Error(`Pusher failed to authenticate because code: ${response.jsonCode} message: ${response.message}`), {auth: ''});
return;
}

Log.info(
'[PusherAuthorizer] Pusher authenticated successfully',
false,
{channelName},
);
callback(null, response);
})
.catch((error) => {
Log.hmmm('[PusherAuthorizer] Unhandled error: ', {channelName, error});
callback(new Error('Push_Authenticate request failed'), {auth: ''});
});
Log.info(
'[PusherAuthorizer] Pusher authenticated successfully',
false,
{channelName},
);
callback(null, response);
}).catch((error) => {
Log.hmmm('[PusherAuthorizer] Unhandled error: ', {channelName, error});
callback(new Error('AuthenticatePusher request failed'), {auth: ''});
});
}

/**
Expand Down
14 changes: 0 additions & 14 deletions src/libs/deprecatedAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,19 +278,6 @@ function PreferredLocale_Update(parameters) {
return Network.post(commandName, parameters);
}

/**
* @param {Object} parameters
* @param {String} parameters.socket_id
* @param {String} parameters.channel_name
* @returns {Promise}
*/
function Push_Authenticate(parameters) {
const commandName = 'Push_Authenticate';
requireParameters(['socket_id', 'channel_name'],
parameters, commandName);
return Network.post(commandName, parameters);
}

/**
* @param {Object} parameters
* @param {Number} parameters.reportID
Expand Down Expand Up @@ -936,7 +923,6 @@ export {
PersonalDetails_Update,
Plaid_GetLinkToken,
Policy_Employees_Merge,
Push_Authenticate,
RejectTransaction,
Report_AddComment,
Report_GetHistory,
Expand Down
2 changes: 1 addition & 1 deletion tests/actions/ReportTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('actions/Report', () => {
Pusher.init({
appKey: CONFIG.PUSHER.APP_KEY,
cluster: CONFIG.PUSHER.CLUSTER,
authEndpoint: `${CONFIG.EXPENSIFY.URL_API_ROOT}api?command=Push_Authenticate`,
authEndpoint: `${CONFIG.EXPENSIFY.URL_API_ROOT}api?command=AuthenticatePusher`,
});

Onyx.init({
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/NetworkTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ test('Sequential queue will succeed if triggered while reauthentication via main
})
.then(() => {
// When we queue both non-persistable and persistable commands that will trigger reauthentication and go offline at the same time
Network.post('Push_Authenticate', {content: 'value1'});
Network.post('AuthenticatePusher', {content: 'value1'});
Onyx.set(ONYXKEYS.NETWORK, {isOffline: true});
expect(NetworkStore.isOffline()).toBe(false);
expect(NetworkStore.isAuthenticating()).toBe(false);
Expand Down Expand Up @@ -678,9 +678,9 @@ test('Sequential queue will succeed if triggered while reauthentication via main
// We are not offline anymore
expect(NetworkStore.isOffline()).toBe(false);

// First call to xhr is the Push_Authenticate request that could not call Authenticate because we went offline
// First call to xhr is the AuthenticatePusher request that could not call Authenticate because we went offline
const [firstCommand] = xhr.mock.calls[0];
expect(firstCommand).toBe('Push_Authenticate');
expect(firstCommand).toBe('AuthenticatePusher');

// Second call to xhr is the MockCommand that also failed with a 407
const [secondCommand] = xhr.mock.calls[1];
Expand Down

0 comments on commit da22ec6

Please sign in to comment.