Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private authCheck(client: Client, publicKey: string) {
if (client.status === ClientStatus.Authed) return true
else if (client.status === ClientStatus.PendingAuth) return false
else if (!isValidRoom(publicKey)) return false
const nonce = sodium.randombytes_buf(sodium.crypto_box_NONCEBYTES)
const box = sodium.crypto_box_seal(nonce, sodium.from_hex(publicKey))
const challenge = sodium.to_base64(box, sodium.base64_variants.URLSAFE_NO_PADDING)
client.status = ClientStatus.PendingAuth
client.authSecret = nonce
this.sendTo(client, {
t: MessageType.AuthChallenge,
c: challenge
})
return false
}
return promise.then((pieces) => {
let metadata = {
header: sodium.to_base64(header, sodium.base64_variants.ORIGINAL_NO_PADDING),
encryptedMetadata: sodium.to_base64(metadataEncrypted, sodium.base64_variants.ORIGINAL_NO_PADDING),
};
return {
pieces,
metadata,
key: sodium.to_base64(key, sodium.base64_variants.URLSAFE_NO_PADDING),
};
});
});
function b64url (input) {
return sodium.to_base64(input, sodium.base64_variants.URLSAFE)
}
return promise.then((pieces) => {
let metadata = {
header: sodium.to_base64(header, sodium.base64_variants.ORIGINAL_NO_PADDING),
encryptedMetadata: sodium.to_base64(metadataEncrypted, sodium.base64_variants.ORIGINAL_NO_PADDING),
};
return {
pieces,
metadata,
key: sodium.to_base64(key, sodium.base64_variants.URLSAFE_NO_PADDING),
};
});
});
decryptMetadata(data) {
if (this.metadata) {
return this.metadata;
}
let header = sodium.from_base64(data.header, sodium.base64_variants.ORIGINAL_NO_PADDING);
let encryptedMetadata = sodium.from_base64(data.encryptedMetadata, sodium.base64_variants.ORIGINAL_NO_PADDING);
this.state = sodium.crypto_secretstream_xchacha20poly1305_init_pull(header, this.key);
let { message, tag } = sodium.crypto_secretstream_xchacha20poly1305_pull(this.state, encryptedMetadata);
message = JSON.parse(sodium.to_string(message));
if (tag !== sodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL) {
throw new Error('metadata chunk not ended with final tag');
}
this.metadata = Object.assign({}, data, message);
return this.metadata;
}
decryptMetadata(data) {
if (this.metadata) {
return this.metadata;
}
let header = sodium.from_base64(data.header, sodium.base64_variants.ORIGINAL_NO_PADDING);
let encryptedMetadata = sodium.from_base64(data.encryptedMetadata, sodium.base64_variants.ORIGINAL_NO_PADDING);
this.state = sodium.crypto_secretstream_xchacha20poly1305_init_pull(header, this.key);
let { message, tag } = sodium.crypto_secretstream_xchacha20poly1305_pull(this.state, encryptedMetadata);
message = JSON.parse(sodium.to_string(message));
if (tag !== sodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL) {
throw new Error('metadata chunk not ended with final tag');
}
this.metadata = Object.assign({}, data, message);
return this.metadata;
}
private solveChallenge(keyPair: KeyPair, data: string) {
const challenge = sodium.from_base64(data, sodium.base64_variants.URLSAFE_NO_PADDING)
const nonce = sodium.crypto_box_seal_open(challenge, keyPair.publicKey, keyPair.privateKey)
const decoded = sodium.to_base64(nonce, sodium.base64_variants.URLSAFE_NO_PADDING)
this.send({
t: MessageType.AuthResponse,
c: decoded
})
}
function toB64URLSafe(buf) {
if (!(buf instanceof Buffer)) { throw new TypeError('Can only encode buffer'); }
return sodium.to_base64(buf, sodium.base64_variants.URLSAFE_NO_PADDING);
}
private solveChallenge(keyPair: KeyPair, data: string) {
const challenge = sodium.from_base64(data, sodium.base64_variants.URLSAFE_NO_PADDING)
const nonce = sodium.crypto_box_seal_open(challenge, keyPair.publicKey, keyPair.privateKey)
const decoded = sodium.to_base64(nonce, sodium.base64_variants.URLSAFE_NO_PADDING)
this.send({
t: MessageType.AuthResponse,
c: decoded
})
}