{"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<string, string>\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, string>): 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, string>): 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,C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EAAE;YACP,MAAM,eAAe,CAAC,uBAAuB,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;AAC3D,SAAA;KACJ,CAAA;IACL,OAAC,aAAA,CAAA;AAAD,CAAC,EAAA;;;;"}
{"version":3,"file":"TimeUtils.js","sources":["../../src/utils/TimeUtils.ts"],"sourcesContent":["/*\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * Utility class which exposes functions for managing date and time operations.\n */\nexport class TimeUtils {\n\n /**\n * return the current time in Unix time (seconds).\n */\n static nowSeconds(): number {\n // Date.getTime() returns in milliseconds.\n return Math.round(new Date().getTime() / 1000.0);\n }\n \n /**\n * check if a token is expired based on given UTC time in seconds.\n * @param expiresOn\n */\n static isTokenExpired(expiresOn: string, offset: number): boolean {\n // check for access token expiry\n const expirationSec = Number(expiresOn) || 0;\n const offsetCurrentTimeSec = TimeUtils.nowSeconds() + offset;\n\n // If current time + offset is greater than token expiration time, then token is expired.\n return (offsetCurrentTimeSec > expirationSec);\n }\n\n /**\n * If the current time is earlier than the time that a token was cached at, we must discard the token\n * i.e. The system clock was turned back after acquiring the cached token\n * @param cachedAt \n * @param offset \n */\n static wasClockTurnedBack(cachedAt: string): boolean {\n const cachedAtSec = Number(cachedAt);\n\n return cachedAtSec > TimeUtils.nowSeconds();\n }\n\n /**\n * Waits for t number of milliseconds\n * @param t number\n * @param value T\n */\n static delay<T>(t: number, value?: T): Promise<T | void> {\n return new Promise((resolve) => setTimeout(() => resolve(value), t));\n }\n}\n"],"names":[],"mappings":";;AAAA;;;AAGG;AAEH;;AAEG;AACH,IAAA,SAAA,kBAAA,YAAA;AAAA,IAAA,SAAA,SAAA,GAAA;KA2CC;AAzCG;;AAEG;AACI,IAAA,SAAA,CAAA,UAAU,GAAjB,YAAA;;AAEI,QAAA,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,GAAG,MAAM,CAAC,CAAC;KACpD,CAAA;AAED;;;AAGG;AACI,IAAA,SAAA,CAAA,cAAc,GAArB,UAAsB,SAAiB,EAAE,MAAc,EAAA;;QAEnD,IAAM,aAAa,GAAG,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;QAC7C,IAAM,oBAAoB,GAAG,SAAS,CAAC,UAAU,EAAE,GAAG,MAAM,CAAC;;AAG7D,QAAA,QAAQ,oBAAoB,GAAG,aAAa,EAAE;KACjD,CAAA;AAED;;;;;AAKG;IACI,SAAkB,CAAA,kBAAA,GAAzB,UAA0B,QAAgB,EAAA;AACtC,QAAA,IAAM,WAAW,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC;AAErC,QAAA,OAAO,WAAW,GAAG,SAAS,CAAC,UAAU,EAAE,CAAC;KAC/C,CAAA;AAED;;;;AAIG;AACI,IAAA,SAAA,CAAA,KAAK,GAAZ,UAAgB,CAAS,EAAE,KAAS,EAAA;QAChC,OAAO,IAAI,OAAO,CAAC,UAAC,OAAO,EAAK,EAAA,OAAA,UAAU,CAAC,YAAM,EAAA,OAAA,OAAO,CAAC,KAAK,CAAC,CAAA,EAAA,EAAE,CAAC,CAAC,CAAA,EAAA,CAAC,CAAC;KACxE,CAAA;IACL,OAAC,SAAA,CAAA;AAAD,CAAC,EAAA;;;;"}
1.[Prerequisites and Usage](#prerequisites-and-usage)
1.[Installation](#installation)
1.[Security Reporting](#security-reporting)
1.[License](#license)
1.[Code of Conduct](#we-value-and-adhere-to-the-microsoft-open-source-code-of-conduct)
## About
The MSAL library for JavaScript enables client-side JavaScript applications to authenticate users using [Azure AD](https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-overview) work and school accounts (AAD), Microsoft personal accounts (MSA) and social identity providers like Facebook, Google, LinkedIn, Microsoft accounts, etc. through [Azure AD B2C](https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-overview#identity-providers) service. It also enables your app to get tokens to access [Microsoft Cloud](https://www.microsoft.com/enterprise) services such as [Microsoft Graph](https://graph.microsoft.io).
The `@azure/msal-common` package described by the code in this folder serves as a common package dependency for the `@azure/msal-browser` package (and in the future, the msal-node package). Be aware that this is an internal library, and is subject to frequent change. **It is not meant for production consumption by itself.**
## FAQ
See [here](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-common/FAQ.md).
## Releases
*Expect us to detail our major and minor releases moving forward, while leaving out our patch releases. Patch release notes can be found in our change log.*
| Date | Release | Announcement | Main features |
| ------| ------- | ---------| --------- |
| August 4, 2020 | @azure/msal-common v1.1.0 | [Release Notes](https://https://github.com/AzureAD/microsoft-authentication-library-for-js/releases/tag/msal-common-v1.1.0)
| July 20, 2020 | @azure/msal-common v1.0.0 | [Release Notes](https://github.com/AzureAD/microsoft-authentication-library-for-js/releases/tag/msal-common-v1.0.0) | Full release version of the `@azure/msal-common` |
| May 11, 2020 | @azure/msal-common v1.0.0-beta | Beta version of the `@azure/msal-common` package |
| January 17, 2020 | @azure/msal-common v1.0.0-alpha | No release notes yet | Alpha version of the `@azure/msal-common` package with authorization code flow for SPAs working in dev. |
## Prerequisites and Usage
This library is not meant for production use. Please use one of these packages specific to the platform you are developing for:
-[MSAL for Single Page Applications (SPAs)](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-browser)
-[MSAL for Node.js](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-node)
## Installation
### Via NPM:
npm install @azure/msal-common
## Security Reporting
If you find a security issue with our libraries or services please report it to [secure@microsoft.com](mailto:secure@microsoft.com) with as much detail as possible. Your submission may be eligible for a bounty through the [Microsoft Bounty](http://aka.ms/bugbounty) program. Please do not post security issues to GitHub Issues or any other public site. We will contact you shortly upon receiving the information. We encourage you to get notifications of when security incidents occur by visiting [this page](https://technet.microsoft.com/en-us/security/dd252948) and subscribing to Security Advisory Alerts.
## License
Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License (the "License");
## We Value and Adhere to the Microsoft Open Source Code of Conduct
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.