-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauth.js
More file actions
135 lines (106 loc) · 4.35 KB
/
Copy pathauth.js
File metadata and controls
135 lines (106 loc) · 4.35 KB
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
128
129
130
131
132
133
134
135
var fs = require('fs');
var httpAuth = require('http-auth');
module.exports = {
init: function(instance, auth, scope, options) {
if (options[scope] && options[scope].type == 'basic') {
auth.basic.bind(instance)(options[scope], options);
} else if (options[scope] && options[scope].type == 'ssl') {
auth.ssl.bind(instance)(options[scope], options);
} else if (options[scope]) {
throw new Error('Unknown authentication method: ' + options[scope].type);
}
},
server: {
basic: function(params) {
if (!params.key && !params.keyFile) {
throw new Error('Need either :key or :keyFile set for Basic Auth');
}
var key = params.key;
if (params.keyFile) {
if (!fs.existsSync(params.keyFile)) {
throw new Error('File does not exist: ' + params.keyFile);
}
key = fs.readFileSync(params.keyFile).toString();
}
var basic = httpAuth.basic({
realm: "Shoal Manager"
});
basic.options.users = [{username: params.username || 'shoal', hash: key}];
this._app.use(httpAuth.connect(basic));
},
ssl: function(params, options) {
if (!params.keyFile || !params.certFile || !params.caFile) {
throw new Error('Need :keyFile, :certFile and :caFile to configure SSL');
}
if (options.configDir) params.keyFile = path.resolve(options.configDir, params.keyFile);
if (options.configDir) params.certFile = path.resolve(options.configDir, params.certFile);
if (options.configDir) params.caFile = path.resolve(options.configDir, params.caFile);
if (!fs.existsSync(params.keyFile)) {
throw new Error('File does not exist: ' + params.keyFile);
}
if (!fs.existsSync(params.certFile)) {
throw new Error('File does not exist: ' + params.certFile);
}
if (!fs.existsSync(params.caFile)) {
throw new Error('File does not exist: ' + params.caFile);
}
this._httpsOptions = {
key: fs.readFileSync(params.keyFile),
cert: fs.readFileSync(params.certFile),
ca: fs.readFileSync(params.caFile),
requestCert: true,
rejectUnauthorized: true
};
if (params.passphrase)
this._httpsOptions.passphrase = params.passphrase;
}
},
client: {
basic: function(params) {
if (!params.key && !params.keyFile) {
throw new Error('Need either :key or :keyFile set for Basic Auth');
}
var key = params.key;
if (params.keyFile) {
if (!fs.existsSync(params.keyFile)) {
throw new Error('File does not exist: ' + params.keyFile);
}
key = fs.readFileSync(params.keyFile).toString();
}
this._basicAuth = {
'user': params.username || 'shoal',
'pass': key,
'sendImmediately': true
};
},
ssl: function(params, options) {
if (!params.keyFile || !params.certFile) {
throw new Error('Need :keyFile, :certFile and :caFile to configure SSL');
}
if (options.configDir && params.keyFile) params.keyFile = path.resolve(options.configDir, params.keyFile);
if (options.configDir && params.certFile) params.certFile = path.resolve(options.configDir, params.certFile);
if (options.configDir && params.caFile) params.caFile = path.resolve(options.configDir, params.caFile);
if (!fs.existsSync(params.keyFile)) {
throw new Error('File does not exist: ' + params.keyFile);
}
if (!fs.existsSync(params.certFile)) {
throw new Error('File does not exist: ' + params.certFile);
}
if (params.caFile && !fs.existsSync(params.caFile)) {
throw new Error('File does not exist: ' + params.caFile);
}
if (!this._httpOptions.agentOptions)
this._httpOptions.agentOptions = {};
this._httpOptions.agentOptions.cert = fs.readFileSync(params.certFile).toString();
this._httpOptions.agentOptions.key = fs.readFileSync(params.keyFile).toString();
this._httpOptions.agentOptions.rejectUnauthorized = false;
if (params.caFile) {
this._httpOptions.agentOptions.ca = fs.readFileSync(params.caFile).toString();
this._httpOptions.agentOptions.rejectUnauthorized = true;
}
if (params.passphrase)
this._httpOptions.agentOptions.passphrase = params.passphrase;
this._httpOptions.protocolStr = 'https';
}
}
};