From 57d5b5f9f01a6e84bbdfff03ad499ad14ebfc8db Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Wed, 30 Sep 2026 16:25:22 +0000 Subject: [PATCH] COG-1160: add XSRF-TOKEN header helper and bootstrap cookie before AdminPortal login Co-Authored-By: patrick.bradley --- AdminPortal/src/app/login.service.ts | 22 +++++++++++++++++----- AdminPortal/src/app/xsrf.ts | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 AdminPortal/src/app/xsrf.ts diff --git a/AdminPortal/src/app/login.service.ts b/AdminPortal/src/app/login.service.ts index a229f8f0..72d98a83 100644 --- a/AdminPortal/src/app/login.service.ts +++ b/AdminPortal/src/app/login.service.ts @@ -1,6 +1,10 @@ import { Injectable } from '@angular/core'; import {Http, Headers} from '@angular/http'; import {Observable} from 'rxjs/Observable'; +import 'rxjs/add/observable/of'; +import 'rxjs/add/operator/catch'; +import 'rxjs/add/operator/mergeMap'; +import {xsrfOptions} from './xsrf'; @Injectable() export class LoginService { @@ -10,12 +14,14 @@ export class LoginService { sendCredential(username: string, password: string) { let url = 'http://localhost:8080/index'; let params = 'username='+username+'&password='+password; - let headers = new Headers( - { - 'Content-Type': 'application/x-www-form-urlencoded' - // 'Access-Control-Allow-Credentials' : true + return this.fetchCsrfToken().mergeMap(() => { + let headers = new Headers( + { + 'Content-Type': 'application/x-www-form-urlencoded' + // 'Access-Control-Allow-Credentials' : true + }); + return this.http.post(url, params, xsrfOptions(headers)); }); - return this.http.post(url, params, {headers: headers, withCredentials : true}); } logout() { @@ -23,4 +29,10 @@ export class LoginService { return this.http.get(url, { withCredentials: true }); } + // GET /index is permitAll, so unlike a protected URL it is not stored as the post-login redirect target. + private fetchCsrfToken(): Observable { + const url = 'http://localhost:8080/index'; + return this.http.get(url, { withCredentials: true }).catch(() => Observable.of(null)); + } + } diff --git a/AdminPortal/src/app/xsrf.ts b/AdminPortal/src/app/xsrf.ts new file mode 100644 index 00000000..142b34b1 --- /dev/null +++ b/AdminPortal/src/app/xsrf.ts @@ -0,0 +1,22 @@ +import { Headers, RequestOptionsArgs } from '@angular/http'; + +export const XSRF_COOKIE_NAME = 'XSRF-TOKEN'; +export const XSRF_HEADER_NAME = 'X-XSRF-TOKEN'; + +export function readXsrfToken(): string { + const match = document.cookie.match(new RegExp('(?:^|;\\s*)' + XSRF_COOKIE_NAME + '=([^;]*)')); + return match ? decodeURIComponent(match[1]) : null; +} + +// The header is only added when the cookie exists: a custom header forces a CORS preflight. +export function xsrfHeaders(headers: Headers = new Headers()): Headers { + const token = readXsrfToken(); + if (token) { + headers.set(XSRF_HEADER_NAME, token); + } + return headers; +} + +export function xsrfOptions(headers?: Headers): RequestOptionsArgs { + return { headers: xsrfHeaders(headers), withCredentials: true }; +}