Skip to content

Commit

Permalink
Set current namespace when checking authz (#1205)
Browse files Browse the repository at this point in the history
* Revert "Revamp default namespace setup (#1194)"

This reverts commit a953b50.

* Ensure current namespace is set when authenticated

* Add missing break.
  • Loading branch information
absoludity authored Oct 8, 2019
1 parent 0df0951 commit 122f4aa
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 61 deletions.
8 changes: 0 additions & 8 deletions dashboard/src/actions/auth.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ describe("authenticate", () => {
payload: { authenticated: true, oidc: false, defaultNamespace: "default" },
type: getType(actions.auth.setAuthenticated),
},
{
payload: "default",
type: getType(actions.namespace.namespaceReceived),
},
];

return store.dispatch(actions.auth.authenticate(token, false)).then(() => {
Expand Down Expand Up @@ -112,10 +108,6 @@ describe("OIDC authentication", () => {
payload: { authenticated: true, oidc: true, defaultNamespace: "default" },
type: getType(actions.auth.setAuthenticated),
},
{
payload: "default",
type: getType(actions.namespace.namespaceReceived),
},
{
payload: { sessionExpired: false },
type: getType(actions.auth.setSessionExpired),
Expand Down
8 changes: 3 additions & 5 deletions dashboard/src/actions/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ActionType, createAction } from "typesafe-actions";

import { Auth } from "../shared/Auth";
import { IStoreState } from "../shared/types";
import { clearNamespaces, NamespaceAction, namespaceReceived } from "./namespace";
import { clearNamespaces, NamespaceAction } from "./namespace";

export const setAuthenticated = createAction("SET_AUTHENTICATED", resolve => {
return (authenticated: boolean, oidc: boolean, defaultNamespace: string) =>
Expand All @@ -27,15 +27,13 @@ export type AuthAction = ActionType<typeof allActions[number]>;
export function authenticate(
token: string,
oidc: boolean,
): ThunkAction<Promise<void>, IStoreState, null, AuthAction | NamespaceAction> {
): ThunkAction<Promise<void>, IStoreState, null, AuthAction> {
return async dispatch => {
dispatch(authenticating());
try {
await Auth.validateToken(token);
Auth.setAuthToken(token, oidc);
const defaultNamespace = Auth.defaultNamespaceFromToken(token);
dispatch(setAuthenticated(true, oidc, defaultNamespace));
dispatch(namespaceReceived(defaultNamespace));
dispatch(setAuthenticated(true, oidc, Auth.defaultNamespaceFromToken(token)));
if (oidc) {
dispatch(setSessionExpired(false));
}
Expand Down
12 changes: 1 addition & 11 deletions dashboard/src/actions/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,7 @@ export const errorNamespaces = createAction("ERROR_NAMESPACES", resolve => {

export const clearNamespaces = createAction("CLEAR_NAMESPACES");

export const namespaceReceived = createAction("NAMESPACE_RECEIVED", resolve => {
return (namespace: string) => resolve(namespace);
});

const allActions = [
setNamespace,
receiveNamespaces,
errorNamespaces,
clearNamespaces,
namespaceReceived,
];
const allActions = [setNamespace, receiveNamespaces, errorNamespaces, clearNamespaces];
export type NamespaceAction = ActionType<typeof allActions[number]>;

export function fetchNamespaces(): ThunkAction<Promise<void>, IStoreState, null, NamespaceAction> {
Expand Down
47 changes: 15 additions & 32 deletions dashboard/src/reducers/namespace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,44 +53,27 @@ describe("namespaceReducer", () => {
});

context("when CLEAR_NAMESPACES", () => {
it("returns to the default namespace", () => {
const clearedState = {
current: "",
namespaces: [],
it("returns to the initial state", () => {
const dirtyState = {
current: "some-other-namespace",
namespaces: ["namespace-one", "namespace-two"],
};
expect(
namespaceReducer(initialState, {
namespaceReducer(dirtyState, {
type: getType(actions.namespace.clearNamespaces),
}),
).toEqual(clearedState);
).toEqual({ current: "default", namespaces: [] });
});
});
});

context("when SET_DEFAULT_NAMESPACE", () => {
it("set current namespace if it's empty", () => {
const initialState = {
current: "",
namespaces: [],
};
expect(
namespaceReducer(initialState, {
type: getType(actions.namespace.namespaceReceived),
payload: "not-default",
}),
).toEqual({ ...initialState, current: "not-default" });
});

it("does not set the current namespace if it is not empty", () => {
const initialState = {
current: "default",
namespaces: [],
};
expect(
namespaceReducer(initialState, {
type: getType(actions.namespace.namespaceReceived),
payload: "not-default",
}),
).toEqual({ ...initialState, current: "default" });
context("when SET_AUTHENTICATED", () => {
it("sets the current namespace to the users default", () => {
expect(
namespaceReducer({ current: "default", namespaces: [] }, {
type: getType(actions.auth.setAuthenticated),
payload: { authenticated: true, oidc: false, defaultNamespace: "foo-bar" }
}),
).toEqual({ current: "foo-bar", namespaces: [] });
});
});
});
17 changes: 12 additions & 5 deletions dashboard/src/reducers/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { LOCATION_CHANGE, LocationChangeAction } from "connected-react-router";
import { getType } from "typesafe-actions";

import actions from "../actions";
import { AuthAction } from "../actions/auth";
import { NamespaceAction } from "../actions/namespace";
import { Auth } from "../shared/Auth";

export interface INamespaceState {
current: string;
Expand All @@ -11,16 +13,17 @@ export interface INamespaceState {
}

const getInitialState: () => INamespaceState = (): INamespaceState => {
const token = Auth.getAuthToken() || "";
return {
current: "",
current: Auth.defaultNamespaceFromToken(token),
namespaces: [],
};
};
const initialState: INamespaceState = getInitialState();

const namespaceReducer = (
state: INamespaceState = initialState,
action: NamespaceAction | LocationChangeAction,
action: NamespaceAction | LocationChangeAction | AuthAction,
): INamespaceState => {
switch (action.type) {
case getType(actions.namespace.receiveNamespaces):
Expand All @@ -31,16 +34,20 @@ const namespaceReducer = (
return { ...state, errorMsg: action.payload.err.message };
case getType(actions.namespace.clearNamespaces):
return { ...initialState };
case getType(actions.namespace.namespaceReceived):
const currentNamespace = state.current === "" ? action.payload : state.current;
return { ...initialState, current: currentNamespace };
case LOCATION_CHANGE:
const pathname = action.payload.location.pathname;
// looks for /ns/:namespace in URL
const matches = pathname.match(/\/ns\/([^/]*)/);
if (matches) {
return { ...state, current: matches[1] };
}
break;
case getType(actions.auth.setAuthenticated):
// Only when a user is authenticated to we set the current namespace from
// the auth default namespace.
if (action.payload.authenticated) {
return { ...state, current: action.payload.defaultNamespace };
}
default:
}
return state;
Expand Down

0 comments on commit 122f4aa

Please sign in to comment.