Skip to content

Commit

Permalink
wip: allow session login
Browse files Browse the repository at this point in the history
use /auth/login/ for cookies
remove last AppContext.user, switch to UserContext.getUsername/isLoggedIn
  • Loading branch information
himdel committed Oct 28, 2024
1 parent 857d566 commit 941a273
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 33 deletions.
1 change: 1 addition & 0 deletions config/start.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = webpackBase({
DEV_PROXY: {
'/api/': proxyTarget,
'/assets/': proxyTarget,
'/auth/': proxyTarget,
'/extensions/': proxyTarget,
'/pulp/': proxyTarget,
'/static/rest_framework/': proxyTarget,
Expand Down
2 changes: 1 addition & 1 deletion pulp-ui-config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"API_BASE_PATH": "/pulp/api/v3/",
"UI_BASE_PATH": "/ui/",
"UI_EXTERNAL_LOGIN_URI": null,
"UI_EXTERNAL_LOGIN_URI": "/auth/login/",
"EXTRA_VERSION": ""
}
2 changes: 1 addition & 1 deletion src/api/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class BaseAPI {
this.http = axios.create({
// adapter + withCredentials ensures no popup on http basic auth fail
adapter: 'fetch',
withCredentials: false,
//withCredentials: false,

// baseURL gets set in PulpAPI
paramsSerializer: {
Expand Down
5 changes: 4 additions & 1 deletion src/api/pulp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ export class PulpAPI extends BaseAPI {

this.http.interceptors.request.use((request) => {
if (!request.auth) {
request.auth = JSON.parse(
const credentials = JSON.parse(
window.sessionStorage.credentials ||
window.localStorage.credentials ||
'{}',
);
if (credentials?.username !== 'HACK') {
request.auth = credentials;
}
}

request.baseURL = config.API_BASE_PATH;
Expand Down
13 changes: 2 additions & 11 deletions src/app-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ export interface IAppContextType {
queueAlert: (alert: AlertType) => void;
setAlerts: (alerts: AlertType[]) => void;
settings; // deprecated
user; // deprecated
}

export const AppContext = createContext<IAppContextType>(undefined);
export const useAppContext = () => useContext(AppContext);

// FIXME: rename to AlertContext*; deal with deprecated featureFlags & settings
export const AppContextProvider = ({ children }: { children: ReactNode }) => {
const [alerts, setAlerts] = useState<AlertType[]>([]);
const { credentials } = useUserContext();
const { hasPermission } = useUserContext();

// hub compat for now
const featureFlags = {
Expand All @@ -43,7 +43,6 @@ export const AppContextProvider = ({ children }: { children: ReactNode }) => {
};

const queueAlert = (alert) => setAlerts((alerts) => [...alerts, alert]);
const hasPermission = (_name) => true; // FIXME: permission handling

return (
<AppContext.Provider
Expand All @@ -54,14 +53,6 @@ export const AppContextProvider = ({ children }: { children: ReactNode }) => {
queueAlert,
setAlerts,
settings,
// FIXME: hack
user: credentials
? {
username: credentials.username,
groups: [],
model_permissions: {},
}
: null,
}}
>
{children}
Expand Down
6 changes: 3 additions & 3 deletions src/components/delete-user-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const DeleteUserModal = ({
user,
}: IProps) => {
const [waiting, setWaiting] = useState(false);
const { credentials } = useUserContext();
const { getUsername } = useUserContext();

if (!user || !isOpen) {
return null;
Expand All @@ -29,11 +29,11 @@ export const DeleteUserModal = ({
<DeleteModal
cancelAction={() => closeModal(false)}
deleteAction={() => deleteUser()}
isDisabled={waiting || user.username === credentials.username}
isDisabled={waiting || user.username === getUsername()}
spinner={waiting}
title={t`Delete user?`}
>
{user.username === credentials.username ? (
{user.username === getUsername() ? (
t`Deleting yourself is not allowed.`
) : (
<Trans>
Expand Down
1 change: 0 additions & 1 deletion src/containers/ansible-remote/tab-access.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ interface TabProps {
featureFlags;
hasPermission;
state: { params };
user;
};
}

Expand Down
1 change: 0 additions & 1 deletion src/containers/ansible-repository/tab-access.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ interface TabProps {
featureFlags;
hasPermission;
state: { params };
user;
};
}

Expand Down
11 changes: 10 additions & 1 deletion src/containers/login/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function PulpLoginPage(_props) {
const { setCredentials, clearCredentials } = useUserContext();
const { next } = useQueryParams();

const [username, setUsername] = useState('');
const [username, setUsername] = useState('HACK');
const [password, setPassword] = useState('');
const [error, setError] = useState('');
const [redirect, setRedirect] = useState('');
Expand All @@ -25,6 +25,15 @@ function PulpLoginPage(_props) {
}, []);

const onLoginClick = (e) => {
if (username === 'HACK') {
setCredentials(username, password, remember);
setRedirect(
next && next !== '/login/' ? next : formatPath(Paths.core.status),
);
e?.preventDefault();
return;
}

PulpLoginAPI.try(username, password)
.then(() => {
// verified, save
Expand Down
8 changes: 3 additions & 5 deletions src/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,14 @@ import { useUserContext } from './user-context';

export const StandaloneLayout = ({ children }: { children: ReactNode }) => {
const [aboutModalVisible, setAboutModalVisible] = useState<boolean>(false);
const { credentials, clearCredentials } = useUserContext();
const { getUsername, clearCredentials } = useUserContext();

const userName = getUsername();
let aboutModal = null;
let docsDropdownItems = [];
let userDropdownItems = [];
let userName: string;

if (credentials) {
userName = credentials.username;

if (userName) {
userDropdownItems = [
<DropdownItem isDisabled key='username'>
<Trans>Username: {userName}</Trans>
Expand Down
6 changes: 3 additions & 3 deletions src/menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const menuSection = (name, options = {}, items = []) => ({
const altPath = (p) => formatPath(p, {}, null, { ignoreMissing: true });

// condition: loggedIn OR condition: and(loggedIn, hasPlugin('rpm'))
const loggedIn = ({ user }) => !!user;
const loggedIn = ({ isLoggedIn }) => isLoggedIn();
const hasPlugin =
(name) =>
({ plugins }) =>
Expand Down Expand Up @@ -284,7 +284,7 @@ export const StandaloneMenu = () => {
const location = useLocation();
const [menu, setMenu] = useState([]);

const { credentials } = useUserContext();
const { isLoggedIn } = useUserContext();

const plugins = usePlugins();

Expand Down Expand Up @@ -312,7 +312,7 @@ export const StandaloneMenu = () => {
<NavList>
<Menu
items={menu}
context={{ user: credentials, plugins }}
context={{ isLoggedIn, plugins }}
expandedSections={expandedSections}
/>
</NavList>
Expand Down
24 changes: 19 additions & 5 deletions src/user-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ import React, {
} from 'react';

interface IUserContextType {
clearCredentials: () => void;
credentials: { username: string; password: string; remember: boolean };
getUsername: () => string;
hasPermission: (name: string) => boolean;
isLoggedIn: () => boolean;
setCredentials: (
username: string,
password: string,
remember?: boolean,
) => void;
clearCredentials: () => void;
updateUsername: (username: string) => void;
updatePassword: (password: string) => void;
updateUsername: (username: string) => void;
}

const UserContext = createContext<IUserContextType>(undefined);
Expand Down Expand Up @@ -47,19 +50,30 @@ export const UserContextProvider = ({ children }: { children: ReactNode }) => {
window.localStorage.removeItem('credentials');
window.sessionStorage.removeItem('credentials');
}

// if (!credentials) {
// setCredentials({ username: 'HACK' });
// }
}, [credentials]);

const getUsername = () => credentials?.username;
const isLoggedIn = () => !!credentials?.username;
const hasPermission = (_name) => true; // FIXME: permission handling

return (
<UserContext.Provider
value={{
clearCredentials: () => setCredentials(null),
credentials,
getUsername,
hasPermission,
isLoggedIn,
setCredentials: (username, password, remember = false) =>
setCredentials({ username, password, remember }),
clearCredentials: () => setCredentials(null),
updateUsername: (username) =>
setCredentials((credentials) => ({ ...credentials, username })),
updatePassword: (password) =>
setCredentials((credentials) => ({ ...credentials, password })),
updateUsername: (username) =>
setCredentials((credentials) => ({ ...credentials, username })),
}}
>
{children}
Expand Down

0 comments on commit 941a273

Please sign in to comment.