Skip to content

Commit

Permalink
Merge pull request #418 from edissyum/2.x
Browse files Browse the repository at this point in the history
2.x
  • Loading branch information
nathan30 authored Dec 9, 2022
2 parents e8ec105 + 10394a6 commit b2113f4
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 109 deletions.
2 changes: 1 addition & 1 deletion dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@
</div>
</div>
</app-root>
<script src="runtime.8f470fc5e7153b88.js" type="module"></script><script src="polyfills.f8c24ea94fd51cd4.js" type="module"></script><script src="scripts.3d24c03247b0b686.js" defer></script><script src="main.1f0c1e65d32816d2.js" type="module"></script>
<script src="runtime.8f470fc5e7153b88.js" type="module"></script><script src="polyfills.f8c24ea94fd51cd4.js" type="module"></script><script src="scripts.3d24c03247b0b686.js" defer></script><script src="main.15c651052774a237.js" type="module"></script>
</body></html>
1 change: 1 addition & 0 deletions dist/main.15c651052774a237.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion dist/main.1f0c1e65d32816d2.js

This file was deleted.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@
"@types/d3-scale": "^4.0.2",
"@types/d3-selection": "^3.0.3",
"@types/d3-shape": "^3.1.0",
"@types/node": "^18.11.11",
"@typescript-eslint/eslint-plugin": "5.45.1",
"@typescript-eslint/parser": "5.45.1",
"@types/node": "^18.11.12",
"@typescript-eslint/eslint-plugin": "5.46.0",
"@typescript-eslint/parser": "5.46.0",
"autoprefixer": "^10.4.13",
"eslint": "^8.29.0",
"eslint-plugin-import": "latest",
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/app/login/login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class LoginComponent implements OnInit {
).pipe(
tap((data: any) => {
this.userService.setUser(data.body.user);
this.authService.setTokens(data.body.auth_token, btoa(JSON.stringify(this.userService.getUser())), data.body.minutes_before_exp);
this.authService.setTokens(data.body.auth_token, btoa(JSON.stringify(this.userService.getUser())));
this.localStorageService.save('task_watcher_minimize_display', 'true');
this.authService.generateHeaders();
this.notify.success(this.translate.instant('AUTH.authenticated'));
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/app/middleware.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class MiddlewareComponent implements HttpInterceptor {
if (environment.production) {
environment['url'] = '../' + environment['url'];
}
const token = this.localStorage.getCookie('OpenCaptureToken_' + customId);
const token = this.localStorage.get('OpenCaptureToken_' + customId);
if (currentCustom && customId !== currentCustom) {
this.router.navigate(['/logout']).then();
}
Expand Down
41 changes: 21 additions & 20 deletions src/frontend/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,61 +64,62 @@ export class AuthService {
return this.localStorage.remove(tokenNames['cachedUrlName']);
}

setTokenCustom(name: string, token: string) {
this.localStorage.save(name, token);
setTokenConfig(config: any) {
const tokenNames = this.getTokenName();
this.localStorage.save(tokenNames['configName'], config);
}

getTokenCustom(name: string) {
return this.localStorage.get(name);
getTokenConfig() {
const tokenNames = this.getTokenName();
return this.localStorage.get(tokenNames['configName']);
}

getTokenName() {
let tokenName = 'OpenCaptureToken';
let userTokenName = 'OpenCaptureToken_user';
let userDataName = 'OpenCaptureUserData';
let cachedUrlName = 'OpenCaptureCachedUrl';
let configName = 'OpenCaptureConfig';
if (environment['customId']) {
tokenName += '_' + environment['customId'];
userTokenName += '_' + environment['customId'];
userDataName += '_' + environment['customId'];
cachedUrlName += '_' + environment['customId'];
configName += '_' + environment['customId'];
} else if (environment['fqdn']) {
tokenName += '_' + environment['fqdn'];
userTokenName += '_' + environment['fqdn'];
userDataName += '_' + environment['fqdn'];
cachedUrlName += '_' + environment['fqdn'];
configName += '_' + environment['fqdn'];
}

return {
'tokenJwt': tokenName,
'userToken': userTokenName,
'configName': configName,
'userData': userDataName,
'cachedUrlName': cachedUrlName
};
}

setTokens(token: string, user_token: string, minutesBeforeExp: number) {
setTokens(token: string, user_token: string) {
const tokenNames = this.getTokenName();
this.localStorage.setCookie(tokenNames['tokenJwt'], token, minutesBeforeExp);
this.localStorage.setCookie(tokenNames['userToken'], user_token, minutesBeforeExp);
}

setTokenUser(user_token: string, minutesBeforeExp: number) {
const tokenNames = this.getTokenName();
this.localStorage.setCookie(tokenNames['userToken'], user_token, minutesBeforeExp);
this.localStorage.save(tokenNames['tokenJwt'], token);
this.localStorage.save(tokenNames['userData'], user_token);
}

getToken() {
const tokenNames = this.getTokenName();
return this.localStorage.getCookie(tokenNames['tokenJwt']);
return this.localStorage.get(tokenNames['tokenJwt']);
}

logout() {
const tokenNames = this.getTokenName();
this.userService.setUser({});
this.localStorage.remove('login_image_b64');
this.localStorage.remove('selectedSettings');
this.localStorage.remove(tokenNames['tokenJwt']);
this.localStorage.remove(tokenNames['userData']);
this.localStorage.remove('splitter_or_verifier');
this.localStorage.remove('selectedParentSettings');
this.localStorage.remove('selectedParentSettings');
this.localStorage.remove('task_watcher_minimize_display');
this.localStorage.deleteCookie(tokenNames['tokenJwt']);
this.localStorage.deleteCookie(tokenNames['userToken']);
this.http.get(environment['url'] + '/ws/auth/logout').pipe(
catchError((err: any) => {
console.debug(err);
Expand Down
12 changes: 6 additions & 6 deletions src/frontend/services/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
@dev : Nathan Cheval <[email protected]> */

import { Injectable } from '@angular/core';
import { of } from "rxjs";
import { environment } from "../app/env";
import { HttpClient } from "@angular/common/http";
import { NotificationService } from "./notifications/notifications.service";
import { Injectable } from '@angular/core';
import { AuthService } from "./auth.service";
import { catchError, tap } from "rxjs/operators";
import { of } from "rxjs";
import { HttpClient } from "@angular/common/http";
import { NotificationService } from "./notifications/notifications.service";

@Injectable({
providedIn: 'root'
Expand Down Expand Up @@ -55,10 +55,10 @@ export class ConfigService {
}

setConfig(config: any) {
this.authService.setTokenCustom('OpenCaptureConfig', btoa(JSON.stringify(config)));
this.authService.setTokenConfig(btoa(JSON.stringify(config)));
}

getConfig() {
return JSON.parse(atob(this.authService.getTokenCustom('OpenCaptureConfig') as string));
return JSON.parse(atob(this.authService.getTokenConfig() as string));
}
}
67 changes: 1 addition & 66 deletions src/frontend/services/local-storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,8 @@ import { Injectable } from '@angular/core';
providedIn: 'root'
})
export class LocalStorageService {
browser: string = '';

constructor() {
if (this.browser === '') {
this.browser = this.detectBrowserVersion();
}
}

detectBrowserVersion() {
let tem;
const userAgent = navigator.userAgent;
let matchTest = userAgent.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];

if(/trident/i.test(matchTest[1])) {
tem = /\brv[ :]+(\d+)/g.exec(userAgent) || [];
return 'IE '+(tem[1] || '');
}

if(matchTest[1]=== 'Chrome') {
tem = userAgent.match(/\b(OPR|Edge)\/(\d+)/);
if(tem!= null) return tem.slice(1).join(' ').replace('OPR', 'Opera');
}
matchTest= matchTest[2]? [matchTest[1]] : [navigator.appName];
if ((tem = userAgent.match(/version\/(\d+)/i)) != null) {
matchTest.splice(1, 1, tem[1]);
}
return matchTest.join(' ');
}
constructor() {}

save(id: string, content: any) {
localStorage.setItem(id, content);
Expand All @@ -61,43 +35,4 @@ export class LocalStorageService {
remove(id: string) {
localStorage.removeItem(id);
}

getCookie(cname: string) {
const name = cname + "=";
const decodedCookie = decodeURIComponent(document.cookie);
const ca = decodedCookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return "";
}

isValidFQDN(str: string) {
return /(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)/g.test(str);
}

setCookie(cname: string, cvalue: string, expMinutes: number) {
let sameSitePolicy = '';
if (this.browser.toLowerCase() === 'chrome' && this.isValidFQDN(window.location.host)) {
sameSitePolicy = 'SameSite=None;Secure';
}
const d = new Date();
if (expMinutes !== 0) {
d.setMinutes(d.getMinutes() + expMinutes);
const expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/;" + sameSitePolicy;
} else {
document.cookie = cname + "=" + cvalue + ";path=/;" + sameSitePolicy;
}
}

deleteCookie(cname: string) {
this.setCookie(cname, '', -1);
}
}
5 changes: 2 additions & 3 deletions src/frontend/services/login-required.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class LoginRequiredService implements CanActivate {
).pipe(
tap((data: any) => {
this.userService.setUser(data.body.user);
this.authService.setTokens(data.body.auth_token, btoa(JSON.stringify(this.userService.getUser())), data.body.minutes_before_exp);
this.authService.setTokens(data.body.auth_token, btoa(JSON.stringify(this.userService.getUser())));
if (!this.authService.headersExists) {
this.authService.generateHeaders();
}
Expand All @@ -91,7 +91,6 @@ export class LoginRequiredService implements CanActivate {

canActivate(): boolean {
const token = this.authService.getToken();
const tokenRouteStatic = this.authService.getTokenCustom('tokenRouteStatic');
let route = '';
if (!token) {
const params = new URLSearchParams(window.location.href);
Expand All @@ -115,7 +114,7 @@ export class LoginRequiredService implements CanActivate {
});
return false;
}
this.login(token, tokenRouteStatic);
this.login(token, null);
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
<i class="fa-solid fa-window-minimize align-top"></i>
</span>
</mat-card-title>
<div class="mx-4 h-20 overflow-y-auto bg-slate-100">
<div class="text-gray-400 my-4 text-center w-full overflow-hidden" *ngIf="tasks.length === 0 && !isFirstCallDone">
<div class="mx-4 h-20 bg-slate-100" [class.overflow-hidden]="tasks.length === 0 && !isFirstCallDone"
[class.overflow-y-auto]="tasks.length !== 0 && isFirstCallDone">
<div class="text-gray-400 my-4 text-center w-full" *ngIf="tasks.length === 0 && !isFirstCallDone">
<i class="fa-solid fa-circle-notch fa-spin fa-2x w-full text-gray-900" style="--fa-animation-duration: 1.5s;"></i>
<span class="block">{{ 'GLOBAL.loading' | translate }}</span>
</div>
<div class="text-gray-400 my-4 text-center w-full" *ngIf="tasks.length === 0 && !getTaskRunning && isFirstCallDone">
<i class="block fa-solid fa-check fa-2x"></i>
<i class="fa-solid fa-check fa-2x w-full text-gray-900 mt-4"></i>
<span class="block">{{ 'GLOBAL.no_task_to_show' | translate }}</span>
</div>
<div *ngFor="let task of tasks" class="h-8 w-full px-4 border-t-2 grid grid-cols-10"
Expand Down
8 changes: 4 additions & 4 deletions src/frontend/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class UserService {
}

getUserFromLocal() {
const token = this.getTokenUser();
const token = this.getUserData();
if (token) {
return JSON.parse(atob(token as string));
} else {
Expand All @@ -55,13 +55,13 @@ export class UserService {
}
}

getTokenUser() {
let userTokenName = 'OpenCaptureToken_user';
getUserData() {
let userTokenName = 'OpenCaptureUserData';
if (environment['customId']) {
userTokenName += '_' + environment['customId'];
} else if (environment['fqdn']) {
userTokenName += '_' + environment['fqdn'];
}
return this.localStorage.getCookie(userTokenName);
return this.localStorage.get(userTokenName);
}
}

0 comments on commit b2113f4

Please sign in to comment.