@iad-os/react-ghost-auth
Advanced tools
Comparing version 0.1.9 to 0.1.10
@@ -23,2 +23,5 @@ import React from 'react'; | ||
status: "LOGIN" | "LOGGING" | "LOGGED"; | ||
userInfo: () => { | ||
[key: string]: any; | ||
} | undefined; | ||
}; |
@@ -5,2 +5,3 @@ import { TokenResponse } from './models/TokenResponse'; | ||
export declare function getRefreshToken(): string | null; | ||
export declare function getIdToken(): string | null; | ||
export declare function clearToken(): void; | ||
@@ -7,0 +8,0 @@ export declare function clear(): void; |
/// <reference types="node" /> | ||
export declare function generateRandomBytes(): Buffer; | ||
export declare function sha256(buffer: string): Buffer; | ||
export declare function generateCodeVerifier(): any; | ||
export declare function generateRandomState(): any; | ||
export declare function pkceChallengeFromVerifier(v: string): any; | ||
export declare function base64urlencode(str: any): any; | ||
export declare function generateCodeVerifier(): string; | ||
export declare function generateRandomState(): string; | ||
export declare function pkceChallengeFromVerifier(verify: string): string; | ||
export declare function base64urlencode(buffer: Buffer): string; | ||
export declare function base64decode(str: string): any; |
import AuthenticationProvider, { useAuthentication } from './auth/Authentication'; | ||
import Anonymous from './auth/components/Anonymous'; | ||
import LogginIn from './auth/components/LogginIn'; | ||
import AutoLogin from './auth/components/AutoLogin'; | ||
@@ -8,2 +8,2 @@ import LoggedIn from './auth/components/LoggedIn'; | ||
export { useAuthentication }; | ||
export { Anonymous, AutoLogin, LoggedIn }; | ||
export { LogginIn, AutoLogin, LoggedIn }; |
@@ -20,13 +20,2 @@ import { jsx, Fragment } from 'react/jsx-runtime'; | ||
var __assign = function() { | ||
__assign = Object.assign || function __assign(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
function __awaiter(thisArg, _arguments, P, generator) { | ||
@@ -42,30 +31,2 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
function __generator(thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
} | ||
// PKCE HELPER FUNCTIONS | ||
@@ -88,8 +49,8 @@ // Generate a secure random string using the browser crypto functions | ||
// Return the base64-urlencoded sha256 hash for the PKCE challenge | ||
function pkceChallengeFromVerifier(v) { | ||
return base64urlencode(sha256(v)); | ||
function pkceChallengeFromVerifier(verify) { | ||
return base64urlencode(sha256(verify)); | ||
} | ||
function base64urlencode(str) { | ||
return str | ||
.toString('base64') | ||
function base64urlencode(buffer) { | ||
return window | ||
.btoa(String.fromCharCode(...buffer)) | ||
.replace(/\+/g, '-') | ||
@@ -99,2 +60,5 @@ .replace(/\//g, '_') | ||
} | ||
function base64decode(str) { | ||
return JSON.parse(window.atob(str)); | ||
} | ||
@@ -689,10 +653,12 @@ function createCommonjsModule(fn) { | ||
var HOSTNAME = window.location.hostname; | ||
var ACCESS_TOKEN = HOSTNAME + "_access_token"; | ||
var REFRESH_TOKEN = HOSTNAME + "_refresh_token"; | ||
var STATE = HOSTNAME + "_state"; | ||
var CODE_VERIFIER = HOSTNAME + "_code_verifier"; | ||
const HOSTNAME = window.location.hostname; | ||
const ACCESS_TOKEN = `${HOSTNAME}_access_token`; | ||
const REFRESH_TOKEN = `${HOSTNAME}_refresh_token`; | ||
const ID_TOKEN = `${HOSTNAME}_id_token`; | ||
const STATE = `${HOSTNAME}_state`; | ||
const CODE_VERIFIER = `${HOSTNAME}_code_verifier`; | ||
function setToken(tokenObj) { | ||
localStorage.setItem(ACCESS_TOKEN, tokenObj.access_token); | ||
localStorage.setItem(REFRESH_TOKEN, tokenObj.refresh_token); | ||
localStorage.setItem(ID_TOKEN, tokenObj.id_token); | ||
} | ||
@@ -705,5 +671,9 @@ function getAccessToken() { | ||
} | ||
function getIdToken() { | ||
return localStorage.getItem(ID_TOKEN); | ||
} | ||
function clear() { | ||
localStorage.removeItem(ACCESS_TOKEN); | ||
localStorage.removeItem(REFRESH_TOKEN); | ||
localStorage.removeItem(ID_TOKEN); | ||
localStorage.removeItem(STATE); | ||
@@ -730,4 +700,4 @@ localStorage.removeItem(CODE_VERIFIER); | ||
function interceptor(axios, serviceUrl, refreshToken) { | ||
axios.interceptors.request.use(function (config) { | ||
var token = getAccessToken(); | ||
axios.interceptors.request.use(config => { | ||
const token = getAccessToken(); | ||
if (config && | ||
@@ -737,14 +707,14 @@ config.headers && | ||
matchHostname(serviceUrl || '', config.url || '')) { | ||
config.headers['Authorization'] = "Bearer " + token; | ||
config.headers = __assign({}, config.headers); | ||
config.headers['Authorization'] = `Bearer ${token}`; | ||
config.headers = Object.assign({}, config.headers); | ||
} | ||
return config; | ||
}, function (error) { | ||
}, error => { | ||
Promise.reject(error); | ||
}); | ||
axios.interceptors.response.use(function (response) { | ||
axios.interceptors.response.use(response => { | ||
return response; | ||
}, function (error) { | ||
}, error => { | ||
var _a, _b; | ||
var originalRequest = error.config; | ||
const originalRequest = error.config; | ||
if ((((_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.status) === 401 || ((_b = error === null || error === void 0 ? void 0 : error.response) === null || _b === void 0 ? void 0 : _b.status) === 403) && | ||
@@ -754,7 +724,7 @@ !originalRequest._retry) { | ||
return refreshToken() | ||
.then(function (res) { | ||
axios.defaults.headers.common['Authorization'] = "Bearer " + res.access_token; | ||
.then(res => { | ||
axios.defaults.headers.common['Authorization'] = `Bearer ${res.access_token}`; | ||
return axios(originalRequest); | ||
}) | ||
.catch(function (error) { return Promise.reject(error); }); | ||
.catch(error => Promise.reject(error)); | ||
} | ||
@@ -766,4 +736,4 @@ return Promise.reject(error); | ||
if (u1 && u2) { | ||
var h1 = new URL(u1).hostname; | ||
var h2 = new URL(u2).hostname; | ||
const h1 = new URL(u1).hostname; | ||
const h2 = new URL(u2).hostname; | ||
return h1 === h2; | ||
@@ -774,14 +744,13 @@ } | ||
var AutenticationContext = React.createContext({}); | ||
const AutenticationContext = React.createContext({}); | ||
function AuthenticationProvider(_props) { | ||
var _this = this; | ||
var redirect_uri = window.location.protocol + "//" + window.location.hostname + (window.location.port !== '' ? ":" + window.location.port : ''); | ||
var axios = _props.axios; | ||
var _a = _props.options, client_id = _a.client_id, authorization_endpoint = _a.authorization_endpoint, requested_scopes = _a.requested_scopes, token_endpoint = _a.token_endpoint, end_session_endpoint = _a.end_session_endpoint; _a.realm; var serviceUrl = _a.serviceUrl; | ||
var _b = useState(!!getState() ? 'LOGGING' : 'LOGIN'), status = _b[0], setStatus = _b[1]; | ||
useEffect(function () { | ||
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'); | ||
useEffect(() => { | ||
interceptor(axios, serviceUrl, refreshToken); | ||
var code = queryString.parse(window.location.search).code; | ||
var stateLocalStorage = getState(); | ||
var code_verifier = getCodeVerifier(); | ||
const code = queryString.parse(window.location.search).code; | ||
const stateLocalStorage = getState(); | ||
const code_verifier = getCodeVerifier(); | ||
if (code && stateLocalStorage && code_verifier) { | ||
@@ -793,6 +762,6 @@ setStatus('LOGGING'); | ||
grant_type: 'authorization_code', | ||
client_id: client_id, | ||
redirect_uri: redirect_uri, | ||
code: code, | ||
code_verifier: code_verifier, | ||
client_id, | ||
redirect_uri, | ||
code, | ||
code_verifier, | ||
}), { | ||
@@ -803,4 +772,3 @@ headers: { | ||
}) | ||
.then(function (_a) { | ||
var data = _a.data; | ||
.then(function ({ data }) { | ||
setToken(data); | ||
@@ -812,3 +780,3 @@ window.location.href = redirect_uri; | ||
}) | ||
.finally(function () { | ||
.finally(() => { | ||
clearCodeVerifierAndSate(); | ||
@@ -824,41 +792,33 @@ }); | ||
}, []); | ||
var refreshToken = function () { return __awaiter(_this, void 0, void 0, function () { | ||
var data, err_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, 2, , 3]); | ||
return [4 /*yield*/, axios.post(token_endpoint, queryString.stringify({ | ||
grant_type: 'refresh_token', | ||
client_id: client_id, | ||
refresh_token: getRefreshToken(), | ||
}), { | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
})]; | ||
case 1: | ||
data = (_a.sent()).data; | ||
setToken(data); | ||
setStatus('LOGGED'); | ||
return [2 /*return*/, data]; | ||
case 2: | ||
err_1 = _a.sent(); | ||
console.log(err_1); | ||
logout(); | ||
return [2 /*return*/, {}]; | ||
case 3: return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; | ||
var login = function () { | ||
var new_code_verifier = generateCodeVerifier(); | ||
var new_state = generateRandomState(); | ||
const refreshToken = () => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const { data } = yield axios.post(token_endpoint, queryString.stringify({ | ||
grant_type: 'refresh_token', | ||
client_id, | ||
refresh_token: getRefreshToken(), | ||
}), { | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
}); | ||
setToken(data); | ||
setStatus('LOGGED'); | ||
return data; | ||
} | ||
catch (err) { | ||
console.log(err); | ||
logout(); | ||
return {}; | ||
} | ||
}); | ||
const login = () => { | ||
const new_code_verifier = generateCodeVerifier(); | ||
const new_state = generateRandomState(); | ||
setState(new_state); | ||
setCodeVerifier(new_code_verifier); | ||
window.location.href = initFlowUrl({ | ||
authorization_endpoint: authorization_endpoint, | ||
client_id: client_id, | ||
redirect_uri: redirect_uri, | ||
requested_scopes: requested_scopes, | ||
authorization_endpoint, | ||
client_id, | ||
redirect_uri, | ||
requested_scopes, | ||
code_challenge: pkceChallengeFromVerifier(new_code_verifier), | ||
@@ -869,41 +829,51 @@ state: new_state, | ||
}; | ||
var logout = function () { | ||
const logout = () => { | ||
clear(); | ||
window.location.href = end_session_endpoint + "?post_logout_redirect_uri=" + redirect_uri; | ||
window.location.href = `${end_session_endpoint}?post_logout_redirect_uri=${redirect_uri}`; | ||
}; | ||
var isAuthenticated = function () { | ||
const isAuthenticated = () => { | ||
return !!getAccessToken(); | ||
}; | ||
return (jsx(AutenticationContext.Provider, __assign({ value: { | ||
login: login, | ||
logout: logout, | ||
isAuthenticated: isAuthenticated, | ||
status: status, | ||
const userInfo = () => { | ||
const idToken = getIdToken(); | ||
if (idToken) { | ||
const [_, payload] = idToken.split('.'); | ||
return base64decode(payload); | ||
} | ||
return undefined; | ||
}; | ||
return (jsx(AutenticationContext.Provider, Object.assign({ value: { | ||
login, | ||
logout, | ||
isAuthenticated, | ||
status, | ||
userInfo, | ||
} }, { children: _props.children }), void 0)); | ||
} | ||
function initFlowUrl(init) { | ||
var authorization_endpoint = init.authorization_endpoint, client_id = init.client_id, redirect_uri = init.redirect_uri, requested_scopes = init.requested_scopes, code_challenge = init.code_challenge, state = init.state, code_challenge_method = init.code_challenge_method; | ||
return authorization_endpoint + "?" + queryString.stringify({ | ||
const { authorization_endpoint, client_id, redirect_uri, requested_scopes, code_challenge, state, code_challenge_method, } = init; | ||
return `${authorization_endpoint}?${queryString.stringify({ | ||
response_type: 'code', | ||
client_id: client_id, | ||
state: state, | ||
client_id, | ||
state, | ||
scope: requested_scopes, | ||
redirect_uri: redirect_uri, | ||
code_challenge: code_challenge, | ||
code_challenge_method: code_challenge_method, | ||
}); | ||
redirect_uri, | ||
code_challenge, | ||
code_challenge_method, | ||
})}`; | ||
} | ||
function useAuthentication() { | ||
var _a = useContext(AutenticationContext), login = _a.login, logout = _a.logout, isAuthenticated = _a.isAuthenticated, status = _a.status; | ||
const { login, logout, isAuthenticated, status, userInfo } = useContext(AutenticationContext); | ||
return { | ||
login: login, | ||
logout: logout, | ||
isAuthenticated: isAuthenticated, | ||
status: status, | ||
login, | ||
logout, | ||
isAuthenticated, | ||
status, | ||
userInfo, | ||
}; | ||
} | ||
function Anonymous(props) { | ||
var children = props.children; | ||
var _a = useAuthentication(), isAuthenticated = _a.isAuthenticated, status = _a.status; | ||
function LogginIn(props) { | ||
const { children } = props; | ||
const { isAuthenticated, status } = useAuthentication(); | ||
return jsx("div", { children: !isAuthenticated() && status === 'LOGGING' && children }, void 0); | ||
@@ -913,4 +883,4 @@ } | ||
function AutoLogin() { | ||
var _a = useAuthentication(), login = _a.login, isAuthenticated = _a.isAuthenticated, status = _a.status; | ||
useEffect(function () { | ||
const { login, isAuthenticated, status } = useAuthentication(); | ||
useEffect(() => { | ||
if (!isAuthenticated() && status === 'LOGIN') { | ||
@@ -924,8 +894,8 @@ login(); | ||
function LoggedIn(props) { | ||
var children = props.children; | ||
var _a = useAuthentication(), isAuthenticated = _a.isAuthenticated, status = _a.status; | ||
const { children } = props; | ||
const { isAuthenticated, status } = useAuthentication(); | ||
return jsx("div", { children: isAuthenticated() && status === 'LOGGED' && children }, void 0); | ||
} | ||
export { Anonymous, AutoLogin, LoggedIn, AuthenticationProvider as default, useAuthentication }; | ||
export { AutoLogin, LoggedIn, LogginIn, AuthenticationProvider as default, useAuthentication }; | ||
//# sourceMappingURL=index.es.js.map |
@@ -28,13 +28,2 @@ 'use strict'; | ||
var __assign = function() { | ||
__assign = Object.assign || function __assign(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
function __awaiter(thisArg, _arguments, P, generator) { | ||
@@ -50,30 +39,2 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
function __generator(thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
} | ||
// PKCE HELPER FUNCTIONS | ||
@@ -96,8 +57,8 @@ // Generate a secure random string using the browser crypto functions | ||
// Return the base64-urlencoded sha256 hash for the PKCE challenge | ||
function pkceChallengeFromVerifier(v) { | ||
return base64urlencode(sha256(v)); | ||
function pkceChallengeFromVerifier(verify) { | ||
return base64urlencode(sha256(verify)); | ||
} | ||
function base64urlencode(str) { | ||
return str | ||
.toString('base64') | ||
function base64urlencode(buffer) { | ||
return window | ||
.btoa(String.fromCharCode(...buffer)) | ||
.replace(/\+/g, '-') | ||
@@ -107,2 +68,5 @@ .replace(/\//g, '_') | ||
} | ||
function base64decode(str) { | ||
return JSON.parse(window.atob(str)); | ||
} | ||
@@ -697,10 +661,12 @@ function createCommonjsModule(fn) { | ||
var HOSTNAME = window.location.hostname; | ||
var ACCESS_TOKEN = HOSTNAME + "_access_token"; | ||
var REFRESH_TOKEN = HOSTNAME + "_refresh_token"; | ||
var STATE = HOSTNAME + "_state"; | ||
var CODE_VERIFIER = HOSTNAME + "_code_verifier"; | ||
const HOSTNAME = window.location.hostname; | ||
const ACCESS_TOKEN = `${HOSTNAME}_access_token`; | ||
const REFRESH_TOKEN = `${HOSTNAME}_refresh_token`; | ||
const ID_TOKEN = `${HOSTNAME}_id_token`; | ||
const STATE = `${HOSTNAME}_state`; | ||
const CODE_VERIFIER = `${HOSTNAME}_code_verifier`; | ||
function setToken(tokenObj) { | ||
localStorage.setItem(ACCESS_TOKEN, tokenObj.access_token); | ||
localStorage.setItem(REFRESH_TOKEN, tokenObj.refresh_token); | ||
localStorage.setItem(ID_TOKEN, tokenObj.id_token); | ||
} | ||
@@ -713,5 +679,9 @@ function getAccessToken() { | ||
} | ||
function getIdToken() { | ||
return localStorage.getItem(ID_TOKEN); | ||
} | ||
function clear() { | ||
localStorage.removeItem(ACCESS_TOKEN); | ||
localStorage.removeItem(REFRESH_TOKEN); | ||
localStorage.removeItem(ID_TOKEN); | ||
localStorage.removeItem(STATE); | ||
@@ -738,4 +708,4 @@ localStorage.removeItem(CODE_VERIFIER); | ||
function interceptor(axios, serviceUrl, refreshToken) { | ||
axios.interceptors.request.use(function (config) { | ||
var token = getAccessToken(); | ||
axios.interceptors.request.use(config => { | ||
const token = getAccessToken(); | ||
if (config && | ||
@@ -745,14 +715,14 @@ config.headers && | ||
matchHostname(serviceUrl || '', config.url || '')) { | ||
config.headers['Authorization'] = "Bearer " + token; | ||
config.headers = __assign({}, config.headers); | ||
config.headers['Authorization'] = `Bearer ${token}`; | ||
config.headers = Object.assign({}, config.headers); | ||
} | ||
return config; | ||
}, function (error) { | ||
}, error => { | ||
Promise.reject(error); | ||
}); | ||
axios.interceptors.response.use(function (response) { | ||
axios.interceptors.response.use(response => { | ||
return response; | ||
}, function (error) { | ||
}, error => { | ||
var _a, _b; | ||
var originalRequest = error.config; | ||
const originalRequest = error.config; | ||
if ((((_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.status) === 401 || ((_b = error === null || error === void 0 ? void 0 : error.response) === null || _b === void 0 ? void 0 : _b.status) === 403) && | ||
@@ -762,7 +732,7 @@ !originalRequest._retry) { | ||
return refreshToken() | ||
.then(function (res) { | ||
axios.defaults.headers.common['Authorization'] = "Bearer " + res.access_token; | ||
.then(res => { | ||
axios.defaults.headers.common['Authorization'] = `Bearer ${res.access_token}`; | ||
return axios(originalRequest); | ||
}) | ||
.catch(function (error) { return Promise.reject(error); }); | ||
.catch(error => Promise.reject(error)); | ||
} | ||
@@ -774,4 +744,4 @@ return Promise.reject(error); | ||
if (u1 && u2) { | ||
var h1 = new URL(u1).hostname; | ||
var h2 = new URL(u2).hostname; | ||
const h1 = new URL(u1).hostname; | ||
const h2 = new URL(u2).hostname; | ||
return h1 === h2; | ||
@@ -782,14 +752,13 @@ } | ||
var AutenticationContext = React__default["default"].createContext({}); | ||
const AutenticationContext = React__default["default"].createContext({}); | ||
function AuthenticationProvider(_props) { | ||
var _this = this; | ||
var redirect_uri = window.location.protocol + "//" + window.location.hostname + (window.location.port !== '' ? ":" + window.location.port : ''); | ||
var axios = _props.axios; | ||
var _a = _props.options, client_id = _a.client_id, authorization_endpoint = _a.authorization_endpoint, requested_scopes = _a.requested_scopes, token_endpoint = _a.token_endpoint, end_session_endpoint = _a.end_session_endpoint; _a.realm; var serviceUrl = _a.serviceUrl; | ||
var _b = React.useState(!!getState() ? 'LOGGING' : 'LOGIN'), status = _b[0], setStatus = _b[1]; | ||
React.useEffect(function () { | ||
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'); | ||
React.useEffect(() => { | ||
interceptor(axios, serviceUrl, refreshToken); | ||
var code = queryString.parse(window.location.search).code; | ||
var stateLocalStorage = getState(); | ||
var code_verifier = getCodeVerifier(); | ||
const code = queryString.parse(window.location.search).code; | ||
const stateLocalStorage = getState(); | ||
const code_verifier = getCodeVerifier(); | ||
if (code && stateLocalStorage && code_verifier) { | ||
@@ -801,6 +770,6 @@ setStatus('LOGGING'); | ||
grant_type: 'authorization_code', | ||
client_id: client_id, | ||
redirect_uri: redirect_uri, | ||
code: code, | ||
code_verifier: code_verifier, | ||
client_id, | ||
redirect_uri, | ||
code, | ||
code_verifier, | ||
}), { | ||
@@ -811,4 +780,3 @@ headers: { | ||
}) | ||
.then(function (_a) { | ||
var data = _a.data; | ||
.then(function ({ data }) { | ||
setToken(data); | ||
@@ -820,3 +788,3 @@ window.location.href = redirect_uri; | ||
}) | ||
.finally(function () { | ||
.finally(() => { | ||
clearCodeVerifierAndSate(); | ||
@@ -832,41 +800,33 @@ }); | ||
}, []); | ||
var refreshToken = function () { return __awaiter(_this, void 0, void 0, function () { | ||
var data, err_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, 2, , 3]); | ||
return [4 /*yield*/, axios.post(token_endpoint, queryString.stringify({ | ||
grant_type: 'refresh_token', | ||
client_id: client_id, | ||
refresh_token: getRefreshToken(), | ||
}), { | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
})]; | ||
case 1: | ||
data = (_a.sent()).data; | ||
setToken(data); | ||
setStatus('LOGGED'); | ||
return [2 /*return*/, data]; | ||
case 2: | ||
err_1 = _a.sent(); | ||
console.log(err_1); | ||
logout(); | ||
return [2 /*return*/, {}]; | ||
case 3: return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; | ||
var login = function () { | ||
var new_code_verifier = generateCodeVerifier(); | ||
var new_state = generateRandomState(); | ||
const refreshToken = () => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const { data } = yield axios.post(token_endpoint, queryString.stringify({ | ||
grant_type: 'refresh_token', | ||
client_id, | ||
refresh_token: getRefreshToken(), | ||
}), { | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
}); | ||
setToken(data); | ||
setStatus('LOGGED'); | ||
return data; | ||
} | ||
catch (err) { | ||
console.log(err); | ||
logout(); | ||
return {}; | ||
} | ||
}); | ||
const login = () => { | ||
const new_code_verifier = generateCodeVerifier(); | ||
const new_state = generateRandomState(); | ||
setState(new_state); | ||
setCodeVerifier(new_code_verifier); | ||
window.location.href = initFlowUrl({ | ||
authorization_endpoint: authorization_endpoint, | ||
client_id: client_id, | ||
redirect_uri: redirect_uri, | ||
requested_scopes: requested_scopes, | ||
authorization_endpoint, | ||
client_id, | ||
redirect_uri, | ||
requested_scopes, | ||
code_challenge: pkceChallengeFromVerifier(new_code_verifier), | ||
@@ -877,41 +837,51 @@ state: new_state, | ||
}; | ||
var logout = function () { | ||
const logout = () => { | ||
clear(); | ||
window.location.href = end_session_endpoint + "?post_logout_redirect_uri=" + redirect_uri; | ||
window.location.href = `${end_session_endpoint}?post_logout_redirect_uri=${redirect_uri}`; | ||
}; | ||
var isAuthenticated = function () { | ||
const isAuthenticated = () => { | ||
return !!getAccessToken(); | ||
}; | ||
return (jsxRuntime.jsx(AutenticationContext.Provider, __assign({ value: { | ||
login: login, | ||
logout: logout, | ||
isAuthenticated: isAuthenticated, | ||
status: status, | ||
const userInfo = () => { | ||
const idToken = getIdToken(); | ||
if (idToken) { | ||
const [_, payload] = idToken.split('.'); | ||
return base64decode(payload); | ||
} | ||
return undefined; | ||
}; | ||
return (jsxRuntime.jsx(AutenticationContext.Provider, Object.assign({ value: { | ||
login, | ||
logout, | ||
isAuthenticated, | ||
status, | ||
userInfo, | ||
} }, { children: _props.children }), void 0)); | ||
} | ||
function initFlowUrl(init) { | ||
var authorization_endpoint = init.authorization_endpoint, client_id = init.client_id, redirect_uri = init.redirect_uri, requested_scopes = init.requested_scopes, code_challenge = init.code_challenge, state = init.state, code_challenge_method = init.code_challenge_method; | ||
return authorization_endpoint + "?" + queryString.stringify({ | ||
const { authorization_endpoint, client_id, redirect_uri, requested_scopes, code_challenge, state, code_challenge_method, } = init; | ||
return `${authorization_endpoint}?${queryString.stringify({ | ||
response_type: 'code', | ||
client_id: client_id, | ||
state: state, | ||
client_id, | ||
state, | ||
scope: requested_scopes, | ||
redirect_uri: redirect_uri, | ||
code_challenge: code_challenge, | ||
code_challenge_method: code_challenge_method, | ||
}); | ||
redirect_uri, | ||
code_challenge, | ||
code_challenge_method, | ||
})}`; | ||
} | ||
function useAuthentication() { | ||
var _a = React.useContext(AutenticationContext), login = _a.login, logout = _a.logout, isAuthenticated = _a.isAuthenticated, status = _a.status; | ||
const { login, logout, isAuthenticated, status, userInfo } = React.useContext(AutenticationContext); | ||
return { | ||
login: login, | ||
logout: logout, | ||
isAuthenticated: isAuthenticated, | ||
status: status, | ||
login, | ||
logout, | ||
isAuthenticated, | ||
status, | ||
userInfo, | ||
}; | ||
} | ||
function Anonymous(props) { | ||
var children = props.children; | ||
var _a = useAuthentication(), isAuthenticated = _a.isAuthenticated, status = _a.status; | ||
function LogginIn(props) { | ||
const { children } = props; | ||
const { isAuthenticated, status } = useAuthentication(); | ||
return jsxRuntime.jsx("div", { children: !isAuthenticated() && status === 'LOGGING' && children }, void 0); | ||
@@ -921,4 +891,4 @@ } | ||
function AutoLogin() { | ||
var _a = useAuthentication(), login = _a.login, isAuthenticated = _a.isAuthenticated, status = _a.status; | ||
React.useEffect(function () { | ||
const { login, isAuthenticated, status } = useAuthentication(); | ||
React.useEffect(() => { | ||
if (!isAuthenticated() && status === 'LOGIN') { | ||
@@ -932,12 +902,12 @@ login(); | ||
function LoggedIn(props) { | ||
var children = props.children; | ||
var _a = useAuthentication(), isAuthenticated = _a.isAuthenticated, status = _a.status; | ||
const { children } = props; | ||
const { isAuthenticated, status } = useAuthentication(); | ||
return jsxRuntime.jsx("div", { children: isAuthenticated() && status === 'LOGGED' && children }, void 0); | ||
} | ||
exports.Anonymous = Anonymous; | ||
exports.AutoLogin = AutoLogin; | ||
exports.LoggedIn = LoggedIn; | ||
exports.LogginIn = LogginIn; | ||
exports["default"] = AuthenticationProvider; | ||
exports.useAuthentication = useAuthentication; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@iad-os/react-ghost-auth", | ||
"version": "0.1.9", | ||
"version": "0.1.10", | ||
"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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
17
149941
1583