-
Notifications
You must be signed in to change notification settings - Fork 266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplify Authenticated Middleware #11096
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
import { Popup, popupWindowOptions } from '@shell/utils/window'; | ||
import { parse as parseUrl, addParam } from '@shell/utils/url'; | ||
import { BACK_TO, SPA, _EDIT, _FLAGGED } from '@shell/config/query-params'; | ||
import { MANAGEMENT } from '@shell/config/types'; | ||
import { | ||
BACK_TO, SPA, _EDIT, _FLAGGED, TIMED_OUT | ||
} from '@shell/config/query-params'; | ||
import { MANAGEMENT, NORMAN } from '@shell/config/types'; | ||
import { allHash } from '@shell/utils/promise'; | ||
import { getProductFromRoute, getResourceFromRoute } from '@shell/utils/router'; | ||
import { NAME as EXPLORER } from '@shell/config/product/explorer'; | ||
import { findBy } from '@shell/utils/array'; | ||
|
||
export function openAuthPopup(url, provider) { | ||
const popup = new Popup(() => { | ||
|
@@ -161,3 +166,146 @@ export const canViewResource = (store, resource) => { | |
|
||
return !!validResource; | ||
}; | ||
|
||
// ************************************************************ | ||
// | ||
// BELOW ARE METHODS THAT ARE A PART OF THE AUTHENTICATED MIDDLEWARE REMOVAL. THIS IS A TEMPORARY HOME FOR THESE UTILS AND SHOULD BE REWRITTEN, MOVED OR DELETED. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for mentioning that this is the first clean round. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated the code and added an issue #11111 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks and nice number 😅 |
||
// | ||
// TODO: Remove and refactor everything below for more clarity and better organization. https://github.com/rancher/dashboard/issues/11111 | ||
// | ||
// ************************************************************ | ||
|
||
/** | ||
* Attempt to set the product in our datastore if the route matches a known product. Otherwise show an error page instead. | ||
*/ | ||
export function setProduct(store, to) { | ||
let product = getProductFromRoute(to); | ||
|
||
// since all products are hardcoded as routes (ex: c-local-explorer), if we match the wildcard route it means that the product does not exist | ||
if ((product && (!to.matched.length || (to.matched.length && to.matched[0].path === '/c/:cluster/:product'))) || | ||
// if the product grabbed from the route is not registered, then we don't have it! | ||
(product && !store.getters['type-map/isProductRegistered'](product))) { | ||
const error = new Error(store.getters['i18n/t']('nav.failWhale.productNotFound', { productNotFound: product }, true)); | ||
|
||
store.dispatch('loadingError', error); | ||
|
||
throw new Error('loadingError', new Error(store.getters['i18n/t']('nav.failWhale.productNotFound', { productNotFound: product }, true))); | ||
} | ||
|
||
if ( !product ) { | ||
product = EXPLORER; | ||
} | ||
|
||
const oldProduct = store.getters['productId']; | ||
const oldStore = store.getters['currentProduct']?.inStore; | ||
|
||
if ( product !== oldProduct ) { | ||
store.commit('setProduct', product); | ||
} | ||
|
||
const neuStore = store.getters['currentProduct']?.inStore; | ||
|
||
if ( neuStore !== oldStore ) { | ||
// If the product store changes, clear the catalog. | ||
// There might be management catalog items in it vs cluster. | ||
store.commit('catalog/reset'); | ||
} | ||
} | ||
|
||
/** | ||
* Check that the resource is valid, if not redirect to fail whale | ||
* | ||
* This requires that | ||
* - product is set | ||
* - product's store is set and setup (so we can check schema's within it) | ||
* - product's store has the schemaFor getter (extension stores might not have it) | ||
* - there's a resource associated with route (meta or param) | ||
*/ | ||
export function validateResource(store, to) { | ||
const product = store.getters['currentProduct']; | ||
const resource = getResourceFromRoute(to); | ||
|
||
// In order to check a resource is valid we need these | ||
if (!product || !resource) { | ||
return false; | ||
} | ||
|
||
if (canViewResource(store, resource)) { | ||
return false; | ||
} | ||
|
||
// Unknown resource, redirect to fail whale | ||
|
||
const error = new Error(store.getters['i18n/t']('nav.failWhale.resourceNotFound', { resource }, true)); | ||
|
||
store.dispatch('loadingError', error); | ||
|
||
throw error; | ||
} | ||
|
||
/** | ||
* Attempt to load the current user's principal | ||
*/ | ||
export async function findMe(store) { | ||
// First thing we do in loadManagement is fetch principals anyway.... so don't ?me=true here | ||
const principals = await store.dispatch('rancher/findAll', { | ||
type: NORMAN.PRINCIPAL, | ||
opt: { | ||
url: '/v3/principals', | ||
redirectUnauthorized: false, | ||
} | ||
}); | ||
|
||
const me = findBy(principals, 'me', true); | ||
|
||
return me; | ||
} | ||
|
||
/** | ||
* Attempt to login with default credentials. Note: I think that this may actually be outdated since we don't use these default credentials anymore on setup. | ||
*/ | ||
export async function tryInitialSetup(store, password = 'admin') { | ||
try { | ||
const res = await store.dispatch('auth/login', { | ||
provider: 'local', | ||
body: { | ||
username: 'admin', | ||
password | ||
}, | ||
}); | ||
|
||
return res._status === 200; | ||
} catch (e) { | ||
console.error('Error trying initial setup', e); // eslint-disable-line no-console | ||
|
||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Record in our state management that we're indeed logged in | ||
*/ | ||
export function isLoggedIn(store, me) { | ||
store.commit('auth/hasAuth', true); | ||
store.commit('auth/loggedInAs', me.id); | ||
} | ||
|
||
/** | ||
* Record in our state management that we're not logged in and then redirect to the login page | ||
*/ | ||
export function notLoggedIn(store, redirect, route) { | ||
store.commit('auth/hasAuth', true); | ||
|
||
if ( route.name === 'index' ) { | ||
return redirect(302, '/auth/login'); | ||
} else { | ||
return redirect(302, `/auth/login?${ TIMED_OUT }`); | ||
} | ||
} | ||
|
||
/** | ||
* Record in our state management that we don't have any auth providers | ||
*/ | ||
export function noAuth(store) { | ||
store.commit('auth/hasAuth', false); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for asking, but I got completely lost from our prior logic and I'm glad you simplified 😅
Is this redirect not necessary anymore, though? I could not find any matching redirect to
'/fail-whale'
so I just wanted to be sure that was not a mistake.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the
loadingError
action actually does the redirect.dashboard/store/index.js
Line 578 in d20719b