react-oauth2-code-pkce
Advanced tools
Comparing version 1.5.1 to 1.5.2
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
@@ -13,2 +29,3 @@ }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.AuthProvider = exports.AuthContext = void 0; | ||
const react_1 = __importStar(require("react")); // eslint-disable-line | ||
@@ -19,3 +36,3 @@ const authentication_1 = require("./authentication"); | ||
const FALLBACK_EXPIRE_TIME = 600; // 10minutes | ||
exports.AuthContext = react_1.createContext({ | ||
exports.AuthContext = (0, react_1.createContext)({ | ||
token: '', | ||
@@ -26,11 +43,11 @@ logOut: () => null, | ||
}); | ||
exports.AuthProvider = ({ authConfig, children }) => { | ||
const [refreshToken, setRefreshToken] = Hooks_1.default('ROCP_refreshToken', undefined); | ||
const [refreshTokenExpire, setRefreshTokenExpire] = Hooks_1.default('ROCP_refreshTokenExpire', authentication_1.epochAtSecondsFromNow(2 * FALLBACK_EXPIRE_TIME)); | ||
const [token, setToken] = Hooks_1.default('ROCP_token', ''); | ||
const [tokenExpire, setTokenExpire] = Hooks_1.default('ROCP_tokenExpire', authentication_1.epochAtSecondsFromNow(FALLBACK_EXPIRE_TIME)); | ||
const [idToken, setIdToken] = Hooks_1.default('ROCP_idToken', undefined); | ||
const [loginInProgress, setLoginInProgress] = Hooks_1.default('ROCP_loginInProgress', false); | ||
const [tokenData, setTokenData] = react_1.useState(); | ||
const [error, setError] = react_1.useState(null); | ||
const AuthProvider = ({ authConfig, children }) => { | ||
const [refreshToken, setRefreshToken] = (0, Hooks_1.default)('ROCP_refreshToken', undefined); | ||
const [refreshTokenExpire, setRefreshTokenExpire] = (0, Hooks_1.default)('ROCP_refreshTokenExpire', (0, authentication_1.epochAtSecondsFromNow)(2 * FALLBACK_EXPIRE_TIME)); | ||
const [token, setToken] = (0, Hooks_1.default)('ROCP_token', ''); | ||
const [tokenExpire, setTokenExpire] = (0, Hooks_1.default)('ROCP_tokenExpire', (0, authentication_1.epochAtSecondsFromNow)(FALLBACK_EXPIRE_TIME)); | ||
const [idToken, setIdToken] = (0, Hooks_1.default)('ROCP_idToken', undefined); | ||
const [loginInProgress, setLoginInProgress] = (0, Hooks_1.default)('ROCP_loginInProgress', false); | ||
const [tokenData, setTokenData] = (0, react_1.useState)(); | ||
const [error, setError] = (0, react_1.useState)(null); | ||
let interval; | ||
@@ -46,8 +63,8 @@ // Set default values and override from passed config | ||
}; | ||
validateAuthConfig_1.validateAuthConfig(config); | ||
(0, validateAuthConfig_1.validateAuthConfig)(config); | ||
function logOut() { | ||
setRefreshToken(undefined); | ||
setToken(''); | ||
setTokenExpire(authentication_1.epochAtSecondsFromNow(FALLBACK_EXPIRE_TIME)); | ||
setRefreshTokenExpire(authentication_1.epochAtSecondsFromNow(FALLBACK_EXPIRE_TIME)); | ||
setTokenExpire((0, authentication_1.epochAtSecondsFromNow)(FALLBACK_EXPIRE_TIME)); | ||
setRefreshTokenExpire((0, authentication_1.epochAtSecondsFromNow)(FALLBACK_EXPIRE_TIME)); | ||
setIdToken(undefined); | ||
@@ -60,3 +77,3 @@ setTokenData(undefined); | ||
setRefreshToken(response.refresh_token); | ||
setTokenExpire(authentication_1.epochAtSecondsFromNow(response.expires_in || FALLBACK_EXPIRE_TIME)); | ||
setTokenExpire((0, authentication_1.epochAtSecondsFromNow)(response.expires_in || FALLBACK_EXPIRE_TIME)); | ||
// If there is no refresh_token_expire, use access_token_expire + 10min. | ||
@@ -68,3 +85,3 @@ // If no access_token_expire, assume double the fallback expire time | ||
} | ||
setRefreshTokenExpire(authentication_1.epochAtSecondsFromNow(refreshTokenExpire)); | ||
setRefreshTokenExpire((0, authentication_1.epochAtSecondsFromNow)(refreshTokenExpire)); | ||
setIdToken(response.id_token); | ||
@@ -74,3 +91,3 @@ setLoginInProgress(false); | ||
if (config.decodeToken) | ||
setTokenData(authentication_1.decodeJWT(response.access_token)); | ||
setTokenData((0, authentication_1.decodeJWT)(response.access_token)); | ||
} | ||
@@ -82,11 +99,11 @@ catch (e) { | ||
function refreshAccessToken() { | ||
if (token && authentication_1.epochTimeIsPast(tokenExpire)) { | ||
if (refreshToken && !authentication_1.epochTimeIsPast(refreshTokenExpire)) { | ||
authentication_1.fetchWithRefreshToken({ config, refreshToken }) | ||
if (token && (0, authentication_1.epochTimeIsPast)(tokenExpire)) { | ||
if (refreshToken && !(0, authentication_1.epochTimeIsPast)(refreshTokenExpire)) { | ||
(0, authentication_1.fetchWithRefreshToken)({ config, refreshToken }) | ||
.then((result) => handleTokenResponse(result)) | ||
.catch((error) => { | ||
setError(error); | ||
if (authentication_1.errorMessageForExpiredRefreshToken(error)) { | ||
if ((0, authentication_1.errorMessageForExpiredRefreshToken)(error)) { | ||
logOut(); | ||
authentication_1.logIn(config); | ||
(0, authentication_1.logIn)(config); | ||
} | ||
@@ -98,3 +115,3 @@ }); | ||
setLoginInProgress(true); | ||
authentication_1.logIn(config); | ||
(0, authentication_1.logIn)(config); | ||
} | ||
@@ -104,3 +121,3 @@ } | ||
// Register the 'check for soon expiring access token' interval (Every minute) | ||
react_1.useEffect(() => { | ||
(0, react_1.useEffect)(() => { | ||
interval = setInterval(() => refreshAccessToken(), 60000); // eslint-disable-line | ||
@@ -110,3 +127,3 @@ return () => clearInterval(interval); | ||
// Runs once on page load | ||
react_1.useEffect(() => { | ||
(0, react_1.useEffect)(() => { | ||
if (loginInProgress) { | ||
@@ -124,3 +141,3 @@ // The client has been redirected back from the Auth endpoint with an auth code | ||
// Request token from auth server with the auth code | ||
authentication_1.fetchTokens(config) | ||
(0, authentication_1.fetchTokens)(config) | ||
.then((tokens) => { | ||
@@ -141,3 +158,3 @@ handleTokenResponse(tokens); | ||
setLoginInProgress(true); | ||
authentication_1.logIn(config); | ||
(0, authentication_1.logIn)(config); | ||
} | ||
@@ -147,3 +164,3 @@ else { | ||
try { | ||
setTokenData(authentication_1.decodeJWT(token)); | ||
setTokenData((0, authentication_1.decodeJWT)(token)); | ||
} | ||
@@ -159,1 +176,2 @@ catch (e) { | ||
}; | ||
exports.AuthProvider = AuthProvider; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.errorMessageForExpiredRefreshToken = exports.epochTimeIsPast = exports.epochAtSecondsFromNow = exports.decodeJWT = exports.fetchWithRefreshToken = exports.fetchTokens = exports.logIn = exports.EXPIRED_REFRESH_TOKEN_ERROR_CODES = void 0; | ||
const pkceUtils_1 = require("./pkceUtils"); | ||
@@ -9,6 +10,6 @@ const codeVerifierStorageKey = 'PKCE_code_verifier'; | ||
// Create and store a random string in localStorage, used as the 'code_verifier' | ||
const codeVerifier = pkceUtils_1.generateRandomString(96); | ||
const codeVerifier = (0, pkceUtils_1.generateRandomString)(96); | ||
localStorage.setItem(codeVerifierStorageKey, codeVerifier); | ||
// Hash and Base64URL encode the code_verifier, used as the 'code_challenge' | ||
pkceUtils_1.generateCodeChallenge(codeVerifier).then((codeChallenge) => { | ||
(0, pkceUtils_1.generateCodeChallenge)(codeVerifier).then((codeChallenge) => { | ||
// Set query parameters and redirect user to OAuth2 authentication endpoint | ||
@@ -62,3 +63,3 @@ const params = new URLSearchParams({ | ||
} | ||
exports.fetchTokens = (config) => { | ||
const fetchTokens = (config) => { | ||
/* | ||
@@ -89,3 +90,4 @@ The browser has been redirected from the authentication endpoint with | ||
}; | ||
exports.fetchWithRefreshToken = (props) => { | ||
exports.fetchTokens = fetchTokens; | ||
const fetchWithRefreshToken = (props) => { | ||
const { config, refreshToken } = props; | ||
@@ -101,6 +103,7 @@ const tokenRequest = { | ||
}; | ||
exports.fetchWithRefreshToken = fetchWithRefreshToken; | ||
/** | ||
* Decodes the base64 encoded JWT. Returns a TToken. | ||
*/ | ||
exports.decodeJWT = (token) => { | ||
const decodeJWT = (token) => { | ||
try { | ||
@@ -123,4 +126,6 @@ const base64Url = token.split('.')[1]; | ||
}; | ||
exports.decodeJWT = decodeJWT; | ||
// Returns epoch time (in seconds) for when the token will expire | ||
exports.epochAtSecondsFromNow = (secondsFromNow) => Math.round(Date.now() / 1000 + secondsFromNow); | ||
const epochAtSecondsFromNow = (secondsFromNow) => Math.round(Date.now() / 1000 + secondsFromNow); | ||
exports.epochAtSecondsFromNow = epochAtSecondsFromNow; | ||
/** | ||
@@ -136,3 +141,3 @@ * Check if the Access Token has expired. | ||
exports.epochTimeIsPast = epochTimeIsPast; | ||
exports.errorMessageForExpiredRefreshToken = (errorMessage) => { | ||
const errorMessageForExpiredRefreshToken = (errorMessage) => { | ||
let expired = false; | ||
@@ -146,1 +151,2 @@ exports.EXPIRED_REFRESH_TOKEN_ERROR_CODES.forEach((errorCode) => { | ||
}; | ||
exports.errorMessageForExpiredRefreshToken = errorMessageForExpiredRefreshToken; |
@@ -5,3 +5,3 @@ "use strict"; | ||
function useLocalStorage(key, initialValue) { | ||
const [storedValue, setStoredValue] = react_1.useState(() => { | ||
const [storedValue, setStoredValue] = (0, react_1.useState)(() => { | ||
const item = localStorage.getItem(key); | ||
@@ -8,0 +8,0 @@ try { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.AuthContext = exports.AuthProvider = void 0; | ||
var AuthContext_1 = require("./AuthContext"); | ||
exports.AuthProvider = AuthContext_1.AuthProvider; | ||
exports.AuthContext = AuthContext_1.AuthContext; | ||
Object.defineProperty(exports, "AuthProvider", { enumerable: true, get: function () { return AuthContext_1.AuthProvider; } }); | ||
Object.defineProperty(exports, "AuthContext", { enumerable: true, get: function () { return AuthContext_1.AuthContext; } }); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.generateCodeChallenge = exports.generateRandomString = exports.getRandomInteger = void 0; | ||
function getRandomInteger(range) { | ||
@@ -4,0 +5,0 @@ const max_range = 256; // Highest possible number in Uint8 |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validateAuthConfig = void 0; | ||
function stringIsUnset(value) { | ||
@@ -4,0 +5,0 @@ const unset = ['', undefined, null]; |
{ | ||
"name": "react-oauth2-code-pkce", | ||
"version": "1.5.1", | ||
"version": "1.5.2", | ||
"description": "Plug-and-play react package for OAuth2 Authorization Code flow with PKCE", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26836
504