react-oauth2-code-pkce
Advanced tools
Comparing version 1.0.3 to 1.1.0
"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,17 +31,28 @@ 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('refreshToken', null), refreshToken = _b[0], setRefreshToken = _b[1]; | ||
var _c = Hooks_1.default('token', null), token = _c[0], setToken = _c[1]; | ||
var _d = Hooks_1.default('loginInProgress', false), loginInProgress = _d[0], setLoginInProgress = _d[1]; | ||
var _e = react_1.useState(), tokenData = _e[0], setTokenData = _e[1]; | ||
validateAuthConfig_1.validateAuthConfig(authConfig); | ||
var logOut = function () { | ||
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]; | ||
(0, validateAuthConfig_1.validateAuthConfig)(authConfig); | ||
function logOut() { | ||
setRefreshToken(null); | ||
setToken(null); | ||
setIdToken(null); | ||
setTokenData(undefined); | ||
authentication_1.logout(authConfig); | ||
}; | ||
react_1.useEffect(function () { | ||
// @ts-ignore | ||
setLoginInProgress(false); | ||
} | ||
function handleTokenResponse(response) { | ||
setRefreshToken(response.refresh_token); | ||
setToken(response.access_token); | ||
setIdToken(response === null || response === void 0 ? void 0 : response.id_token); | ||
// @ts-ignore | ||
setLoginInProgress(false); | ||
setTokenData((0, authentication_1.decodeToken)(response.access_token)); | ||
} | ||
(0, react_1.useEffect)(function () { | ||
if (loginInProgress) { // The client has been redirected back from the Auth endpoint with an auth code | ||
@@ -39,16 +63,8 @@ var urlParams = new URLSearchParams(window.location.search); | ||
// Clearing all site data... | ||
setRefreshToken(null); | ||
setToken(null); | ||
setTokenData(undefined); | ||
// @ts-ignore | ||
setLoginInProgress(false); | ||
logOut(); | ||
location.reload(); | ||
} | ||
else { // Request token from auth server with the auth code | ||
authentication_1.getTokens(authConfig).then(function (response) { | ||
setRefreshToken(response.refresh_token); | ||
setToken(response.access_token); | ||
// @ts-ignore | ||
setLoginInProgress(false); | ||
setTokenData(authentication_1.decodeToken(response.access_token)); | ||
(0, authentication_1.getTokens)(authConfig).then(function (response) { | ||
handleTokenResponse(response); | ||
history.replaceState(null, "", location.pathname); // Clear ugly url params | ||
@@ -61,14 +77,10 @@ }); | ||
setLoginInProgress(true); | ||
authentication_1.login(authConfig); | ||
(0, authentication_1.login)(authConfig); | ||
} | ||
else if (refreshToken) { // A refresh token is stored in client | ||
if (authentication_1.tokenExpired(token)) { // The client has an expired token. Will try to get a new one with the refreshToken | ||
authentication_1.getAccessTokenFromRefreshToken({ 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.getAccessTokenFromRefreshToken)({ authConfig: authConfig, refreshToken: refreshToken }) | ||
.then(function (_a) { | ||
var response = _a.response; | ||
setRefreshToken(response.refresh_token); | ||
setToken(response.access_token); | ||
setTokenData(authentication_1.decodeToken(response.access_token)); | ||
// @ts-ignore | ||
setLoginInProgress(false); | ||
handleTokenResponse(response); | ||
}) | ||
@@ -79,11 +91,12 @@ .catch(function (error) { | ||
setLoginInProgress(true); | ||
authentication_1.login(authConfig); | ||
(0, authentication_1.login)(authConfig); | ||
}); | ||
} | ||
else { | ||
setTokenData(authentication_1.decodeToken(token)); | ||
else { // The client still has a valid token | ||
setTokenData((0, authentication_1.decodeToken)(token)); | ||
} | ||
} | ||
}, []); | ||
return (react_1.default.createElement(exports.AuthContext.Provider, { value: { tokenData: tokenData, token: token, logOut: logOut } }, children)); | ||
return (react_1.default.createElement(exports.AuthContext.Provider, { value: { tokenData: tokenData, token: token, idToken: idToken, logOut: logOut } }, children)); | ||
}; | ||
exports.AuthProvider = AuthProvider; |
@@ -1,5 +0,4 @@ | ||
import { AuthConfig, TTokenData } from "./Types"; | ||
export declare function logout(authConfig: AuthConfig): void; | ||
export declare function login(authConfig: AuthConfig): Promise<void>; | ||
export declare const getTokens: (authConfig: AuthConfig) => Promise<any>; | ||
import { TAuthConfig, TTokenData } from "./Types"; | ||
export declare function login(authConfig: TAuthConfig): Promise<void>; | ||
export declare const getTokens: (authConfig: TAuthConfig) => Promise<any>; | ||
export declare const getAccessTokenFromRefreshToken: ({ authConfig, refreshToken }: any) => Promise<any>; | ||
@@ -6,0 +5,0 @@ /** |
@@ -39,8 +39,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.tokenExpired = exports.decodeToken = exports.getAccessTokenFromRefreshToken = exports.getTokens = exports.login = void 0; | ||
var pkceUtils_1 = require("./pkceUtils"); | ||
var codeVerifierStorageKey = "PKCE_code_verifier"; | ||
function logout(authConfig) { | ||
location.href = authConfig.logoutEndpoint + "?post_logout_redirect_uri=" + (authConfig.logoutRedirect || authConfig.redirectUri); | ||
} | ||
exports.logout = logout; | ||
function login(authConfig) { | ||
@@ -50,6 +47,6 @@ return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
codeVerifier = pkceUtils_1.generateRandomString(20); | ||
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 | ||
@@ -71,3 +68,3 @@ var params = new URLSearchParams({ | ||
exports.login = login; | ||
exports.getTokens = function (authConfig) { | ||
var getTokens = function (authConfig) { | ||
/* | ||
@@ -96,7 +93,8 @@ The browser has been redirected from the authentication endpoint with | ||
method: 'POST', | ||
body: formData | ||
body: formData, | ||
}) | ||
.then(function (response) { return response.json(); }); | ||
}; | ||
exports.getAccessTokenFromRefreshToken = function (_a) { | ||
exports.getTokens = getTokens; | ||
var getAccessTokenFromRefreshToken = function (_a) { | ||
var authConfig = _a.authConfig, refreshToken = _a.refreshToken; | ||
@@ -116,6 +114,7 @@ var params = new URLSearchParams({ | ||
}; | ||
exports.getAccessTokenFromRefreshToken = getAccessTokenFromRefreshToken; | ||
/** | ||
* Decodes the the base64 encoded JWT. Returns a TToken. | ||
*/ | ||
exports.decodeToken = function (token) { | ||
var decodeToken = function (token) { | ||
var base64Url = token.split('.')[1]; | ||
@@ -131,2 +130,3 @@ var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/'); | ||
}; | ||
exports.decodeToken = decodeToken; | ||
/** | ||
@@ -136,7 +136,8 @@ * Check if the Access Token has expired by looking at the 'exp' JWT header. | ||
*/ | ||
exports.tokenExpired = function (token) { | ||
var tokenExpired = function (token) { | ||
var bufferTimeInMilliseconds = 30 * 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.tokenExpired = tokenExpired; |
@@ -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); |
export { AuthProvider, AuthContext } from "./AuthContext"; | ||
export type { TAuthConfig } from "./Types"; |
"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 |
@@ -10,3 +10,3 @@ import { ReactChildren } from "react"; | ||
} | ||
export declare type AuthConfig = { | ||
export declare type TAuthConfig = { | ||
clientId: string; | ||
@@ -13,0 +13,0 @@ authorizationEndpoint: string; |
@@ -1,2 +0,2 @@ | ||
import { AuthConfig } from "./Types"; | ||
export declare function validateAuthConfig(authConfig: AuthConfig): void; | ||
import { TAuthConfig } from "./Types"; | ||
export declare function validateAuthConfig(authConfig: TAuthConfig): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validateAuthConfig = void 0; | ||
function validateAuthConfig(authConfig) { | ||
@@ -4,0 +5,0 @@ if (authConfig.clientId === "" || null || undefined) |
{ | ||
"name": "react-oauth2-code-pkce", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"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
23397
419