Blacked and isorted the python files
Prettied and eslinted the typescript/html files
This commit is contained in:
@ -1,26 +1,27 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
|
||||
|
||||
import { AuthService } from './auth.service';
|
||||
import { ToasterService } from '../core/toaster.service';
|
||||
|
||||
@Injectable({providedIn: 'root'})
|
||||
import { AuthService } from './auth.service';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class AuthGuard implements CanActivate {
|
||||
constructor(
|
||||
private router: Router,
|
||||
private authService: AuthService,
|
||||
private toaster: ToasterService
|
||||
) {
|
||||
}
|
||||
private toaster: ToasterService,
|
||||
) {}
|
||||
|
||||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
|
||||
const user = this.authService.user;
|
||||
const permission = (route.data['permission'] === undefined) ? route.data['permission'] : route.data['permission']
|
||||
.replace(/ /g, '-')
|
||||
.toLowerCase();
|
||||
const { user } = this.authService;
|
||||
const permission =
|
||||
route.data.permission === undefined
|
||||
? route.data.permission
|
||||
: route.data.permission.replace(/ /g, '-').toLowerCase();
|
||||
if (!user) {
|
||||
// not logged in so redirect to login page with the return url
|
||||
this.router.navigate(['/login'], {queryParams: {returnUrl: state.url}});
|
||||
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url } });
|
||||
return false;
|
||||
}
|
||||
if (permission !== undefined && user.perms.indexOf(permission) === -1) {
|
||||
@ -29,6 +30,5 @@ export class AuthGuard implements CanActivate {
|
||||
}
|
||||
// logged in so return true
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,16 +1,16 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { BehaviorSubject, Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
|
||||
import { User } from '../core/user';
|
||||
import { environment } from '../../environments/environment';
|
||||
import { User } from '../core/user';
|
||||
|
||||
const loginUrl = '/token';
|
||||
const refreshUrl = '/refresh';
|
||||
const JWT_USER = 'JWT_USER';
|
||||
|
||||
@Injectable({providedIn: 'root'})
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class AuthService {
|
||||
private currentUserSubject: BehaviorSubject<User>;
|
||||
public currentUser: Observable<User>;
|
||||
@ -20,16 +20,37 @@ export class AuthService {
|
||||
this.currentUser = this.currentUserSubject.asObservable();
|
||||
}
|
||||
|
||||
static parseJwt(token): User {
|
||||
const base64Url = token.split('.')[1];
|
||||
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
|
||||
const jsonPayload = decodeURIComponent(
|
||||
atob(base64)
|
||||
.split('')
|
||||
.map((c) => `%${`00${c.charCodeAt(0).toString(16)}`.slice(-2)}`)
|
||||
.join(''),
|
||||
);
|
||||
|
||||
const decoded = JSON.parse(jsonPayload);
|
||||
return new User({
|
||||
id: decoded.userId,
|
||||
name: decoded.sub,
|
||||
lockedOut: decoded.lockedOut,
|
||||
perms: decoded.scopes,
|
||||
access_token: token,
|
||||
exp: decoded.exp,
|
||||
ver: decoded.ver,
|
||||
});
|
||||
}
|
||||
|
||||
checkStorage(): User {
|
||||
const existingToken: User = JSON.parse(localStorage.getItem(JWT_USER));
|
||||
if (existingToken === null || Date.now() > existingToken.exp * 1000) {
|
||||
localStorage.removeItem(JWT_USER);
|
||||
this.currentUserSubject = new BehaviorSubject<User>(null);
|
||||
return null;
|
||||
} else {
|
||||
this.currentUserSubject = new BehaviorSubject<User>(existingToken);
|
||||
return existingToken;
|
||||
}
|
||||
this.currentUserSubject = new BehaviorSubject<User>(existingToken);
|
||||
return existingToken;
|
||||
}
|
||||
|
||||
public get user(): User {
|
||||
@ -57,37 +78,22 @@ export class AuthService {
|
||||
formData.append('password', password);
|
||||
formData.append('otp', otp);
|
||||
formData.append('grant_type', 'password');
|
||||
return this.http.post<any>(loginUrl, formData)
|
||||
.pipe(map(u => u.access_token))
|
||||
.pipe(map(u => this.parseJwt(u)))
|
||||
.pipe(map(user => {
|
||||
// store user details and jwt token in local storage to keep user logged in between page refreshes
|
||||
localStorage.setItem(JWT_USER, JSON.stringify(user));
|
||||
this.currentUserSubject.next(user);
|
||||
return user;
|
||||
}));
|
||||
}
|
||||
|
||||
parseJwt(token): User {
|
||||
const base64Url = token.split('.')[1];
|
||||
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
|
||||
const jsonPayload = decodeURIComponent(atob(base64).split('').map(function (c) {
|
||||
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
|
||||
}).join(''));
|
||||
|
||||
const decoded = JSON.parse(jsonPayload);
|
||||
return new User({
|
||||
id: decoded.userId,
|
||||
name: decoded.sub,
|
||||
lockedOut: decoded.lockedOut,
|
||||
perms: decoded.scopes,
|
||||
access_token: token,
|
||||
exp: decoded.exp
|
||||
});
|
||||
return this.http
|
||||
.post<any>(loginUrl, formData)
|
||||
.pipe(map((u) => u.access_token))
|
||||
.pipe(map((u) => AuthService.parseJwt(u)))
|
||||
.pipe(
|
||||
map((user) => {
|
||||
// store user details and jwt token in local storage to keep user logged in between page refreshes
|
||||
localStorage.setItem(JWT_USER, JSON.stringify(user));
|
||||
this.currentUserSubject.next(user);
|
||||
return user;
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
needsRefreshing(): boolean {
|
||||
return Date.now() > (this.user.exp - (environment.ACCESS_TOKEN_REFRESH_MINUTES * 60)) * 1000;
|
||||
return Date.now() > (this.user.exp - environment.ACCESS_TOKEN_REFRESH_MINUTES * 60) * 1000;
|
||||
}
|
||||
|
||||
logout() {
|
||||
@ -97,14 +103,17 @@ export class AuthService {
|
||||
}
|
||||
|
||||
refreshToken() {
|
||||
return this.http.post<any>(refreshUrl, {})
|
||||
.pipe(map(u => u.access_token))
|
||||
.pipe(map(u => this.parseJwt(u)))
|
||||
.pipe(map(user => {
|
||||
// store user details and jwt token in local storage to keep user logged in between page refreshes
|
||||
localStorage.setItem(JWT_USER, JSON.stringify(user));
|
||||
this.currentUserSubject.next(user);
|
||||
return user;
|
||||
}));
|
||||
return this.http
|
||||
.post<any>(refreshUrl, {})
|
||||
.pipe(map((u) => u.access_token))
|
||||
.pipe(map((u) => AuthService.parseJwt(u)))
|
||||
.pipe(
|
||||
map((user) => {
|
||||
// store user details and jwt token in local storage to keep user logged in between page refreshes
|
||||
localStorage.setItem(JWT_USER, JSON.stringify(user));
|
||||
this.currentUserSubject.next(user);
|
||||
return user;
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,29 +7,48 @@
|
||||
<div fxLayout="row" fxLayout.lt-md="column" fxLayoutGap="20px" fxLayoutGap.lt-md="0px">
|
||||
<mat-form-field fxFlex>
|
||||
<mat-label>Username</mat-label>
|
||||
<input matInput #nameElement placeholder="Username" formControlName="username">
|
||||
<input matInput #nameElement placeholder="Username" formControlName="username" />
|
||||
</mat-form-field>
|
||||
</div>
|
||||
<div fxLayout="row" fxLayout.lt-md="column" fxLayoutGap="20px" fxLayoutGap.lt-md="0px">
|
||||
<mat-form-field fxFlex>
|
||||
<mat-label>Password</mat-label>
|
||||
<input matInput placeholder="Password" formControlName="password" [type]="hide ? 'password' : 'text'"
|
||||
(keyup.enter)="login()">
|
||||
<mat-icon matSuffix (click)="hide = !hide">{{hide ? 'visibility' : 'visibility_off'}}</mat-icon>
|
||||
<input
|
||||
matInput
|
||||
placeholder="Password"
|
||||
formControlName="password"
|
||||
[type]="hide ? 'password' : 'text'"
|
||||
(keyup.enter)="login()"
|
||||
/>
|
||||
<mat-icon matSuffix (click)="hide = !hide">{{
|
||||
hide ? 'visibility' : 'visibility_off'
|
||||
}}</mat-icon>
|
||||
</mat-form-field>
|
||||
</div>
|
||||
<mat-divider></mat-divider>
|
||||
<h2 *ngIf="showOtp">Client ID: {{clientId}}</h2>
|
||||
<div fxLayout="row" fxLayout.lt-md="column" fxLayoutGap="20px" fxLayoutGap.lt-md="0px" *ngIf="showOtp">
|
||||
<h2 *ngIf="showOtp">Client ID: {{ clientId }}</h2>
|
||||
<div
|
||||
fxLayout="row"
|
||||
fxLayout.lt-md="column"
|
||||
fxLayoutGap="20px"
|
||||
fxLayoutGap.lt-md="0px"
|
||||
*ngIf="showOtp"
|
||||
>
|
||||
<mat-form-field fxFlex>
|
||||
<mat-label>Otp</mat-label>
|
||||
<input matInput placeholder="Otp" formControlName="otp">
|
||||
<input matInput placeholder="Otp" formControlName="otp" />
|
||||
</mat-form-field>
|
||||
</div>
|
||||
<div fxLayout="row" fxLayout.lt-md="column" fxLayoutGap="20px" fxLayoutGap.lt-md="0px" *ngIf="showOtp">
|
||||
<div
|
||||
fxLayout="row"
|
||||
fxLayout.lt-md="column"
|
||||
fxLayoutGap="20px"
|
||||
fxLayoutGap.lt-md="0px"
|
||||
*ngIf="showOtp"
|
||||
>
|
||||
<mat-form-field fxFlex>
|
||||
<mat-label>Client Name</mat-label>
|
||||
<input matInput placeholder="Client Name" formControlName="clientName">
|
||||
<input matInput placeholder="Client Name" formControlName="clientName" />
|
||||
</mat-form-field>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
@ -1,14 +1,15 @@
|
||||
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
|
||||
import { AuthService } from '../auth.service';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import { ToasterService } from '../../core/toaster.service';
|
||||
import { FormBuilder, FormGroup } from '@angular/forms';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
|
||||
import { ToasterService } from '../../core/toaster.service';
|
||||
import { CookieService } from '../../shared/cookie.service';
|
||||
import { AuthService } from '../auth.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-login',
|
||||
templateUrl: './login.component.html',
|
||||
styleUrls: ['./login.component.css']
|
||||
styleUrls: ['./login.component.css'],
|
||||
})
|
||||
export class LoginComponent implements OnInit, AfterViewInit {
|
||||
@ViewChild('nameElement', { static: true }) nameElement: ElementRef;
|
||||
@ -18,12 +19,13 @@ export class LoginComponent implements OnInit, AfterViewInit {
|
||||
clientId: string;
|
||||
private returnUrl: string;
|
||||
|
||||
constructor(private route: ActivatedRoute,
|
||||
private auth: AuthService,
|
||||
private router: Router,
|
||||
private toaster: ToasterService,
|
||||
private cs: CookieService,
|
||||
private fb: FormBuilder
|
||||
constructor(
|
||||
private route: ActivatedRoute,
|
||||
private auth: AuthService,
|
||||
private router: Router,
|
||||
private toaster: ToasterService,
|
||||
private cs: CookieService,
|
||||
private fb: FormBuilder,
|
||||
) {
|
||||
this.hide = true;
|
||||
this.showOtp = false;
|
||||
@ -34,12 +36,12 @@ export class LoginComponent implements OnInit, AfterViewInit {
|
||||
this.form = this.fb.group({
|
||||
username: '',
|
||||
password: '',
|
||||
otp: ''
|
||||
otp: '',
|
||||
});
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
|
||||
this.returnUrl = this.route.snapshot.queryParams.returnUrl || '/';
|
||||
}
|
||||
|
||||
ngAfterViewInit() {
|
||||
@ -50,22 +52,23 @@ export class LoginComponent implements OnInit, AfterViewInit {
|
||||
|
||||
login(): void {
|
||||
const formModel = this.form.value;
|
||||
const username = formModel.username;
|
||||
const password = formModel.password;
|
||||
const otp = formModel.otp;
|
||||
this.auth.login(username, password, otp)
|
||||
const { username } = formModel;
|
||||
const { password } = formModel;
|
||||
const { otp } = formModel;
|
||||
this.auth
|
||||
.login(username, password, otp)
|
||||
// .pipe(first())
|
||||
.subscribe(
|
||||
data => {
|
||||
() => {
|
||||
this.router.navigate([this.returnUrl]);
|
||||
},
|
||||
(error) => {
|
||||
if (error.status === 401 && 'Client is not registered' === error.error.detail) {
|
||||
if (error.status === 401 && error.error.detail === 'Client is not registered') {
|
||||
this.showOtp = true;
|
||||
this.clientId = this.cs.getCookie('client_id');
|
||||
}
|
||||
this.toaster.show('Danger', error.error.details);
|
||||
}
|
||||
)
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,19 +1,15 @@
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { Router } from '@angular/router';
|
||||
|
||||
import { AuthService } from '../auth.service';
|
||||
import { ToasterService } from '../../core/toaster.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-logout',
|
||||
template: ''
|
||||
template: '',
|
||||
})
|
||||
export class LogoutComponent implements OnInit {
|
||||
|
||||
constructor(private auth: AuthService) {
|
||||
}
|
||||
constructor(private auth: AuthService) {}
|
||||
|
||||
ngOnInit() {
|
||||
this.auth.logout();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user