|
| 1 | +module.exports = async function (context, req) { |
| 2 | + context.log("JavaScript HTTP trigger function processed a request v1."); |
| 3 | + const API_VERSION = "1.0.0"; |
| 4 | + |
| 5 | + // Parse Basic Auth username and password |
| 6 | + var header = req.headers["authorization"] || "", // get the header |
| 7 | + token = header.split(/\s+/).pop() || "", // and the encoded auth token |
| 8 | + auth = new Buffer.from(token, "base64").toString(), // convert from base64 |
| 9 | + parts = auth.split(/:/), // split on colon |
| 10 | + username = parts[0], |
| 11 | + password = parts[1]; |
| 12 | + |
| 13 | + // Check for HTTP Basic Authentication, return HTTP 401 error if invalid credentials. |
| 14 | + if ( |
| 15 | + username !== process.env["BASIC_AUTH_USERNAME"] || |
| 16 | + password !== process.env["BASIC_AUTH_PASSWORD"] |
| 17 | + ) { |
| 18 | + context.res = { |
| 19 | + status: 401, |
| 20 | + }; |
| 21 | + context.log("Invalid Authentication"); |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + // If input data is null, return error. |
| 26 | + const INVALID_REQUEST = { |
| 27 | + status: 400, |
| 28 | + body: { |
| 29 | + version: API_VERSION, |
| 30 | + code: "INVALID_REQUEST", |
| 31 | + }, |
| 32 | + }; |
| 33 | + |
| 34 | + if (!(req.body && req.body.email)) { |
| 35 | + context.res = INVALID_REQUEST; |
| 36 | + context.log("Invalid Request"); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + // Log the request body |
| 41 | + context.log(`Request body: ${JSON.stringify(req.body)}`); |
| 42 | + |
| 43 | + // Get domain of email address |
| 44 | + const domain = req.body.email.split("@")[1]; |
| 45 | + const allowedDomains = ["ioet.com"]; |
| 46 | + |
| 47 | + context.log("Validation: ", allowedDomains.includes(domain.toLowerCase())); |
| 48 | + // Check that the domain of the email is from a specific other tenant |
| 49 | + if (!allowedDomains.includes(domain.toLowerCase())) { |
| 50 | + context.res = { |
| 51 | + body: { |
| 52 | + version: API_VERSION, |
| 53 | + action: "ShowBlockPage", |
| 54 | + userMessage: |
| 55 | + "You must have an account from a valid domain to register as an user for ioet Inc.", |
| 56 | + code: "SignUp-BlockByEmailDomain-0", |
| 57 | + }, |
| 58 | + }; |
| 59 | + context.log(context.res); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + // Email domain and user collected attribute are valid, return continuation response. |
| 64 | + context.res = { |
| 65 | + body: { version: API_VERSION, action: "Continue" }, |
| 66 | + }; |
| 67 | + |
| 68 | + context.log(context.res); |
| 69 | + return; |
| 70 | +}; |
0 commit comments