Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PB-1337]: Feature/Add SDKmanager #11

Merged
merged 42 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
d85b2f8
added internxt libs and sdk
larryrider Feb 12, 2024
9b80ac1
improved tsconfig to allow ts and json imports
larryrider Feb 12, 2024
6759e5e
added sdkmanager
larryrider Feb 12, 2024
a830260
added 2fa check functionality
larryrider Feb 12, 2024
7aebda7
improved env management and added new needed envs
larryrider Feb 12, 2024
b610b7e
Merge branch 'main' into feature/add-sdkmanager
PixoDev Feb 12, 2024
0dfbd2f
fixed sdk and openpgp build problems
larryrider Feb 12, 2024
eb2c7d7
fixed packageJSON import
larryrider Feb 12, 2024
93e63c1
Merge branch 'feature/add-sdkmanager' of internxt:internxt/cli into f…
larryrider Feb 12, 2024
81156cd
removed noEmit fromtsconfig
larryrider Feb 12, 2024
815a800
moved services
larryrider Feb 12, 2024
fc17e9a
added missing type to whoami test
larryrider Feb 12, 2024
5390403
change services to class
larryrider Feb 12, 2024
abae74a
added missing crypto environment vars
larryrider Feb 12, 2024
9068274
changed packageJSON import from require to import
larryrider Feb 12, 2024
e743da7
Merge branch 'main' into feature/add-sdkmanager
PixoDev Feb 12, 2024
732c000
Merge branch 'main' into feature/add-sdkmanager
PixoDev Feb 12, 2024
3ce31d8
Merge branch 'main' into feature/add-sdkmanager
PixoDev Feb 13, 2024
59777b5
Merge branch 'main' into feature/add-sdkmanager
PixoDev Feb 13, 2024
18949e1
Merge branch 'main' into feature/add-sdkmanager
PixoDev Feb 13, 2024
e9f8591
added keys service test and moved and fixed openpgp class
larryrider Feb 13, 2024
cda41f0
removed unused oclif type
larryrider Feb 13, 2024
da8907a
added config service tests
larryrider Feb 13, 2024
c5ff9cf
added sinon-chai
larryrider Feb 14, 2024
7f8f869
added auth service tests
larryrider Feb 14, 2024
3ff871b
added sdkmanager tests
larryrider Feb 14, 2024
2b4efff
added missing tests from sdkmanager
larryrider Feb 14, 2024
d8ef2d2
improved keys service tests
larryrider Feb 15, 2024
3c1fad0
added crypto service tests
larryrider Feb 15, 2024
b6a6d84
migrated cryptojs deprecated dependency to nodejs crypto
larryrider Feb 15, 2024
ee9e653
added descriptions
larryrider Feb 15, 2024
82199c2
added getKeyAndIvFrom util for better aes management
larryrider Feb 15, 2024
a5ed047
Merge branch 'main' into feature/add-sdkmanager
PixoDev Feb 15, 2024
5d4a9d1
Merge branch 'feature/add-sdkmanager' into feat/add-service-tests
PixoDev Feb 15, 2024
50a9e43
Merge branch 'main' into feature/add-sdkmanager
PixoDev Feb 15, 2024
a19f985
Merge branch 'feature/add-sdkmanager' into feat/add-service-tests
PixoDev Feb 15, 2024
1da5c1e
added readonly properties
larryrider Feb 15, 2024
970de43
Merge branch 'feat/add-service-tests' of internxt:internxt/cli into f…
larryrider Feb 15, 2024
dec8801
Merge pull request #12 from internxt/feat/add-service-tests
larry-internxt Feb 15, 2024
5a2c551
some maintainability fixes
larryrider Feb 15, 2024
7014b10
added a disabled cryptoJS test
larryrider Feb 15, 2024
7c1aaa6
added comment to disabled crypto test
larryrider Feb 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
DRIVE_API_URL=
DRIVE_NEW_API_URL=
DRIVE_NEW_API_URL=
PAYMENTS_API_URL=
PHOTOS_API_URL=
APP_CRYPTO_SECRET=
APP_MAGIC_IV=
APP_MAGIC_SALT=
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,21 @@
],
"dependencies": {
"@internxt/eslint-config-internxt": "^1.0.9",
"@internxt/lib": "^1.2.0",
"@internxt/prettier-config": "^1.0.2",
"@internxt/sdk": "^1.4.67",
"@internxt/sdk": "^1.4.70",
"@oclif/core": "^3",
"dotenv": "^16.4.1"
"dotenv": "^16.4.1",
"openpgp": "^5.11.0"
},
"devDependencies": {
"@oclif/prettier-config": "^0.2.1",
"@oclif/test": "^3",
"@openpgp/web-stream-tools": "0.0.11-patch-0",
"@types/chai": "^4",
"@types/mocha": "^10",
"@types/node": "^18",
"@types/sinon-chai": "^3.2.12",
"chai": "^4",
"eslint": "^8",
"eslint-config-oclif": "^5",
Expand All @@ -53,6 +57,7 @@
"oclif": "^4.4.8",
"prettier": "^3.2.5",
"shx": "^0.3.4",
"sinon-chai": "^3.7.0",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
},
Expand Down
154 changes: 154 additions & 0 deletions src/services/SDKManager.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import { Auth, Drive, photos } from '@internxt/sdk';
import { Trash } from '@internxt/sdk/dist/drive';
import { ApiSecurity, AppDetails } from '@internxt/sdk/dist/shared';
import { ConfigService } from './config.service';
import packageJson from '../../package.json';

export type SdkManagerApiSecurity = ApiSecurity & { newToken: string };
/**
* Manages all the sdk submodules initialization
* based on the current apiSecurity details
*/
export class SdkManager {
public static readonly instance: SdkManager = new SdkManager();
private static apiSecurity?: SdkManagerApiSecurity;

/**
* Sets the security details needed to create SDK clients
* @param apiSecurity Security properties to be setted
**/
public static readonly init = (apiSecurity: SdkManagerApiSecurity) => {
SdkManager.apiSecurity = apiSecurity;
};

/**
* Cleans the security details
**/
public static readonly clean = () => {
SdkManager.apiSecurity = undefined;
};

/**
* Returns the security details needed to create SDK clients
* @param config Config object to handle error throwing when there is not apiSecurity defined
* @throws {Error} When throwErrorOnMissingCredentials is setted to true and there is not apiSecurity defined
* @returns The SDK Manager api security details
**/
public static readonly getApiSecurity = (
config = { throwErrorOnMissingCredentials: true },
): SdkManagerApiSecurity => {
if (!SdkManager.apiSecurity && config.throwErrorOnMissingCredentials)
throw new Error('Api security properties not found in SdkManager');

return SdkManager.apiSecurity as SdkManagerApiSecurity;
};

/**
* Returns the application details from package.json
* @returns The name and the version of the app from package.json
**/
public static readonly getAppDetails = (): AppDetails => {
return {
clientName: packageJson.name,
clientVersion: packageJson.version,
};
};

/** Auth SDK */
getAuthV2() {
const DRIVE_NEW_API_URL = ConfigService.instance.get('DRIVE_NEW_API_URL');

const apiSecurity = SdkManager.getApiSecurity({ throwErrorOnMissingCredentials: false });
const appDetails = SdkManager.getAppDetails();

return Auth.client(DRIVE_NEW_API_URL, appDetails, apiSecurity);
}

/** Auth old client SDK */
getAuth() {
const DRIVE_API_URL = ConfigService.instance.get('DRIVE_API_URL');

const apiSecurity = SdkManager.getApiSecurity({ throwErrorOnMissingCredentials: false });
const appDetails = SdkManager.getAppDetails();

return Auth.client(DRIVE_API_URL, appDetails, apiSecurity);
}

/** Payments SDK */
getPayments() {
const PAYMENTS_API_URL = ConfigService.instance.get('PAYMENTS_API_URL');

const newToken = SdkManager.getApiSecurity().newToken;
const appDetails = SdkManager.getAppDetails();

return Drive.Payments.client(PAYMENTS_API_URL, appDetails, {
// Weird, normal accessToken doesn't work here
token: newToken,
});
}

/** Users SDK */
getUsers() {
const DRIVE_API_URL = ConfigService.instance.get('DRIVE_API_URL');

const apiSecurity = SdkManager.getApiSecurity({ throwErrorOnMissingCredentials: false });
const appDetails = SdkManager.getAppDetails();

return Drive.Users.client(DRIVE_API_URL, appDetails, apiSecurity);
}

/** Referrals SDK */
getReferrals() {
const DRIVE_API_URL = ConfigService.instance.get('DRIVE_API_URL');

const apiSecurity = SdkManager.getApiSecurity();
const appDetails = SdkManager.getAppDetails();

return Drive.Referrals.client(DRIVE_API_URL, appDetails, apiSecurity);
}

/** Storage SDK */
getStorage() {
const DRIVE_API_URL = ConfigService.instance.get('DRIVE_API_URL');

const apiSecurity = SdkManager.getApiSecurity();
const appDetails = SdkManager.getAppDetails();

return Drive.Storage.client(DRIVE_API_URL, appDetails, apiSecurity);
}

/** Trash SDK */
getTrash() {
const DRIVE_NEW_API_URL = ConfigService.instance.get('DRIVE_NEW_API_URL');

const newToken = SdkManager.getApiSecurity().newToken;
const appDetails = SdkManager.getAppDetails();

return Trash.client(DRIVE_NEW_API_URL, appDetails, {
// Weird, normal accessToken doesn't work here
token: newToken,
});
}

/** Photos SDK */
getPhotos() {
const PHOTOS_API_URL = ConfigService.instance.get('PHOTOS_API_URL');

const newToken = SdkManager.getApiSecurity().newToken;

return new photos.Photos(PHOTOS_API_URL, newToken);
}

/** Share SDK */
getShare() {
const DRIVE_NEW_API_URL = ConfigService.instance.get('DRIVE_NEW_API_URL');

const newToken = SdkManager.getApiSecurity().newToken;
const appDetails = SdkManager.getAppDetails();

return Drive.Share.client(DRIVE_NEW_API_URL, appDetails, {
// Weird, normal accessToken doesn't work here
token: newToken,
});
}
}
79 changes: 79 additions & 0 deletions src/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { LoginDetails } from '@internxt/sdk';
import { UserSettings } from '@internxt/sdk/dist/shared/types/userSettings.js';
import { SdkManager } from './SDKManager.service';
import { KeysService } from './keys.service';
import { CryptoService } from './crypto.service';

export class AuthService {
public static readonly instance: AuthService = new AuthService();

/**
* Login with user credentials and returns its tokens and properties
* @param email The user's email
* @param password The user's password
* @param twoFactorCode (Optional) The temporal two factor auth code
* @returns The user's properties and the tokens needed for auth
* @async
**/
public doLogin = async (
email: string,
password: string,
twoFactorCode?: string,
): Promise<{
user: UserSettings;
token: string;
newToken: string;
mnemonic: string;
}> => {
const authClient = SdkManager.instance.getAuth();
const loginDetails: LoginDetails = {
email: email.toLowerCase(),
password: password,
tfaCode: twoFactorCode,
};

const data = await authClient.login(loginDetails, CryptoService.cryptoProvider);
const { user, token, newToken } = data;
const { privateKey, publicKey } = user;

const plainPrivateKeyInBase64 = privateKey
? Buffer.from(KeysService.instance.decryptPrivateKey(privateKey, password)).toString('base64')
: '';

if (privateKey) {
await KeysService.instance.assertPrivateKeyIsValid(privateKey, password);
await KeysService.instance.assertValidateKeys(
Buffer.from(plainPrivateKeyInBase64, 'base64').toString(),
Buffer.from(publicKey, 'base64').toString(),
);
}

const clearMnemonic = CryptoService.instance.decryptTextWithKey(user.mnemonic, password);
const clearUser = {
...user,
mnemonic: clearMnemonic,
privateKey: plainPrivateKeyInBase64,
};
return {
user: clearUser,
token: token,
newToken: newToken,
mnemonic: clearMnemonic,
};
};

/**
* Checks from user's security details if it has enabled two factor auth
* @param email The user's email
* @throws {Error} If auth.securityDetails endpoint fails
* @returns True if user has enabled two factor auth
* @async
**/
public is2FANeeded = async (email: string): Promise<boolean> => {
const authClient = SdkManager.instance.getAuth();
const securityDetails = await authClient.securityDetails(email).catch((error) => {
throw new Error(error.message ?? 'Login error');
});
return securityDetails.tfaEnabled;
};
}
10 changes: 8 additions & 2 deletions src/services/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ import { ConfigKeys } from '../types/config.types';
export class ConfigService {
public static readonly instance: ConfigService = new ConfigService();

get(key: keyof ConfigKeys): string {
/**
* Gets the value from an environment key
* @param key The environment key to retrieve
* @throws {Error} If key is not found in process.env
* @returns The value from the environment variable
**/
public get = (key: keyof ConfigKeys): string => {
const value = process.env[key];
if (!value) throw new Error(`Config key ${key} was not found in process.env`);
return value;
}
};
}
Loading
Loading