auth guards

This commit is contained in:
Kyle Spearrin 2018-06-09 22:02:45 -04:00
parent 9f1c7a0a32
commit 7ebd18b00d
8 changed files with 67 additions and 19 deletions

View File

@ -15,7 +15,7 @@
<span [hidden]="form.loading">{{'submit' | i18n}}</span> <span [hidden]="form.loading">{{'submit' | i18n}}</span>
<i class="fa fa-spinner fa-spin" [hidden]="!form.loading"></i> <i class="fa fa-spinner fa-spin" [hidden]="!form.loading"></i>
</button> </button>
<a routerLink="/login" class="btn btn-outline-secondary btn-block ml-2 mt-0"> <a routerLink="/" class="btn btn-outline-secondary btn-block ml-2 mt-0">
{{'cancel' | i18n}} {{'cancel' | i18n}}
</a> </a>
</div> </div>

View File

@ -1,4 +1,7 @@
import { Component } from '@angular/core'; import {
Component,
OnInit,
} from '@angular/core';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { ToasterService } from 'angular2-toaster'; import { ToasterService } from 'angular2-toaster';
@ -16,7 +19,7 @@ import { LockComponent as BaseLockComponent } from 'jslib/angular/components/loc
selector: 'app-lock', selector: 'app-lock',
templateUrl: 'lock.component.html', templateUrl: 'lock.component.html',
}) })
export class LockComponent extends BaseLockComponent { export class LockComponent extends BaseLockComponent implements OnInit {
constructor(router: Router, analytics: Angulartics2, constructor(router: Router, analytics: Angulartics2,
toasterService: ToasterService, i18nService: I18nService, toasterService: ToasterService, i18nService: I18nService,
platformUtilsService: PlatformUtilsService, messagingService: MessagingService, platformUtilsService: PlatformUtilsService, messagingService: MessagingService,
@ -24,4 +27,14 @@ export class LockComponent extends BaseLockComponent {
super(router, analytics, toasterService, i18nService, platformUtilsService, super(router, analytics, toasterService, i18nService, platformUtilsService,
messagingService, userService, cryptoService); messagingService, userService, cryptoService);
} }
async ngOnInit() {
const authed = await this.userService.isAuthenticated();
const key = await this.cryptoService.getKey();
if (!authed) {
this.router.navigate(['/']);
} else if (key != null) {
this.router.navigate(['vault']);
}
}
} }

View File

@ -41,7 +41,7 @@
<span [hidden]="form.loading">{{'submit' | i18n}}</span> <span [hidden]="form.loading">{{'submit' | i18n}}</span>
<i class="fa fa-spinner fa-spin" [hidden]="!form.loading"></i> <i class="fa fa-spinner fa-spin" [hidden]="!form.loading"></i>
</button> </button>
<a routerLink="/login" class="btn btn-outline-secondary btn-block ml-2 mt-0"> <a routerLink="/" class="btn btn-outline-secondary btn-block ml-2 mt-0">
{{'cancel' | i18n}} {{'cancel' | i18n}}
</a> </a>
</div> </div>

View File

@ -1,7 +1,7 @@
<form id="two-factor-page" #form (ngSubmit)="submit()" [appApiAction]="formPromise"> <form id="two-factor-page" #form (ngSubmit)="submit()" [appApiAction]="formPromise">
<header> <header>
<div class="left"> <div class="left">
<a routerLink="/login">{{'back' | i18n}}</a> <a routerLink="/">{{'back' | i18n}}</a>
</div> </div>
<div class="center"> <div class="center">
<span class="title">{{title}}</span> <span class="title">{{title}}</span>

View File

@ -16,31 +16,34 @@ import { TwoFactorComponent } from './accounts/two-factor.component';
import { VaultComponent } from './vault/vault.component'; import { VaultComponent } from './vault/vault.component';
import { UnauthGuardService } from './services/unauth-guard.service';
import { AuthGuardService } from 'jslib/angular/services/auth-guard.service';
const routes: Routes = [ const routes: Routes = [
{
path: '',
component: UserLayoutComponent,
children: [
{ path: '', redirectTo: 'vault', pathMatch: 'full' },
{ path: 'vault', component: VaultComponent },
],
},
{ {
path: '', path: '',
component: FrontendLayoutComponent, component: FrontendLayoutComponent,
children: [ children: [
{ path: 'login', component: LoginComponent }, { path: '', pathMatch: 'full', component: LoginComponent, canActivate: [UnauthGuardService] },
{ path: '2fa', component: TwoFactorComponent }, { path: '2fa', component: TwoFactorComponent, canActivate: [UnauthGuardService] },
{ path: 'register', component: RegisterComponent }, { path: 'register', component: RegisterComponent, canActivate: [UnauthGuardService] },
{ path: 'hint', component: HintComponent }, { path: 'hint', component: HintComponent, canActivate: [UnauthGuardService] },
{ path: 'lock', component: LockComponent }, { path: 'lock', component: LockComponent },
], ],
}, },
{
path: '',
component: UserLayoutComponent,
children: [
{ path: 'vault', component: VaultComponent, canActivate: [AuthGuardService] },
],
},
{ {
path: 'organization/:organizationId', path: 'organization/:organizationId',
component: OrganizationLayoutComponent, component: OrganizationLayoutComponent,
children: [ children: [
{ path: 'vault', component: VaultComponent }, { path: 'vault', component: VaultComponent, canActivate: [AuthGuardService] },
], ],
}, },
{ path: '**', redirectTo: '' }, { path: '**', redirectTo: '' },

View File

@ -143,7 +143,7 @@ export class AppComponent implements OnDestroy, OnInit {
this.toasterService.popAsync('warning', this.i18nService.t('loggedOut'), this.toasterService.popAsync('warning', this.i18nService.t('loggedOut'),
this.i18nService.t('loginExpired')); this.i18nService.t('loginExpired'));
} }
this.router.navigate(['login']); this.router.navigate(['/']);
}); });
} }

View File

@ -11,6 +11,8 @@ import { I18nService } from '../services/i18n.service';
import { MemoryStorageService } from '../services/memoryStorage.service'; import { MemoryStorageService } from '../services/memoryStorage.service';
import { WebPlatformUtilsService } from '../services/webPlatformUtils.service'; import { WebPlatformUtilsService } from '../services/webPlatformUtils.service';
import { UnauthGuardService } from './services/unauth-guard.service';
import { AuthGuardService } from 'jslib/angular/services/auth-guard.service'; import { AuthGuardService } from 'jslib/angular/services/auth-guard.service';
import { BroadcasterService } from 'jslib/angular/services/broadcaster.service'; import { BroadcasterService } from 'jslib/angular/services/broadcaster.service';
import { ValidationService } from 'jslib/angular/services/validation.service'; import { ValidationService } from 'jslib/angular/services/validation.service';
@ -135,6 +137,7 @@ export function initFactory(): Function {
providers: [ providers: [
ValidationService, ValidationService,
AuthGuardService, AuthGuardService,
UnauthGuardService,
{ provide: AuditServiceAbstraction, useValue: auditService }, { provide: AuditServiceAbstraction, useValue: auditService },
{ provide: AuthServiceAbstraction, useValue: authService }, { provide: AuthServiceAbstraction, useValue: authService },
{ provide: CipherServiceAbstraction, useValue: cipherService }, { provide: CipherServiceAbstraction, useValue: cipherService },

View File

@ -0,0 +1,29 @@
import { Injectable } from '@angular/core';
import {
CanActivate,
Router,
} from '@angular/router';
import { CryptoService } from 'jslib/abstractions/crypto.service';
import { UserService } from 'jslib/abstractions/user.service';
@Injectable()
export class UnauthGuardService implements CanActivate {
constructor(private cryptoService: CryptoService, private userService: UserService,
private router: Router) { }
async canActivate() {
const isAuthed = await this.userService.isAuthenticated();
if (isAuthed) {
const key = await this.cryptoService.getKey();
if (key == null) {
this.router.navigate(['lock']);
} else {
this.router.navigate(['vault']);
}
return false;
}
return true;
}
}