/*
MIT License
Copyright (c) 2021 Max Kas
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import { BitConverter } from "../../../simple-io/convert/bit_converter.js";
import { Generator } from "../../../salmon-core/salmon/generator.js";
import { Status, NonceSequence } from "../../../salmon-core/salmon/sequence/nonce_sequence.js";
import { Nonce } from "../../../salmon-core/salmon/nonce.js";
import { SequenceException } from "../../../salmon-core/salmon/sequence/sequence_exception.js";
import { MemoryStream } from "../../../simple-io/streams/memory_stream.js";
/**
* Generates nonces based on a sequencer backed by a file.
*/
export class FileSequencer {
#sequenceFile;
#serializer;
/**
* Instantiate a nonce file sequencer.
*
* @param {IFile} sequenceFile The sequence file (json format).
* @param {INonceSequenceSerializer} serializer The serializer to be used.
* @throws IOException Thrown if there is an IO error.
* @throws SequenceException Thrown if error with the nonce sequence
*/
constructor(sequenceFile, serializer) {
this.#sequenceFile = sequenceFile;
this.#serializer = serializer;
}
/**
* Initialize the file sequencer
*/
async initialize() {
if (!await this.#sequenceFile.exists()) {
let parent = await this.#sequenceFile.getParent();
if (parent == null)
throw new Error("Could not get parent");
this.#sequenceFile = await parent.createFile(this.#sequenceFile.getName());
await this.saveSequenceFile(new Map());
}
}
/**
* Get the sequence file
* @returns {IFile} The sequence file
*/
getSequenceFile() {
return this.#sequenceFile;
}
/**
* Create a sequence for the drive ID and auth ID provided.
*
* @param {string} driveId The drive ID.
* @param {string} authId The authorization ID of the drive.
* @throws SequenceException Thrown if error with the nonce sequence
*/
async createSequence(driveId, authId) {
let contents = await this.getContents();
let configs = this.#serializer.deserialize(contents);
let sequence = FileSequencer.#getSequence(configs, driveId);
if (sequence)
throw new SequenceException("Sequence already exists");
let nsequence = new NonceSequence(driveId, authId, null, null, Status.New);
configs.set(driveId + ":" + authId, nsequence);
await this.saveSequenceFile(configs);
}
/**
* Initialize the sequence.
*
* @param {string} driveId The drive ID.
* @param {string} authId The auth ID of the device for the drive.
* @param {Uint8Array} startNonce The starting nonce.
* @param {Uint8Array} maxNonce The maximum nonce.
* @throws SequenceException Thrown if error with the nonce sequence
* @throws IOException Thrown if there is an IO error.
*/
async initializeSequence(driveId, authId, startNonce, maxNonce) {
let contents = await this.getContents();
let configs = this.#serializer.deserialize(contents);
let sequence = FileSequencer.#getSequence(configs, driveId);
if (sequence == null)
throw new SequenceException("Sequence does not exist");
if (sequence.getNextNonce())
throw new SequenceException("Cannot reinitialize sequence");
sequence.setNextNonce(startNonce);
sequence.setMaxNonce(maxNonce);
sequence.setStatus(Status.Active);
await this.saveSequenceFile(configs);
}
/**
* Set the maximum nonce.
*
* @param {string} driveId The drive ID.
* @param {string} authId The auth ID of the device for the drive.
* @param {Uint8Array} maxNonce The maximum nonce.
* @throws SequenceException Thrown if error with the nonce sequence
*/
async setMaxNonce(driveId, authId, maxNonce) {
let contents = await this.getContents();
let configs = this.#serializer.deserialize(contents);
let sequence = FileSequencer.#getSequence(configs, driveId);
if (sequence == null || sequence.getStatus() == Status.Revoked)
throw new SequenceException("Sequence does not exist");
let currMaxNonce = sequence.getMaxNonce();
if (currMaxNonce == null)
throw new SequenceException("Could not find current max nonce");
if (BitConverter.toLong(currMaxNonce, 0, Generator.NONCE_LENGTH)
< BitConverter.toLong(maxNonce, 0, Generator.NONCE_LENGTH))
throw new SequenceException("Max nonce cannot be increased");
sequence.setMaxNonce(maxNonce);
await this.saveSequenceFile(configs);
}
/**
* Get the next nonce.
*
* @param {string} driveId The drive ID.
* @returns {Promise<Uint8Array | null>} The next nonce
* @throws SequenceException Thrown if error with the nonce sequence
* @throws SalmonRangeExceededException Thrown if nonce has exceeded range
*/
async nextNonce(driveId) {
let contents = await this.getContents();
let configs = this.#serializer.deserialize(contents);
let sequence = FileSequencer.#getSequence(configs, driveId);
if (sequence == null || sequence.getNextNonce() == null || sequence.getMaxNonce() == null)
throw new SequenceException("Device not Authorized");
//We get the next nonce
let nextNonce = sequence.getNextNonce();
let incrNonce = sequence.getNextNonce();
if (incrNonce == null)
throw new SequenceException("Could not increase nonce");
let currMaxNonce = sequence.getMaxNonce();
if (currMaxNonce == null)
throw new SequenceException("Could not get current max nonce");
sequence.setNextNonce(Nonce.increaseNonce(incrNonce, currMaxNonce));
await this.saveSequenceFile(configs);
return nextNonce;
}
/**
* Get the contents of a sequence file.
*
* @returns {Promise<string>} The file contents.
* @throws SequenceException Thrown if error with the nonce sequence
*/
async getContents() {
let stream = null;
let outputStream = null;
try {
stream = await this.#sequenceFile.getInputStream();
outputStream = new MemoryStream();
await stream.copyTo(outputStream);
}
catch (ex) {
console.error(ex);
throw new SequenceException("Could not get contents", ex);
}
finally {
if (stream) {
try {
await stream.close();
}
catch (e) {
throw new SequenceException("Could not get contents", e);
}
}
if (outputStream) {
try {
await outputStream.flush();
await outputStream.close();
}
catch (e) {
throw new SequenceException("Could not get contents", e);
}
}
}
return new TextDecoder().decode(outputStream.toArray());
}
/**
* Revoke the current sequence for a specific drive.
*
* @param {string} driveId The drive ID.
* @throws SequenceException Thrown if error with the nonce sequence
*/
async revokeSequence(driveId) {
let contents = await this.getContents();
let configs = this.#serializer.deserialize(contents);
let sequence = FileSequencer.#getSequence(configs, driveId);
if (sequence == null)
throw new SequenceException("Sequence does not exist");
if (sequence.getStatus() == Status.Revoked)
throw new SequenceException("Sequence already revoked");
sequence.setStatus(Status.Revoked);
await this.saveSequenceFile(configs);
}
/**
* Get the sequence by the drive ID.
*
* @param {string} driveId The drive ID.
* @returns {Promise<NonceSequence | null>} The sequence
* @throws SequenceException Thrown if error with the nonce sequence
*/
async getSequence(driveId) {
let contents = await this.getContents();
let configs = this.#serializer.deserialize(contents);
let sequence = FileSequencer.#getSequence(configs, driveId);
return sequence;
}
/**
* Close this file sequencer.
*/
close() {
}
/**
* Save the sequence file.
*
* @param {Map<string, NonceSequence>} sequences The sequences.
* @throws SequenceException Thrown if error with the nonce sequence
*/
async saveSequenceFile(sequences) {
try {
let contents = this.#serializer.serialize(sequences);
await this.saveContents(contents);
}
catch (ex) {
console.error(ex);
throw new SequenceException("Could not serialize sequences", ex);
}
}
/**
* Save the contents of the file
* @param {string} contents The contents
*/
async saveContents(contents) {
let inputStream = null;
let outputStream = null;
try {
outputStream = await this.#sequenceFile.getOutputStream();
// FileSystemDirectoryHandle.removeEntry() does not always work in time
// to avoid NoModificationAllowedError we force truncate
await outputStream.setLength(0);
inputStream = new MemoryStream(new TextEncoder().encode(contents));
let buffer = new Uint8Array(32768);
let bytesRead;
while ((bytesRead = await inputStream.read(buffer, 0, buffer.length)) > 0) {
await outputStream.write(buffer, 0, bytesRead);
}
}
catch (ex) {
console.error(ex);
throw new SequenceException("Could not save sequence file", ex);
}
finally {
if (outputStream) {
await outputStream.flush();
try {
await outputStream.close();
}
catch (e) {
throw new SequenceException("Could not save sequence file", e);
}
}
if (inputStream) {
try {
await inputStream.close();
}
catch (e) {
throw new SequenceException("Could not save sequence file", e);
}
}
}
let parent = await this.#sequenceFile.getParent();
this.#sequenceFile = await parent.getChild(this.#sequenceFile.getName());
}
/**
* Get the sequence for the drive provided.
*
* @param {Map<string, NonceSequence>} configs All sequence configurations.
* @param {string} driveId The drive ID.
* @returns {NonceSequence | null} The nonce sequence
* @throws SequenceException Thrown if error with the nonce sequence
*/
static #getSequence(configs, driveId) {
let sequence = null;
for (let [key, seq] of configs) {
if (driveId.toUpperCase() == seq.getId().toUpperCase()) {
// there should be only one sequence available
if (seq.getStatus() == Status.Active || seq.getStatus() == Status.New) {
if (sequence)
throw new SequenceException("Corrupt sequence config");
sequence = seq;
}
}
}
return sequence;
}
}