Skip to content

Commit

Permalink
improved env management and added new needed envs
Browse files Browse the repository at this point in the history
  • Loading branch information
larryrider committed Feb 12, 2024
1 parent a830260 commit 7aebda7
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 60 deletions.
5 changes: 4 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
DRIVE_API_URL=
DRIVE_NEW_API_URL=
DRIVE_NEW_API_URL=
PAYMENTS_API_URL=
PHOTOS_API_URL=
REACT_APP_CRYPTO_SECRET=
3 changes: 3 additions & 0 deletions src/types/config.types.ts
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion src/webdav-server/app/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
17 changes: 5 additions & 12 deletions src/webdav-server/app/crypto/services/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import CryptoJS from 'crypto-js';
import { ConfigService } from '../../../../services/config.service';

interface PassObjectInterface {
salt?: string | null;
Expand All @@ -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
Expand All @@ -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);

Expand Down
9 changes: 0 additions & 9 deletions src/webdav-server/core/SDKExceptions.ts

This file was deleted.

57 changes: 20 additions & 37 deletions src/webdav-server/core/SDKManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Check failure on line 4 in src/webdav-server/core/SDKManager.ts

View workflow job for this annotation

GitHub Actions / commands-unit-tests (ubuntu-latest, 20.11.0)

File '/home/runner/work/cli/cli/package.json' is not under 'rootDir' '/home/runner/work/cli/cli/src'. 'rootDir' is expected to contain all source files.

Check failure on line 4 in src/webdav-server/core/SDKManager.ts

View workflow job for this annotation

GitHub Actions / commands-unit-tests (ubuntu-latest, 20.11.0)

File '/home/runner/work/cli/cli/package.json' is not listed within the file list of project '/home/runner/work/cli/cli/tsconfig.json'. Projects must list all files or use an 'include' pattern.
import { NoEnvDefined } from './SDKExceptions';
import { ConfigService } from '../../services/config.service';

export type SdkManagerApiSecurity = ApiSecurity & { newToken: string };
/**
Expand Down Expand Up @@ -51,114 +51,97 @@ 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,
});
}

/** 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,
});
}

/** 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,
});
Expand Down

0 comments on commit 7aebda7

Please sign in to comment.