-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracted the authentication portion of the authenticated middleware …
…into a navigation guard
- Loading branch information
1 parent
bc74f41
commit b81a1f8
Showing
8 changed files
with
84 additions
and
61 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
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,63 @@ | ||
import { routeRequiresAuthentication } from '@shell/utils/router'; | ||
import { isLoggedIn, notLoggedIn, noAuth, findMe } from '@shell/utils/auth'; | ||
|
||
export function install(router, context) { | ||
router.beforeEach((to, from, next) => authenticate(to, from, next, context)); | ||
} | ||
|
||
export async function authenticate(to, from, next, { store }) { | ||
if (!routeRequiresAuthentication(to)) { | ||
return next(); | ||
} | ||
|
||
if ( store.getters['auth/enabled'] !== false && !store.getters['auth/loggedIn'] ) { | ||
// `await` so we have one successfully request whilst possibly logged in (ensures fromHeader is populated from `x-api-cattle-auth`) | ||
await store.dispatch('auth/getUser'); | ||
|
||
const v3User = store.getters['auth/v3User'] || {}; | ||
|
||
if (v3User?.mustChangePassword) { | ||
return next({ name: 'auth-setup' }); | ||
} | ||
|
||
// In newer versions the API calls return the auth state instead of having to make a new call all the time. | ||
const fromHeader = store.getters['auth/fromHeader']; | ||
|
||
if ( fromHeader === 'none' ) { | ||
noAuth(store); | ||
} else if ( fromHeader === 'true' ) { | ||
const me = await findMe(store); | ||
|
||
isLoggedIn(store, me); | ||
} else if ( fromHeader === 'false' ) { | ||
notLoggedIn(store, next, to); | ||
|
||
return; | ||
} else { | ||
// Older versions look at principals and see what happens | ||
try { | ||
const me = await findMe(store); | ||
|
||
isLoggedIn(store, me); | ||
} catch (e) { | ||
const status = e?._status; | ||
|
||
if ( status === 404 ) { | ||
noAuth(store); | ||
} else { | ||
if ( status === 401 ) { | ||
notLoggedIn(store, next, to); | ||
} else { | ||
store.commit('setError', { error: e, locationError: new Error('Auth Middleware') }); | ||
} | ||
|
||
return; | ||
} | ||
} | ||
} | ||
|
||
store.dispatch('gcStartIntervals'); | ||
} | ||
|
||
next(); | ||
} |
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
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