@teamnetwork-nz/sonicwall-api-mcp-server 0.4.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +134 -100
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -95,29 +95,28 @@ function md5(s) {
|
|
|
95
95
|
function sha256(s) {
|
|
96
96
|
return (0, import_crypto.createHash)("sha256").update(s).digest("hex");
|
|
97
97
|
}
|
|
98
|
-
function
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
if (!
|
|
98
|
+
function parseAuthenticateHeader(value) {
|
|
99
|
+
const segments = value.split(/,\s*(?=Digest\s)/i).filter((s) => /^\s*Digest\s/i.test(s)).map((s) => s.replace(/^\s*Digest\s+/i, ""));
|
|
100
|
+
const challenges = [];
|
|
101
|
+
for (const seg of segments) {
|
|
102
|
+
const params = {};
|
|
103
|
+
for (const m of seg.matchAll(/(\w+)="([^"]*)"/g)) params[m[1]] = m[2];
|
|
104
|
+
for (const m of seg.matchAll(/(\w+)=([^",\s]+)/g)) if (!(m[1] in params)) params[m[1]] = m[2];
|
|
105
|
+
if (params.realm && params.nonce) {
|
|
106
|
+
challenges.push({
|
|
107
|
+
realm: params.realm,
|
|
108
|
+
nonce: params.nonce,
|
|
109
|
+
qop: params.qop,
|
|
110
|
+
opaque: params.opaque,
|
|
111
|
+
algorithm: params.algorithm ?? "MD5"
|
|
112
|
+
});
|
|
113
|
+
}
|
|
105
114
|
}
|
|
106
|
-
return
|
|
115
|
+
return challenges;
|
|
107
116
|
}
|
|
108
|
-
function
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const hash = algo === "SHA-256" ? sha256 : md5;
|
|
112
|
-
const nc = "00000001";
|
|
113
|
-
const cnonce = Math.random().toString(36).substring(2, 10);
|
|
114
|
-
const ha1 = hash(`${username}:${realm}:${password}`);
|
|
115
|
-
const ha2 = hash(`${method}:${uri}`);
|
|
116
|
-
const response = qop === "auth" ? hash(`${ha1}:${nonce}:${nc}:${cnonce}:auth:${ha2}`) : hash(`${ha1}:${nonce}:${ha2}`);
|
|
117
|
-
let h = `Digest username="${username}", realm="${realm}", nonce="${nonce}", uri="${uri}", response="${response}"`;
|
|
118
|
-
if (qop === "auth") h += `, qop=auth, nc=${nc}, cnonce="${cnonce}"`;
|
|
119
|
-
if (algo !== "MD5") h += `, algorithm=${algo}`;
|
|
120
|
-
return h;
|
|
117
|
+
function selectChallenge(challenges) {
|
|
118
|
+
if (challenges.length === 0) return null;
|
|
119
|
+
return challenges.find((c) => c.algorithm.toUpperCase().startsWith("SHA-256")) ?? challenges[0];
|
|
121
120
|
}
|
|
122
121
|
var AuthManager = class {
|
|
123
122
|
constructor(username, password) {
|
|
@@ -126,16 +125,21 @@ var AuthManager = class {
|
|
|
126
125
|
}
|
|
127
126
|
username;
|
|
128
127
|
password;
|
|
129
|
-
|
|
128
|
+
challenge = null;
|
|
129
|
+
nc = 0;
|
|
130
|
+
bearerToken = null;
|
|
131
|
+
// some firmware returns one in the body
|
|
132
|
+
loggedIn = false;
|
|
130
133
|
lastUsed = 0;
|
|
131
134
|
pending = null;
|
|
132
135
|
isStale() {
|
|
133
|
-
return !this.
|
|
136
|
+
return !this.loggedIn || Date.now() - this.lastUsed > REAUTH_AFTER;
|
|
134
137
|
}
|
|
135
|
-
|
|
138
|
+
/** Ensure a POST /auth session has been established (and is fresh). */
|
|
139
|
+
async ensureLoggedIn(baseUrl) {
|
|
136
140
|
if (!this.isStale()) {
|
|
137
141
|
this.lastUsed = Date.now();
|
|
138
|
-
return
|
|
142
|
+
return;
|
|
139
143
|
}
|
|
140
144
|
if (!this.pending) {
|
|
141
145
|
this.pending = this._doLogin(baseUrl).finally(() => {
|
|
@@ -143,84 +147,114 @@ var AuthManager = class {
|
|
|
143
147
|
});
|
|
144
148
|
}
|
|
145
149
|
await this.pending;
|
|
146
|
-
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Authorization header for an individual request. Returns null if we have no
|
|
153
|
+
* cached challenge yet (the caller should send the request, capture the 401
|
|
154
|
+
* challenge via updateChallenge, then retry).
|
|
155
|
+
*/
|
|
156
|
+
authHeader(method, uri) {
|
|
157
|
+
if (this.bearerToken) return `Bearer ${this.bearerToken}`;
|
|
158
|
+
if (this.challenge) return this._buildDigest(method, uri);
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
/** Absorb a fresh challenge from a 401 response. Returns true if usable. */
|
|
162
|
+
updateChallenge(wwwAuthenticate) {
|
|
163
|
+
if (!wwwAuthenticate) return false;
|
|
164
|
+
const next = selectChallenge(parseAuthenticateHeader(wwwAuthenticate));
|
|
165
|
+
if (!next) return false;
|
|
166
|
+
this.challenge = next;
|
|
167
|
+
this.nc = 0;
|
|
168
|
+
return true;
|
|
147
169
|
}
|
|
148
170
|
invalidate() {
|
|
149
|
-
this.
|
|
171
|
+
this.loggedIn = false;
|
|
150
172
|
this.lastUsed = 0;
|
|
151
173
|
}
|
|
174
|
+
touch() {
|
|
175
|
+
this.lastUsed = Date.now();
|
|
176
|
+
}
|
|
152
177
|
async logout(baseUrl) {
|
|
153
|
-
if (!this.
|
|
178
|
+
if (!this.loggedIn) return;
|
|
154
179
|
try {
|
|
155
180
|
const headers = { "Accept": "application/json" };
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
181
|
+
const auth = this.authHeader("DELETE", new URL(`${baseUrl}/auth`).pathname);
|
|
182
|
+
if (auth) headers["Authorization"] = auth;
|
|
159
183
|
await fetch(`${baseUrl}/auth`, { method: "DELETE", headers });
|
|
160
184
|
} catch {
|
|
161
185
|
} finally {
|
|
162
|
-
this.
|
|
186
|
+
this.challenge = null;
|
|
187
|
+
this.bearerToken = null;
|
|
188
|
+
this.loggedIn = false;
|
|
163
189
|
this.lastUsed = 0;
|
|
164
190
|
}
|
|
165
191
|
}
|
|
192
|
+
_buildDigest(method, uri) {
|
|
193
|
+
const c = this.challenge;
|
|
194
|
+
const algo = c.algorithm.toUpperCase();
|
|
195
|
+
const sess = algo.endsWith("-SESS");
|
|
196
|
+
const hash = algo.startsWith("SHA-256") ? sha256 : md5;
|
|
197
|
+
this.nc += 1;
|
|
198
|
+
const ncHex = this.nc.toString(16).padStart(8, "0");
|
|
199
|
+
const cnonce = (0, import_crypto.randomBytes)(8).toString("hex");
|
|
200
|
+
let ha1 = hash(`${this.username}:${c.realm}:${this.password}`);
|
|
201
|
+
if (sess) ha1 = hash(`${ha1}:${c.nonce}:${cnonce}`);
|
|
202
|
+
const ha2 = hash(`${method}:${uri}`);
|
|
203
|
+
const response = c.qop ? hash(`${ha1}:${c.nonce}:${ncHex}:${cnonce}:${c.qop}:${ha2}`) : hash(`${ha1}:${c.nonce}:${ha2}`);
|
|
204
|
+
let h = `Digest username="${this.username}", realm="${c.realm}", nonce="${c.nonce}", uri="${uri}", response="${response}", algorithm=${c.algorithm}`;
|
|
205
|
+
if (c.qop) h += `, qop=${c.qop}, nc=${ncHex}, cnonce="${cnonce}"`;
|
|
206
|
+
if (c.opaque) h += `, opaque="${c.opaque}"`;
|
|
207
|
+
return h;
|
|
208
|
+
}
|
|
166
209
|
async _doLogin(baseUrl) {
|
|
167
210
|
const url = `${baseUrl}/auth`;
|
|
168
|
-
const
|
|
211
|
+
const uri = new URL(url).pathname;
|
|
212
|
+
let challengeResp;
|
|
169
213
|
try {
|
|
170
|
-
|
|
171
|
-
if (challenge) {
|
|
172
|
-
const authHeader = buildDigestHeader("POST", path, this.username, this.password, challenge);
|
|
173
|
-
const resp2 = await fetch(url, {
|
|
174
|
-
method: "POST",
|
|
175
|
-
headers: {
|
|
176
|
-
"Authorization": authHeader,
|
|
177
|
-
"Accept": "application/json",
|
|
178
|
-
"Content-Type": "application/json"
|
|
179
|
-
},
|
|
180
|
-
body: "{}"
|
|
181
|
-
});
|
|
182
|
-
if (resp2.status !== 401) {
|
|
183
|
-
await this._handleLoginResponse(resp2, url);
|
|
184
|
-
return;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
} catch (e) {
|
|
188
|
-
if (e instanceof AuthenticationError || e instanceof SessionConflictError) throw e;
|
|
189
|
-
}
|
|
190
|
-
const basicCreds = Buffer.from(`${this.username}:${this.password}`).toString("base64");
|
|
191
|
-
let resp;
|
|
192
|
-
try {
|
|
193
|
-
resp = await fetch(url, {
|
|
214
|
+
challengeResp = await fetch(url, {
|
|
194
215
|
method: "POST",
|
|
195
|
-
headers: {
|
|
196
|
-
"Authorization": `Basic ${basicCreds}`,
|
|
197
|
-
"Accept": "application/json",
|
|
198
|
-
"Content-Type": "application/json"
|
|
199
|
-
},
|
|
216
|
+
headers: { "Accept": "application/json", "Content-Type": "application/json" },
|
|
200
217
|
body: "{}"
|
|
201
218
|
});
|
|
202
219
|
} catch (e) {
|
|
203
220
|
throw new AuthenticationError(`Cannot connect to ${url}: ${e.message}`);
|
|
204
221
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
222
|
+
if (challengeResp.status < 400) {
|
|
223
|
+
await this._handleLoginResponse(challengeResp);
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
const haveChallenge = this.updateChallenge(challengeResp.headers.get("WWW-Authenticate"));
|
|
227
|
+
if (haveChallenge) {
|
|
228
|
+
const resp2 = await fetch(url, {
|
|
211
229
|
method: "POST",
|
|
212
|
-
headers: {
|
|
230
|
+
headers: {
|
|
231
|
+
"Authorization": this._buildDigest("POST", uri),
|
|
232
|
+
"Accept": "application/json",
|
|
233
|
+
"Content-Type": "application/json"
|
|
234
|
+
},
|
|
213
235
|
body: "{}"
|
|
214
236
|
});
|
|
215
|
-
|
|
216
|
-
|
|
237
|
+
if (resp2.status !== 401) {
|
|
238
|
+
await this._handleLoginResponse(resp2);
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
throw new AuthenticationError(
|
|
242
|
+
"Digest authentication rejected. Verify the username/password and that the account has SonicOS API access."
|
|
243
|
+
);
|
|
217
244
|
}
|
|
218
|
-
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
245
|
+
const basic = Buffer.from(`${this.username}:${this.password}`).toString("base64");
|
|
246
|
+
const resp = await fetch(url, {
|
|
247
|
+
method: "POST",
|
|
248
|
+
headers: {
|
|
249
|
+
"Authorization": `Basic ${basic}`,
|
|
250
|
+
"Accept": "application/json",
|
|
251
|
+
"Content-Type": "application/json"
|
|
252
|
+
},
|
|
253
|
+
body: "{}"
|
|
254
|
+
});
|
|
255
|
+
await this._handleLoginResponse(resp);
|
|
222
256
|
}
|
|
223
|
-
async _handleLoginResponse(resp
|
|
257
|
+
async _handleLoginResponse(resp) {
|
|
224
258
|
if (resp.status === 403) {
|
|
225
259
|
throw new SessionConflictError(
|
|
226
260
|
"Another admin session is currently active on this SonicWall appliance. Log out from the management UI or wait for the existing session to expire."
|
|
@@ -233,19 +267,20 @@ var AuthManager = class {
|
|
|
233
267
|
const text = await resp.text().catch(() => "");
|
|
234
268
|
throw new AuthenticationError(`Login failed with HTTP ${resp.status}: ${text.slice(0, 200)}`);
|
|
235
269
|
}
|
|
236
|
-
let token = null;
|
|
237
270
|
try {
|
|
238
271
|
const body = await resp.json();
|
|
239
272
|
const status = body.status;
|
|
240
273
|
const info = status?.info;
|
|
274
|
+
let token = null;
|
|
241
275
|
if (Array.isArray(info) && info.length > 0) {
|
|
242
276
|
token = info[0].bearer_token ?? null;
|
|
243
277
|
} else if (info && typeof info === "object") {
|
|
244
278
|
token = info.bearer_token ?? null;
|
|
245
279
|
}
|
|
280
|
+
if (token) this.bearerToken = token;
|
|
246
281
|
} catch {
|
|
247
282
|
}
|
|
248
|
-
this.
|
|
283
|
+
this.loggedIn = true;
|
|
249
284
|
this.lastUsed = Date.now();
|
|
250
285
|
}
|
|
251
286
|
};
|
|
@@ -337,38 +372,37 @@ var SonicWallClient = class {
|
|
|
337
372
|
);
|
|
338
373
|
url += `?${qs}`;
|
|
339
374
|
}
|
|
340
|
-
const
|
|
341
|
-
|
|
342
|
-
|
|
375
|
+
const u = new URL(url);
|
|
376
|
+
const target = u.pathname + u.search;
|
|
377
|
+
const m = method.toUpperCase();
|
|
378
|
+
await this.auth.ensureLoggedIn(this.baseUrl);
|
|
379
|
+
const buildHeaders = () => {
|
|
380
|
+
const headers = {
|
|
343
381
|
"Accept": "application/json",
|
|
344
382
|
"Content-Type": "application/json"
|
|
345
383
|
};
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
return headers2;
|
|
384
|
+
const auth = this.auth.authHeader(m, target);
|
|
385
|
+
if (auth) headers["Authorization"] = auth;
|
|
386
|
+
return headers;
|
|
350
387
|
};
|
|
351
|
-
let headers = await makeHeaders();
|
|
352
388
|
for (let attempt = 0; attempt < 2; attempt++) {
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
signal: AbortSignal.timeout(this.profile.requestTimeout * 1e3)
|
|
360
|
-
});
|
|
361
|
-
} catch (e) {
|
|
362
|
-
throw e;
|
|
363
|
-
}
|
|
389
|
+
const resp = await fetchWithRetry(url, {
|
|
390
|
+
method,
|
|
391
|
+
headers: buildHeaders(),
|
|
392
|
+
body: options?.body !== void 0 ? JSON.stringify(options.body) : void 0,
|
|
393
|
+
signal: AbortSignal.timeout(this.profile.requestTimeout * 1e3)
|
|
394
|
+
});
|
|
364
395
|
if (resp.status === 401 && attempt === 0) {
|
|
365
|
-
this.auth.
|
|
366
|
-
|
|
396
|
+
if (!this.auth.updateChallenge(resp.headers.get("WWW-Authenticate"))) {
|
|
397
|
+
this.auth.invalidate();
|
|
398
|
+
await this.auth.ensureLoggedIn(this.baseUrl);
|
|
399
|
+
}
|
|
367
400
|
continue;
|
|
368
401
|
}
|
|
369
402
|
if (resp.status >= 400) {
|
|
370
403
|
await this._throwApiError(resp);
|
|
371
404
|
}
|
|
405
|
+
this.auth.touch();
|
|
372
406
|
const text = await resp.text();
|
|
373
407
|
return text ? JSON.parse(text) : {};
|
|
374
408
|
}
|
|
@@ -2017,7 +2051,7 @@ async function main() {
|
|
|
2017
2051
|
const registry = new DeviceRegistry(config.profiles);
|
|
2018
2052
|
const server = new import_mcp.McpServer({
|
|
2019
2053
|
name: "sonicwall-mcp-server",
|
|
2020
|
-
version: "0.4.
|
|
2054
|
+
version: "0.4.1",
|
|
2021
2055
|
instructions: `SonicWall NSA MCP server \u2014 full read/write access to SonicWall firewalls via the SonicOS REST API.
|
|
2022
2056
|
|
|
2023
2057
|
## Device targeting
|