Skip to content
Open
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
9 changes: 9 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"printWidth": 120,
"singleQuote": true,
"useTabs": false,
"tabWidth": 2,
"semi": true,
"bracketSpacing": true,
"trailingComma": "none"
}
21 changes: 15 additions & 6 deletions lib/api.activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
* permissions and limitations under the License.
*/

import { performRestRequest, encodeURIComponent } from './util.js';
import { callbackify } from 'node:util';
import { parseResponse, performRestRequest, encodeURIComponent } from './util.js';

const doRequest = callbackify(performRestRequest);

const HTTP_GET = 'GET';
const HTTP_POST = 'POST';
Expand All @@ -31,7 +34,7 @@ const HTTP_POST = 'POST';
* @param {ActivityStream} callback.activityStream The stream of activities
*/
const getCurrentUserActivityStream = (restCtx, options, callback) => {
performRestRequest(restCtx, '/api/activity', HTTP_GET, options, callback);
doRequest(restCtx, '/api/activity', HTTP_GET, options, parseResponse(callback));
};

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

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

/**
Expand All @@ -76,7 +85,7 @@ const getNotificationStream = (restCtx, options, 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', HTTP_POST, null, callback);
doRequest(restCtx, '/api/notifications/markRead', HTTP_POST, null, parseResponse(callback));
};

/**
Expand All @@ -87,7 +96,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', HTTP_POST, null, callback);
doRequest(restCtx, '/api/activity/collect', HTTP_POST, null, parseResponse(callback));
};

export { collect, markNotificationsRead, getNotificationStream, getActivityStream, getCurrentUserActivityStream };
20 changes: 11 additions & 9 deletions lib/api.admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@
* permissions and limitations under the License.
*/

import { format } from 'node:util';
import { callbackify, format } from 'node:util';
import { defaultTo, compose, not, equals } from 'ramda';

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

const doRequest = callbackify(performRestRequest);

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

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

/**
* This file aggregates those REST calls that are only beneficial to
Expand All @@ -42,7 +44,7 @@ const isNot302 = compose(not, equals(302));
* @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', HTTP_GET, { tenant: tenantAlias }, callback);
doRequest(globalAdminRestCtx, '/api/auth/signed/tenant', HTTP_GET, { tenant: tenantAlias }, parseResponse(callback));
};

/**
Expand All @@ -55,7 +57,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', HTTP_GET, { becomeUserId }, callback);
doRequest(adminRestCtx, '/api/auth/signed/become', HTTP_GET, { becomeUserId }, parseResponse(callback));
};

/**
Expand All @@ -71,10 +73,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', HTTP_POST, body, (error, body, response) => {
doRequest(restCtx, '/api/auth/signed', HTTP_POST, body, (error, response) => {
if (error) return callback(error);

if (isNot302(response.statusCode)) {
if (aint302(response.statusCode)) {
return callback({
code: response.statusCode,
msg: 'Unexpected response code'
Expand Down Expand Up @@ -181,7 +183,7 @@ const importUsers = (restCtx, tenantAlias, csvGenerator, authenticationStrategy,
forceProfileUpdate,
file: csvGenerator
};
performRestRequest(restCtx, '/api/user/import', HTTP_POST, parameters, callback);
doRequest(restCtx, '/api/user/import', HTTP_POST, parameters, parseResponse(callback));
};

/**
Expand All @@ -195,7 +197,7 @@ const importUsers = (restCtx, tenantAlias, csvGenerator, authenticationStrategy,
*/
const getAllUsersForTenant = (adminRestCtx, tenantAlias, callback) => {
const url = `/api/tenants/${encodeURIComponent(tenantAlias)}/users`;
performRestRequest(adminRestCtx, url, HTTP_GET, {}, callback);
doRequest(adminRestCtx, url, HTTP_GET, {}, parseResponse(callback));
};

export {
Expand Down
76 changes: 36 additions & 40 deletions lib/api.authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
* permissions and limitations under the License.
*/

import * as RestUtil from './util.js';
import { callbackify } from 'node:util';
import { parseResponse, performRestRequest, encodeURIComponent } from './util.js';

const doRequest = callbackify(performRestRequest);

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

/**
Expand All @@ -39,7 +42,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', HTTP_POST, null, callback);
doRequest(restCtx, '/api/auth/logout', HTTP_POST, null, parseResponse(callback));
};

/**
Expand All @@ -58,12 +61,12 @@ const changePassword = (restCtx, userId, oldPassword, newPassword, callback) =>
newPassword
};

RestUtil.performRestRequest(
doRequest(
restCtx,
`/api/user/${RestUtil.encodeURIComponent(userId)}/password`,
`/api/user/${encodeURIComponent(userId)}/password`,
HTTP_POST,
parameters,
callback
parseResponse(callback)
);
};

Expand All @@ -76,13 +79,7 @@ const changePassword = (restCtx, userId, oldPassword, newPassword, callback) =>
* @param {Object} callback.err Error object containing error code and error message
*/
const exists = (restCtx, username, callback) => {
RestUtil.performRestRequest(
restCtx,
`/api/auth/exists/${RestUtil.encodeURIComponent(username)}`,
HTTP_GET,
null,
callback
);
doRequest(restCtx, `/api/auth/exists/${encodeURIComponent(username)}`, HTTP_GET, null, parseResponse(callback));
};

/**
Expand All @@ -95,12 +92,12 @@ const exists = (restCtx, username, callback) => {
* @param {Object} callback.err Error object containing error code and error message
*/
const existsOnTenant = (restCtx, tenantAlias, username, callback) => {
RestUtil.performRestRequest(
doRequest(
restCtx,
`/api/auth/${RestUtil.encodeURIComponent(tenantAlias)}/exists/${RestUtil.encodeURIComponent(username)}`,
`/api/auth/${encodeURIComponent(tenantAlias)}/exists/${encodeURIComponent(username)}`,
HTTP_GET,
null,
callback
parseResponse(callback)
);
};

Expand All @@ -114,13 +111,7 @@ const existsOnTenant = (restCtx, tenantAlias, username, callback) => {
* @param {Object} callback.loginIds Object containing the login ids for a user
*/
const getUserLoginIds = (restCtx, userId, callback) => {
RestUtil.performRestRequest(
restCtx,
`/api/auth/loginIds/${RestUtil.encodeURIComponent(userId)}`,
HTTP_GET,
null,
callback
);
doRequest(restCtx, `/api/auth/loginIds/${encodeURIComponent(userId)}`, HTTP_GET, null, parseResponse(callback));
};

/**
Expand All @@ -137,7 +128,7 @@ const getUserLoginIds = (restCtx, userId, callback) => {
* @param {Object} callback.response The HTTP response object
*/
const twitterRedirect = (restCtx, callback) => {
RestUtil.performRestRequest(restCtx, '/api/auth/twitter', HTTP_POST, null, callback);
doRequest(restCtx, '/api/auth/twitter', HTTP_POST, null, parseResponse(callback));
};

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

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

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

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

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

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

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

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

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

/**
Expand All @@ -278,9 +269,14 @@ const shibbolethSPRedirect = (restCtx, parameters, callback) => {
*/
const shibbolethSPCallback = (restCtx, attributes, callback) => {
restCtx.additionalHeaders = attributes;
RestUtil.performRestRequest(restCtx, '/api/auth/shibboleth/sp/callback', HTTP_GET, null, function (...args) {
doRequest(restCtx, '/api/auth/shibboleth/sp/callback', HTTP_GET, null, (error, response) => {
delete restCtx.additionalHeaders;
return callback.apply(this, args);

return parseResponse(callback)(error, response);

// if (error) return callback(JSON.parse(error.message));
// return callback(null, );
// Return parseResponse(callback).apply(this, args);
});
};

Expand All @@ -298,7 +294,7 @@ const shibbolethSPCallback = (restCtx, attributes, callback) => {
* @param {Object} callback.response The HTTP response object
*/
const shibbolethTenantCallback = (restCtx, parameters, callback) => {
RestUtil.performRestRequest(restCtx, '/api/auth/shibboleth/callback', HTTP_GET, parameters, callback);
doRequest(restCtx, '/api/auth/shibboleth/callback', HTTP_GET, parameters, parseResponse(callback));
};

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

/**
Expand All @@ -323,7 +319,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}`, HTTP_GET, null, callback);
doRequest(restCtx, `/api/auth/local/reset/secret/${username}`, HTTP_GET, null, parseResponse(callback));
};

/**
Expand All @@ -337,12 +333,12 @@ const getResetPasswordSecret = (restCtx, username, callback) => {
* @param {Object} callback.err An error that occurred, if any
*/
const resetPassword = (restCtx, username, secret, newPassword, callback) => {
RestUtil.performRestRequest(
doRequest(
restCtx,
`/api/auth/local/reset/password/${username}`,
HTTP_POST,
{ secret, newPassword },
callback
parseResponse(callback)
);
};

Expand Down
Loading