-
Notifications
You must be signed in to change notification settings - Fork 0
/
dkimValidator.js
58 lines (48 loc) · 1.4 KB
/
dkimValidator.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
//parses TXT record into an array
var buildItemsArray = (item) => {
var splittedItem = item.trim().split('=');
var itemKey = splittedItem[0];
var itemValue = splittedItem[1];
return { [itemKey]: itemValue };
};
exports.validate = (response) => {
if (response === undefined) {
return {
result: false,
reason: 'unknown_response_from_server'
}
}
//validation rules
var validationRules = { v: 'DKIM1', g: '*', k: 'rsa', p: /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/};
//get DKIM TXT record from result
var dkimData = response[0][0].split(';');
//parse it into an array
var itemsArray = dkimData.map(buildItemsArray);
var validationsErrors = {};
itemsArray.forEach((item) => {
var key = Object.keys(item)[0];
var value = item[key];
if (key !== 'p') {
if (value !== validationRules[key]) {
validationsErrors[key] = {found: value, shouldBe: validationRules[key]};
}
} else {
//public key regexp test
if (validationRules.p.test(value) === false) {
validationsErrors.p = {found: value, message: 'Invalid public key'};
}
}
});
if (Object.keys(validationsErrors).length === 0) {
return {
result: true,
dkimData: itemsArray
};
}
return {
result: false,
reason: 'invalid_dkim',
dkimData: itemsArray,
validationsErrors: validationsErrors
};
};