Skip to content

Commit d676c9a

Browse files
author
Lasim
committed
feat(gateway): refactor device detection and caching system for improved performance
1 parent 62caf9c commit d676c9a

File tree

7 files changed

+530
-36
lines changed

7 files changed

+530
-36
lines changed

services/gateway/src/core/auth/api-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import fetch from 'node-fetch';
33
import { StoredCredentials, UserInfo, TokenInfo, Team, AuthError, AuthenticationError } from '../../types/auth';
44
import { buildAuthConfig } from '../../utils/auth-config';
5-
import { Device, DeviceInfo } from '../../utils/device-detection';
5+
import { Device, DeviceInfo } from '../../types/device-cache';
66

77
export class DeployStackAPI {
88
private credentials: StoredCredentials;

services/gateway/src/core/auth/oauth.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { generateCodeVerifier, generateCodeChallenge, generateState } from '../.
55
import { buildAuthConfig } from '../../utils/auth-config';
66
import { CallbackServer } from './callback-server';
77
import { BrowserManager } from './browser';
8-
import { detectDeviceInfo } from '../../utils/device-detection';
8+
import { generateDeviceInfoFresh } from '../../utils/device-detection';
9+
import { DeviceInfoCache } from '../../utils/device-cache';
910
import {
1011
AuthenticationResult,
1112
AuthenticationOptions,
@@ -172,8 +173,31 @@ export class OAuth2Client {
172173
codeVerifier: string;
173174
}, spinner?: ReturnType<typeof ora>): Promise<TokenResponse> {
174175
try {
175-
// Detect device information for automatic registration
176-
const deviceInfo = await detectDeviceInfo();
176+
// Generate fresh device information during login (expensive operation)
177+
// This is the one time we want to do full device fingerprinting
178+
if (spinner) {
179+
spinner.text = 'Generating device fingerprint...';
180+
}
181+
182+
const deviceInfo = await generateDeviceInfoFresh();
183+
184+
// Cache the device info for future operations (30x performance improvement)
185+
try {
186+
await DeviceInfoCache.store(deviceInfo);
187+
if (spinner) {
188+
spinner.text = 'Device fingerprint cached successfully...';
189+
} else {
190+
console.log(chalk.green('📱 Device fingerprint cached for future performance'));
191+
}
192+
} catch (cacheError) {
193+
// Cache storage failure is non-critical during login, but log it prominently
194+
if (spinner) {
195+
spinner.text = 'Device fingerprint generated (cache storage failed)...';
196+
} else {
197+
console.log(chalk.yellow('⚠️ Device fingerprint generated but cache storage failed'));
198+
}
199+
console.warn('Cache storage error:', cacheError instanceof Error ? cacheError.message : String(cacheError));
200+
}
177201

178202
// Add device_name field required by backend schema
179203
const deviceInfoWithName = {

services/gateway/src/types/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Device } from '../utils/device-detection';
1+
import { Device } from './device-cache';
22

33
export interface StoredCredentials {
44
accessToken: string;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
export interface CachedDeviceInfo {
2+
// Core device information
3+
deviceInfo: DeviceInfo;
4+
5+
// Cache metadata
6+
cachedAt: string; // ISO timestamp of cache creation
7+
cacheVersion: string; // Cache format version for migrations
8+
9+
// Integrity protection
10+
checksum: string; // SHA256 of deviceInfo + salt
11+
salt: string; // Random salt for checksum
12+
13+
// Hardware change detection
14+
hardwareSignature: string; // Lightweight hardware signature for validation
15+
}
16+
17+
export interface DeviceInfo {
18+
hostname: string;
19+
os_type: string;
20+
os_version: string;
21+
arch: string;
22+
node_version: string;
23+
hardware_id: string;
24+
user_agent: string;
25+
}
26+
27+
export interface Device {
28+
id: string;
29+
user_id: string;
30+
device_name: string;
31+
hostname: string | null;
32+
hardware_id: string | null;
33+
os_type: string | null;
34+
os_version: string | null;
35+
arch: string | null;
36+
node_version: string | null;
37+
last_ip: string | null;
38+
user_agent: string | null;
39+
is_active: boolean;
40+
is_trusted: boolean;
41+
last_login_at: string | null;
42+
last_activity_at: string | null;
43+
created_at: string;
44+
updated_at: string;
45+
}

0 commit comments

Comments
 (0)