Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
constructor(options: { onProgress?: OnProgress } = {}) {
// $FlowIKnow Use of Object.prototype
if (!options || typeof options !== 'object' || Object.getPrototypeOf(options) !== Object.prototype)
throw new InvalidArgument('options', 'object', options);
if ('onProgress' in options) {
const { onProgress } = options;
if (typeof onProgress !== 'function')
throw new InvalidArgument('options.onProgress', 'functions', onProgress);
this._onProgress = onProgress;
} else {
// default to no-op
this._onProgress = (report: ProgressReport) => {}; // eslint-disable-line no-unused-vars
}
}
_parseIdentity(identityB64: b64string) {
// Type verif arguments
if (!identityB64 || typeof identityB64 !== 'string')
throw new InvalidArgument('identity', 'b64string', identityB64);
// End type verif
const userData = extractUserData(identityB64);
const userDataTrustchainId = utils.toBase64(userData.trustchainId);
if (this.trustchainId !== userDataTrustchainId)
throw new InvalidArgument(`The provided identity was not signed by the private key of the current trustchain: expected trustchain id "${this.trustchainId}", but got "${userDataTrustchainId}"`);
return userData;
}
export function _deserializePublicIdentity(identity: b64string): PublicIdentity { // eslint-disable-line no-underscore-dangle
try {
return utils.fromB64Json(identity);
} catch (e) {
throw new InvalidArgument(`Invalid public identity provided: ${identity}`);
}
}
this._trustchainId = options.appId;
} else if ('trustchainId' in options) {
if (typeof options.trustchainId !== 'string') {
throw new InvalidArgument('options.trustchainId', 'string', options.trustchainId);
}
this._trustchainId = options.trustchainId;
console.warn('"trustchainId" option has been deprecated in favor of "appId", it will be removed in the next major release.');
} else {
throw new InvalidArgument('options.appId', 'string', options.appId);
}
if (typeof options.dataStore !== 'object' || options.dataStore instanceof Array) {
throw new InvalidArgument('options.dataStore', 'object', options.dataStore);
} else if (typeof options.dataStore.adapter !== 'function') {
throw new InvalidArgument('options.dataStore.adapter', 'function', options.dataStore.adapter);
}
if (typeof options.sdkType !== 'string') {
throw new InvalidArgument('options.sdkType', 'string', options.sdkType);
}
this._options = options;
const clientOptions: ClientOptions = {
sdkInfo: {
version: Tanker.version,
type: options.sdkType,
trustchainId: this._trustchainId,
}
};
if (options.socket) { clientOptions.socket = options.socket; }
if (options.url) { clientOptions.url = options.url; }
async verifyProvisionalIdentity(verification: EmailVerification | OIDCVerification) {
if (!('email' in verification) && !('oidcIdToken' in verification))
throw new InternalError(`Assertion error: unsupported verification method for provisional identity: ${JSON.stringify(verification)}`);
if (!this._provisionalIdentity)
throw new PreconditionFailed('Cannot call verifyProvisionalIdentity() without having called attachProvisionalIdentity() before');
if (verification.email && this._provisionalIdentity.value !== verification.email)
throw new InvalidArgument('"verification.email" does not match provisional identity');
if (verification.oidcIdToken) {
let jwtPayload;
try {
jwtPayload = JSON.parse(utils.toString(utils.fromBase64(verification.oidcIdToken.split('.')[1])));
} catch (e) {
throw new InvalidArgument('Failed to parse "verification.oidcIdToken"');
}
if (this._provisionalIdentity.value !== jwtPayload.email)
throw new InvalidArgument('"verification.oidcIdToken" does not match provisional identity');
}
const tankerKeys = await this._verifyAndGetProvisionalIdentityKeys(verification);
if (tankerKeys)
await this._claimProvisionalIdentity(this._provisionalIdentity, tankerKeys);
async _getInternalGroupById(groupId: Uint8Array): Promise {
const blocks = await this._getGroupsBlocksById([groupId]);
const groups = await this._groupsFromBlocks(blocks);
assertExpectedGroups(groups, [groupId]);
const group = groups[0];
if (!group.encryptionKeyPair) {
throw new InvalidArgument('Current user is not a group member');
}
return group;
}
_transform(clearData: Uint8Array, encoding: ?string, done: DoneCallback) {
if (!(clearData instanceof Uint8Array)) {
done(new InvalidArgument('clearData', 'Uint8Array', clearData));
} else {
this._resizerStream.write(clearData, encoding, done);
}
}
async share(resourceIds: Array, options: SharingOptions): Promise {
this.assert(statuses.READY, 'share');
if (!(resourceIds instanceof Array) || resourceIds.some(id => typeof id !== 'string'))
throw new InvalidArgument('resourceIds', 'Array', resourceIds);
const sharingOptions = extractSharingOptions(options);
if (isSharingOptionsEmpty(sharingOptions)) {
throw new InvalidArgument(
'options.shareWith*',
'options.shareWithUsers or options.shareWithGroups must contain recipients',
options
);
}
return this._session.share(resourceIds, sharingOptions);
}
async createGroup(users: Array): Promise {
this.assert(statuses.READY, 'create a group');
if (!(users instanceof Array))
throw new InvalidArgument('users', 'Array', users);
return this._session.createGroup(users);
}
export const assertVerification = (verification: Verification) => {
if (!verification || typeof verification !== 'object' || verification instanceof Array)
throw new InvalidArgument('verification', 'object', verification);
if (Object.keys(verification).some(k => validKeys.indexOf(k) === -1))
throw new InvalidArgument('verification', `should only contain keys in ${JSON.stringify(validKeys)}`, verification);
const methodCound = validMethods.reduce((count, key) => count + (key in verification ? 1 : 0), 0);
if (methodCound !== 1)
throw new InvalidArgument('verification', `should contain a single verification method in ${JSON.stringify(validMethods)}`, verification);
if ('email' in verification) {
if (typeof verification.email !== 'string')
throw new InvalidArgument('verification', 'email should be a string', verification.email);
if (!('verificationCode' in verification))
throw new InvalidArgument('verification', 'verification should also have a verificationCode', verification);
if (typeof verification.verificationCode !== 'string')
throw new InvalidArgument('verification', 'verificationCode should be a string', verification.verificationCode);
} else if ('passphrase' in verification && typeof verification.passphrase !== 'string') {
throw new InvalidArgument('verification', 'passphrase should be a string', verification.passphrase);
} else if ('verificationKey' in verification && typeof verification.verificationKey !== 'string') {
throw new InvalidArgument('verification', 'verificationKey should be a string', verification.verificationKey);
} else if ('oidcIdToken' in verification && typeof verification.oidcIdToken !== 'string') {
throw new InvalidArgument('verification', 'oidcIdToken should be a string', verification.oidcIdToken);
}
};