@@ -25,19 +25,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
2525 mod
2626) ) ;
2727var __toCommonJS = ( mod ) => __copyProps ( __defProp ( { } , "__esModule" , { value : true } ) , mod ) ;
28- var __accessCheck = ( obj , member , msg ) => {
29- if ( ! member . has ( obj ) )
30- throw TypeError ( "Cannot " + msg ) ;
31- } ;
32- var __privateAdd = ( obj , member , value ) => {
33- if ( member . has ( obj ) )
34- throw TypeError ( "Cannot add the same private member more than once" ) ;
35- member instanceof WeakSet ? member . add ( obj ) : member . set ( obj , value ) ;
36- } ;
37- var __privateMethod = ( obj , member , method ) => {
38- __accessCheck ( obj , member , "access private method" ) ;
39- return method ;
40- } ;
4128
4229// src/client.js
4330var client_exports = { } ;
@@ -61,30 +48,28 @@ var AUTH_USER_SUPPLIED = "user-supplied";
6148var AUTH_USERNAME_PASSWORD = "username-password" ;
6249var AUTH_OAUTH_CLIENT_CREDENTIALS = "oauth-client-credentials" ;
6350var AUTH_OAUTH_JWT_BEARER = "oauth-jwt-bearer" ;
64- var _checkMandatoryVariables , checkMandatoryVariables_fn ;
65- var _Configuration = class {
51+ var Configuration = class _Configuration {
6652 static load ( ) {
67- var _a , _b , _c , _d ;
6853 dotenv . config ( ) ;
69- __privateMethod ( _a = _Configuration , _checkMandatoryVariables , checkMandatoryVariables_fn ) . call ( _a , [
54+ _Configuration . #checkMandatoryVariables ( [
7055 "SALESFORCE_AUTH_TYPE" ,
7156 "PUB_SUB_ENDPOINT"
7257 ] ) ;
7358 if ( _Configuration . isUsernamePasswordAuth ( ) ) {
74- __privateMethod ( _b = _Configuration , _checkMandatoryVariables , checkMandatoryVariables_fn ) . call ( _b , [
59+ _Configuration . #checkMandatoryVariables ( [
7560 "SALESFORCE_LOGIN_URL" ,
7661 "SALESFORCE_USERNAME" ,
7762 "SALESFORCE_PASSWORD" ,
7863 "SALESFORCE_TOKEN"
7964 ] ) ;
8065 } else if ( _Configuration . isOAuthClientCredentialsAuth ( ) ) {
81- __privateMethod ( _c = _Configuration , _checkMandatoryVariables , checkMandatoryVariables_fn ) . call ( _c , [
66+ _Configuration . #checkMandatoryVariables ( [
8267 "SALESFORCE_LOGIN_URL" ,
8368 "SALESFORCE_CLIENT_ID" ,
8469 "SALESFORCE_CLIENT_SECRET"
8570 ] ) ;
8671 } else if ( _Configuration . isOAuthJwtBearerAuth ( ) ) {
87- __privateMethod ( _d = _Configuration , _checkMandatoryVariables , checkMandatoryVariables_fn ) . call ( _d , [
72+ _Configuration . #checkMandatoryVariables ( [
8873 "SALESFORCE_LOGIN_URL" ,
8974 "SALESFORCE_CLIENT_ID" ,
9075 "SALESFORCE_USERNAME" ,
@@ -140,17 +125,14 @@ var _Configuration = class {
140125 static isOAuthJwtBearerAuth ( ) {
141126 return _Configuration . getAuthType ( ) === AUTH_OAUTH_JWT_BEARER ;
142127 }
128+ static #checkMandatoryVariables( varNames ) {
129+ varNames . forEach ( ( varName ) => {
130+ if ( ! process . env [ varName ] ) {
131+ throw new Error ( `Missing ${ varName } environment variable` ) ;
132+ }
133+ } ) ;
134+ }
143135} ;
144- var Configuration = _Configuration ;
145- _checkMandatoryVariables = new WeakSet ( ) ;
146- checkMandatoryVariables_fn = function ( varNames ) {
147- varNames . forEach ( ( varName ) => {
148- if ( ! process . env [ varName ] ) {
149- throw new Error ( `Missing ${ varName } environment variable` ) ;
150- }
151- } ) ;
152- } ;
153- __privateAdd ( Configuration , _checkMandatoryVariables ) ;
154136
155137// src/eventParser.js
156138var import_avro_js = __toESM ( require ( "avro-js" ) , 1 ) ;
@@ -271,127 +253,114 @@ function hexToBin(hex) {
271253var import_crypto = __toESM ( require ( "crypto" ) , 1 ) ;
272254var import_jsforce = __toESM ( require ( "jsforce" ) , 1 ) ;
273255var import_undici = require ( "undici" ) ;
274- var _authWithUsernamePassword , authWithUsernamePassword_fn , _authWithOAuthClientCredentials , authWithOAuthClientCredentials_fn , _authWithJwtBearer , authWithJwtBearer_fn , _authWithOAuth , authWithOAuth_fn ;
275- var _SalesforceAuth = class {
256+ var SalesforceAuth = class _SalesforceAuth {
276257 /**
277258 * Authenticates with the auth mode specified in configuration
278259 * @returns {ConnectionMetadata }
279260 */
280261 static async authenticate ( ) {
281- var _a , _b , _c ;
282262 if ( Configuration . isUsernamePasswordAuth ( ) ) {
283- return __privateMethod ( _a = _SalesforceAuth , _authWithUsernamePassword , authWithUsernamePassword_fn ) . call ( _a ) ;
263+ return _SalesforceAuth . #authWithUsernamePassword ( ) ;
284264 } else if ( Configuration . isOAuthClientCredentialsAuth ( ) ) {
285- return __privateMethod ( _b = _SalesforceAuth , _authWithOAuthClientCredentials , authWithOAuthClientCredentials_fn ) . call ( _b ) ;
265+ return _SalesforceAuth . #authWithOAuthClientCredentials ( ) ;
286266 } else if ( Configuration . isOAuthJwtBearerAuth ( ) ) {
287- return __privateMethod ( _c = _SalesforceAuth , _authWithJwtBearer , authWithJwtBearer_fn ) . call ( _c ) ;
267+ return _SalesforceAuth . #authWithJwtBearer ( ) ;
288268 } else {
289269 throw new Error ( "Unsupported authentication mode." ) ;
290270 }
291271 }
292- } ;
293- var SalesforceAuth = _SalesforceAuth ;
294- _authWithUsernamePassword = new WeakSet ( ) ;
295- authWithUsernamePassword_fn = async function ( ) {
296- const sfConnection = new import_jsforce . default . Connection ( {
297- loginUrl : Configuration . getSfLoginUrl ( )
298- } ) ;
299- await sfConnection . login (
300- Configuration . getSfUsername ( ) ,
301- Configuration . getSfSecuredPassword ( )
302- ) ;
303- return {
304- accessToken : sfConnection . accessToken ,
305- instanceUrl : sfConnection . instanceUrl ,
306- organizationId : sfConnection . userInfo . organizationId ,
307- username : Configuration . getSfUsername ( )
308- } ;
309- } ;
310- _authWithOAuthClientCredentials = new WeakSet ( ) ;
311- authWithOAuthClientCredentials_fn = async function ( ) {
312- var _a ;
313- const params = new URLSearchParams ( ) ;
314- params . append ( "grant_type" , "client_credentials" ) ;
315- params . append ( "client_id" , Configuration . getSfClientId ( ) ) ;
316- params . append ( "client_secret" , Configuration . getSfClientSecret ( ) ) ;
317- return __privateMethod ( _a = _SalesforceAuth , _authWithOAuth , authWithOAuth_fn ) . call ( _a , params . toString ( ) ) ;
318- } ;
319- _authWithJwtBearer = new WeakSet ( ) ;
320- authWithJwtBearer_fn = async function ( ) {
321- var _a ;
322- const header = JSON . stringify ( { alg : "RS256" } ) ;
323- const claims = JSON . stringify ( {
324- iss : Configuration . getSfClientId ( ) ,
325- sub : Configuration . getSfUsername ( ) ,
326- aud : Configuration . getSfLoginUrl ( ) ,
327- exp : Math . floor ( Date . now ( ) / 1e3 ) + 60 * 5
328- } ) ;
329- let token = `${ base64url ( header ) } .${ base64url ( claims ) } ` ;
330- const sign = import_crypto . default . createSign ( "RSA-SHA256" ) ;
331- sign . update ( token ) ;
332- sign . end ( ) ;
333- token += `.${ base64url ( sign . sign ( Configuration . getSfPrivateKey ( ) ) ) } ` ;
334- const body = `grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=${ token } ` ;
335- return __privateMethod ( _a = _SalesforceAuth , _authWithOAuth , authWithOAuth_fn ) . call ( _a , body ) ;
336- } ;
337- _authWithOAuth = new WeakSet ( ) ;
338- authWithOAuth_fn = async function ( body ) {
339- const loginResponse = await ( 0 , import_undici . fetch ) (
340- `${ Configuration . getSfLoginUrl ( ) } /services/oauth2/token` ,
341- {
342- method : "post" ,
343- headers : {
344- "Content-Type" : "application/x-www-form-urlencoded"
345- } ,
346- body
347- }
348- ) ;
349- if ( loginResponse . status !== 200 ) {
350- throw new Error (
351- `Authentication error: HTTP ${ loginResponse . status } - ${ await loginResponse . text ( ) } `
272+ /**
273+ * Authenticates with the username/password flow
274+ * @returns {ConnectionMetadata }
275+ */
276+ static async #authWithUsernamePassword( ) {
277+ const sfConnection = new import_jsforce . default . Connection ( {
278+ loginUrl : Configuration . getSfLoginUrl ( )
279+ } ) ;
280+ await sfConnection . login (
281+ Configuration . getSfUsername ( ) ,
282+ Configuration . getSfSecuredPassword ( )
352283 ) ;
284+ return {
285+ accessToken : sfConnection . accessToken ,
286+ instanceUrl : sfConnection . instanceUrl ,
287+ organizationId : sfConnection . userInfo . organizationId ,
288+ username : Configuration . getSfUsername ( )
289+ } ;
290+ }
291+ /**
292+ * Authenticates with the OAuth 2.0 client credentials flow
293+ * @returns {ConnectionMetadata }
294+ */
295+ static async #authWithOAuthClientCredentials( ) {
296+ const params = new URLSearchParams ( ) ;
297+ params . append ( "grant_type" , "client_credentials" ) ;
298+ params . append ( "client_id" , Configuration . getSfClientId ( ) ) ;
299+ params . append ( "client_secret" , Configuration . getSfClientSecret ( ) ) ;
300+ return _SalesforceAuth . #authWithOAuth( params . toString ( ) ) ;
353301 }
354- const { access_token, instance_url } = await loginResponse . json ( ) ;
355- const userInfoResponse = await ( 0 , import_undici . fetch ) (
356- `${ Configuration . getSfLoginUrl ( ) } /services/oauth2/userinfo` ,
357- {
358- headers : { authorization : `Bearer ${ access_token } ` }
302+ /**
303+ * Authenticates with the OAuth 2.0 JWT bearer flow
304+ * @returns {ConnectionMetadata }
305+ */
306+ static async #authWithJwtBearer( ) {
307+ const header = JSON . stringify ( { alg : "RS256" } ) ;
308+ const claims = JSON . stringify ( {
309+ iss : Configuration . getSfClientId ( ) ,
310+ sub : Configuration . getSfUsername ( ) ,
311+ aud : Configuration . getSfLoginUrl ( ) ,
312+ exp : Math . floor ( Date . now ( ) / 1e3 ) + 60 * 5
313+ } ) ;
314+ let token = `${ base64url ( header ) } .${ base64url ( claims ) } ` ;
315+ const sign = import_crypto . default . createSign ( "RSA-SHA256" ) ;
316+ sign . update ( token ) ;
317+ sign . end ( ) ;
318+ token += `.${ base64url ( sign . sign ( Configuration . getSfPrivateKey ( ) ) ) } ` ;
319+ const body = `grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=${ token } ` ;
320+ return _SalesforceAuth . #authWithOAuth( body ) ;
321+ }
322+ /**
323+ * Generic OAuth 2.0 connect method
324+ * @param {string } body URL encoded body
325+ * @returns {ConnectionMetadata } connection metadata
326+ */
327+ static async #authWithOAuth( body ) {
328+ const loginResponse = await ( 0 , import_undici . fetch ) (
329+ `${ Configuration . getSfLoginUrl ( ) } /services/oauth2/token` ,
330+ {
331+ method : "post" ,
332+ headers : {
333+ "Content-Type" : "application/x-www-form-urlencoded"
334+ } ,
335+ body
336+ }
337+ ) ;
338+ if ( loginResponse . status !== 200 ) {
339+ throw new Error (
340+ `Authentication error: HTTP ${ loginResponse . status } - ${ await loginResponse . text ( ) } `
341+ ) ;
359342 }
360- ) ;
361- if ( userInfoResponse . status !== 200 ) {
362- throw new Error (
363- `Failed to retrieve user info: HTTP ${ userInfoResponse . status } - ${ await userInfoResponse . text ( ) } `
343+ const { access_token, instance_url } = await loginResponse . json ( ) ;
344+ const userInfoResponse = await ( 0 , import_undici . fetch ) (
345+ `${ Configuration . getSfLoginUrl ( ) } /services/oauth2/userinfo` ,
346+ {
347+ headers : { authorization : `Bearer ${ access_token } ` }
348+ }
364349 ) ;
350+ if ( userInfoResponse . status !== 200 ) {
351+ throw new Error (
352+ `Failed to retrieve user info: HTTP ${ userInfoResponse . status } - ${ await userInfoResponse . text ( ) } `
353+ ) ;
354+ }
355+ const { organization_id, preferred_username } = await userInfoResponse . json ( ) ;
356+ return {
357+ accessToken : access_token ,
358+ instanceUrl : instance_url ,
359+ organizationId : organization_id ,
360+ username : preferred_username
361+ } ;
365362 }
366- const { organization_id, preferred_username } = await userInfoResponse . json ( ) ;
367- return {
368- accessToken : access_token ,
369- instanceUrl : instance_url ,
370- organizationId : organization_id ,
371- username : preferred_username
372- } ;
373363} ;
374- /**
375- * Authenticates with the username/password flow
376- * @returns {ConnectionMetadata }
377- */
378- __privateAdd ( SalesforceAuth , _authWithUsernamePassword ) ;
379- /**
380- * Authenticates with the OAuth 2.0 client credentials flow
381- * @returns {ConnectionMetadata }
382- */
383- __privateAdd ( SalesforceAuth , _authWithOAuthClientCredentials ) ;
384- /**
385- * Authenticates with the OAuth 2.0 JWT bearer flow
386- * @returns {ConnectionMetadata }
387- */
388- __privateAdd ( SalesforceAuth , _authWithJwtBearer ) ;
389- /**
390- * Generic OAuth 2.0 connect method
391- * @param {string } body URL encoded body
392- * @returns {ConnectionMetadata } connection metadata
393- */
394- __privateAdd ( SalesforceAuth , _authWithOAuth ) ;
395364function base64url ( input ) {
396365 const buf = Buffer . from ( input , "utf8" ) ;
397366 return buf . toString ( "base64url" ) ;
@@ -458,6 +427,21 @@ var PubSubApiClient = class {
458427 * @returns {Promise<void> } Promise that resolves once the connection is established
459428 */
460429 async connectWithAuth ( accessToken , instanceUrl , organizationId , username ) {
430+ if ( ! instanceUrl || ! instanceUrl . startsWith ( "https://" ) ) {
431+ throw new Error (
432+ `Invalid Salesforce Instance URL format supplied: ${ instanceUrl } `
433+ ) ;
434+ }
435+ if ( ! organizationId || organizationId . length !== 15 && organizationId . length !== 18 ) {
436+ throw new Error (
437+ `Invalid Salesforce Org ID format supplied: ${ organizationId } `
438+ ) ;
439+ }
440+ if ( ! username || username . indexOf ( "@" ) === - 1 ) {
441+ throw new Error (
442+ `Invalid Salesforce username format supplied: ${ username } `
443+ ) ;
444+ }
461445 return this . #connectToPubSubApi( {
462446 accessToken,
463447 instanceUrl,
@@ -707,5 +691,3 @@ var PubSubApiClient = class {
707691 } ) ;
708692 }
709693} ;
710- // Annotate the CommonJS export names for ESM import in node:
711- 0 && ( module . exports = { } ) ;
0 commit comments