azurite
Version:
An open source Azure Storage API compatible server
105 lines • 4.23 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccountSASResourceType = void 0;
var AccountSASResourceType;
(function (AccountSASResourceType) {
AccountSASResourceType["Service"] = "s";
AccountSASResourceType["Container"] = "c";
AccountSASResourceType["Object"] = "o";
AccountSASResourceType["Any"] = "AnyResourceType"; // This is only used for blob batch operation.
})(AccountSASResourceType || (exports.AccountSASResourceType = AccountSASResourceType = {}));
/**
* This is a helper class to construct a string representing the resources accessible by an AccountSAS. Setting a value
* to true means that any SAS which uses these permissions will grant access to that resource type. Once all the
* values are set, this should be serialized with toString and set as the resources field on an
* {@link AccountSASSignatureValues} object. It is possible to construct the resources string without this class, but
* the order of the resources is particular and this class guarantees correctness.
*
* @export
* @class AccountSASResourceTypes
*/
class AccountSASResourceTypes {
constructor() {
/**
* Permission to access service level APIs granted.
*
* @type {boolean}
* @memberof AccountSASResourceTypes
*/
this.service = false;
/**
* Permission to access container level APIs (Blob Containers, Tables, Queues, File Shares) granted.
*
* @type {boolean}
* @memberof AccountSASResourceTypes
*/
this.container = false;
/**
* Permission to access object level APIs (Blobs, Table Entities, Queue Messages, Files) granted.
*
* @type {boolean}
* @memberof AccountSASResourceTypes
*/
this.object = false;
}
/**
* Creates an {@link AccountSASResourceType} from the specified resource types string. This method will throw an
* Error if it encounters a character that does not correspond to a valid resource type.
*
* @static
* @param {string} resourceTypes
* @returns {AccountSASResourceTypes}
* @memberof AccountSASResourceTypes
*/
static parse(resourceTypes) {
const accountSASResourceTypes = new AccountSASResourceTypes();
for (const c of resourceTypes) {
switch (c) {
case AccountSASResourceType.Service:
if (accountSASResourceTypes.service) {
throw new RangeError(`Duplicated permission character: ${c}`);
}
accountSASResourceTypes.service = true;
break;
case AccountSASResourceType.Container:
if (accountSASResourceTypes.container) {
throw new RangeError(`Duplicated permission character: ${c}`);
}
accountSASResourceTypes.container = true;
break;
case AccountSASResourceType.Object:
if (accountSASResourceTypes.object) {
throw new RangeError(`Duplicated permission character: ${c}`);
}
accountSASResourceTypes.object = true;
break;
default:
throw new RangeError(`Invalid resource type: ${c}`);
}
}
return accountSASResourceTypes;
}
/**
* Converts the given resource types to a string.
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/constructing-an-account-sas
*
* @returns {string}
* @memberof AccountSASResourceTypes
*/
toString() {
const resourceTypes = [];
if (this.service) {
resourceTypes.push(AccountSASResourceType.Service);
}
if (this.container) {
resourceTypes.push(AccountSASResourceType.Container);
}
if (this.object) {
resourceTypes.push(AccountSASResourceType.Object);
}
return resourceTypes.join("");
}
}
exports.default = AccountSASResourceTypes;
//# sourceMappingURL=AccountSASResourceTypes.js.map
;