@iad-os/react-ghost-auth
Advanced tools
Comparing version 0.4.11 to 0.5.0
import { AxiosStatic } from 'axios'; | ||
import React from 'react'; | ||
import { TokenResponse } from './models/TokenResponse'; | ||
declare type ProviderOptions = { | ||
name: string; | ||
authorization_endpoint: string; | ||
token_endpoint: string; | ||
client_id: string; | ||
requested_scopes: string; | ||
access_type?: string; | ||
redirect_uri: string; | ||
redirect_logout_uri?: string; | ||
end_session_endpoint: string; | ||
client_secret?: string; | ||
}; | ||
export declare type AuthenticationConfig = { | ||
default?: string; | ||
providers: { | ||
[key in string]: ProviderOptions; | ||
}; | ||
serviceUrl?: string; | ||
}; | ||
declare type EStatus = 'INIT' | 'LOGIN' | 'LOGGING' | 'LOGGED'; | ||
import { AuthenticationConfig, EStatus, TokenResponse } from './types'; | ||
declare type ProviderInfoType = { | ||
@@ -29,3 +9,2 @@ selected: string; | ||
}; | ||
declare type TokenInfo = Pick<TokenResponse, 'access_token' | 'refresh_token'>; | ||
declare type AuthCtxType = { | ||
@@ -36,8 +15,4 @@ login: (provider?: string) => void; | ||
status: EStatus; | ||
userInfo: () => { | ||
[key: string]: any; | ||
} | undefined; | ||
changeStatus: (status: EStatus) => void; | ||
providerInfo: () => ProviderInfoType | undefined; | ||
tokenInfo: () => TokenInfo | undefined; | ||
}; | ||
@@ -65,2 +40,4 @@ export declare type AuthorizationProps = { | ||
export declare function useAuthentication(): AuthCtxType; | ||
export declare function useToken(): string; | ||
export declare function useUserInfo<T>(): T; | ||
export {}; |
@@ -1,2 +0,2 @@ | ||
import { TokenResponse } from './models/TokenResponse'; | ||
import { TokenResponse } from './types'; | ||
export declare function setTokens(tokenObj: TokenResponse): void; | ||
@@ -3,0 +3,0 @@ export declare function getAccessToken(): string | null; |
@@ -1,3 +0,2 @@ | ||
import AuthenticationProvider from './Authentication'; | ||
import { useAuthentication, AuthenticationConfig } from './Authentication'; | ||
import AuthenticationProvider, { useAuthentication, useToken, useUserInfo } from './Authentication'; | ||
import AutoLogin from './components/AutoLogin'; | ||
@@ -8,7 +7,6 @@ import LoggedIn from './components/LoggedIn'; | ||
import RequireAuth from './components/RequireAuth'; | ||
import { TokenResponse } from './models/TokenResponse'; | ||
import * as AuthStoreService from './AuthStoreService'; | ||
import { TokenResponse, AuthenticationConfig } from './types'; | ||
export default AuthenticationProvider; | ||
export { useAuthentication }; | ||
export { useAuthentication, useToken, useUserInfo }; | ||
export type { AuthenticationConfig, TokenResponse }; | ||
export { AutoLogin, LoggedIn, LogginIn, Public, RequireAuth, AuthStoreService }; | ||
export { AutoLogin, LoggedIn, LogginIn, Public, RequireAuth }; |
@@ -668,6 +668,2 @@ import { jsx, jsxs } from 'react/jsx-runtime'; | ||
} | ||
function clearToken() { | ||
tokenInfo.setData(null); | ||
localStorage.removeItem(PROVIDER_OIDC); | ||
} | ||
function clear() { | ||
@@ -715,19 +711,2 @@ tokenInfo.setData(null); | ||
var AuthStoreService = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
setTokens: setTokens, | ||
getAccessToken: getAccessToken, | ||
getRefreshToken: getRefreshToken, | ||
getIdToken: getIdToken, | ||
clearToken: clearToken, | ||
clear: clear, | ||
getState: getState, | ||
getCodeVerifier: getCodeVerifier, | ||
setState: setState, | ||
setCodeVerifier: setCodeVerifier, | ||
clearCodeVerifierAndSate: clearCodeVerifierAndSate, | ||
setProviderOidc: setProviderOidc, | ||
getProviderOidc: getProviderOidc | ||
}); | ||
function interceptor(axios, serviceUrl, refreshToken, needAuthorization = (serviceUrl, requestUrl) => serviceUrl === '/' || matchHostname(serviceUrl, requestUrl)) { | ||
@@ -787,8 +766,5 @@ axios.interceptors.request.use(config => { | ||
} | ||
function generateCodeVerifier() { | ||
function generateRandomString() { | ||
return base64urlencode(generateRandomBytes()); | ||
} | ||
function generateRandomState() { | ||
return base64urlencode(generateRandomBytes()); | ||
} | ||
// Return the base64-urlencoded sha256 hash for the PKCE challenge | ||
@@ -817,2 +793,9 @@ function pkceChallengeFromVerifier(verify) { | ||
} | ||
function openIdInitialFlowUrl(init) { | ||
const { authorization_endpoint, client_id, redirect_uri, requested_scopes, code_challenge, state, code_challenge_method, access_type, } = init; | ||
return `${authorization_endpoint}?${queryString.stringify(Object.assign({ response_type: 'code', client_id, | ||
state, scope: requested_scopes, redirect_uri, | ||
code_challenge, | ||
code_challenge_method }, (access_type && { access_type })))}`; | ||
} | ||
@@ -917,8 +900,8 @@ const AutenticationContext = React.createContext({}); | ||
const { authorization_endpoint, client_id, redirect_uri, requested_scopes, access_type, } = _provider; | ||
const new_code_verifier = generateCodeVerifier(); | ||
const new_state = generateRandomState(); | ||
const new_code_verifier = generateRandomString(); | ||
const new_state = generateRandomString(); | ||
setState(new_state); | ||
setCodeVerifier(new_code_verifier); | ||
pkceChallengeFromVerifier(new_code_verifier).then(code_challenge => { | ||
window.location.replace(initFlowUrl({ | ||
window.location.replace(openIdInitialFlowUrl({ | ||
authorization_endpoint, | ||
@@ -953,10 +936,2 @@ client_id, | ||
}; | ||
const userInfo = () => { | ||
const idToken = getIdToken(); | ||
if (idToken) { | ||
const [_, payload] = idToken.split('.'); | ||
return base64decode(payload); | ||
} | ||
return undefined; | ||
}; | ||
const changeStatus = useCallback((status) => setStatus(status), [status]); | ||
@@ -970,8 +945,2 @@ const providerInfo = () => providers | ||
: undefined; | ||
const tokenInfo = () => isAuthenticated() | ||
? { | ||
access_token: getAccessToken(), | ||
refresh_token: getRefreshToken(), | ||
} | ||
: undefined; | ||
return (jsx(AutenticationContext.Provider, Object.assign({ value: { | ||
@@ -982,18 +951,26 @@ login, | ||
status, | ||
userInfo, | ||
changeStatus, | ||
providerInfo, | ||
tokenInfo, | ||
} }, { children: children }))); | ||
} | ||
function initFlowUrl(init) { | ||
const { authorization_endpoint, client_id, redirect_uri, requested_scopes, code_challenge, state, code_challenge_method, access_type, } = init; | ||
return `${authorization_endpoint}?${queryString.stringify(Object.assign({ response_type: 'code', client_id, | ||
state, scope: requested_scopes, redirect_uri, | ||
code_challenge, | ||
code_challenge_method }, (access_type && { access_type })))}`; | ||
} | ||
function useAuthentication() { | ||
return useContext(AutenticationContext); | ||
} | ||
function useToken() { | ||
const { isAuthenticated } = useAuthentication(); | ||
const token = getAccessToken(); | ||
if (!isAuthenticated() || !token) { | ||
throw new Error('User not authenticated!'); | ||
} | ||
return token; | ||
} | ||
function useUserInfo() { | ||
const { isAuthenticated } = useAuthentication(); | ||
const idToken = getIdToken(); | ||
if (!isAuthenticated() || !idToken) { | ||
throw new Error('User not authenticated!'); | ||
} | ||
const [_, payload] = idToken.split('.'); | ||
return base64decode(payload); | ||
} | ||
@@ -1065,3 +1042,3 @@ function AutoLogin(props) { | ||
export { AuthStoreService, AutoLogin, LoggedIn, LogginIn, Public, RequireAuth, AuthenticationProvider as default, useAuthentication }; | ||
export { AutoLogin, LoggedIn, LogginIn, Public, RequireAuth, AuthenticationProvider as default, useAuthentication, useToken, useUserInfo }; | ||
//# sourceMappingURL=index.es.js.map |
@@ -676,6 +676,2 @@ 'use strict'; | ||
} | ||
function clearToken() { | ||
tokenInfo.setData(null); | ||
localStorage.removeItem(PROVIDER_OIDC); | ||
} | ||
function clear() { | ||
@@ -723,19 +719,2 @@ tokenInfo.setData(null); | ||
var AuthStoreService = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
setTokens: setTokens, | ||
getAccessToken: getAccessToken, | ||
getRefreshToken: getRefreshToken, | ||
getIdToken: getIdToken, | ||
clearToken: clearToken, | ||
clear: clear, | ||
getState: getState, | ||
getCodeVerifier: getCodeVerifier, | ||
setState: setState, | ||
setCodeVerifier: setCodeVerifier, | ||
clearCodeVerifierAndSate: clearCodeVerifierAndSate, | ||
setProviderOidc: setProviderOidc, | ||
getProviderOidc: getProviderOidc | ||
}); | ||
function interceptor(axios, serviceUrl, refreshToken, needAuthorization = (serviceUrl, requestUrl) => serviceUrl === '/' || matchHostname(serviceUrl, requestUrl)) { | ||
@@ -795,8 +774,5 @@ axios.interceptors.request.use(config => { | ||
} | ||
function generateCodeVerifier() { | ||
function generateRandomString() { | ||
return base64urlencode(generateRandomBytes()); | ||
} | ||
function generateRandomState() { | ||
return base64urlencode(generateRandomBytes()); | ||
} | ||
// Return the base64-urlencoded sha256 hash for the PKCE challenge | ||
@@ -825,2 +801,9 @@ function pkceChallengeFromVerifier(verify) { | ||
} | ||
function openIdInitialFlowUrl(init) { | ||
const { authorization_endpoint, client_id, redirect_uri, requested_scopes, code_challenge, state, code_challenge_method, access_type, } = init; | ||
return `${authorization_endpoint}?${queryString.stringify(Object.assign({ response_type: 'code', client_id, | ||
state, scope: requested_scopes, redirect_uri, | ||
code_challenge, | ||
code_challenge_method }, (access_type && { access_type })))}`; | ||
} | ||
@@ -925,8 +908,8 @@ const AutenticationContext = React__default["default"].createContext({}); | ||
const { authorization_endpoint, client_id, redirect_uri, requested_scopes, access_type, } = _provider; | ||
const new_code_verifier = generateCodeVerifier(); | ||
const new_state = generateRandomState(); | ||
const new_code_verifier = generateRandomString(); | ||
const new_state = generateRandomString(); | ||
setState(new_state); | ||
setCodeVerifier(new_code_verifier); | ||
pkceChallengeFromVerifier(new_code_verifier).then(code_challenge => { | ||
window.location.replace(initFlowUrl({ | ||
window.location.replace(openIdInitialFlowUrl({ | ||
authorization_endpoint, | ||
@@ -961,10 +944,2 @@ client_id, | ||
}; | ||
const userInfo = () => { | ||
const idToken = getIdToken(); | ||
if (idToken) { | ||
const [_, payload] = idToken.split('.'); | ||
return base64decode(payload); | ||
} | ||
return undefined; | ||
}; | ||
const changeStatus = React.useCallback((status) => setStatus(status), [status]); | ||
@@ -978,8 +953,2 @@ const providerInfo = () => providers | ||
: undefined; | ||
const tokenInfo = () => isAuthenticated() | ||
? { | ||
access_token: getAccessToken(), | ||
refresh_token: getRefreshToken(), | ||
} | ||
: undefined; | ||
return (jsxRuntime.jsx(AutenticationContext.Provider, Object.assign({ value: { | ||
@@ -990,18 +959,26 @@ login, | ||
status, | ||
userInfo, | ||
changeStatus, | ||
providerInfo, | ||
tokenInfo, | ||
} }, { children: children }))); | ||
} | ||
function initFlowUrl(init) { | ||
const { authorization_endpoint, client_id, redirect_uri, requested_scopes, code_challenge, state, code_challenge_method, access_type, } = init; | ||
return `${authorization_endpoint}?${queryString.stringify(Object.assign({ response_type: 'code', client_id, | ||
state, scope: requested_scopes, redirect_uri, | ||
code_challenge, | ||
code_challenge_method }, (access_type && { access_type })))}`; | ||
} | ||
function useAuthentication() { | ||
return React.useContext(AutenticationContext); | ||
} | ||
function useToken() { | ||
const { isAuthenticated } = useAuthentication(); | ||
const token = getAccessToken(); | ||
if (!isAuthenticated() || !token) { | ||
throw new Error('User not authenticated!'); | ||
} | ||
return token; | ||
} | ||
function useUserInfo() { | ||
const { isAuthenticated } = useAuthentication(); | ||
const idToken = getIdToken(); | ||
if (!isAuthenticated() || !idToken) { | ||
throw new Error('User not authenticated!'); | ||
} | ||
const [_, payload] = idToken.split('.'); | ||
return base64decode(payload); | ||
} | ||
@@ -1073,3 +1050,2 @@ function AutoLogin(props) { | ||
exports.AuthStoreService = AuthStoreService; | ||
exports.AutoLogin = AutoLogin; | ||
@@ -1082,2 +1058,4 @@ exports.LoggedIn = LoggedIn; | ||
exports.useAuthentication = useAuthentication; | ||
exports.useToken = useToken; | ||
exports.useUserInfo = useUserInfo; | ||
//# sourceMappingURL=index.js.map |
import { AxiosStatic } from 'axios'; | ||
import { AuthorizationProps } from './Authentication'; | ||
import { TokenResponse } from './models/TokenResponse'; | ||
import { TokenResponse } from './types'; | ||
export declare function interceptor(axios: AxiosStatic, serviceUrl: string, refreshToken: () => Promise<TokenResponse>, needAuthorization?: AuthorizationProps['needAuthorization']): void; |
@@ -1,5 +0,6 @@ | ||
export declare function generateCodeVerifier(): string; | ||
export declare function generateRandomState(): string; | ||
import { InitFlowUrlType } from './types'; | ||
export declare function generateRandomString(): string; | ||
export declare function pkceChallengeFromVerifier(verify: string): Promise<string>; | ||
export declare function base64urlencode(str: string): string; | ||
export declare function base64decode(str: string): any; | ||
export declare function openIdInitialFlowUrl(init: InitFlowUrlType): string; |
{ | ||
"name": "@iad-os/react-ghost-auth", | ||
"version": "0.4.11", | ||
"version": "0.5.0", | ||
"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
170727
16
1894