-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
Comments to understand functions in the backend #57
Changes from 4 commits
61d2940
08e03eb
6a12b91
2a59ae3
7e036de
879cbdd
20733f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
"use strict"; | ||
|
||
import akismetLib from "akismet"; | ||
import akismetLib from "akismet"; // spam protection for user-submitted text | ||
import AWS from "aws-sdk"; | ||
import badwords from "badwords/object"; | ||
import { Promise as BluebirdPromise } from "bluebird"; | ||
|
@@ -234,6 +234,7 @@ function haltOnTimeout(req: { timedout: any }, res: any, next: () => void) { | |
} | ||
} | ||
|
||
// checks if a property (name) exists in a source object and copies that property to a dest object | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To enable IDEs recognize the comment and shows the comment when developer hover on the function where it is been used, the following syntax would be better: /**
* Checks if a property exists in a source object and copies it to a destination object.
*
* @param name - The name of the property to check and copy.
* @param source - The source object containing the property.
* @param dest - The destination object to copy the property to. If undefined, a new object will be created.
* @returns The destination object with the property copied if it exists in the source object. Returns undefined if the source object is undefined.
* @throws {Error} If the source object is not an object or the destination object is not an object or undefined.
**/ More about TSDoc specification. https://tsdoc.org/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Second this! |
||
function ifDefinedSet( | ||
name: string, | ||
source: { [x: string]: any }, | ||
|
@@ -297,13 +298,15 @@ function hasAuthToken(req: { cookies: { [x: string]: any } }) { | |
return !!req.cookies[COOKIES.TOKEN]; | ||
} | ||
|
||
// returns uid associated with provided apikey | ||
function getUidForApiKey(apikey: any) { | ||
return pgQueryP_readOnly_wRetryIfEmpty( | ||
"select uid from apikeysndvweifu WHERE apikey = ($1);", | ||
[apikey] | ||
); | ||
} | ||
// http://en.wikipedia.org/wiki/Basic_access_authentication#Client_side | ||
// parses the apikey (username) from the header and calls doApiKeyAuth with it | ||
function doApiKeyBasicAuth( | ||
assigner: any, | ||
header: string, | ||
|
@@ -321,6 +324,7 @@ function doApiKeyBasicAuth( | |
return doApiKeyAuth(assigner, apikey, isOptional, req, res, next); | ||
} | ||
|
||
// verifies that apikey can be found in the apikeysndvweifu table and assigns the associated uid to req object | ||
function doApiKeyAuth( | ||
assigner: (arg0: any, arg1: string, arg2: number) => void, | ||
apikey: string, | ||
|
@@ -374,11 +378,12 @@ const getXidRecordByXidOwnerId = User.getXidRecordByXidOwnerId; | |
// }); | ||
// } | ||
|
||
// given an apikey and xid, retrieves the associated uid and sets it, along with xid, owner_id, and org_id, in the req object | ||
function doXidApiKeyAuth( | ||
assigner: (arg0: any, arg1: string, arg2: number) => void, | ||
apikey: any, | ||
xid: any, | ||
isOptional: any, | ||
isOptional: any, // whether | ||
req: AuthRequest, | ||
res: { status: (arg0: number) => void }, | ||
next: { | ||
|
@@ -387,7 +392,7 @@ function doXidApiKeyAuth( | |
(arg0?: string | undefined): void; | ||
} | ||
) { | ||
getUidForApiKey(apikey) | ||
getUidForApiKey(apikey) // returns uid associated with the provided apikey from apikeysndvweifu table | ||
.then( | ||
// Argument of type '(rows: string | any[]) => Promise<void> | undefined' is not assignable to parameter of type '(value: unknown) => void | PromiseLike<void | undefined> | undefined'. | ||
// Types of parameters 'rows' and 'value' are incompatible. | ||
|
@@ -425,7 +430,7 @@ function doXidApiKeyAuth( | |
} | ||
} | ||
let uidForCurrentUser = Number(rows[0].uid); | ||
assigner(req, "uid", uidForCurrentUser); | ||
assigner(req, "uid", uidForCurrentUser); // the uid associated with the owner and xid in the xids table | ||
assigner(req, "xid", xid); | ||
assigner(req, "owner_uid", uidForApiKey); | ||
assigner(req, "org_id", uidForApiKey); | ||
|
@@ -444,6 +449,9 @@ function doXidApiKeyAuth( | |
next("polis_err_auth_misc_23423"); | ||
}); | ||
} | ||
|
||
// checks that the auth_token in req.headers is associated with the uid value in req.body as per the auth_tokens table | ||
// runs the assigner function with the found uid and calls next - assigns uid to req object? | ||
function doHeaderAuth( | ||
assigner: (arg0: any, arg1: string, arg2: number) => void, | ||
isOptional: any, | ||
|
@@ -461,7 +469,7 @@ function doHeaderAuth( | |
next("polis_err_auth_no_such_token"); | ||
return; | ||
} | ||
if (req.body.uid && req.body.uid !== uid) { | ||
if (req.body.uid && req.body.uid !== uid) { // compares the uid retrieved based on the token to the req.body's uid | ||
res.status(401); | ||
next("polis_err_auth_mismatch_uid"); | ||
return; | ||
|
@@ -538,6 +546,7 @@ function initializePolisHelpers() { | |
const getPidPromise = User.getPidPromise; | ||
const getPidForParticipant = User.getPidForParticipant; | ||
|
||
// inserts into permanentCookieZidJoin | ||
function recordPermanentCookieZidJoin(permanentCookieToken: any, zid: any) { | ||
function doInsert() { | ||
return pgQueryP( | ||
|
@@ -831,17 +840,20 @@ function initializePolisHelpers() { | |
// }); | ||
// } | ||
|
||
// given an xid and conversation_id (zinvite?), verifies that the xid is whitelisted, and assigns the associated uid of that xid to the req object | ||
// calls onDone regardless of auth outcome | ||
function doXidConversationIdAuth( | ||
assigner: (arg0: any, arg1: string, arg2: number) => void, | ||
xid: any, | ||
conversation_id: any, | ||
isOptional: any, | ||
isOptional: any, // whether passing the auth check is optional | ||
req: AuthRequest, | ||
res: { status: (arg0: number) => void }, | ||
onDone: { (err: any): void; (arg0?: string): void } | ||
) { | ||
return getConversationInfoByConversationId(conversation_id) | ||
.then((conv: { org_id: any; zid: any }) => { | ||
// | ||
return getXidRecordByXidOwnerId( | ||
xid, | ||
conv.org_id, | ||
|
@@ -856,6 +868,7 @@ function initializePolisHelpers() { | |
// Type 'unknown' is not assignable to type 'any[]'.ts(2345) | ||
// @ts-ignore | ||
).then((rows: string | any[]) => { | ||
// if conversation requires a whitelist check and the xid is not whitelisted | ||
if (!rows || !rows.length) { | ||
if (isOptional) { | ||
return onDone(); | ||
|
@@ -875,7 +888,11 @@ function initializePolisHelpers() { | |
onDone(err); | ||
}); | ||
} | ||
|
||
// returns a middleware that after authenticating the current user, uses the passed assigner to assign the user's uid to the req object | ||
function _auth(assigner: any, isOptional: boolean) { | ||
|
||
// looks for key property in the body, headers, and query | ||
function getKey( | ||
req: { | ||
body: Body; | ||
|
@@ -887,6 +904,7 @@ function initializePolisHelpers() { | |
return req.body[key] || req?.headers?.[key] || req?.query?.[key]; | ||
} | ||
|
||
// returns a Promise that that when resolved (as a a result of auth success), assigns the uid of the current user to the req object | ||
function doAuth( | ||
req: { | ||
cookies: { [x: string]: any }; | ||
|
@@ -897,7 +915,7 @@ function initializePolisHelpers() { | |
res: { status: (arg0: number) => void } | ||
) { | ||
//var token = req.body.token; | ||
let token = req.cookies[COOKIES.TOKEN]; | ||
let token = req.cookies[COOKIES.TOKEN]; // currently req.cookies['token2'] | ||
let xPolisToken = req?.headers?.["x-polis"]; | ||
|
||
return new Promise(function ( | ||
|
@@ -915,8 +933,8 @@ function initializePolisHelpers() { | |
} | ||
if (xPolisToken) { | ||
logger.info("authtype: doHeaderAuth"); | ||
doHeaderAuth(assigner, isOptional, req, res, onDone); | ||
} else if (getKey(req, "polisApiKey") && getKey(req, "ownerXid")) { | ||
doHeaderAuth(assigner, isOptional, req, res, onDone); // runs assigner with the uid associated with xPolisToken and calls onDone after | ||
} else if (getKey(req, "polisApiKey") && getKey(req, "ownerXid")) { // if poliApiKey and ownerXid can be found in the body, headers or query properties of req | ||
doXidApiKeyAuth( | ||
assigner, | ||
getKey(req, "polisApiKey"), | ||
|
@@ -982,12 +1000,12 @@ function initializePolisHelpers() { | |
res, | ||
onDone | ||
); | ||
} else if (req.body.agid) { | ||
} else if (req.body.agid) { // create a new user, ... | ||
// Auto Gen user ID | ||
createDummyUser() | ||
.then( | ||
function (uid?: any) { | ||
let shouldAddCookies = _.isUndefined(req.body.xid); | ||
let shouldAddCookies = _.isUndefined(req.body.xid); // if there is no xid in req.body, shouldAddCookies is true | ||
if (!shouldAddCookies) { | ||
req.p = req.p || {}; | ||
req.p.uid = uid; | ||
|
@@ -1025,6 +1043,7 @@ function initializePolisHelpers() { | |
} | ||
}); | ||
} | ||
|
||
return function ( | ||
req: any, | ||
res: { status: (arg0: number) => void }, | ||
|
@@ -1089,6 +1108,7 @@ function initializePolisHelpers() { | |
return _auth(assigner, false); | ||
} | ||
|
||
// setting the so-called auto gen id in req.body | ||
function enableAgid(req: { body: Body }, res: any, next: () => void) { | ||
req.body.agid = 1; | ||
next(); | ||
|
@@ -1679,7 +1699,8 @@ function initializePolisHelpers() { | |
}); | ||
}); | ||
} | ||
|
||
|
||
// redirect to about page is request has zid but no conversation_id | ||
function redirectIfHasZidButNoConversationId( | ||
req: { body: { zid: any; conversation_id: any }; headers?: any }, | ||
res: { | ||
|
@@ -1984,6 +2005,7 @@ function initializePolisHelpers() { | |
] | ||
); | ||
} | ||
|
||
if ( | ||
Config.runPeriodicExportTests && | ||
!devMode && | ||
|
@@ -2031,6 +2053,7 @@ function initializePolisHelpers() { | |
}; | ||
setInterval(runExportTest, 6 * 60 * 60 * 1000); // every 6 hours | ||
} | ||
|
||
function handle_GET_dataExport( | ||
req: { p: { uid?: any; zid: any; unixTimestamp: number; format: any } }, | ||
res: { json: (arg0: {}) => void } | ||
|
@@ -2056,6 +2079,7 @@ function initializePolisHelpers() { | |
fail(res, 500, "polis_err_data_export123b", err); | ||
}); | ||
} | ||
|
||
function handle_GET_dataExport_results( | ||
req: { p: { filename: string } }, | ||
res: { redirect: (arg0: any) => void } | ||
|
@@ -2261,6 +2285,7 @@ function initializePolisHelpers() { | |
} | ||
}); | ||
} | ||
|
||
function handle_GET_bidToPid( | ||
req: { p: { zid: any; math_tick: any } }, | ||
res: { | ||
|
@@ -2285,6 +2310,7 @@ function initializePolisHelpers() { | |
); | ||
} | ||
|
||
// given a zid, get all the pids, xids involved in the conversation | ||
function getXids(zid: any) { | ||
// 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.ts(7009) | ||
// @ts-ignore | ||
|
@@ -2307,6 +2333,9 @@ function initializePolisHelpers() { | |
} | ||
); | ||
} | ||
|
||
// handler function | ||
// if the uid in the req object is the same as the zid's owner, retrieve and return the xids associated with the zid | ||
function handle_GET_xids( | ||
req: { p: { uid?: any; zid: any } }, | ||
res: { | ||
|
@@ -2330,14 +2359,15 @@ function initializePolisHelpers() { | |
} | ||
); | ||
} else { | ||
fail(res, 403, "polis_err_get_xids_not_authorized"); | ||
fail(res, 403, "polis_err_get_xids_not_authorized"); // uid of the req object is not the zid owner | ||
} | ||
}, | ||
function (err: any) { | ||
fail(res, 500, "polis_err_get_xids", err); | ||
} | ||
); | ||
} | ||
|
||
function handle_POST_xidWhitelist( | ||
req: { p: { xid_whitelist: any; uid?: any } }, | ||
res: { | ||
|
@@ -2676,6 +2706,8 @@ Feel free to reply to this email if you need help.`; | |
JSON.stringify(res?._headers?.["set-cookie"]) | ||
); | ||
} | ||
|
||
// verifies that the uid in req.body is associated with the token found in the cookies and then assigns it in the req object | ||
function doCookieAuth( | ||
assigner: (arg0: any, arg1: string, arg2: number) => void, | ||
isOptional: any, | ||
|
@@ -3725,6 +3757,7 @@ Feel free to reply to this email if you need help.`; | |
); | ||
} | ||
|
||
// given a zid and uid, checks if the uid is the same as the conversation owner's uid | ||
function isOwner(zid: any, uid: string) { | ||
return getConversationInfo(zid).then(function (info: any) { | ||
return info.owner === uid; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a comment block at the beginning of your file to explain the purpose and key features of imported libraries is a good practice, especially when those libraries are not immediately obvious from the code itself or are less commonly used.
Here's how you might structure such a comment block at the beginning of the file, using the
akismet
example: