Skip to content

Commit

Permalink
refactor: even more enhancements and CC issues
Browse files Browse the repository at this point in the history
  • Loading branch information
brecke committed Apr 17, 2020
1 parent 32ed891 commit e0ef8dd
Show file tree
Hide file tree
Showing 26 changed files with 628 additions and 497 deletions.
13 changes: 8 additions & 5 deletions lib/api.activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

import { performRestRequest, encodeURIComponent } from './util';

const HTTP_GET = 'GET';
const HTTP_POST = 'POST';

/**
* Get the activity stream of the user in context.
*
Expand All @@ -28,7 +31,7 @@ import { performRestRequest, encodeURIComponent } from './util';
* @param {ActivityStream} callback.activityStream The stream of activities
*/
const getCurrentUserActivityStream = (restCtx, opts, callback) => {
performRestRequest(restCtx, '/api/activity', 'GET', opts, callback);
performRestRequest(restCtx, '/api/activity', HTTP_GET, opts, callback);
};

/**
Expand All @@ -45,7 +48,7 @@ const getCurrentUserActivityStream = (restCtx, opts, callback) => {
* @param {ActivityStream} callback.activityStream The stream of activities
*/
const getActivityStream = (restCtx, activityStreamId, opts, callback) => {
performRestRequest(restCtx, '/api/activity/' + encodeURIComponent(activityStreamId), 'GET', opts, callback);
performRestRequest(restCtx, '/api/activity/' + encodeURIComponent(activityStreamId), HTTP_GET, opts, callback);
};

/**
Expand All @@ -61,7 +64,7 @@ const getActivityStream = (restCtx, activityStreamId, opts, callback) => {
* @param {ActivityStream} callback.notificationStream The stream of notifications
*/
const getNotificationStream = (restCtx, opts, callback) => {
performRestRequest(restCtx, '/api/notifications', 'GET', opts, callback);
performRestRequest(restCtx, '/api/notifications', HTTP_GET, opts, callback);
};

/**
Expand All @@ -73,7 +76,7 @@ const getNotificationStream = (restCtx, opts, callback) => {
* @param {Number} callback.lastReadTime The timestamp (millis since epoch) detailing the last time notifications were marked as read
*/
const markNotificationsRead = (restCtx, callback) => {
performRestRequest(restCtx, '/api/notifications/markRead', 'POST', null, callback);
performRestRequest(restCtx, '/api/notifications/markRead', HTTP_POST, null, callback);
};

/**
Expand All @@ -84,7 +87,7 @@ const markNotificationsRead = (restCtx, callback) => {
* @param {Object} callback.err Error object containing error code and error message
*/
const collect = (restCtx, callback) => {
performRestRequest(restCtx, '/api/activity/collect', 'POST', null, callback);
performRestRequest(restCtx, '/api/activity/collect', HTTP_POST, null, callback);
};

export { collect, markNotificationsRead, getNotificationStream, getActivityStream, getCurrentUserActivityStream };
39 changes: 19 additions & 20 deletions lib/api.admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@
*/

import util from 'util';
import { defaultTo, compose, not, equals } from 'ramda';

import { RestContext } from './model';
import { performRestRequest, encodeURIComponent } from './util';

const HTTP_GET = 'GET';
const HTTP_POST = 'POST';

// Auxiliary functions
const isNot302 = compose(not, equals(302));

// This file aggregates those REST calls that are only beneficial to a global and/or tenant administrators.
// It's expected that the RestContext objects that are passed into these methods reflect authenticated users whom are all administrators

Expand All @@ -31,7 +38,7 @@ import { performRestRequest, encodeURIComponent } from './util';
* @param {Object} callback.requestInfo The request info object containing the `url` and signed POST `body` to use to authenticate to the specified tenant
*/
const getSignedTenantAuthenticationRequestInfo = (globalAdminRestCtx, tenantAlias, callback) => {
performRestRequest(globalAdminRestCtx, '/api/auth/signed/tenant', 'GET', { tenant: tenantAlias }, callback);
performRestRequest(globalAdminRestCtx, '/api/auth/signed/tenant', HTTP_GET, { tenant: tenantAlias }, callback);
};

/**
Expand All @@ -44,7 +51,7 @@ const getSignedTenantAuthenticationRequestInfo = (globalAdminRestCtx, tenantAlia
* @param {Object} callback.requestInfo The request info object containing the `url` and signed POST `body` to use to authenticate and become the specified user
*/
const getSignedBecomeUserAuthenticationRequestInfo = (adminRestCtx, becomeUserId, callback) => {
performRestRequest(adminRestCtx, '/api/auth/signed/become', 'GET', { becomeUserId }, callback);
performRestRequest(adminRestCtx, '/api/auth/signed/become', HTTP_GET, { becomeUserId }, callback);
};

/**
Expand All @@ -60,12 +67,10 @@ const getSignedBecomeUserAuthenticationRequestInfo = (adminRestCtx, becomeUserId
* @param {Object} callback.err An error that occurred, if any
*/
const doSignedAuthentication = (restCtx, body, callback) => {
performRestRequest(restCtx, '/api/auth/signed', 'POST', body, (err, body, response) => {
if (err) {
return callback(err);
}
performRestRequest(restCtx, '/api/auth/signed', HTTP_POST, body, (err, body, response) => {
if (err) return callback(err);

if (response.statusCode !== 302) {
if (isNot302(response.statusCode)) {
return callback({
code: response.statusCode,
msg: 'Unexpected response code'
Expand All @@ -92,15 +97,13 @@ const doSignedAuthentication = (restCtx, body, callback) => {
const _doSignedAuthenticationWithRequestInfo = (requestInfo, internalBaseUrl, strictSSL, callback) => {
const parsedUrl = new URL(requestInfo.url);

const baseUrl = internalBaseUrl || util.format('%s//%s', parsedUrl.protocol, parsedUrl.host);
const baseUrl = defaultTo(util.format('%s//%s', parsedUrl.protocol, parsedUrl.host), internalBaseUrl);
const authenticatingRestCtx = new RestContext(baseUrl, {
hostHeader: parsedUrl.host,
strictSSL
});
doSignedAuthentication(authenticatingRestCtx, requestInfo.body, err => {
if (err) {
return callback(err);
}
if (err) return callback(err);

return callback(null, authenticatingRestCtx);
});
Expand All @@ -118,9 +121,7 @@ const _doSignedAuthenticationWithRequestInfo = (requestInfo, internalBaseUrl, st
*/
const loginAsUser = (adminRestCtx, becomeUserId, targetInternalBaseUrl, callback) => {
getSignedBecomeUserAuthenticationRequestInfo(adminRestCtx, becomeUserId, (err, requestInfo) => {
if (err) {
return callback(err);
}
if (err) return callback(err);

return _doSignedAuthenticationWithRequestInfo(requestInfo, targetInternalBaseUrl, adminRestCtx.strictSSL, callback);
});
Expand All @@ -138,9 +139,7 @@ const loginAsUser = (adminRestCtx, becomeUserId, targetInternalBaseUrl, callback
*/
const loginOnTenant = (globalAdminRestCtx, tenantAlias, targetInternalBaseUrl, callback) => {
getSignedTenantAuthenticationRequestInfo(globalAdminRestCtx, tenantAlias, (err, requestInfo) => {
if (err) {
return callback(err);
}
if (err) return callback(err);

return _doSignedAuthenticationWithRequestInfo(
requestInfo,
Expand Down Expand Up @@ -178,7 +177,7 @@ const importUsers = (restCtx, tenantAlias, csvGenerator, authenticationStrategy,
forceProfileUpdate,
file: csvGenerator
};
performRestRequest(restCtx, '/api/user/import', 'POST', params, callback);
performRestRequest(restCtx, '/api/user/import', HTTP_POST, params, callback);
};

/**
Expand All @@ -191,8 +190,8 @@ const importUsers = (restCtx, tenantAlias, csvGenerator, authenticationStrategy,
* @param {User[]} callback.response The users for the given tenantAlias
*/
const getAllUsersForTenant = (adminRestCtx, tenantAlias, callback) => {
const url = '/api/tenants/' + encodeURIComponent(tenantAlias) + '/users';
performRestRequest(adminRestCtx, url, 'GET', {}, callback);
const url = `/api/tenants/${encodeURIComponent(tenantAlias)}/users`;
performRestRequest(adminRestCtx, url, HTTP_GET, {}, callback);
};

export {
Expand Down
61 changes: 32 additions & 29 deletions lib/api.authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

import * as RestUtil from './util';

const HTTP_GET = 'GET';
const HTTP_POST = 'POST';

/**
* Log a user in through the REST API
*
Expand All @@ -25,7 +28,7 @@ import * as RestUtil from './util';
* @param {Object} callback.err Error object containing error code and error message
*/
const login = (restCtx, username, password, callback) => {
RestUtil.performRestRequest(restCtx, '/api/auth/login', 'POST', { username, password }, callback);
RestUtil.performRestRequest(restCtx, '/api/auth/login', HTTP_POST, { username, password }, callback);
};

/**
Expand All @@ -36,7 +39,7 @@ const login = (restCtx, username, password, callback) => {
* @param {Object} callback.err Error object containing error code and error message
*/
const logout = (restCtx, callback) => {
RestUtil.performRestRequest(restCtx, '/api/auth/logout', 'POST', null, callback);
RestUtil.performRestRequest(restCtx, '/api/auth/logout', HTTP_POST, null, callback);
};

/**
Expand All @@ -57,8 +60,8 @@ const changePassword = (restCtx, userId, oldPassword, newPassword, callback) =>

RestUtil.performRestRequest(
restCtx,
'/api/user/' + RestUtil.encodeURIComponent(userId) + '/password',
'POST',
`/api/user/${RestUtil.encodeURIComponent(userId)}/password`,
HTTP_POST,
params,
callback
);
Expand All @@ -75,8 +78,8 @@ const changePassword = (restCtx, userId, oldPassword, newPassword, callback) =>
const exists = (restCtx, username, callback) => {
RestUtil.performRestRequest(
restCtx,
'/api/auth/exists/' + RestUtil.encodeURIComponent(username),
'GET',
`/api/auth/exists/${RestUtil.encodeURIComponent(username)}`,
HTTP_GET,
null,
callback
);
Expand All @@ -94,8 +97,8 @@ const exists = (restCtx, username, callback) => {
const existsOnTenant = (restCtx, tenantAlias, username, callback) => {
RestUtil.performRestRequest(
restCtx,
'/api/auth/' + RestUtil.encodeURIComponent(tenantAlias) + '/exists/' + RestUtil.encodeURIComponent(username),
'GET',
`/api/auth/${RestUtil.encodeURIComponent(tenantAlias)}/exists/${RestUtil.encodeURIComponent(username)}`,
HTTP_GET,
null,
callback
);
Expand All @@ -113,16 +116,16 @@ const existsOnTenant = (restCtx, tenantAlias, username, callback) => {
const getUserLoginIds = (restCtx, userId, callback) => {
RestUtil.performRestRequest(
restCtx,
'/api/auth/loginIds/' + RestUtil.encodeURIComponent(userId),
'GET',
`/api/auth/loginIds/${RestUtil.encodeURIComponent(userId)}`,
HTTP_GET,
null,
callback
);
};

/// /////////////////////////////////////
// External authentication strategies //
/// /////////////////////////////////////
/**
* External authentication strategies
*/

/**
* Initiate the three-legged OAuth authorization steps for Twitter authentication
Expand All @@ -134,7 +137,7 @@ const getUserLoginIds = (restCtx, userId, callback) => {
* @param {Object} callback.response The HTTP response object
*/
const twitterRedirect = (restCtx, callback) => {
RestUtil.performRestRequest(restCtx, '/api/auth/twitter', 'POST', null, callback);
RestUtil.performRestRequest(restCtx, '/api/auth/twitter', HTTP_POST, null, callback);
};

/**
Expand All @@ -148,7 +151,7 @@ const twitterRedirect = (restCtx, callback) => {
* @param {Object} callback.response The HTTP response object
*/
const twitterCallback = (restCtx, params, callback) => {
RestUtil.performRestRequest(restCtx, '/api/auth/twitter/callback', 'GET', params, callback);
RestUtil.performRestRequest(restCtx, '/api/auth/twitter/callback', HTTP_GET, params, callback);
};

/**
Expand All @@ -161,7 +164,7 @@ const twitterCallback = (restCtx, params, callback) => {
* @param {Object} callback.response The HTTP response object
*/
const facebookRedirect = (restCtx, callback) => {
RestUtil.performRestRequest(restCtx, '/api/auth/facebook', 'POST', null, callback);
RestUtil.performRestRequest(restCtx, '/api/auth/facebook', HTTP_POST, null, callback);
};

/**
Expand All @@ -175,7 +178,7 @@ const facebookRedirect = (restCtx, callback) => {
* @param {Object} callback.response The HTTP response object
*/
const facebookCallback = (restCtx, params, callback) => {
RestUtil.performRestRequest(restCtx, '/api/auth/facebook/callback', 'GET', params, callback);
RestUtil.performRestRequest(restCtx, '/api/auth/facebook/callback', HTTP_GET, params, callback);
};

/**
Expand All @@ -188,7 +191,7 @@ const facebookCallback = (restCtx, params, callback) => {
* @param {Object} callback.response The HTTP response object
*/
const googleRedirect = (restCtx, callback) => {
RestUtil.performRestRequest(restCtx, '/api/auth/google', 'POST', null, callback);
RestUtil.performRestRequest(restCtx, '/api/auth/google', HTTP_POST, null, callback);
};

/**
Expand All @@ -202,7 +205,7 @@ const googleRedirect = (restCtx, callback) => {
* @param {Object} callback.response The HTTP response object
*/
const googleCallback = (restCtx, params, callback) => {
RestUtil.performRestRequest(restCtx, '/api/auth/google/callback', 'GET', params, callback);
RestUtil.performRestRequest(restCtx, '/api/auth/google/callback', HTTP_GET, params, callback);
};

/**
Expand All @@ -215,7 +218,7 @@ const googleCallback = (restCtx, params, callback) => {
* @param {Object} callback.response The HTTP response object
*/
const casRedirect = (restCtx, callback) => {
RestUtil.performRestRequest(restCtx, '/api/auth/cas', 'POST', null, callback);
RestUtil.performRestRequest(restCtx, '/api/auth/cas', HTTP_POST, null, callback);
};

/**
Expand All @@ -229,7 +232,7 @@ const casRedirect = (restCtx, callback) => {
* @param {Object} callback.response The HTTP response object
*/
const casCallback = (restCtx, params, callback) => {
RestUtil.performRestRequest(restCtx, '/api/auth/cas/callback', 'GET', params, callback);
RestUtil.performRestRequest(restCtx, '/api/auth/cas/callback', HTTP_GET, params, callback);
};

/**
Expand All @@ -243,7 +246,7 @@ const casCallback = (restCtx, params, callback) => {
* @param {Object} callback.response The HTTP response object
*/
const shibbolethTenantRedirect = (restCtx, redirectUrl, callback) => {
RestUtil.performRestRequest(restCtx, '/api/auth/shibboleth', 'POST', { redirectUrl }, callback);
RestUtil.performRestRequest(restCtx, '/api/auth/shibboleth', HTTP_POST, { redirectUrl }, callback);
};

/**
Expand All @@ -260,7 +263,7 @@ const shibbolethTenantRedirect = (restCtx, redirectUrl, callback) => {
* @param {Object} callback.response The HTTP response object
*/
const shibbolethSPRedirect = (restCtx, params, callback) => {
RestUtil.performRestRequest(restCtx, '/api/auth/shibboleth/sp', 'GET', params, callback);
RestUtil.performRestRequest(restCtx, '/api/auth/shibboleth/sp', HTTP_GET, params, callback);
};

/**
Expand All @@ -275,7 +278,7 @@ const shibbolethSPRedirect = (restCtx, params, callback) => {
*/
const shibbolethSPCallback = (restCtx, attributes, callback) => {
restCtx.additionalHeaders = attributes;
RestUtil.performRestRequest(restCtx, '/api/auth/shibboleth/sp/callback', 'GET', null, function(...args) {
RestUtil.performRestRequest(restCtx, '/api/auth/shibboleth/sp/callback', HTTP_GET, null, function(...args) {
delete restCtx.additionalHeaders;
return callback.apply(this, args);
});
Expand All @@ -295,7 +298,7 @@ const shibbolethSPCallback = (restCtx, attributes, callback) => {
* @param {Object} callback.response The HTTP response object
*/
const shibbolethTenantCallback = (restCtx, params, callback) => {
RestUtil.performRestRequest(restCtx, '/api/auth/shibboleth/callback', 'GET', params, callback);
RestUtil.performRestRequest(restCtx, '/api/auth/shibboleth/callback', HTTP_GET, params, callback);
};

/**
Expand All @@ -308,7 +311,7 @@ const shibbolethTenantCallback = (restCtx, params, callback) => {
* @param {Object} callback.err An error that occurred, if any
*/
const ldapLogin = (restCtx, username, password, callback) => {
RestUtil.performRestRequest(restCtx, '/api/auth/ldap', 'POST', { username, password }, callback);
RestUtil.performRestRequest(restCtx, '/api/auth/ldap', HTTP_POST, { username, password }, callback);
};

/**
Expand All @@ -320,7 +323,7 @@ const ldapLogin = (restCtx, username, password, callback) => {
* @param {Object} callback.err An error that occurred, if any
*/
const getResetPasswordSecret = (restCtx, username, callback) => {
RestUtil.performRestRequest(restCtx, '/api/auth/local/reset/secret/' + username, 'GET', null, callback);
RestUtil.performRestRequest(restCtx, `/api/auth/local/reset/secret/${username}`, HTTP_GET, null, callback);
};

/**
Expand All @@ -336,8 +339,8 @@ const getResetPasswordSecret = (restCtx, username, callback) => {
const resetPassword = (restCtx, username, secret, newPassword, callback) => {
RestUtil.performRestRequest(
restCtx,
'/api/auth/local/reset/password/' + username,
'POST',
`/api/auth/local/reset/password/${username}`,
HTTP_POST,
{ secret, newPassword },
callback
);
Expand Down
Loading

0 comments on commit e0ef8dd

Please sign in to comment.