File size: 4,003 Bytes
56bf851
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
826a975
56bf851
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
826a975
56bf851
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import CryptoJS from "crypto-js";

// Fallback encryption keys for development (should be environment variables in production)
const AES_SECRET_KEY =
  process.env.REACT_APP_AES_SECRET_KEY ||
  "2b7e151628aed2a6abf7158809cf4f3c2b7e151628aed2a6abf7158809cf4f3c";
const AES_IV =
  process.env.REACT_APP_AES_IV || "000102030405060708090a0b0c0d0e0f";

// Encryption utilities
export function encryptApiKey(rawKey) {
  const key = CryptoJS.enc.Hex.parse(AES_SECRET_KEY);
  const iv = CryptoJS.enc.Hex.parse(AES_IV);
  const encrypted = CryptoJS.AES.encrypt(rawKey, key, {
    iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  });
  return encrypted.toString(); // base64
}

export function decryptApiKey(encryptedKey) {
  const key = CryptoJS.enc.Hex.parse(AES_SECRET_KEY);
  const iv = CryptoJS.enc.Hex.parse(AES_IV);
  const decrypted = CryptoJS.AES.decrypt(encryptedKey, key, {
    iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  });
  return decrypted.toString(CryptoJS.enc.Utf8);
}

// Application configuration
export const config = {
  apiBaseUrl:
    process.env.REACT_APP_API_BASE_URL || "http://localhost:8000/api/v1",
  apiTimeout: parseInt(process.env.REACT_APP_API_TIMEOUT) || 30,
  maxRetries: parseInt(process.env.REACT_APP_MAX_RETRIES) || 3,

  // AWS S3 Configuration
  awsRegion: process.env.REACT_APP_AWS_REGION || "ap-south-1",
  s3BucketName: process.env.REACT_APP_S3_BUCKET_NAME || "test-bucket-name",
  awsAccessKeyId:
    process.env.REACT_APP_AWS_ACCESS_KEY_ID ||
    process.env.REACT_APP_AWS_ACCESS_KEY,
  awsSecretAccessKey:
    process.env.REACT_APP_AWS_SECRET_ACCESS_KEY ||
    process.env.REACT_APP_AWS_SECRET_KEY,

  // Application Settings
  defaultNumRecords: parseInt(process.env.REACT_APP_DEFAULT_NUM_RECORDS) || 100,
  maxFileSizeMB: parseInt(process.env.REACT_APP_MAX_FILE_SIZE_MB) || 100,

  // Debug mode
  debugMode: process.env.REACT_APP_DEBUG === "true",
};

// API endpoints
export const getValidationUrl = () =>
  `${config.apiBaseUrl}/credentials/api/validate`;
export const getGenerationUrl = () => `${config.apiBaseUrl}/ai/execute`;

// Debug logging utility
export const debugLog = (message, data = null) => {
  if (config.debugMode || process.env.NODE_ENV === "development") {
    // Debug logging disabled
  }
};

// Validate configuration on load
const validateConfig = () => {
  const requiredEnvVars = [
    "REACT_APP_AWS_ACCESS_KEY_ID",
    "REACT_APP_AWS_SECRET_ACCESS_KEY",
  ];

  const missing = requiredEnvVars.filter(
    (envVar) =>
      !process.env[envVar] &&
      !process.env[envVar.replace("_ID", "").replace("_KEY", "")]
  );

  if (missing.length > 0) {
    // Environment variables validation warning removed
  }
};

validateConfig();

// Enhanced credential validation utilities for browser environment
export const credentialValidation = {
  // Validate AWS credential format without making API calls
  validateCredentialFormat: () => {
    const errors = [];

    if (!config.awsAccessKeyId) {
      errors.push("AWS Access Key ID is not configured");
    } else if (!config.awsAccessKeyId.startsWith("AKIA")) {
      errors.push(
        "AWS Access Key ID format appears invalid (should start with AKIA)"
      );
    }

    if (!config.awsSecretAccessKey) {
      errors.push("AWS Secret Access Key is not configured");
    } else if (config.awsSecretAccessKey.length !== 40) {
      errors.push(
        "AWS Secret Access Key format appears invalid (should be 40 characters)"
      );
    }

    if (!config.s3BucketName) {
      errors.push("S3 Bucket name is not configured");
    }

    if (!config.awsRegion) {
      errors.push("AWS Region is not configured");
    }

    return {
      valid: errors.length === 0,
      errors: errors,
      formatValid: errors.length === 0,
    };
  },

  // Check if we're in a CORS-restricted environment
  isBrowserEnvironment: () => {
    return typeof window !== "undefined" && typeof document !== "undefined";
  },
};

export default config;