From 7aebda7313c645b4a0c9c32a4d0b83d2fb4b707a Mon Sep 17 00:00:00 2001 From: larry-internxt Date: Mon, 12 Feb 2024 12:00:25 +0100 Subject: [PATCH] improved env management and added new needed envs --- .env.template | 5 +- src/types/config.types.ts | 3 + .../app/auth/services/auth.service.ts | 2 +- .../app/crypto/services/utils.ts | 17 ++---- src/webdav-server/core/SDKExceptions.ts | 9 --- src/webdav-server/core/SDKManager.ts | 57 +++++++------------ 6 files changed, 33 insertions(+), 60 deletions(-) delete mode 100644 src/webdav-server/core/SDKExceptions.ts diff --git a/.env.template b/.env.template index a2e16e0..3b6631b 100644 --- a/.env.template +++ b/.env.template @@ -1,2 +1,5 @@ DRIVE_API_URL= -DRIVE_NEW_API_URL= \ No newline at end of file +DRIVE_NEW_API_URL= +PAYMENTS_API_URL= +PHOTOS_API_URL= +REACT_APP_CRYPTO_SECRET= diff --git a/src/types/config.types.ts b/src/types/config.types.ts index 2964370..66b5765 100644 --- a/src/types/config.types.ts +++ b/src/types/config.types.ts @@ -1,4 +1,7 @@ export interface ConfigKeys { readonly DRIVE_API_URL: string; readonly DRIVE_NEW_API_URL: string; + readonly REACT_APP_CRYPTO_SECRET: string; + readonly PAYMENTS_API_URL: string; + readonly PHOTOS_API_URL: string; } diff --git a/src/webdav-server/app/auth/services/auth.service.ts b/src/webdav-server/app/auth/services/auth.service.ts index 164b211..62dec6b 100644 --- a/src/webdav-server/app/auth/services/auth.service.ts +++ b/src/webdav-server/app/auth/services/auth.service.ts @@ -1,5 +1,5 @@ import { CryptoProvider, LoginDetails } from '@internxt/sdk'; -import { Keys, Password, UserAccessError } from '@internxt/sdk/dist/auth'; +import { Keys, Password } from '@internxt/sdk/dist/auth'; import { UserSettings } from '@internxt/sdk/dist/shared/types/userSettings.js'; import { aes } from '@internxt/lib'; import { SdkManager } from '../../../core/SDKManager.ts'; diff --git a/src/webdav-server/app/crypto/services/utils.ts b/src/webdav-server/app/crypto/services/utils.ts index f941a03..b3082ba 100644 --- a/src/webdav-server/app/crypto/services/utils.ts +++ b/src/webdav-server/app/crypto/services/utils.ts @@ -1,4 +1,5 @@ import CryptoJS from 'crypto-js'; +import { ConfigService } from '../../../../services/config.service'; interface PassObjectInterface { salt?: string | null; @@ -19,18 +20,14 @@ function passToHash(passObject: PassObjectInterface): { salt: string; hash: stri // AES Plain text encryption method function encryptText(textToEncrypt: string): string { - if (!process.env.REACT_APP_CRYPTO_SECRET) { - throw new Error('env variable REACT_APP_CRYPTO_SECRET is not defined'); - } - return encryptTextWithKey(textToEncrypt, process.env.REACT_APP_CRYPTO_SECRET); + const REACT_APP_CRYPTO_SECRET = ConfigService.instance.get('REACT_APP_CRYPTO_SECRET'); + return encryptTextWithKey(textToEncrypt, REACT_APP_CRYPTO_SECRET); } // AES Plain text decryption method function decryptText(encryptedText: string): string { - if (!process.env.REACT_APP_CRYPTO_SECRET) { - throw new Error('env variable REACT_APP_CRYPTO_SECRET is not defined'); - } - return decryptTextWithKey(encryptedText, process.env.REACT_APP_CRYPTO_SECRET); + const REACT_APP_CRYPTO_SECRET = ConfigService.instance.get('REACT_APP_CRYPTO_SECRET'); + return decryptTextWithKey(encryptedText, REACT_APP_CRYPTO_SECRET); } // AES Plain text encryption method with enc. key @@ -43,10 +40,6 @@ function encryptTextWithKey(textToEncrypt: string, keyToEncrypt: string): string // AES Plain text decryption method with enc. key function decryptTextWithKey(encryptedText: string, keyToDecrypt: string): string { - if (!keyToDecrypt) { - throw new Error('No key defined. Check .env file'); - } - const reb = CryptoJS.enc.Hex.parse(encryptedText); const bytes = CryptoJS.AES.decrypt(reb.toString(CryptoJS.enc.Base64), keyToDecrypt); diff --git a/src/webdav-server/core/SDKExceptions.ts b/src/webdav-server/core/SDKExceptions.ts deleted file mode 100644 index 3fcf1f0..0000000 --- a/src/webdav-server/core/SDKExceptions.ts +++ /dev/null @@ -1,9 +0,0 @@ -//type EnvironmentVars = process.env; - -export class NoEnvDefined extends Error { - constructor(envProperty: string) { - super('ENV variable ' + envProperty + ' is not defined'); - - Object.setPrototypeOf(this, NoEnvDefined.prototype); - } -} diff --git a/src/webdav-server/core/SDKManager.ts b/src/webdav-server/core/SDKManager.ts index dc85971..4359517 100644 --- a/src/webdav-server/core/SDKManager.ts +++ b/src/webdav-server/core/SDKManager.ts @@ -2,7 +2,7 @@ import { Auth, Drive, photos } from '@internxt/sdk'; import { Trash } from '@internxt/sdk/dist/drive'; import { ApiSecurity, AppDetails } from '@internxt/sdk/dist/shared'; import packageJson from '../../../package.json'; -import { NoEnvDefined } from './SDKExceptions'; +import { ConfigService } from '../../services/config.service'; export type SdkManagerApiSecurity = ApiSecurity & { newToken: string }; /** @@ -51,38 +51,32 @@ export class SdkManager { /** Auth SDK */ get authV2() { - if (!process.env.DRIVE_NEW_API_URL) { - throw new NoEnvDefined('DRIVE_NEW_API_URL'); - } + const DRIVE_NEW_API_URL = ConfigService.instance.get('DRIVE_NEW_API_URL'); const apiSecurity = this.getApiSecurity({ throwErrorOnMissingCredentials: false }); const appDetails = SdkManager.getAppDetails(); - return Auth.client(process.env.DRIVE_NEW_API_URL, appDetails, apiSecurity); + return Auth.client(DRIVE_NEW_API_URL, appDetails, apiSecurity); } /** Auth old client SDK */ get auth() { - if (!process.env.DRIVE_API_URL) { - throw new NoEnvDefined('DRIVE_API_URL'); - } + const DRIVE_API_URL = ConfigService.instance.get('DRIVE_API_URL'); const apiSecurity = this.getApiSecurity({ throwErrorOnMissingCredentials: false }); const appDetails = SdkManager.getAppDetails(); - return Auth.client(process.env.DRIVE_API_URL, appDetails, apiSecurity); + return Auth.client(DRIVE_API_URL, appDetails, apiSecurity); } /** Payments SDK */ get payments() { - if (!process.env.PAYMENTS_API_URL) { - throw new NoEnvDefined('PAYMENTS_API_URL'); - } + const PAYMENTS_API_URL = ConfigService.instance.get('PAYMENTS_API_URL'); const newToken = this.getApiSecurity().newToken; const appDetails = SdkManager.getAppDetails(); - return Drive.Payments.client(process.env.PAYMENTS_API_URL, appDetails, { + return Drive.Payments.client(PAYMENTS_API_URL, appDetails, { // Weird, normal accessToken doesn't work here token: newToken, }); @@ -90,50 +84,42 @@ export class SdkManager { /** Users SDK */ get users() { - if (!process.env.DRIVE_API_URL) { - throw new NoEnvDefined('DRIVE_API_URL'); - } + const DRIVE_API_URL = ConfigService.instance.get('DRIVE_API_URL'); const apiSecurity = this.getApiSecurity({ throwErrorOnMissingCredentials: false }); const appDetails = SdkManager.getAppDetails(); - return Drive.Users.client(process.env.DRIVE_API_URL, appDetails, apiSecurity); + return Drive.Users.client(DRIVE_API_URL, appDetails, apiSecurity); } /** Referrals SDK */ get referrals() { - if (!process.env.DRIVE_API_URL) { - throw new NoEnvDefined('DRIVE_API_URL'); - } + const DRIVE_API_URL = ConfigService.instance.get('DRIVE_API_URL'); const apiSecurity = this.getApiSecurity(); const appDetails = SdkManager.getAppDetails(); - return Drive.Referrals.client(process.env.DRIVE_API_URL, appDetails, apiSecurity); + return Drive.Referrals.client(DRIVE_API_URL, appDetails, apiSecurity); } /** Storage SDK */ get storage() { - if (!process.env.DRIVE_API_URL) { - throw new NoEnvDefined('DRIVE_API_URL'); - } + const DRIVE_API_URL = ConfigService.instance.get('DRIVE_API_URL'); const apiSecurity = this.getApiSecurity(); const appDetails = SdkManager.getAppDetails(); - return Drive.Storage.client(process.env.DRIVE_API_URL, appDetails, apiSecurity); + return Drive.Storage.client(DRIVE_API_URL, appDetails, apiSecurity); } /** Trash SDK */ get trash() { - if (!process.env.DRIVE_NEW_API_URL) { - throw new NoEnvDefined('DRIVE_NEW_API_URL'); - } + const DRIVE_NEW_API_URL = ConfigService.instance.get('DRIVE_NEW_API_URL'); const newToken = this.getApiSecurity().newToken; const appDetails = SdkManager.getAppDetails(); - return Trash.client(process.env.DRIVE_NEW_API_URL, appDetails, { + return Trash.client(DRIVE_NEW_API_URL, appDetails, { // Weird, normal accessToken doesn't work here token: newToken, }); @@ -141,24 +127,21 @@ export class SdkManager { /** Photos SDK */ get photos() { - if (!process.env.PHOTOS_API_URL) { - throw new NoEnvDefined('PHOTOS_API_URL'); - } + const PHOTOS_API_URL = ConfigService.instance.get('PHOTOS_API_URL'); const newToken = this.getApiSecurity().newToken; - return new photos.Photos(process.env.PHOTOS_API_URL, newToken); + + return new photos.Photos(PHOTOS_API_URL, newToken); } /** Share SDK */ get share() { - if (!process.env.DRIVE_NEW_API_URL) { - throw new NoEnvDefined('DRIVE_NEW_API_URL'); - } + const DRIVE_NEW_API_URL = ConfigService.instance.get('DRIVE_NEW_API_URL'); const newToken = this.getApiSecurity().newToken; const appDetails = SdkManager.getAppDetails(); - return Drive.Share.client(process.env.DRIVE_NEW_API_URL, appDetails, { + return Drive.Share.client(DRIVE_NEW_API_URL, appDetails, { // Weird, normal accessToken doesn't work here token: newToken, });