Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it(`should redirect to ${unauthRedirectRoute} if user is null`, async () => {
// arrange
(userManager.getUser).returns(null);
// act
await authorizationStep.run(navigationInstruction, next);
// assert
sinon.assert.calledWith(next.cancel, new Redirect(unauthRedirectRoute));
});
it(`redirect should be to the unauthorized route with original URL as LoginRedirect parameter`, async () => {
// arrange
(userManager.getUser).returns(null);
// act
await authorizationStep.run(navigationInstruction, next);
// assert
sinon.assert.calledWith(next.cancel, new Redirect(expectedRedirect));
});
canActivate() {
// Don't show as router-view if sidebar is not hidden
return window.innerWidth < config.SCREEN_SMALL_BREAKPOINT_PX || new Redirect('');
}
// TODO: Make this open for extension,
// so that user-land can configure multiple, arbitrary roles.
if (this.requiresRole(navigationInstruction, OpenIdConnectRoles.Authenticated)) {
if (user === null || user.expired) {
this.logger.debug('Requires authenticated role.');
// capture the URL to which the user was originally navigating
// include that URL in a query string parameter on the redirect
let loginRedirect = navigationInstruction.fragment;
if (navigationInstruction.queryString && navigationInstruction.queryString.length) {
loginRedirect += `?${navigationInstruction.queryString}`;
}
const loginRedirectValue = encodeURIComponent(loginRedirect);
const queryString = `?${LoginRedirectKey}=${loginRedirectValue}`;
const redirect = new Redirect(this.configuration.unauthorizedRedirectRoute + queryString);
return next.cancel(redirect);
}
}
return next();
}
run(routingContext, next) {
// Check if the route has an "auth" key
// The reason for using `nextInstructions` is because this includes child routes.
if (routingContext.getAllInstructions()
.some(i => i.config.auth)) {
if (!this.appState.Authenticated) {
this.logger.warn('Not authenticated');
let url = routingContext.router.generate('login', { status: 401 });
this.appState.Authenticated = false;
this.appState.session('AppCurrentRoute', [routingContext.config.route, routingContext.params]);
this.appState.session('AppCurrentFragment', routingContext.fragment);
return next.reject(new Redirect(url));
}
}
return next();
}
}
run(routingContext, next) {
// Check if the route has an "auth" key
// The reason for using `nextInstructions` is because this includes child routes.
if (routingContext.getAllInstructions()
.some(i => i.config.auth)) {
if (!this.appState.Authenticated) {
this.logger.warn('Not authenticated');
let url = routingContext.router.generate('login', { status: 401 });
this.appState.Authenticated = false;
this.appState.session('AppCurrentRoute', [routingContext.config.route, routingContext.params]);
this.appState.session('AppCurrentFragment', routingContext.fragment);
return next.reject(new Redirect(url));
}
}
return next();
}
}
run(routingContext, next) {
// Check if the route has an "auth" key
// The reason for using `nextInstructions` is because this includes child routes.
if (routingContext.getAllInstructions()
.some(i => i.config.auth)) {
if (!this.appState.IsAuthenticated) {
this.logger.warn('Not authenticated');
let url = routingContext.router.generate('login', { status: 401 });
this.appState.IsAuthenticated = false;
this.appState.session('AppCurrentRoute', [routingContext.config.route, routingContext.params]);
this.appState.session('AppCurrentFragment', routingContext.fragment);
return next.reject(new Redirect(url));
}
}
return next();
}
}
resolve(false);
});
}
//#endregion
});
const result: boolean = await _modal;
// TODO: Extract Business Rule
if (result === false) {
/*
* As suggested in https://github.com/aurelia/router/issues/302, we use
* the router directly to navigate back, which results in staying on this
* component-- and this is the desired behaviour.
*/
return new Redirect(this._router.currentInstruction.fragment, {trigger: false, replace: false});
}
}
run(routingContext, next) {
var isLoggedIn = this.auth.isAuthenticated();
var loginRoute = this.auth.getLoginRoute();
if (routingContext.getAllInstructions().some(i => i.config.auth)) {
if (!isLoggedIn) {
this.auth.setInitialUrl(window.location.href);
return next.cancel(new Redirect(loginRoute));
}
} else if (isLoggedIn && routingContext.getAllInstructions().some(i => i.fragment == loginRoute)) {
var loginRedirect = this.auth.getLoginRedirect();
return next.cancel(new Redirect(loginRedirect));
}
return next();
}
}
run(routingContext, next) {
var isLoggedIn = this.auth.isAuthenticated();
var loginRoute = this.auth.getLoginRoute();
if (routingContext.getAllInstructions().some(i => i.config.auth)) {
if (!isLoggedIn) {
this.auth.setInitialUrl(window.location.href);
return next.cancel(new Redirect(loginRoute));
}
} else if (isLoggedIn && routingContext.getAllInstructions().some(i => i.fragment == loginRoute)) {
var loginRedirect = this.auth.getLoginRedirect();
return next.cancel(new Redirect(loginRedirect));
}
return next();
}
}