-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auth appended to the routing, no_auth + database_auth injected
- Loading branch information
1 parent
bfcf87d
commit 9e30197
Showing
14 changed files
with
269 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from "react"; | ||
import { AuthConfig, LocalConfig } from "./PrimaryAuthController"; | ||
import DatabaseAuthProvider from "./Providers/DatabaseAuthProvider"; | ||
import KeycloakAuthContext from "./Providers/KeycloakAuthContext"; | ||
import NoAuthProvider from "./Providers/NoAuthProvider"; | ||
|
||
interface AuthProviderProps { | ||
children: React.ReactNode; | ||
config: undefined | AuthConfig | LocalConfig; | ||
keycloakUrl: string | undefined; | ||
} | ||
|
||
const AuthProvider = ({ children, config, keycloakUrl }: AuthProviderProps) => { | ||
if (config?.method === "database") { | ||
return <DatabaseAuthProvider>{children}</DatabaseAuthProvider>; | ||
} | ||
if (config?.method === "oidc") { | ||
return ( | ||
<KeycloakAuthContext config={{ ...config, url: keycloakUrl }}> | ||
{children} | ||
</KeycloakAuthContext> | ||
); | ||
} | ||
return <NoAuthProvider>{children}</NoAuthProvider>; | ||
}; | ||
|
||
export default AuthProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { createContext } from "react"; | ||
export interface AuthContextProps { | ||
user: string | undefined; | ||
login: () => void; | ||
logout: () => void; | ||
updateUser: (user: string, token: string) => void; | ||
getToken: () => string | null; | ||
} | ||
export const defaultAuthContext: AuthContextProps = { | ||
user: undefined, | ||
login: () => {}, | ||
logout: () => {}, | ||
updateUser: (_user: string, _token: string) => {}, | ||
getToken: () => null, | ||
}; | ||
export const AuthContext = createContext(defaultAuthContext); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import React, { useState } from "react"; | ||
import { useNavigate } from "react-router-dom"; | ||
import { | ||
createCookie, | ||
getCookie, | ||
removeCookie, | ||
} from "../../Common/CookieHelper"; | ||
import { AuthContext } from "./AuthContext"; | ||
|
||
interface AuthProviderProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
const DatabaseAuthProvider = ({ children }: AuthProviderProps) => { | ||
const [user, setUser] = useState<string | undefined>(undefined); | ||
const navigate = useNavigate(); | ||
|
||
const logout = () => { | ||
removeCookie("inmanta_user"); | ||
localStorage.removeItem("inmanta_user"); | ||
navigate("/login"); | ||
}; | ||
|
||
const login = () => navigate("/login"); | ||
|
||
const getToken = () => getCookie("inmanta_user"); | ||
|
||
const updateUser = (username: string, token: string) => { | ||
setUser(username); | ||
createCookie("inmanta_user", token, 1); | ||
}; | ||
|
||
return ( | ||
<AuthContext.Provider value={{ user, login, logout, updateUser, getToken }}> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
}; | ||
|
||
export default DatabaseAuthProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React, { useState, useEffect, useCallback, useMemo } from "react"; | ||
import Keycloak, { KeycloakConfig } from "keycloak-js"; | ||
import { AuthContext } from "./AuthContext"; | ||
|
||
interface AuthProviderProps { | ||
children: React.ReactNode; | ||
config: KeycloakConfig; | ||
} | ||
|
||
const KeycloakAuthProvider = ({ children, config }: AuthProviderProps) => { | ||
const keycloakInstance = useMemo(() => new Keycloak(config), [config]); | ||
|
||
const getName = useCallback((): string | undefined => { | ||
return keycloakInstance && | ||
keycloakInstance.profile && | ||
keycloakInstance.profile.username | ||
? keycloakInstance.profile.username | ||
: undefined; | ||
}, [keycloakInstance]); | ||
|
||
const [user, setUser] = useState<string | undefined>(undefined); | ||
|
||
const logout = () => { | ||
keycloakInstance.logout(); | ||
}; | ||
|
||
const login = () => { | ||
keycloakInstance.clearToken(); | ||
keycloakInstance.login(); | ||
}; | ||
const getToken = () => { | ||
return keycloakInstance.token || null; | ||
}; | ||
|
||
useEffect(() => { | ||
setUser(getName()); | ||
}, [getName]); | ||
|
||
return ( | ||
<AuthContext.Provider | ||
value={{ | ||
user, | ||
getToken, | ||
login, | ||
logout, | ||
updateUser: (_user, _token) => ({}), | ||
}} | ||
> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
}; | ||
|
||
export default KeycloakAuthProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React from "react"; | ||
import { AuthContext, defaultAuthContext } from "./AuthContext"; | ||
|
||
interface AuthProviderProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
const NoAuthProvider = ({ children }: AuthProviderProps) => { | ||
return ( | ||
<AuthContext.Provider | ||
value={{ | ||
...defaultAuthContext, | ||
getToken: () => { | ||
return "No token"; | ||
}, | ||
}} | ||
> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
}; | ||
|
||
export default NoAuthProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.