@entur/auth-provider
Advanced tools
Comparing version 0.4.0 to 1.0.0
@@ -6,7 +6,6 @@ import React from 'react'; | ||
auth0ClaimsNamespace?: string; | ||
defaultAuthMethod?: AuthMethod; | ||
children?: React.ReactNode; | ||
loginAutomatically?: boolean; | ||
} | ||
export declare const AuthProvider: ({ keycloakConfigUrl, auth0Config, auth0ClaimsNamespace, defaultAuthMethod, children, loginAutomatically }: Props) => JSX.Element; | ||
export declare const AuthProvider: ({ auth0Config, auth0ClaimsNamespace, children, loginAutomatically }: Props) => JSX.Element; | ||
export {}; |
import React from 'react'; | ||
export declare function getAuthMethod(defaultAuthMethod: AuthMethod): string; | ||
export declare const defaultAuthState: { | ||
@@ -4,0 +3,0 @@ isLoading: boolean; |
@@ -5,16 +5,4 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var React__default = _interopDefault(React); | ||
var Keycloak = _interopDefault(require('keycloak-js')); | ||
var auth0React = require('@auth0/auth0-react'); | ||
function getAuthMethod(defaultAuthMethod) { | ||
if (defaultAuthMethod === 'auth0') { | ||
return 'auth0'; | ||
} else if (window.location.search.indexOf('authMethod=auth0') > -1) { | ||
return 'auth0'; | ||
} else if (window.location.search.indexOf('authMethod=kc') > -1) { | ||
return 'kc'; | ||
} else { | ||
return defaultAuthMethod || 'auth0'; | ||
} | ||
} | ||
var defaultAuthState = { | ||
@@ -33,66 +21,2 @@ isLoading: false, | ||
var KeycloakAuthProvider = function KeycloakAuthProvider(_ref) { | ||
var children = _ref.children, | ||
configUrl = _ref.configUrl; | ||
var _useState = React.useState(defaultAuthState), | ||
auth = _useState[0], | ||
setAuth = _useState[1]; | ||
React.useEffect(function () { | ||
var kc = Keycloak(configUrl); | ||
var token; | ||
var updater; | ||
kc.init({ | ||
onLoad: 'login-required', | ||
checkLoginIframe: false | ||
}).success(function (authenticated) { | ||
if (authenticated) { | ||
token = kc.token; | ||
setAuth({ | ||
isLoading: false, | ||
isAuthenticated: true, | ||
user: { | ||
name: kc.tokenParsed.preferred_username | ||
}, | ||
roleAssignments: kc.tokenParsed.roles || [], | ||
getAccessToken: function () { | ||
return Promise.resolve(token); | ||
}, | ||
logout: function logout() { | ||
return kc.logout(); | ||
}, | ||
login: function login() { | ||
return kc.login(); | ||
} | ||
}); | ||
updater = setInterval(function () { | ||
try { | ||
try { | ||
kc.updateToken(10); | ||
token = kc.token || ''; | ||
} catch (_) { | ||
kc.logout(); | ||
} | ||
return Promise.resolve(); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}, 10000); | ||
} else { | ||
return kc.login(); | ||
} | ||
}); | ||
return function () { | ||
if (updater) { | ||
clearInterval(updater); | ||
} | ||
}; | ||
}, [configUrl]); | ||
return React__default.createElement(AuthContext.Provider, { | ||
value: auth | ||
}, children); | ||
}; | ||
var Auth0AuthProvider = function Auth0AuthProvider(_ref) { | ||
@@ -154,44 +78,14 @@ var children = _ref.children, | ||
var AuthProvider = function AuthProvider(_ref) { | ||
var keycloakConfigUrl = _ref.keycloakConfigUrl, | ||
auth0Config = _ref.auth0Config, | ||
var auth0Config = _ref.auth0Config, | ||
auth0ClaimsNamespace = _ref.auth0ClaimsNamespace, | ||
defaultAuthMethod = _ref.defaultAuthMethod, | ||
children = _ref.children, | ||
_ref$loginAutomatical = _ref.loginAutomatically, | ||
loginAutomatically = _ref$loginAutomatical === void 0 ? true : _ref$loginAutomatical; | ||
var _useState = React.useState(getAuthMethod(defaultAuthMethod)), | ||
authProvider = _useState[0], | ||
setAuthProvider = _useState[1]; | ||
React.useEffect(function () { | ||
var popstateHandler = function popstateHandler() { | ||
var nextAuthProvider = getAuthMethod(defaultAuthMethod); | ||
if (authProvider !== nextAuthProvider) { | ||
setAuthProvider(nextAuthProvider); | ||
} | ||
}; | ||
window.addEventListener('popstate', popstateHandler); | ||
return function () { | ||
window.removeEventListener('popstate', popstateHandler); | ||
}; | ||
}, []); | ||
if (authProvider === 'kc') { | ||
return React__default.createElement(KeycloakAuthProvider, { | ||
configUrl: keycloakConfigUrl | ||
}, children); | ||
} else if (authProvider === 'auth0' && auth0Config !== undefined) { | ||
return React__default.createElement(auth0React.Auth0Provider, Object.assign({ | ||
useRefreshToken: true, | ||
cacheLocation: window.location.hostname.indexOf('localhost') > -1 ? 'localstorage' : 'memory' | ||
}, auth0Config), React__default.createElement(Auth0AuthProvider, { | ||
claimsNamespace: auth0ClaimsNamespace, | ||
loginAutomatically: loginAutomatically | ||
}, children)); | ||
} else { | ||
throw new Error('No authentication providers were configured.'); | ||
} | ||
return React__default.createElement(auth0React.Auth0Provider, Object.assign({ | ||
useRefreshToken: true, | ||
cacheLocation: window.location.hostname.indexOf('localhost') > -1 ? 'localstorage' : 'memory' | ||
}, auth0Config), React__default.createElement(Auth0AuthProvider, { | ||
claimsNamespace: auth0ClaimsNamespace, | ||
loginAutomatically: loginAutomatically | ||
}, children)); | ||
}; | ||
@@ -198,0 +92,0 @@ |
import React, { useState, useEffect, useContext } from 'react'; | ||
import Keycloak from 'keycloak-js'; | ||
import { useAuth0, Auth0Provider } from '@auth0/auth0-react'; | ||
function getAuthMethod(defaultAuthMethod) { | ||
if (defaultAuthMethod === 'auth0') { | ||
return 'auth0'; | ||
} else if (window.location.search.indexOf('authMethod=auth0') > -1) { | ||
return 'auth0'; | ||
} else if (window.location.search.indexOf('authMethod=kc') > -1) { | ||
return 'kc'; | ||
} else { | ||
return defaultAuthMethod || 'auth0'; | ||
} | ||
} | ||
var defaultAuthState = { | ||
@@ -29,66 +17,2 @@ isLoading: false, | ||
var KeycloakAuthProvider = function KeycloakAuthProvider(_ref) { | ||
var children = _ref.children, | ||
configUrl = _ref.configUrl; | ||
var _useState = useState(defaultAuthState), | ||
auth = _useState[0], | ||
setAuth = _useState[1]; | ||
useEffect(function () { | ||
var kc = Keycloak(configUrl); | ||
var token; | ||
var updater; | ||
kc.init({ | ||
onLoad: 'login-required', | ||
checkLoginIframe: false | ||
}).success(function (authenticated) { | ||
if (authenticated) { | ||
token = kc.token; | ||
setAuth({ | ||
isLoading: false, | ||
isAuthenticated: true, | ||
user: { | ||
name: kc.tokenParsed.preferred_username | ||
}, | ||
roleAssignments: kc.tokenParsed.roles || [], | ||
getAccessToken: function () { | ||
return Promise.resolve(token); | ||
}, | ||
logout: function logout() { | ||
return kc.logout(); | ||
}, | ||
login: function login() { | ||
return kc.login(); | ||
} | ||
}); | ||
updater = setInterval(function () { | ||
try { | ||
try { | ||
kc.updateToken(10); | ||
token = kc.token || ''; | ||
} catch (_) { | ||
kc.logout(); | ||
} | ||
return Promise.resolve(); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}, 10000); | ||
} else { | ||
return kc.login(); | ||
} | ||
}); | ||
return function () { | ||
if (updater) { | ||
clearInterval(updater); | ||
} | ||
}; | ||
}, [configUrl]); | ||
return React.createElement(AuthContext.Provider, { | ||
value: auth | ||
}, children); | ||
}; | ||
var Auth0AuthProvider = function Auth0AuthProvider(_ref) { | ||
@@ -150,44 +74,14 @@ var children = _ref.children, | ||
var AuthProvider = function AuthProvider(_ref) { | ||
var keycloakConfigUrl = _ref.keycloakConfigUrl, | ||
auth0Config = _ref.auth0Config, | ||
var auth0Config = _ref.auth0Config, | ||
auth0ClaimsNamespace = _ref.auth0ClaimsNamespace, | ||
defaultAuthMethod = _ref.defaultAuthMethod, | ||
children = _ref.children, | ||
_ref$loginAutomatical = _ref.loginAutomatically, | ||
loginAutomatically = _ref$loginAutomatical === void 0 ? true : _ref$loginAutomatical; | ||
var _useState = useState(getAuthMethod(defaultAuthMethod)), | ||
authProvider = _useState[0], | ||
setAuthProvider = _useState[1]; | ||
useEffect(function () { | ||
var popstateHandler = function popstateHandler() { | ||
var nextAuthProvider = getAuthMethod(defaultAuthMethod); | ||
if (authProvider !== nextAuthProvider) { | ||
setAuthProvider(nextAuthProvider); | ||
} | ||
}; | ||
window.addEventListener('popstate', popstateHandler); | ||
return function () { | ||
window.removeEventListener('popstate', popstateHandler); | ||
}; | ||
}, []); | ||
if (authProvider === 'kc') { | ||
return React.createElement(KeycloakAuthProvider, { | ||
configUrl: keycloakConfigUrl | ||
}, children); | ||
} else if (authProvider === 'auth0' && auth0Config !== undefined) { | ||
return React.createElement(Auth0Provider, Object.assign({ | ||
useRefreshToken: true, | ||
cacheLocation: window.location.hostname.indexOf('localhost') > -1 ? 'localstorage' : 'memory' | ||
}, auth0Config), React.createElement(Auth0AuthProvider, { | ||
claimsNamespace: auth0ClaimsNamespace, | ||
loginAutomatically: loginAutomatically | ||
}, children)); | ||
} else { | ||
throw new Error('No authentication providers were configured.'); | ||
} | ||
return React.createElement(Auth0Provider, Object.assign({ | ||
useRefreshToken: true, | ||
cacheLocation: window.location.hostname.indexOf('localhost') > -1 ? 'localstorage' : 'memory' | ||
}, auth0Config), React.createElement(Auth0AuthProvider, { | ||
claimsNamespace: auth0ClaimsNamespace, | ||
loginAutomatically: loginAutomatically | ||
}, children)); | ||
}; | ||
@@ -194,0 +88,0 @@ |
{ | ||
"name": "@entur/auth-provider", | ||
"version": "0.4.0", | ||
"version": "1.0.0", | ||
"description": "React components and helpers for authentication", | ||
@@ -66,5 +66,4 @@ "author": "entur/nrp", | ||
"dependencies": { | ||
"@auth0/auth0-react": "1.2.0", | ||
"keycloak-js": "3.1.0" | ||
"@auth0/auth0-react": "1.2.0" | ||
} | ||
} |
@@ -21,13 +21,6 @@ # @entur/auth-provider | ||
// Provide either auth0Config, keycloakConfigUrl or both | ||
const auth0Config = {}; //<-- see auth0 docs for required fields | ||
const auth0ClaimsNamespace = ''; // <-- required for custom roles parsing | ||
const keycloakConfigUrl = ''; // <-- where to find keycloak config | ||
// defaultAuthMethod is optional, kc (keycloak) is default. User can override with query parameter: | ||
// ?authMethod=kc|auth0 | ||
const defaultAuthMethod = 'auth0'; | ||
// If set to false, app will not automatically redirect to login page | ||
// Note: Only works on auth0 - keycloak will always redirect automatically | ||
const loginAutomatically = false; | ||
@@ -51,4 +44,2 @@ | ||
auth0ClaimsNamespace={auth0ClaimsNamespace} | ||
keycloakConfigUrl={keycloakConfigUrl} | ||
defaultAuthMethod={defaultAuthMethod} | ||
loginAutomatically={loginAutomatically} | ||
@@ -55,0 +46,0 @@ > |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
2
1
21457
213
74
- Removedkeycloak-js@3.1.0
- Removedkeycloak-js@3.1.0(transitive)