Skip to content

Commit dcf122b

Browse files
authored
Merge pull request #39 from ioet/TT-153-new-user-flow-for-sign-up-sign-in
feat: TT-153 Sign Up Validation Function
2 parents c491cdc + 40cfd51 commit dcf122b

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

SignUpValidation/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Sign up Validation with Azure Function
2+
3+
## Description
4+
5+
This function restricts external users through email validation to allow only @ioet emails.
6+
7+
## Installation
8+
9+
The function doesn't require the installation of any external package.
10+
11+
## Usage
12+
13+
Tha function is called by the Azure Connector API, so you need to configure in the Connector API.
14+
15+
## Credits
16+
17+
[Sign up Validation with Azure Function](https://github.com/ioet/time-tracker-ui/wiki/Sign-up-Validation-with-Azure-Function)

SignUpValidation/function.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"bindings": [
3+
{
4+
"authLevel": "function",
5+
"type": "httpTrigger",
6+
"direction": "in",
7+
"name": "req",
8+
"methods": [
9+
"get",
10+
"post"
11+
]
12+
},
13+
{
14+
"type": "http",
15+
"direction": "out",
16+
"name": "res"
17+
}
18+
]
19+
}

SignUpValidation/index.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
};

SignUpValidation/sample.dat

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"ui_locales": "en-US",
3+
"email": "[email protected]",
4+
"surname": "Test User",
5+
"displayName": "Test User",
6+
"givenName": "Test User",
7+
"identities": [
8+
{
9+
"signInType": "federated",
10+
"issuer": "google.com",
11+
"issuerAssignedId": "102373679322388401018"
12+
}
13+
]
14+
}

0 commit comments

Comments
 (0)