Skip to content
Merged
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
17 changes: 17 additions & 0 deletions SignUpValidation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Sign up Validation with Azure Function

## Description

This function restricts external users through email validation to allow only @ioet emails.

## Installation

The function doesn't require the installation of any external package.

## Usage

Tha function is called by the Azure Connector API, so you need to configure in the Connector API.

## Credits

[Sign up Validation with Azure Function](https://github.com/ioet/time-tracker-ui/wiki/Sign-up-Validation-with-Azure-Function)
19 changes: 19 additions & 0 deletions SignUpValidation/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this functionality should be implemented as an Azure function. Why not making it part of the API?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep is a good idea, I created as azure function because the sign-up validation was part of the azure scope and azure provides some examples with azure function to validate the sign-up process with the API Connector tool, but I can create a ticket to migrate this function to the API, because now is implemented in the user flow.

"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
70 changes: 70 additions & 0 deletions SignUpValidation/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
module.exports = async function (context, req) {
context.log("JavaScript HTTP trigger function processed a request v1.");
const API_VERSION = "1.0.0";

// Parse Basic Auth username and password
var header = req.headers["authorization"] || "", // get the header
token = header.split(/\s+/).pop() || "", // and the encoded auth token
auth = new Buffer.from(token, "base64").toString(), // convert from base64
parts = auth.split(/:/), // split on colon
username = parts[0],
password = parts[1];

// Check for HTTP Basic Authentication, return HTTP 401 error if invalid credentials.
if (
username !== process.env["BASIC_AUTH_USERNAME"] ||
password !== process.env["BASIC_AUTH_PASSWORD"]
) {
context.res = {
status: 401,
};
context.log("Invalid Authentication");
return;
}

// If input data is null, return error.
const INVALID_REQUEST = {
status: 400,
body: {
version: API_VERSION,
code: "INVALID_REQUEST",
},
};

if (!(req.body && req.body.email)) {
context.res = INVALID_REQUEST;
context.log("Invalid Request");
return;
}

// Log the request body
context.log(`Request body: ${JSON.stringify(req.body)}`);

// Get domain of email address
const domain = req.body.email.split("@")[1];
const allowedDomains = ["ioet.com"];

context.log("Validation: ", allowedDomains.includes(domain.toLowerCase()));
// Check that the domain of the email is from a specific other tenant
if (!allowedDomains.includes(domain.toLowerCase())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're only looking for ioet.com as a valid domain?
I don't think so, you should find a way to make sure the token is correct and it is not expired.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point we didn't have the token yet.
Since is before the user is created, and it's validating the email with which the user is trying to log in with the google provider.

context.res = {
body: {
version: API_VERSION,
action: "ShowBlockPage",
userMessage:
"You must have an account from a valid domain to register as an user for ioet Inc.",
code: "SignUp-BlockByEmailDomain-0",
},
};
context.log(context.res);
return;
}

// Email domain and user collected attribute are valid, return continuation response.
context.res = {
body: { version: API_VERSION, action: "Continue" },
};

context.log(context.res);
return;
};
14 changes: 14 additions & 0 deletions SignUpValidation/sample.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"ui_locales": "en-US",
"email": "[email protected]",
"surname": "Test User",
"displayName": "Test User",
"givenName": "Test User",
"identities": [
{
"signInType": "federated",
"issuer": "google.com",
"issuerAssignedId": "102373679322388401018"
}
]
}