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,aAAa,CAAC,EAAE;QACnE,YAAY;QACZ,MAAM,eAAe,GAAG,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;QACvD,IAAI,UAA8B,CAAC;QACnC,IAAI,eAAe,EAAE;YACnB,UAAU,GAAG,cAAc,CAAC,eAAe,CAAC,CAAC;SAC9C;QACD,wFAAwF;QACxF,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,UAAU,EAAE,CAAC;KAC5E;SAAM;QACL,QAAQ;QACR,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;KAC/D;AACH,CAAC;AAED,SAAS,cAAc,CAAC,KAAa;IACnC,uGAAuG;IACvG,IAAI,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE;QACvB,OAAO,QAAQ,CAAC,KAAK,EAAE,EAAE,CAAC,GAAG,IAAI,CAAC;KACnC;IACD,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAChC,IAAI,KAAK,CAAC,KAAK,CAAC,EAAE;QAChB,MAAM,IAAI,UAAU,CAClB,+BAA+B,EAC/B,oEAAoE,GAAG,KAAK,CAC7E,CAAC;KACH;IACD,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IACvB,MAAM,UAAU,GAAG,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC;IACnC,OAAO,UAAU,CAAC,OAAO,EAAE,GAAG,GAAG,CAAC;AACpC,CAAC;AAED,MAAM,CAAC,MAAM,EAAE,aAAa,EAAE,UAAU,EAAE,GAAG,WAAW,CAAC","sourcesContent":["import { CodedError, UnavailabilityError } from '@unimodules/core';\nimport invariant from 'invariant';\n\nimport {\n OAuthBaseProps,\n OAuthProps,\n OAuthRevokeOptions,\n OAuthServiceConfiguration,\n TokenResponse,\n} from './AppAuth.types';\nimport ExpoAppAuth from './ExpoAppAuth';\n\nexport * from './AppAuth.types';\n\nfunction isValidServiceConfiguration(config?: OAuthServiceConfiguration): boolean {\n return !!(\n config &&\n typeof config.authorizationEndpoint === 'string' &&\n typeof config.tokenEndpoint === 'string'\n );\n}\n\nfunction assertValidClientId(clientId?: string): void {\n if (typeof clientId !== 'string' || !clientId.length) {\n throw new CodedError(\n 'ERR_APP_AUTH_INVALID_CONFIG',\n '`clientId` must be a string with more than 0 characters'\n );\n }\n}\n\nfunction assertValidProps({\n issuer,\n redirectUrl,\n clientId,\n serviceConfiguration,\n}: OAuthProps): void {\n if (typeof issuer !== 'string' && !isValidServiceConfiguration(serviceConfiguration)) {\n throw new CodedError(\n 'ERR_APP_AUTH_INVALID_CONFIG',\n 'You must provide either an `issuer` or both `authorizationEndpoint` and `tokenEndpoint`'\n );\n }\n if (typeof redirectUrl !== 'string') {\n throw new CodedError('ERR_APP_AUTH_INVALID_CONFIG', '`redirectUrl` must be a string');\n }\n assertValidClientId(clientId);\n}\n\nasync function _executeAsync(props: OAuthProps): Promise<TokenResponse> {\n if (!props.redirectUrl) {\n props.redirectUrl = getDefaultOAuthRedirect();\n }\n assertValidProps(props);\n return await ExpoAppAuth.executeAsync(props);\n}\n\nexport function getDefaultOAuthRedirect(): string {\n return `${ExpoAppAuth.OAuthRedirect}:/oauthredirect`;\n}\n\nexport async function authAsync(props: OAuthProps): Promise<TokenResponse> {\n if (!ExpoAppAuth.executeAsync) {\n throw new UnavailabilityError('expo-app-auth', 'authAsync');\n }\n return await _executeAsync(props);\n}\n\nexport async function refreshAsync(\n props: OAuthProps,\n refreshToken: string\n): Promise<TokenResponse> {\n if (!ExpoAppAuth.executeAsync) {\n throw new UnavailabilityError('expo-app-auth', 'refreshAsync');\n }\n if (!refreshToken) {\n throw new CodedError('ERR_APP_AUTH_TOKEN', 'Cannot refresh with null `refreshToken`');\n }\n return await _executeAsync({\n isRefresh: true,\n refreshToken,\n ...props,\n });\n}\n\n/* JS Method */\nexport async function revokeAsync(\n { clientId, issuer, serviceConfiguration }: OAuthBaseProps,\n { token, isClientIdProvided = false }: OAuthRevokeOptions\n): Promise<any> {\n if (!token) {\n throw new CodedError('ERR_APP_AUTH_TOKEN', 'Cannot revoke a null `token`');\n }\n\n assertValidClientId(clientId);\n\n let revocationEndpoint;\n if (serviceConfiguration && serviceConfiguration.revocationEndpoint) {\n revocationEndpoint = serviceConfiguration.revocationEndpoint;\n } else {\n // For Open IDC providers only.\n const response = await fetch(`${issuer}/.well-known/openid-configuration`);\n const openidConfig = await response.json();\n\n invariant(\n openidConfig.revocation_endpoint,\n 'The OpenID config does not specify a revocation endpoint'\n );\n\n revocationEndpoint = openidConfig.revocation_endpoint;\n }\n\n const encodedClientID = encodeURIComponent(clientId);\n const encodedToken = encodeURIComponent(token);\n const body = `token=${encodedToken}${isClientIdProvided ? `&client_id=${encodedClientID}` : ''}`;\n const headers = { 'Content-Type': 'application/x-www-form-urlencoded' };\n try {\n // https://tools.ietf.org/html/rfc7009#section-2.2\n const results = await fetch(revocationEndpoint, {\n method: 'POST',\n headers,\n body,\n });\n\n return results;\n } catch (error) {\n throw new CodedError('ERR_APP_AUTH_REVOKE_FAILED', error.message);\n }\n}\n\n// NOTE: This function is unused; delete it if we don't need it\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nasync function parseAuthRevocationResults(results: Response): Promise<any> {\n const data = await results.json();\n const token = results.headers['update-client-auth'];\n // the token has been revoked successfully or the client submitted an invalid token.\n if (results.ok) {\n // successful op\n return { type: 'success', status: results.status, data, token };\n } else if (results.status === 503 && results.headers['retry-after']) {\n // Failed op\n const retryAfterValue = results.headers['retry-after'];\n let retryAfter: number | undefined;\n if (retryAfterValue) {\n retryAfter = parseRetryTime(retryAfterValue);\n }\n // the client must assume the token still exists and may retry after a reasonable delay.\n return { type: 'failed', status: results.status, data, token, retryAfter };\n } else {\n // Error\n return { type: 'error', status: results.status, data, token };\n }\n}\n\nfunction parseRetryTime(value: string): number {\n // In accordance with RFC2616, Section 14.37. Timout may be of format seconds or future date time value\n if (/^\\d+$/.test(value)) {\n return parseInt(value, 10) * 1000;\n }\n const retry = Date.parse(value);\n if (isNaN(retry)) {\n throw new CodedError(\n 'ERR_APP_AUTH_FETCH_RETRY_TIME',\n 'Cannot parse the Retry-After header value returned by the server: ' + value\n );\n }\n const now = Date.now();\n const parsedDate = new Date(retry);\n return parsedDate.getTime() - now;\n}\n\nexport const { OAuthRedirect, URLSchemes } = ExpoAppAuth;\n"]}
\No newline at end of file