{"version":3,"file":"ProtocolUtils.js","sources":["../../src/utils/ProtocolUtils.ts"],"sourcesContent":["/*\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { StringUtils } from \"./StringUtils\";\nimport { Constants } from \"./Constants\";\nimport { ICrypto } from \"../crypto/ICrypto\";\nimport { ClientAuthError } from \"../error/ClientAuthError\";\n\n/**\n * Type which defines the object that is stringified, encoded and sent in the state value.\n * Contains the following:\n * - id - unique identifier for this request\n * - ts - timestamp for the time the request was made. Used to ensure that token expiration is not calculated incorrectly.\n * - platformState - string value sent from the platform.\n */\nexport type LibraryStateObject = {\n id: string,\n meta?: Record\n};\n\n/**\n * Type which defines the stringified and encoded object sent to the service in the authorize request.\n */\nexport type RequestStateObject = {\n userRequestState: string,\n libraryState: LibraryStateObject\n};\n\n/**\n * Class which provides helpers for OAuth 2.0 protocol specific values\n */\nexport class ProtocolUtils {\n\n /**\n * Appends user state with random guid, or returns random guid.\n * @param userState \n * @param randomGuid \n */\n static setRequestState(cryptoObj: ICrypto, userState?: string, meta?: Record): string {\n const libraryState = ProtocolUtils.generateLibraryState(cryptoObj, meta);\n return !StringUtils.isEmpty(userState) ? `${libraryState}${Constants.RESOURCE_DELIM}${userState}` : libraryState;\n }\n\n /**\n * Generates the state value used by the common library.\n * @param randomGuid \n * @param cryptoObj \n */\n static generateLibraryState(cryptoObj: ICrypto, meta?: Record): string {\n if (!cryptoObj) {\n throw ClientAuthError.createNoCryptoObjectError(\"generateLibraryState\");\n }\n\n // Create a state object containing a unique id and the timestamp of the request creation\n const stateObj: LibraryStateObject = {\n id: cryptoObj.createNewGuid()\n };\n\n if (meta) {\n stateObj.meta = meta;\n }\n\n const stateString = JSON.stringify(stateObj);\n\n return cryptoObj.base64Encode(stateString);\n }\n\n /**\n * Parses the state into the RequestStateObject, which contains the LibraryState info and the state passed by the user.\n * @param state \n * @param cryptoObj \n */\n static parseRequestState(cryptoObj: ICrypto, state: string): RequestStateObject {\n if (!cryptoObj) {\n throw ClientAuthError.createNoCryptoObjectError(\"parseRequestState\");\n }\n\n if (StringUtils.isEmpty(state)) {\n throw ClientAuthError.createInvalidStateError(state, \"Null, undefined or empty state\");\n }\n\n try {\n // Split the state between library state and user passed state and decode them separately\n const splitState = state.split(Constants.RESOURCE_DELIM);\n const libraryState = splitState[0];\n const userState = splitState.length > 1 ? splitState.slice(1).join(Constants.RESOURCE_DELIM) : Constants.EMPTY_STRING;\n const libraryStateString = cryptoObj.base64Decode(libraryState);\n const libraryStateObj = JSON.parse(libraryStateString) as LibraryStateObject;\n return {\n userRequestState: !StringUtils.isEmpty(userState) ? userState : Constants.EMPTY_STRING,\n libraryState: libraryStateObj\n };\n } catch(e) {\n throw ClientAuthError.createInvalidStateError(state, e);\n }\n }\n}\n"],"names":[],"mappings":";;;;;;AAAA;;;AAGG;AA2BH;;AAEG;AACH,IAAA,aAAA,kBAAA,YAAA;AAAA,IAAA,SAAA,aAAA,GAAA;KAiEC;AA/DG;;;;AAIG;AACI,IAAA,aAAA,CAAA,eAAe,GAAtB,UAAuB,SAAkB,EAAE,SAAkB,EAAE,IAA6B,EAAA;QACxF,IAAM,YAAY,GAAG,aAAa,CAAC,oBAAoB,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;QACzE,OAAO,CAAC,WAAW,CAAC,OAAO,CAAC,SAAS,CAAC,GAAG,EAAA,GAAG,YAAY,GAAG,SAAS,CAAC,cAAc,GAAG,SAAW,GAAG,YAAY,CAAC;KACpH,CAAA;AAED;;;;AAIG;AACI,IAAA,aAAA,CAAA,oBAAoB,GAA3B,UAA4B,SAAkB,EAAE,IAA6B,EAAA;QACzE,IAAI,CAAC,SAAS,EAAE;AACZ,YAAA,MAAM,eAAe,CAAC,yBAAyB,CAAC,sBAAsB,CAAC,CAAC;AAC3E,SAAA;;AAGD,QAAA,IAAM,QAAQ,GAAuB;AACjC,YAAA,EAAE,EAAE,SAAS,CAAC,aAAa,EAAE;SAChC,CAAC;AAEF,QAAA,I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