Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-oauth2-code-pkce

Package Overview
Dependencies
Maintainers
1
Versions
79
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-oauth2-code-pkce - npm Package Compare versions

Comparing version 1.2.1 to 1.2.2-a

56

dist/AuthContext.js
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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 +25,3 @@ };

Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthProvider = exports.AuthContext = void 0;
var react_1 = __importStar(require("react"));

@@ -18,13 +31,13 @@ var authentication_1 = require("./authentication");

var validateAuthConfig_1 = require("./validateAuthConfig");
exports.AuthContext = react_1.createContext({});
exports.AuthProvider = function (_a) {
exports.AuthContext = (0, react_1.createContext)({});
var AuthProvider = function (_a) {
var authConfig = _a.authConfig, children = _a.children;
var _b = Hooks_1.default('ROCP_refreshToken', null), refreshToken = _b[0], setRefreshToken = _b[1];
var _c = Hooks_1.default('ROCP_token', null), token = _c[0], setToken = _c[1];
var _d = Hooks_1.default('ROCP_idToken', null), idToken = _d[0], setIdToken = _d[1];
var _e = Hooks_1.default('ROCP_loginInProgress', false), loginInProgress = _e[0], setLoginInProgress = _e[1];
var _f = react_1.useState(), tokenData = _f[0], setTokenData = _f[1];
var _g = react_1.useState(null), error = _g[0], setError = _g[1];
var _b = (0, Hooks_1.default)('ROCP_refreshToken', null), refreshToken = _b[0], setRefreshToken = _b[1];
var _c = (0, Hooks_1.default)('ROCP_token', null), token = _c[0], setToken = _c[1];
var _d = (0, Hooks_1.default)('ROCP_idToken', null), idToken = _d[0], setIdToken = _d[1];
var _e = (0, Hooks_1.default)('ROCP_loginInProgress', false), loginInProgress = _e[0], setLoginInProgress = _e[1];
var _f = (0, react_1.useState)(), tokenData = _f[0], setTokenData = _f[1];
var _g = (0, react_1.useState)(null), error = _g[0], setError = _g[1];
var interval;
validateAuthConfig_1.validateAuthConfig(authConfig);
(0, validateAuthConfig_1.validateAuthConfig)(authConfig);
function logOut() {

@@ -42,8 +55,8 @@ setRefreshToken(null);

setLoginInProgress(false);
setTokenData(authentication_1.decodeToken(response.access_token));
setTokenData((0, authentication_1.decodeToken)(response.access_token));
}
function refreshAccessToken() {
if (refreshToken) {
if (authentication_1.tokenExpired(token)) { // The client has an expired token. Will try to get a new one with the refreshToken
authentication_1.fetchWithRefreshToken({ authConfig: authConfig, refreshToken: refreshToken })
if ((0, authentication_1.tokenExpired)(token)) { // The client has an expired token. Will try to get a new one with the refreshToken
(0, authentication_1.fetchWithRefreshToken)({ authConfig: authConfig, refreshToken: refreshToken })
.then(function (result) { return handleTokenResponse(result); })

@@ -53,5 +66,5 @@ .catch(function (error) {

setError(error);
if (authentication_1.errorMessageForExpiredRefreshToken(error)) {
if ((0, authentication_1.errorMessageForExpiredRefreshToken)(error)) {
logOut();
authentication_1.login(authConfig);
(0, authentication_1.login)(authConfig);
}

@@ -61,3 +74,3 @@ });

else { // The client still has a valid token
setTokenData(authentication_1.decodeToken(token));
setTokenData((0, authentication_1.decodeToken)(token));
}

@@ -71,3 +84,3 @@ }

// Register the 'check for soon expiring access token' interval (Every minute)
react_1.useEffect(function () {
(0, react_1.useEffect)(function () {
interval = setInterval(function () { return refreshAccessToken(); }, 60000);

@@ -77,3 +90,3 @@ return function () { return clearInterval(interval); };

// Runs once on page load
react_1.useEffect(function () {
(0, react_1.useEffect)(function () {
if (loginInProgress) { // The client has been redirected back from the Auth endpoint with an auth code

@@ -89,3 +102,3 @@ var urlParams = new URLSearchParams(window.location.search);

else { // Request token from auth server with the auth code
authentication_1.fetchTokens(authConfig)
(0, authentication_1.fetchTokens)(authConfig)
.then(function (tokens) {

@@ -106,3 +119,3 @@ handleTokenResponse(tokens);

setLoginInProgress(true);
authentication_1.login(authConfig);
(0, authentication_1.login)(authConfig);
}

@@ -115,1 +128,2 @@ else {

};
exports.AuthProvider = AuthProvider;

@@ -39,2 +39,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.errorMessageForExpiredRefreshToken = exports.tokenExpired = exports.decodeToken = exports.fetchWithRefreshToken = exports.fetchTokens = exports.login = exports.EXPIRED_REFRESH_TOKEN_ERROR_CODES = void 0;
var pkceUtils_1 = require("./pkceUtils");

@@ -48,6 +49,6 @@ var codeVerifierStorageKey = "PKCE_code_verifier";

return __generator(this, function (_a) {
codeVerifier = pkceUtils_1.generateRandomString(40);
codeVerifier = (0, pkceUtils_1.generateRandomString)(40);
localStorage.setItem(codeVerifierStorageKey, codeVerifier);
// Hash and Base64URL encode the code_verifier, used as the 'code_challenge'
pkceUtils_1.generateCodeChallenge(codeVerifier).then(function (codeChallenge) {
(0, pkceUtils_1.generateCodeChallenge)(codeVerifier).then(function (codeChallenge) {
// Set query parameters and redirect user to OAuth2 authentication endpoint

@@ -65,3 +66,3 @@ var params = new URLSearchParams({

authConfig.preLogin();
location.replace(authConfig.authorizationEndpoint + "?" + params.toString());
location.replace("".concat(authConfig.authorizationEndpoint, "?").concat(params.toString()));
});

@@ -73,3 +74,3 @@ return [2 /*return*/];

exports.login = login;
exports.fetchTokens = function (authConfig) {
var fetchTokens = function (authConfig) {
/*

@@ -108,3 +109,4 @@ The browser has been redirected from the authentication endpoint with

};
exports.fetchWithRefreshToken = function (props) {
exports.fetchTokens = fetchTokens;
var fetchWithRefreshToken = function (props) {
var authConfig = props.authConfig, refreshToken = props.refreshToken;

@@ -129,6 +131,7 @@ var formData = new FormData();

};
exports.fetchWithRefreshToken = fetchWithRefreshToken;
/**
* Decodes the the base64 encoded JWT. Returns a TToken.
*/
exports.decodeToken = function (token) {
var decodeToken = function (token) {
var base64Url = token.split('.')[1];

@@ -144,2 +147,3 @@ var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');

};
exports.decodeToken = decodeToken;
/**

@@ -149,9 +153,10 @@ * Check if the Access Token has expired by looking at the 'exp' JWT header.

*/
exports.tokenExpired = function (token) {
var tokenExpired = function (token) {
var bufferTimeInMilliseconds = 10 * 60 * 1000; // minutes * seconds * toMilliseconds
var exp = exports.decodeToken(token).exp;
var exp = (0, exports.decodeToken)(token).exp;
var expirationTimeWithBuffer = new Date(exp * 1000 - bufferTimeInMilliseconds);
return new Date() > expirationTimeWithBuffer;
};
exports.errorMessageForExpiredRefreshToken = function (errorMessage) {
exports.tokenExpired = tokenExpired;
var errorMessageForExpiredRefreshToken = function (errorMessage) {
var expired = false;

@@ -165,1 +170,2 @@ exports.EXPIRED_REFRESH_TOKEN_ERROR_CODES.forEach(function (errorCode) {

};
exports.errorMessageForExpiredRefreshToken = errorMessageForExpiredRefreshToken;

@@ -5,3 +5,3 @@ "use strict";

function useLocalStorage(key, initialValue) {
var _a = react_1.useState(function () {
var _a = (0, react_1.useState)(function () {
try {

@@ -8,0 +8,0 @@ var item = localStorage.getItem(key);

"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; } });

@@ -39,2 +39,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.generateCodeChallenge = exports.generateRandomString = exports.getRandomInteger = void 0;
function getRandomInteger(range) {

@@ -41,0 +42,0 @@ var 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 @@ var unset = ["", undefined, null];

{
"name": "react-oauth2-code-pkce",
"version": "1.2.1",
"version": "1.2.2a",
"description": "Plug-and-play react package for OAuth2 Authorization Code flow with PKCE",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc