-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
127 lines (114 loc) · 4.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* Copyright 2019 Benny Megidish
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const fs = require('fs');
const path = require('path');
const blacklist = require('./database/domains-blacklist');
const tld = require('./database/academic-tlds');
/**
* Exposes academic email verifier API
* @author Benny Megidish
*/
class Verifier {
static get domainsBasePath() {
return path.resolve(__dirname, './domains');
}
/**
* check's if the email address is valid (in an proper email format - RFC 2822)
* @param {string} emailAddress email address to be checked
* @returns {boolean} true, if the email address is valid, false, otherwise
*/
static isValidEmailAddress(emailAddress) {
let re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(emailAddress).toLowerCase());
}
/**
* converts tld to url format
* @param {string} emailAddress email address to be converted
* @returns {string} tld as url
*/
static domainToUrl(domain) {
let domainUrl = domain.split('.');
domainUrl[0] = domainUrl[0] + '.txt';
domainUrl = domainUrl.reverse().join('/');
return domainUrl;
}
/**
* check's if the email address is academic
* @param {string} emailAddress email address to be checked
* @returns {Promise<boolean>} true, if the email address is academic, false, otherwise
*/
static isAcademic(emailAddress) {
return new Promise((resolve, reject) => {
if (!this.isValidEmailAddress(emailAddress)) {
reject(Error("Email address is not properly formatted"));
} else {
let domain = emailAddress.split("@").reverse()[0];
let domainUrl = this.domainToUrl(domain);
let domainPath = path.join(this.domainsBasePath, domainUrl);
// check if the domain is blacklisted
if (blacklist.indexOf(domain) === -1) {
// check if the domain file exist
fs.access(domainPath, fs.constants.F_OK, (error) => {
if (error) {
resolve(false);
} else {
resolve(true);
}
});
} else {
resolve(false);
}
}
});
}
/**
* query email address institution name
* @param {string} emailAddress email address to be checked
* @returns {Promise<string>} academic institution name, if exist, '', otherwise
*/
static getInstitutionName(emailAddress) {
return new Promise((resolve, reject) => {
if (!this.isValidEmailAddress(emailAddress)) {
reject(Error("Email address is not properly formatted"));
} else {
let domain = emailAddress.split("@").reverse()[0];
let domainUrl = this.domainToUrl(domain);
let domainPath = path.join(this.domainsBasePath, domainUrl);
// check if the domain is blacklisted
if (blacklist.indexOf(domain) === -1) {
fs.access(domainPath, fs.constants.R_OK, (error) => {
if (error) {
resolve('');
} else {
// try to read the domain institution name
fs.readFile(domainPath, (error, data) => {
if (error) {
console.error('[Academic Email Verifier]', error);
reject('');
} else {
resolve(data.toString('utf8'));
}
});
}
});
} else {
resolve('');
}
}
});
}
}
module.exports.Verifier = Verifier;