@striderlabs/mcp-booking 0.1.0

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/src/auth.ts ADDED
@@ -0,0 +1,118 @@
1
+ /**
2
+ * Strider Labs - Booking.com Auth/Session Management
3
+ *
4
+ * Handles cookie persistence and session management for Booking.com.
5
+ */
6
+
7
+ import * as fs from "fs";
8
+ import * as path from "path";
9
+ import * as os from "os";
10
+ import type { BrowserContext, Cookie } from "playwright";
11
+
12
+ const CONFIG_DIR = path.join(os.homedir(), ".strider", "booking");
13
+ const COOKIES_FILE = path.join(CONFIG_DIR, "cookies.json");
14
+ const SESSION_FILE = path.join(CONFIG_DIR, "session.json");
15
+
16
+ export interface SessionInfo {
17
+ isLoggedIn: boolean;
18
+ userEmail?: string;
19
+ userName?: string;
20
+ lastUpdated: string;
21
+ currency?: string;
22
+ }
23
+
24
+ /**
25
+ * Ensure config directory exists
26
+ */
27
+ function ensureConfigDir(): void {
28
+ if (!fs.existsSync(CONFIG_DIR)) {
29
+ fs.mkdirSync(CONFIG_DIR, { recursive: true });
30
+ }
31
+ }
32
+
33
+ /**
34
+ * Save cookies from browser context to disk
35
+ */
36
+ export async function saveCookies(context: BrowserContext): Promise<void> {
37
+ ensureConfigDir();
38
+ const cookies = await context.cookies();
39
+ fs.writeFileSync(COOKIES_FILE, JSON.stringify(cookies, null, 2));
40
+ }
41
+
42
+ /**
43
+ * Load cookies from disk and apply to browser context
44
+ */
45
+ export async function loadCookies(context: BrowserContext): Promise<boolean> {
46
+ if (!fs.existsSync(COOKIES_FILE)) {
47
+ return false;
48
+ }
49
+
50
+ try {
51
+ const cookiesJson = fs.readFileSync(COOKIES_FILE, "utf-8");
52
+ const cookies: Cookie[] = JSON.parse(cookiesJson);
53
+
54
+ // Filter out expired cookies
55
+ const now = Date.now() / 1000;
56
+ const validCookies = cookies.filter((c) => !c.expires || c.expires > now);
57
+
58
+ if (validCookies.length > 0) {
59
+ await context.addCookies(validCookies);
60
+ return true;
61
+ }
62
+ } catch (error) {
63
+ console.error("Failed to load cookies:", error);
64
+ }
65
+
66
+ return false;
67
+ }
68
+
69
+ /**
70
+ * Save session info to disk
71
+ */
72
+ export function saveSessionInfo(info: SessionInfo): void {
73
+ ensureConfigDir();
74
+ fs.writeFileSync(SESSION_FILE, JSON.stringify(info, null, 2));
75
+ }
76
+
77
+ /**
78
+ * Load session info from disk
79
+ */
80
+ export function loadSessionInfo(): SessionInfo | null {
81
+ if (!fs.existsSync(SESSION_FILE)) {
82
+ return null;
83
+ }
84
+
85
+ try {
86
+ const sessionJson = fs.readFileSync(SESSION_FILE, "utf-8");
87
+ return JSON.parse(sessionJson);
88
+ } catch (error) {
89
+ console.error("Failed to load session info:", error);
90
+ return null;
91
+ }
92
+ }
93
+
94
+ /**
95
+ * Clear all saved auth data
96
+ */
97
+ export function clearAuthData(): void {
98
+ if (fs.existsSync(COOKIES_FILE)) {
99
+ fs.unlinkSync(COOKIES_FILE);
100
+ }
101
+ if (fs.existsSync(SESSION_FILE)) {
102
+ fs.unlinkSync(SESSION_FILE);
103
+ }
104
+ }
105
+
106
+ /**
107
+ * Check if we have saved cookies (may or may not still be valid)
108
+ */
109
+ export function hasSavedCookies(): boolean {
110
+ return fs.existsSync(COOKIES_FILE);
111
+ }
112
+
113
+ /**
114
+ * Get the config directory path (useful for debugging)
115
+ */
116
+ export function getConfigDir(): string {
117
+ return CONFIG_DIR;
118
+ }