ripple-ai-detector
Version:
🌊 Ripple AI Bug Detector - Built by an AI that knows its flaws. Catch AI-generated bugs before you commit.
172 lines • 6.1 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthManager = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const os_1 = __importDefault(require("os"));
const usage_tracker_1 = require("../usage/usage-tracker");
class AuthManager {
authFile;
usageTracker;
constructor() {
// Store auth data in user's home directory
const rippleDir = path_1.default.join(os_1.default.homedir(), '.ripple');
this.authFile = path_1.default.join(rippleDir, 'auth.json');
this.usageTracker = new usage_tracker_1.UsageTracker();
}
// Login with license key
async login(licenseKey) {
try {
// Validate license key format
if (!this.isValidLicenseKeyFormat(licenseKey)) {
throw new Error('Invalid license key format');
}
// For MVP, we'll simulate license validation
// In production, this would call the API
const result = await this.validateLicenseKey(licenseKey);
// Save auth data
const authData = {
licenseKey,
user: result.user,
expiresAt: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000).toISOString(), // 30 days
lastValidated: new Date().toISOString()
};
await this.saveAuthData(authData);
// Update usage tracker with new plan
await this.usageTracker.updatePlan(result.user.plan, licenseKey);
return result;
}
catch (error) {
throw new Error(`Login failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
// Get authentication status
async getStatus() {
try {
const authData = await this.loadAuthData();
if (!authData) {
return { authenticated: false };
}
// Check if auth is expired
if (new Date(authData.expiresAt) < new Date()) {
await this.logout();
return { authenticated: false };
}
// Get current usage
const usage = await this.usageTracker.getUsage();
return {
authenticated: true,
licenseKey: authData.licenseKey,
user: authData.user,
usage: {
current: usage.current,
limit: usage.limit
}
};
}
catch (error) {
return { authenticated: false };
}
}
// Logout and clear credentials
async logout() {
try {
if (await fs_extra_1.default.pathExists(this.authFile)) {
await fs_extra_1.default.remove(this.authFile);
}
}
catch (error) {
// Fail silently
}
}
// Check if user is authenticated
async isAuthenticated() {
const status = await this.getStatus();
return status.authenticated;
}
// Get current license key
async getLicenseKey() {
const status = await this.getStatus();
return status.licenseKey || null;
}
// Validate license key (MVP implementation)
async validateLicenseKey(licenseKey) {
// For MVP, we'll use a simple validation
// In production, this would call the actual API
if (licenseKey.startsWith('rpl_free_')) {
return {
user: {
email: 'user@example.com',
plan: 'free',
id: 'user_123'
},
limits: {
validationsPerMonth: 50,
currentUsage: 0
}
};
}
if (licenseKey.startsWith('rpl_pro_')) {
return {
user: {
email: 'pro@example.com',
plan: 'pro',
id: 'user_456'
},
limits: {
validationsPerMonth: -1, // unlimited
currentUsage: 0
}
};
}
// For demo purposes, accept any key that looks valid
if (licenseKey.startsWith('rpl_') && licenseKey.length > 10) {
return {
user: {
email: 'demo@example.com',
plan: 'free',
id: 'user_demo'
},
limits: {
validationsPerMonth: 50,
currentUsage: 0
}
};
}
throw new Error('Invalid license key');
}
// Check license key format
isValidLicenseKeyFormat(licenseKey) {
// License keys should start with 'rpl_' and be at least 10 characters
return licenseKey.startsWith('rpl_') && licenseKey.length >= 10;
}
// Load auth data from file
async loadAuthData() {
try {
if (await fs_extra_1.default.pathExists(this.authFile)) {
return await fs_extra_1.default.readJson(this.authFile);
}
}
catch (error) {
// If file is corrupted, treat as not authenticated
}
return null;
}
// Save auth data to file
async saveAuthData(data) {
try {
await fs_extra_1.default.ensureDir(path_1.default.dirname(this.authFile));
await fs_extra_1.default.writeJson(this.authFile, data, { spaces: 2 });
// Set restrictive permissions (user only)
await fs_extra_1.default.chmod(this.authFile, 0o600);
}
catch (error) {
throw new Error(`Failed to save authentication data: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
}
exports.AuthManager = AuthManager;
//# sourceMappingURL=auth-manager.js.map