Http-Interceptor hinzugefügt.

This commit is contained in:
Developer 02 2024-06-24 11:56:49 +02:00
parent d376065246
commit 1c11a0e8f0
17 changed files with 193 additions and 358 deletions

View File

@ -45,8 +45,8 @@
"budgets": [ "budgets": [
{ {
"type": "initial", "type": "initial",
"maximumWarning": "500kb", "maximumWarning": "1.5mb",
"maximumError": "1mb" "maximumError": "2mb"
}, },
{ {
"type": "anyComponentStyle", "type": "anyComponentStyle",
@ -114,4 +114,4 @@
} }
} }
} }
} }

View File

@ -6,7 +6,8 @@ import { provideAnimationsAsync } from '@angular/platform-browser/animations/asy
import { APP_BASE_HREF } from '@angular/common'; import { APP_BASE_HREF } from '@angular/common';
import { UrlService } from './services/url.service'; import { UrlService } from './services/url.service';
import { API_URL } from './tokens/index' import { API_URL } from './tokens/index'
import { provideHttpClient, withFetch } from '@angular/common/http'; import { HTTP_INTERCEPTORS, provideHttpClient, withFetch } from '@angular/common/http';
import { HttpRequestInterceptor } from './http.interceptor';
export const appConfig: ApplicationConfig = { export const appConfig: ApplicationConfig = {
providers: [ providers: [
@ -23,6 +24,11 @@ export const appConfig: ApplicationConfig = {
provide: API_URL, provide: API_URL,
useFactory: (urlService: UrlService) => urlService.getApiUrl(), useFactory: (urlService: UrlService) => urlService.getApiUrl(),
deps: [UrlService] deps: [UrlService]
},
{
provide: HTTP_INTERCEPTORS,
useClass: HttpRequestInterceptor,
multi: true
} }
] ]
}; };

View File

@ -3,11 +3,13 @@ import { inject } from '@angular/core';
import { CanActivateFn, Router } from '@angular/router'; import { CanActivateFn, Router } from '@angular/router';
import { AuthService } from '../services/auth.service'; import { AuthService } from '../services/auth.service';
import { map } from 'rxjs/operators'; import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';
export const authGuard: CanActivateFn = (route, state) => { export const authGuard: CanActivateFn = (route, state) => {
const authService = inject(AuthService); const authService = inject(AuthService);
const router = inject(Router); const router = inject(Router);
authService.isAuthenticated().subscribe({next: res => console.log(res)})
return authService.isAuthenticated().pipe( return authService.isAuthenticated().pipe(
map(isAuthenticated => { map(isAuthenticated => {
if (!isAuthenticated) { if (!isAuthenticated) {

View File

@ -0,0 +1,17 @@
import { TestBed } from '@angular/core/testing';
import { HttpInterceptorFn } from '@angular/common/http';
import { httpInterceptor } from './http.interceptor';
describe('httpInterceptor', () => {
const interceptor: HttpInterceptorFn = (req, next) =>
TestBed.runInInjectionContext(() => httpInterceptor(req, next));
beforeEach(() => {
TestBed.configureTestingModule({});
});
it('should be created', () => {
expect(interceptor).toBeTruthy();
});
});

View File

@ -0,0 +1,21 @@
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable()
export class HttpRequestInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const secureReq = req.clone({
withCredentials: true,
setHeaders: {
'X-Insecure-Request': 'true',
'Content-Type': 'application/json',
...req.headers.keys().reduce((headers, key) => {
headers[key] = req.headers.get(key) || '';
return headers;
}, {} as { [name: string]: string })
}
});
return next.handle(secureReq);
}
}

View File

@ -9,16 +9,12 @@ import { API_URL } from '../tokens/index';
export class EnvelopeReceiverService { export class EnvelopeReceiverService {
private url: string; private url: string;
constructor(private http: HttpClient) { constructor(private http: HttpClient) {
const api_url = inject(API_URL); const api_url = inject(API_URL);
this.url = `${api_url}/envelopereceiver`; this.url = `${api_url}/envelopereceiver`;
} }
getEnvelopeReceiver(): Observable<any> { getEnvelopeReceiver(): Observable<any> {
const headers = new HttpHeaders({ return this.http.get<any>(this.url);
'Content-Type': 'application/json',
});
return this.http.get<any>(this.url, { withCredentials: true , headers });
} }
} }

View File

@ -64,6 +64,7 @@ namespace EnvelopeGenerator.GeneratorAPI.Controllers
{ {
IsPersistent = true, IsPersistent = true,
AllowRefresh = true, AllowRefresh = true,
ExpiresUtc = DateTime.Now.AddMinutes(180)
}; };
// Sign in // Sign in
@ -72,8 +73,6 @@ namespace EnvelopeGenerator.GeneratorAPI.Controllers
new ClaimsPrincipal(claimsIdentity), new ClaimsPrincipal(claimsIdentity),
authProperties); authProperties);
_dirSearchService.SetSearchRootCache(user.Username, login.Password);
return Ok(); return Ok();
} }
catch(Exception ex) catch(Exception ex)

View File

@ -44,7 +44,6 @@ builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationSc
options.Cookie.SameSite = SameSiteMode.Strict; // Protects against CSRF attacks by restricting how cookies are sent with requests from external sites options.Cookie.SameSite = SameSiteMode.Strict; // Protects against CSRF attacks by restricting how cookies are sent with requests from external sites
options.LoginPath = "/api/auth/login"; options.LoginPath = "/api/auth/login";
options.LogoutPath = "/api/auth/logout"; options.LogoutPath = "/api/auth/logout";
options.ExpireTimeSpan = TimeSpan.FromMinutes(60);
options.SlidingExpiration = true; options.SlidingExpiration = true;
}); });

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long