@salesforce/core
Version:
Core libraries to interact with SFDX projects, orgs, and APIs.
100 lines • 3.72 kB
JavaScript
;
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.matchesJwtAccessToken = exports.matchesOpaqueAccessToken = exports.sfdxAuthUrlRegex = exports.jwtTokenRegex = exports.accessTokenRegex = exports.validatePathDoesNotContainInvalidChars = exports.validateSalesforceId = exports.validateEmail = exports.validateApiVersion = void 0;
exports.trimTo15 = trimTo15;
exports.matchesAccessToken = matchesAccessToken;
function trimTo15(id) {
if (!id) {
return undefined;
}
if (id.length && id.length > 15) {
return id.substring(0, 15);
}
return id;
}
/**
* Tests whether an API version matches the format `i.0`.
*
* @param value The API version as a string.
*/
const validateApiVersion = (value) => value == null || /^[1-9]\d\.0$/.test(value);
exports.validateApiVersion = validateApiVersion;
/**
* Tests whether an email matches the format `me@my.org`
*
* @param value The email as a string.
*/
const validateEmail = (value) => /^[^.][^@]*@[^.]+(\.[^.\s]+)+$/.test(value);
exports.validateEmail = validateEmail;
/**
* Tests whether a Salesforce ID is in the correct format, a 15- or 18-character length string with only letters and numbers
*
* @param value The ID as a string.
*/
const validateSalesforceId = (value) => /[a-zA-Z0-9]{18}|[a-zA-Z0-9]{15}/.test(value) && (value.length === 15 || value.length === 18);
exports.validateSalesforceId = validateSalesforceId;
/**
* Tests whether a path is in the correct format; the value doesn't include the characters "[", "]", "?", "<", ">", "?", "|"
*
* @param value The path as a string.
*/
const validatePathDoesNotContainInvalidChars = (value) =>
// eslint-disable-next-line no-useless-escape
!/[\["\?<>\|\]]+/.test(value);
exports.validatePathDoesNotContainInvalidChars = validatePathDoesNotContainInvalidChars;
exports.accessTokenRegex = /(00D\w{12,15})![.\w]*/;
// 'eyJ' strongly suggests that this is a base64 JSON, and so the general shape of the rest of it is enough to presume it's a JWT.
exports.jwtTokenRegex = /eyJ[A-Za-z0-9+=_-]+\.[A-Za-z0-9+=_-]+\.[A-Za-z0-9+=_-]+/;
exports.sfdxAuthUrlRegex = /force:\/\/([a-zA-Z0-9._-]+):([a-zA-Z0-9._-]*):([a-zA-Z0-9._-]+={0,2})@([a-zA-Z0-9._-]+)/;
/**
* Tests whether a given string is an opaque access token, a JWT token, or neither.
*
* @param value
*/
function matchesAccessToken(value) {
return (0, exports.matchesOpaqueAccessToken)(value) || (0, exports.matchesJwtAccessToken)(value);
}
/**
* Tests whether a given string is an opaque access token.
*
* @param value
*/
const matchesOpaqueAccessToken = (value) => exports.accessTokenRegex.test(value);
exports.matchesOpaqueAccessToken = matchesOpaqueAccessToken;
/**
* Tests whether a given string is a JWT-formatted access token.
*
* @param value
*/
const matchesJwtAccessToken = (value) => {
const segments = value.split('.');
if (segments.length !== 3) {
return false;
}
if (!isValidJson(segments[0], true)) {
return false;
}
return isValidJson(segments[1], false);
};
exports.matchesJwtAccessToken = matchesJwtAccessToken;
const isValidJson = (str, checkForTyp) => {
try {
const parsedJson = JSON.parse(Buffer.from(str, 'base64').toString('utf-8'));
if (checkForTyp) {
return 'typ' in parsedJson && parsedJson.typ === 'JWT';
}
else {
return true;
}
}
catch (e) {
return false;
}
};
//# sourceMappingURL=sfdc.js.map