/** * Minified by jsDelivr using Terser v5.19.2. * Original file: /npm/@azure/communication-common@2.3.1/dist/index.js * * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files */ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var jwtDecode=require("jwt-decode"),crypto=require("crypto"),coreUtil=require("@azure/core-util"),coreRestPipeline=require("@azure/core-rest-pipeline"),coreAuth=require("@azure/core-auth");const parseToken=e=>{const{exp:i}=jwtDecode.jwtDecode(e);return{token:e,expiresOnTimestamp:1e3*i}},expiredToken={token:"",expiresOnTimestamp:-10},minutesToMs=e=>1e3*e*60,defaultExpiringSoonInterval=minutesToMs(10),defaultRefreshAfterLifetimePercentage=.5;class AutoRefreshTokenCredential{constructor(e){this.expiringSoonIntervalInMs=defaultExpiringSoonInterval,this.refreshAfterLifetimePercentage=.5,this.activeTokenFetching=null,this.activeTokenUpdating=null,this.disposed=!1;const{tokenRefresher:i,token:t,refreshProactively:n}=e;this.refresh=i,this.currentToken=t?parseToken(t):expiredToken,this.refreshProactively=null!=n&&n,this.refreshProactively&&this.scheduleRefresh()}async getToken(e){if(!this.isTokenExpiringSoon(this.currentToken))return this.currentToken;if(!this.isTokenValid(this.currentToken)){const i=this.updateTokenAndReschedule(null==e?void 0:e.abortSignal);await i}return this.currentToken}dispose(){this.disposed=!0,this.activeTokenFetching=null,this.activeTokenUpdating=null,this.currentToken=expiredToken,this.activeTimeout&&clearTimeout(this.activeTimeout)}async updateTokenAndReschedule(e){if(this.activeTokenUpdating)return this.activeTokenUpdating;this.activeTokenUpdating=this.refreshTokenAndReschedule(e);try{await this.activeTokenUpdating}finally{this.activeTokenUpdating=null}}async refreshTokenAndReschedule(e){const i=await this.refreshToken(e);if(!this.isTokenValid(i))throw new Error("The token returned from the tokenRefresher is expired.");this.currentToken=i,this.refreshProactively&&this.scheduleRefresh()}async refreshToken(e){try{return this.activeTokenFetching||(this.activeTokenFetching=this.refresh(e)),parseToken(await this.activeTokenFetching)}finally{this.activeTokenFetching=null}}scheduleRefresh(){if(this.disposed)return;this.activeTimeout&&clearTimeout(this.activeTimeout);const e=this.currentToken.expiresOnTimestamp-Date.now();let i=null;i=this.isTokenExpiringSoon(this.currentToken)?e*this.refreshAfterLifetimePercentage:e-this.expiringSoonIntervalInMs,this.activeTimeout=setTimeout((()=>this.updateTokenAndReschedule()),i)}isTokenValid(e){return e&&Date.now()=e.expiresOnTimestamp-this.expiringSoonIntervalInMs}}class StaticTokenCredential{constructor(e){this.token=e}async getToken(){return this.token}dispose(){}}class AzureCommunicationTokenCredential{constructor(e){this.disposed=!1,this.tokenCredential="string"==typeof e?new StaticTokenCredential(parseToken(e)):new AutoRefreshTokenCredential(e)}async getToken(e){this.throwIfDisposed();const i=await this.tokenCredential.getToken(e);return this.throwIfDisposed(),i}dispose(){this.disposed=!0,this.tokenCredential.dispose()}throwIfDisposed(){if(this.disposed)throw new Error("User credential is disposed")}}const shaHash=async e=>crypto.createHash("sha256").update(e).digest("base64"),shaHMAC=async(e,i)=>{const t=Buffer.from(e,"base64");return crypto.createHmac("sha256",t).update(i).digest("base64")},communicationAccessKeyCredentialPolicy="CommunicationAccessKeyCredentialPolicy";function createCommunicationAccessKeyCredentialPolicy(e){return{name:communicationAccessKeyCredentialPolicy,async sendRequest(i,t){var n;const r=i.method.toUpperCase(),s=(new Date).toUTCString(),o=await shaHash((null===(n=i.body)||void 0===n?void 0:n.toString())||""),a="x-ms-date",c=`${a};host;x-ms-content-sha256`,d=new URL(i.url),u=d.searchParams.toString(),m=u?`${d.pathname}?${u}`:d.pathname,p=d.port,l=p?`${d.host}:${p}`:d.host,h=`${r}\n${m}\n${s};${l};${o}`,f=await shaHMAC(e.key,h);return coreUtil.isNode&&i.headers.set("Host",l||""),i.headers.set(a,s),i.headers.set("x-ms-content-sha256",o),i.headers.set("Authorization",`HMAC-SHA256 SignedHeaders=${c}&Signature=${f}`),t(i)}}}function createCommunicationAuthPolicy(e){if(coreAuth.isTokenCredential(e)){const i={credential:e,scopes:["https://communication.azure.com//.default"]};return coreRestPipeline.bearerTokenAuthenticationPolicy(i)}return createCommunicationAccessKeyCredentialPolicy(e)}const CONNECTION_STRING_REGEX=/endpoint=(.*);accesskey=(.*)/i,tryParseConnectionString=e=>{const i=e.match(CONNECTION_STRING_REGEX);if((null==i?void 0:i[1])&&i[2])return{endpoint:i[1],credential:new coreAuth.AzureKeyCredential(i[2])}},parseConnectionString=e=>{const i=tryParseConnectionString(e);if(i)return i;throw new Error(`Invalid connection string ${e}`)},isValidEndpoint=e=>{var i;const t=new URL(e);return!!(null===(i=t.protocol)||void 0===i?void 0:i.match(/^http[s]?/))&&void 0!==t.host&&""!==t.host&&(void 0===t.pathname||""===t.pathname||"/"===t.pathname)},assertValidEndpoint=e=>{if(!isValidEndpoint(e))throw new Error(`Invalid endpoint url ${e}`)},isKeyCredential=e=>{const i=e;return i&&"string"==typeof i.key&&void 0===i.getToken},parseClientArguments=(e,i)=>{if(isKeyCredential(i)||coreAuth.isTokenCredential(i))return assertValidEndpoint(e),{url:e,credential:i};{const{endpoint:i,credential:t}=parseConnectionString(e);return assertValidEndpoint(i),{url:i,credential:t}}},isCommunicationUserIdentifier=e=>"string"==typeof e.communicationUserId,isPhoneNumberIdentifier=e=>"string"==typeof e.phoneNumber,isMicrosoftTeamsUserIdentifier=e=>"string"==typeof e.microsoftTeamsUserId,isMicrosoftTeamsAppIdentifier=e=>"string"==typeof e.teamsAppId,isUnknownIdentifier=e=>"string"==typeof e.id,getIdentifierKind=e=>isCommunicationUserIdentifier(e)?Object.assign(Object.assign({},e),{kind:"communicationUser"}):isPhoneNumberIdentifier(e)?Object.assign(Object.assign({},e),{kind:"phoneNumber"}):isMicrosoftTeamsUserIdentifier(e)?Object.assign(Object.assign({},e),{kind:"microsoftTeamsUser"}):isMicrosoftTeamsAppIdentifier(e)?Object.assign(Object.assign({},e),{kind:"microsoftTeamsApp"}):Object.assign(Object.assign({},e),{kind:"unknown"}),getIdentifierRawId=e=>{const i=getIdentifierKind(e);switch(i.kind){case"communicationUser":return i.communicationUserId;case"microsoftTeamsUser":{const{microsoftTeamsUserId:e,rawId:t,cloud:n,isAnonymous:r}=i;if(t)return t;if(r)return`8:teamsvisitor:${e}`;switch(n){case"dod":return`8:dod:${e}`;case"gcch":return`8:gcch:${e}`;case"public":return`8:orgid:${e}`}return`8:orgid:${e}`}case"microsoftTeamsApp":{const{teamsAppId:e,rawId:t,cloud:n}=i;if(t)return t;switch(n){case"dod":return`28:dod:${e}`;case"gcch":return`28:gcch:${e}`}return`28:orgid:${e}`}case"phoneNumber":{const{phoneNumber:e,rawId:t}=i;return t||`4:${e}`}case"unknown":return i.id}},buildMicrosoftTeamsAppIdentifier=(e,i)=>({kind:"microsoftTeamsApp",teamsAppId:e,cloud:i}),buildMicrosoftTeamsUserIdentifier=(e,i,t)=>({kind:"microsoftTeamsUser",microsoftTeamsUserId:e,isAnonymous:t,cloud:i}),createIdentifierFromRawId=e=>{if(e.startsWith("4:"))return{kind:"phoneNumber",phoneNumber:`${e.substring(2)}`};const i=e.split(":");if(3!==i.length)return{kind:"unknown",id:e};const t=`${i[0]}:${i[1]}:`,n=i[2];switch(t){case"8:teamsvisitor:":return{kind:"microsoftTeamsUser",microsoftTeamsUserId:n,isAnonymous:!0};case"8:orgid:":return buildMicrosoftTeamsUserIdentifier(n,"public",!1);case"8:dod:":return buildMicrosoftTeamsUserIdentifier(n,"dod",!1);case"8:gcch:":return buildMicrosoftTeamsUserIdentifier(n,"gcch",!1);case"8:acs:":case"8:spool:":case"8:dod-acs:":case"8:gcch-acs:":return{kind:"communicationUser",communicationUserId:e};case"28:orgid:":return buildMicrosoftTeamsAppIdentifier(n,"public");case"28:gcch:":return buildMicrosoftTeamsAppIdentifier(n,"gcch");case"28:dod:":return buildMicrosoftTeamsAppIdentifier(n,"dod")}return{kind:"unknown",id:e}},assertNotNullOrUndefined=(e,i)=>{const t=Object.keys(e)[0],n=e[t];if(i in n)return n[i];throw new Error(`Property ${i} is required for identifier of type ${t}.`)},assertMaximumOneNestedModel=e=>{const i=[];if(void 0!==e.communicationUser&&i.push("communicationUser"),void 0!==e.microsoftTeamsUser&&i.push("microsoftTeamsUser"),void 0!==e.microsoftTeamsApp&&i.push("microsoftTeamsApp"),void 0!==e.phoneNumber&&i.push("phoneNumber"),i.length>1)throw new Error(`Only one of the properties in ${JSON.stringify(i)} should be present.`)},serializeCommunicationIdentifier=e=>{var i,t,n,r,s,o;const a=getIdentifierKind(e);switch(a.kind){case"communicationUser":return{rawId:getIdentifierRawId(a),communicationUser:{id:a.communicationUserId}};case"phoneNumber":return{rawId:null!==(i=a.rawId)&&void 0!==i?i:getIdentifierRawId(a),phoneNumber:{value:a.phoneNumber}};case"microsoftTeamsUser":return{rawId:null!==(t=a.rawId)&&void 0!==t?t:getIdentifierRawId(a),microsoftTeamsUser:{userId:a.microsoftTeamsUserId,isAnonymous:null!==(n=a.isAnonymous)&&void 0!==n&&n,cloud:null!==(r=a.cloud)&&void 0!==r?r:"public"}};case"microsoftTeamsApp":return{rawId:null!==(s=a.rawId)&&void 0!==s?s:getIdentifierRawId(a),microsoftTeamsApp:{appId:a.teamsAppId,cloud:null!==(o=a.cloud)&&void 0!==o?o:"public"}};case"unknown":return{rawId:a.id};default:throw new Error(`Can't serialize an identifier with kind ${a.kind}`)}},getKind=e=>e.communicationUser?"communicationUser":e.phoneNumber?"phoneNumber":e.microsoftTeamsUser?"microsoftTeamsUser":e.microsoftTeamsApp?"microsoftTeamsApp":"unknown",deserializeCommunicationIdentifier=e=>{var i;assertMaximumOneNestedModel(e);const{communicationUser:t,microsoftTeamsUser:n,microsoftTeamsApp:r,phoneNumber:s}=e,o=null!==(i=e.kind)&&void 0!==i?i:getKind(e);return"communicationUser"===o&&t?{kind:"communicationUser",communicationUserId:assertNotNullOrUndefined({communicationUser:t},"id")}:"phoneNumber"===o&&s?{kind:"phoneNumber",phoneNumber:assertNotNullOrUndefined({phoneNumber:s},"value"),rawId:assertNotNullOrUndefined({phoneNumber:e},"rawId")}:"microsoftTeamsUser"===o&&n?{kind:"microsoftTeamsUser",microsoftTeamsUserId:assertNotNullOrUndefined({microsoftTeamsUser:n},"userId"),isAnonymous:assertNotNullOrUndefined({microsoftTeamsUser:n},"isAnonymous"),cloud:assertNotNullOrUndefined({microsoftTeamsUser:n},"cloud"),rawId:assertNotNullOrUndefined({microsoftTeamsUser:e},"rawId")}:"microsoftTeamsApp"===o&&r?{kind:"microsoftTeamsApp",teamsAppId:assertNotNullOrUndefined({microsoftTeamsApp:r},"appId"),cloud:assertNotNullOrUndefined({microsoftTeamsApp:r},"cloud"),rawId:assertNotNullOrUndefined({microsoftTeamsApp:e},"rawId")}:{kind:"unknown",id:assertNotNullOrUndefined({unknown:e},"rawId")}};exports.AzureCommunicationTokenCredential=AzureCommunicationTokenCredential,exports.createCommunicationAccessKeyCredentialPolicy=createCommunicationAccessKeyCredentialPolicy,exports.createCommunicationAuthPolicy=createCommunicationAuthPolicy,exports.createIdentifierFromRawId=createIdentifierFromRawId,exports.deserializeCommunicationIdentifier=deserializeCommunicationIdentifier,exports.getIdentifierKind=getIdentifierKind,exports.getIdentifierRawId=getIdentifierRawId,exports.isCommunicationUserIdentifier=isCommunicationUserIdentifier,exports.isKeyCredential=isKeyCredential,exports.isMicrosoftTeamsAppIdentifier=isMicrosoftTeamsAppIdentifier,exports.isMicrosoftTeamsUserIdentifier=isMicrosoftTeamsUserIdentifier,exports.isPhoneNumberIdentifier=isPhoneNumberIdentifier,exports.isUnknownIdentifier=isUnknownIdentifier,exports.parseClientArguments=parseClientArguments,exports.parseConnectionString=parseConnectionString,exports.serializeCommunicationIdentifier=serializeCommunicationIdentifier; //# sourceMappingURL=/sm/48338a91759e1f5e9eb5000b8a51499e5caf10a6d30ba7091659dda1f037b6fe.map