-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidators.js
37 lines (29 loc) · 908 Bytes
/
validators.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
const os = require("os");
const LOCAL_LINE_FEED = os.EOL;
class MissingLineFeedError extends Error {}
function resolveValidationFunction(validationType) {
switch (validationType) {
case "ssh":
return validateSsh;
default:
throw new Error(`Unrecognized validation type: ${validationType}`);
}
}
function validateSsh(sshKey) {
if (!sshKey.endsWith(LOCAL_LINE_FEED)) {
throw new MissingLineFeedError("Missing line feed character at the end of the file.");
}
const beginKeyRegexp = /^-----BEGIN [\w\s]{1,50} KEY-----\n/;
if (!beginKeyRegexp.test(sshKey)) {
throw new Error("Missing key beginning designation.");
}
const endKeyRegexp = /-----END [\w\s]{1,50} KEY-----\n\s*$/;
if (!endKeyRegexp.test(sshKey)) {
throw new Error("Missing key end designation.");
}
return true;
}
module.exports = {
resolveValidationFunction,
MissingLineFeedError,
};