UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

125 lines (124 loc) 5.46 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const crypto = __importStar(require("crypto")); const IAuthenticationToken_1 = require("./IAuthenticationToken"); class HttpUtilities { /** * Creates a short, human-readable request description for logging. * Format: [HH:MM:SS] [usr:AABBCC] METHOD /path: * Or with token: [HH:MM:SS] [usr:AABBCCDDEE] METHOD /path: * @param req The HTTP request * @param clientIp The client IP address (used to generate user thumbprint) * @param tokenThumb Optional 4-char token thumbprint to append for authenticated users */ static getShortReqDescription(req, clientIp, tokenThumb) { const now = new Date(); const time = `${String(now.getHours()).padStart(2, "0")}:${String(now.getMinutes()).padStart(2, "0")}:${String(now.getSeconds()).padStart(2, "0")}`; let userPart = ""; if (clientIp) { const ipThumb = this.getThumbprint(clientIp); if (this.isLocalIp(clientIp)) { // For local connections, show "local" instead of IP thumbprint userPart = tokenThumb ? `[usr:local-${tokenThumb}]` : `[usr:local]`; } else { userPart = tokenThumb ? `[usr:${ipThumb}-${tokenThumb}]` : `[usr:${ipThumb}]`; } return `[${time}] ${userPart} ${req.method} ${req.url}: `; } return `[${time}] ${req.method} ${req.url}: `; } /** * Creates a 6-character thumbprint from an IP address or token for session identification. * * NOTE: This is NOT password hashing - it creates a short visual identifier for display/logging. * SHA-256 is appropriate here as we only need a collision-resistant fingerprint. */ static getThumbprint(value) { // Not password hashing; this is only a short display thumbprint. const hash = crypto.createHash("sha256").update(value).digest("hex"); return hash.substring(0, 6).toUpperCase(); } /** * Creates a 4-character thumbprint from an identifier for appending to user identification in logs. * * NOTE: This is NOT password hashing - it creates a short visual identifier for display/logging. * SHA-256 is appropriate here as we only need a collision-resistant fingerprint, not a secure * password hash. Actual authentication uses AES-GCM encryption, not this thumbprint. * * @param identifier A unique session identifier (NOT a password) to create a thumbprint from */ static getTokenThumbprint(identifier) { // Not password hashing; this is only a short display thumbprint. const hash = crypto.createHash("sha256").update(identifier).digest("hex"); return hash.substring(0, 4).toUpperCase(); } /** * Checks if an IP address is a local/loopback address. */ static isLocalIp(clientIp) { return clientIp === "127.0.0.1" || clientIp === "::1" || clientIp === "::ffff:127.0.0.1"; } /** * Formats a client IP address for display, with friendly names for local addresses. */ static formatClientIp(clientIp) { if (this.isLocalIp(clientIp)) { return "(local)"; } return clientIp; } /** * Returns a human-readable name for a server permission level. */ static getPermissionLevelName(level) { switch (level) { case IAuthenticationToken_1.ServerPermissionLevel.none: return "none"; case IAuthenticationToken_1.ServerPermissionLevel.displayReadOnly: return "display-read-only"; case IAuthenticationToken_1.ServerPermissionLevel.fullReadOnly: return "full-read-only"; case IAuthenticationToken_1.ServerPermissionLevel.updateState: return "update-state"; case IAuthenticationToken_1.ServerPermissionLevel.admin: return "admin"; default: return `unknown(${level})`; } } } exports.default = HttpUtilities;