Files
pedwfrontend/pages/api/endpoint/getportallogin_api.js
T

88 lines
2.4 KiB
JavaScript

/**
* @swagger
* /api/endpoint/getportallogin_api:
* get:
* tags: [Login]
* description: Returns portal login
* summary: Phase 2
* parameters:
* - name: emailaddress
* in: query
* required: true
* schema:
* type: string
* responses:
* 200:
* description: hello world
*/
import { azureHeadersPaged } from "../../../actions/core/headers";
import { hashAPIPath } from "../../../actions/core/hash";
import { respondError } from "../middleware/apiResponse";
import { relayGet } from "../middleware/relayForwarding";
export default async function ApiProxy(req, res) {
const emailAddress = req.query.emailAddress;
const checkHash = req.query.hash;
if (typeof emailAddress !== "string" || emailAddress.trim().length === 0) {
return respondError(res, {
status: 400,
code: "EMAIL_ADDRESS_REQUIRED",
message: "emailAddress is required"
});
}
if (typeof checkHash !== "string" || checkHash.length === 0) {
return respondError(res, {
status: 400,
code: "HASH_REQUIRED",
message: "hash is required"
});
}
const checkquerypath =
"/api/endpoint/getportallogin_api?emailAddress=" + emailAddress;
const encodedCheckquerypath =
"/api/endpoint/getportallogin_api?emailAddress=" +
encodeURIComponent(emailAddress);
const expectedHashCandidates = [
hashAPIPath(checkquerypath),
hashAPIPath(encodedCheckquerypath)
];
if (!expectedHashCandidates.includes("&hash=" + checkHash)) {
return respondError(res, {
status: 400,
code: "INVALID_HASH",
message: "Invalid hash"
});
}
const queryUrl =
"contacts?$filter=emailaddress1 eq '" +
emailAddress +
"' and statuscode eq 1&$count=true&$select=emailaddress1,contactid,yomifullname,firstname,lastname";
const relayPolicy = {
method: "GET",
timeoutMs: 5000,
maxRetries: 0,
retryBaseDelayMs: 0,
retryMaxDelayMs: 0
};
return relayGet({
queryUrl,
res,
requestOptionsBuilder: (accessToken) => azureHeadersPaged(accessToken),
relayPolicy,
errorResponse: {
status: 400,
code: "PORTAL_LOGIN_FETCH_FAILED",
message: "Failed to fetch portal login"
}
});
}