@iad-os/react-ghost-auth
Advanced tools
Comparing version 0.1.10 to 0.1.11
@@ -0,5 +1,5 @@ | ||
import { AxiosStatic } from 'axios'; | ||
import React from 'react'; | ||
import { AxiosStatic } from 'axios'; | ||
export declare type AuthenticationOptions = { | ||
serviceUrl: string; | ||
serviceUrl?: string; | ||
authorization_endpoint: string; | ||
@@ -10,18 +10,23 @@ token_endpoint: string; | ||
redirect_uri: string; | ||
redirect_logout_uri?: string; | ||
end_session_endpoint: string; | ||
realm: string; | ||
}; | ||
export default function AuthenticationProvider(_props: { | ||
axios: AxiosStatic; | ||
options: AuthenticationOptions; | ||
children: React.ReactNode; | ||
}): JSX.Element; | ||
export declare function useAuthentication(): { | ||
declare type EStatus = 'INIT' | 'LOGIN' | 'LOGGING' | 'LOGGED'; | ||
declare type AuthCtxType = { | ||
login: () => void; | ||
logout: () => void; | ||
isAuthenticated: () => boolean; | ||
status: "LOGIN" | "LOGGING" | "LOGGED"; | ||
status: EStatus; | ||
userInfo: () => { | ||
[key: string]: any; | ||
} | undefined; | ||
updateStatus: (status: EStatus) => void; | ||
}; | ||
export default function AuthenticationProvider(_props: { | ||
axios: AxiosStatic; | ||
options: AuthenticationOptions; | ||
children: React.ReactNode; | ||
}): JSX.Element; | ||
export declare function useAuthentication(): AuthCtxType; | ||
export {}; |
@@ -5,5 +5,6 @@ import AuthenticationProvider, { useAuthentication } from './auth/Authentication'; | ||
import LoggedIn from './auth/components/LoggedIn'; | ||
import RequireAuth from './auth/components/RequireAuth'; | ||
export default AuthenticationProvider; | ||
export type { AuthenticationOptions } from './auth/Authentication'; | ||
export { useAuthentication }; | ||
export { LogginIn, AutoLogin, LoggedIn }; | ||
export { LogginIn, AutoLogin, LoggedIn, RequireAuth }; |
@@ -1,3 +0,3 @@ | ||
import { jsx, Fragment } from 'react/jsx-runtime'; | ||
import React, { useState, useEffect, useContext } from 'react'; | ||
import { jsx, Fragment, jsxs } from 'react/jsx-runtime'; | ||
import React, { useState, useEffect, useCallback, useContext } from 'react'; | ||
import { randomBytes, createHash } from 'crypto'; | ||
@@ -30,33 +30,2 @@ | ||
// PKCE HELPER FUNCTIONS | ||
// Generate a secure random string using the browser crypto functions | ||
function generateRandomBytes() { | ||
return randomBytes(64); | ||
} | ||
// Calculate the SHA256 hash of the input text. | ||
// Returns a promise that resolves to an ArrayBuffer | ||
function sha256(buffer) { | ||
return createHash('sha256').update(buffer).digest(); | ||
} | ||
function generateCodeVerifier() { | ||
return base64urlencode(generateRandomBytes()); | ||
} | ||
function generateRandomState() { | ||
return base64urlencode(generateRandomBytes()); | ||
} | ||
// Return the base64-urlencoded sha256 hash for the PKCE challenge | ||
function pkceChallengeFromVerifier(verify) { | ||
return base64urlencode(sha256(verify)); | ||
} | ||
function base64urlencode(buffer) { | ||
return window | ||
.btoa(String.fromCharCode(...buffer)) | ||
.replace(/\+/g, '-') | ||
.replace(/\//g, '_') | ||
.replace(/=/g, ''); | ||
} | ||
function base64decode(str) { | ||
return JSON.parse(window.atob(str)); | ||
} | ||
function createCommonjsModule(fn) { | ||
@@ -735,10 +704,40 @@ var module = { exports: {} }; | ||
// PKCE HELPER FUNCTIONS | ||
// Generate a secure random string using the browser crypto functions | ||
function generateRandomBytes() { | ||
return randomBytes(64); | ||
} | ||
// Calculate the SHA256 hash of the input text. | ||
// Returns a promise that resolves to an ArrayBuffer | ||
function sha256(buffer) { | ||
return createHash('sha256').update(buffer).digest(); | ||
} | ||
function generateCodeVerifier() { | ||
return base64urlencode(generateRandomBytes()); | ||
} | ||
function generateRandomState() { | ||
return base64urlencode(generateRandomBytes()); | ||
} | ||
// Return the base64-urlencoded sha256 hash for the PKCE challenge | ||
function pkceChallengeFromVerifier(verify) { | ||
return base64urlencode(sha256(verify)); | ||
} | ||
function base64urlencode(buffer) { | ||
return window | ||
.btoa(String.fromCharCode(...buffer)) | ||
.replace(/\+/g, '-') | ||
.replace(/\//g, '_') | ||
.replace(/=/g, ''); | ||
} | ||
function base64decode(str) { | ||
return JSON.parse(window.atob(str)); | ||
} | ||
const AutenticationContext = React.createContext({}); | ||
function AuthenticationProvider(_props) { | ||
const redirect_uri = `${window.location.protocol}//${window.location.hostname}${window.location.port !== '' ? `:${window.location.port}` : ''}`; | ||
const { axios } = _props; | ||
const { client_id, authorization_endpoint, requested_scopes, token_endpoint, end_session_endpoint, realm, serviceUrl, } = _props.options; | ||
const [status, setStatus] = useState(!!getState() ? 'LOGGING' : 'LOGIN'); | ||
const { client_id, authorization_endpoint, requested_scopes, token_endpoint, end_session_endpoint, realm, serviceUrl, redirect_uri, redirect_logout_uri, } = _props.options; | ||
const [status, setStatus] = useState(!!getState() ? 'LOGGING' : 'INIT'); | ||
useEffect(() => { | ||
interceptor(axios, serviceUrl, refreshToken); | ||
interceptor(axios, serviceUrl !== null && serviceUrl !== void 0 ? serviceUrl : '', refreshToken); | ||
const code = queryString.parse(window.location.search).code; | ||
@@ -777,3 +776,3 @@ const stateLocalStorage = getState(); | ||
else { | ||
setStatus('LOGIN'); | ||
setStatus('INIT'); | ||
} | ||
@@ -810,3 +809,3 @@ }, []); | ||
client_id, | ||
redirect_uri, | ||
redirect_uri: redirect_uri !== null && redirect_uri !== void 0 ? redirect_uri : window.location.href, | ||
requested_scopes, | ||
@@ -820,3 +819,3 @@ code_challenge: pkceChallengeFromVerifier(new_code_verifier), | ||
clear(); | ||
window.location.href = `${end_session_endpoint}?post_logout_redirect_uri=${redirect_uri}`; | ||
window.location.href = `${end_session_endpoint}?post_logout_redirect_uri=${redirect_logout_uri !== null && redirect_logout_uri !== void 0 ? redirect_logout_uri : redirect_uri}`; | ||
}; | ||
@@ -834,2 +833,3 @@ const isAuthenticated = () => { | ||
}; | ||
const updateStatus = useCallback((status) => setStatus(status), [status]); | ||
return (jsx(AutenticationContext.Provider, Object.assign({ value: { | ||
@@ -841,2 +841,3 @@ login, | ||
userInfo, | ||
updateStatus, | ||
} }, { children: _props.children }), void 0)); | ||
@@ -857,10 +858,3 @@ } | ||
function useAuthentication() { | ||
const { login, logout, isAuthenticated, status, userInfo } = useContext(AutenticationContext); | ||
return { | ||
login, | ||
logout, | ||
isAuthenticated, | ||
status, | ||
userInfo, | ||
}; | ||
return useContext(AutenticationContext); | ||
} | ||
@@ -890,3 +884,23 @@ | ||
export { AutoLogin, LoggedIn, LogginIn, AuthenticationProvider as default, useAuthentication }; | ||
function RequireAuth(props) { | ||
const { authRequired: _authRequired = true, children } = props; | ||
const { status, updateStatus, isAuthenticated } = useAuthentication(); | ||
const [authRequired, setAuthRequired] = useState(false); | ||
useEffect(() => { | ||
if (typeof _authRequired === 'function') { | ||
_authRequired().then(res => setAuthRequired(res)); | ||
} | ||
else { | ||
setAuthRequired(_authRequired); | ||
} | ||
}, []); | ||
useEffect(() => { | ||
if (status === 'INIT' && authRequired) { | ||
updateStatus('LOGIN'); | ||
} | ||
}, [authRequired]); | ||
return (jsxs(Fragment, { children: [authRequired && jsx(LoggedIn, { children: children }, void 0), !authRequired && jsx(Fragment, { children: children }, void 0)] }, void 0)); | ||
} | ||
export { AutoLogin, LoggedIn, LogginIn, RequireAuth, AuthenticationProvider as default, useAuthentication }; | ||
//# sourceMappingURL=index.es.js.map |
@@ -38,33 +38,2 @@ 'use strict'; | ||
// PKCE HELPER FUNCTIONS | ||
// Generate a secure random string using the browser crypto functions | ||
function generateRandomBytes() { | ||
return crypto.randomBytes(64); | ||
} | ||
// Calculate the SHA256 hash of the input text. | ||
// Returns a promise that resolves to an ArrayBuffer | ||
function sha256(buffer) { | ||
return crypto.createHash('sha256').update(buffer).digest(); | ||
} | ||
function generateCodeVerifier() { | ||
return base64urlencode(generateRandomBytes()); | ||
} | ||
function generateRandomState() { | ||
return base64urlencode(generateRandomBytes()); | ||
} | ||
// Return the base64-urlencoded sha256 hash for the PKCE challenge | ||
function pkceChallengeFromVerifier(verify) { | ||
return base64urlencode(sha256(verify)); | ||
} | ||
function base64urlencode(buffer) { | ||
return window | ||
.btoa(String.fromCharCode(...buffer)) | ||
.replace(/\+/g, '-') | ||
.replace(/\//g, '_') | ||
.replace(/=/g, ''); | ||
} | ||
function base64decode(str) { | ||
return JSON.parse(window.atob(str)); | ||
} | ||
function createCommonjsModule(fn) { | ||
@@ -743,10 +712,40 @@ var module = { exports: {} }; | ||
// PKCE HELPER FUNCTIONS | ||
// Generate a secure random string using the browser crypto functions | ||
function generateRandomBytes() { | ||
return crypto.randomBytes(64); | ||
} | ||
// Calculate the SHA256 hash of the input text. | ||
// Returns a promise that resolves to an ArrayBuffer | ||
function sha256(buffer) { | ||
return crypto.createHash('sha256').update(buffer).digest(); | ||
} | ||
function generateCodeVerifier() { | ||
return base64urlencode(generateRandomBytes()); | ||
} | ||
function generateRandomState() { | ||
return base64urlencode(generateRandomBytes()); | ||
} | ||
// Return the base64-urlencoded sha256 hash for the PKCE challenge | ||
function pkceChallengeFromVerifier(verify) { | ||
return base64urlencode(sha256(verify)); | ||
} | ||
function base64urlencode(buffer) { | ||
return window | ||
.btoa(String.fromCharCode(...buffer)) | ||
.replace(/\+/g, '-') | ||
.replace(/\//g, '_') | ||
.replace(/=/g, ''); | ||
} | ||
function base64decode(str) { | ||
return JSON.parse(window.atob(str)); | ||
} | ||
const AutenticationContext = React__default["default"].createContext({}); | ||
function AuthenticationProvider(_props) { | ||
const redirect_uri = `${window.location.protocol}//${window.location.hostname}${window.location.port !== '' ? `:${window.location.port}` : ''}`; | ||
const { axios } = _props; | ||
const { client_id, authorization_endpoint, requested_scopes, token_endpoint, end_session_endpoint, realm, serviceUrl, } = _props.options; | ||
const [status, setStatus] = React.useState(!!getState() ? 'LOGGING' : 'LOGIN'); | ||
const { client_id, authorization_endpoint, requested_scopes, token_endpoint, end_session_endpoint, realm, serviceUrl, redirect_uri, redirect_logout_uri, } = _props.options; | ||
const [status, setStatus] = React.useState(!!getState() ? 'LOGGING' : 'INIT'); | ||
React.useEffect(() => { | ||
interceptor(axios, serviceUrl, refreshToken); | ||
interceptor(axios, serviceUrl !== null && serviceUrl !== void 0 ? serviceUrl : '', refreshToken); | ||
const code = queryString.parse(window.location.search).code; | ||
@@ -785,3 +784,3 @@ const stateLocalStorage = getState(); | ||
else { | ||
setStatus('LOGIN'); | ||
setStatus('INIT'); | ||
} | ||
@@ -818,3 +817,3 @@ }, []); | ||
client_id, | ||
redirect_uri, | ||
redirect_uri: redirect_uri !== null && redirect_uri !== void 0 ? redirect_uri : window.location.href, | ||
requested_scopes, | ||
@@ -828,3 +827,3 @@ code_challenge: pkceChallengeFromVerifier(new_code_verifier), | ||
clear(); | ||
window.location.href = `${end_session_endpoint}?post_logout_redirect_uri=${redirect_uri}`; | ||
window.location.href = `${end_session_endpoint}?post_logout_redirect_uri=${redirect_logout_uri !== null && redirect_logout_uri !== void 0 ? redirect_logout_uri : redirect_uri}`; | ||
}; | ||
@@ -842,2 +841,3 @@ const isAuthenticated = () => { | ||
}; | ||
const updateStatus = React.useCallback((status) => setStatus(status), [status]); | ||
return (jsxRuntime.jsx(AutenticationContext.Provider, Object.assign({ value: { | ||
@@ -849,2 +849,3 @@ login, | ||
userInfo, | ||
updateStatus, | ||
} }, { children: _props.children }), void 0)); | ||
@@ -865,10 +866,3 @@ } | ||
function useAuthentication() { | ||
const { login, logout, isAuthenticated, status, userInfo } = React.useContext(AutenticationContext); | ||
return { | ||
login, | ||
logout, | ||
isAuthenticated, | ||
status, | ||
userInfo, | ||
}; | ||
return React.useContext(AutenticationContext); | ||
} | ||
@@ -898,7 +892,28 @@ | ||
function RequireAuth(props) { | ||
const { authRequired: _authRequired = true, children } = props; | ||
const { status, updateStatus, isAuthenticated } = useAuthentication(); | ||
const [authRequired, setAuthRequired] = React.useState(false); | ||
React.useEffect(() => { | ||
if (typeof _authRequired === 'function') { | ||
_authRequired().then(res => setAuthRequired(res)); | ||
} | ||
else { | ||
setAuthRequired(_authRequired); | ||
} | ||
}, []); | ||
React.useEffect(() => { | ||
if (status === 'INIT' && authRequired) { | ||
updateStatus('LOGIN'); | ||
} | ||
}, [authRequired]); | ||
return (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [authRequired && jsxRuntime.jsx(LoggedIn, { children: children }, void 0), !authRequired && jsxRuntime.jsx(jsxRuntime.Fragment, { children: children }, void 0)] }, void 0)); | ||
} | ||
exports.AutoLogin = AutoLogin; | ||
exports.LoggedIn = LoggedIn; | ||
exports.LogginIn = LogginIn; | ||
exports.RequireAuth = RequireAuth; | ||
exports["default"] = AuthenticationProvider; | ||
exports.useAuthentication = useAuthentication; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@iad-os/react-ghost-auth", | ||
"version": "0.1.10", | ||
"version": "0.1.11", | ||
"maintainers": [ | ||
@@ -5,0 +5,0 @@ { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
152291
18
1623
0