37 lines
2.4 KiB
TypeScript
37 lines
2.4 KiB
TypeScript
import { sys } from 'cc';
|
|
import signing from '../../../common-key/common_key.json';
|
|
import { GuestError, GuestLogin, GuestSettings } from './GuestContracts';
|
|
import { GuestHttpClient } from './GuestHttpClient';
|
|
import { GuestLoginService } from './GuestLoginService';
|
|
import { GuestSessionStore } from './GuestSessionStore';
|
|
import { HmacGuestSigner } from './GuestSignature';
|
|
|
|
/** 沿用已确认的服务配置,本地身份使用 Fruit Pop 命名空间。 / Reuse the approved service configuration with a Fruit Pop storage namespace. */
|
|
export const GUEST_SERVICE = { origin: 'https://gate.vivarisdigitalgate.com', product: 'VeryWellSlots', timeoutMs: 30000 } as const;
|
|
export function guestDeviceType(userAgent: string, touchPoints: number): 'iphone' | 'ipad' {
|
|
return /iPad|Tablet/i.test(userAgent) || (/Macintosh/i.test(userAgent) && touchPoints > 1) ? 'ipad' : 'iphone';
|
|
}
|
|
/** 只能使用安全随机源,不降级到 Math.random。 / Require secure randomness; never fall back to Math.random. */
|
|
export function secureGuestId(): string {
|
|
if (!globalThis.crypto || typeof globalThis.crypto.getRandomValues !== 'function') {
|
|
throw new GuestError('configuration', 'Secure sign-in is unavailable on this platform.');
|
|
}
|
|
const bytes = new Uint8Array(16);
|
|
globalThis.crypto.getRandomValues(bytes);
|
|
return Array.from(bytes, value => value.toString(16).padStart(2, '0')).join('');
|
|
}
|
|
/** 组合根:在这里连接配置、平台能力与独立领域服务。 / Composition root connecting configuration, platform ports and the independent service. */
|
|
export function createGuestLogin(): GuestLogin {
|
|
if (typeof globalThis.fetch !== 'function' || typeof AbortController === 'undefined') {
|
|
throw new GuestError('configuration', 'Network sign-in is unavailable on this platform.');
|
|
}
|
|
const settings: GuestSettings = {
|
|
...GUEST_SERVICE,
|
|
deviceType: typeof navigator === 'undefined' ? 'iphone' : guestDeviceType(navigator.userAgent, navigator.maxTouchPoints || 0),
|
|
registerDevice: sys.isNative ? (sys.os === sys.OS.ANDROID ? 'android-google' : 'ios') : 'web',
|
|
};
|
|
return new GuestLoginService(settings, new GuestSessionStore(sys.localStorage, settings.origin, settings.product),
|
|
new HmacGuestSigner(signing.key, secureGuestId),
|
|
new GuestHttpClient(settings.origin, settings.timeoutMs, globalThis.fetch.bind(globalThis)), secureGuestId);
|
|
}
|