TFA + JWT token manage in cookie
This commit is contained in:
parent
df71ff1bec
commit
b4e3bf9f21
14
CLAUDE.md
14
CLAUDE.md
|
|
@ -630,9 +630,17 @@ What the earlier 19 → 21 upgrade fixed in place, so don't undo it:
|
||||||
`hrmopen-ai`, `calendar`, `survey`, …), each under `src/app/<name>/` with its own `<name>.module.ts`.
|
`hrmopen-ai`, `calendar`, `survey`, …), each under `src/app/<name>/` with its own `<name>.module.ts`.
|
||||||
- `app.api.service.ts` (`ApiService`) is the single HTTP wrapper (`httpGet`, `httpPost`,
|
- `app.api.service.ts` (`ApiService`) is the single HTTP wrapper (`httpGet`, `httpPost`,
|
||||||
`httpPostFile`, `httpDownloadFile`, …). The base URL comes from `environment.apiBaseUrl`
|
`httpPostFile`, `httpDownloadFile`, …). The base URL comes from `environment.apiBaseUrl`
|
||||||
(`src/environments/environment.ts` → `https://localhost:44374/api`) in dev, and from the page's
|
(`src/environments/environment.ts` → relative `/api`) in dev — `proxy.json` (the `serve`
|
||||||
`<base href>` + `api` in production. The auth token is a **static** `ApiService.AuthenticationToken`
|
`proxyConfig`) forwards it to `https://localhost:44374` — and from the page's `<base href>` + `api`
|
||||||
sent as a Bearer header.
|
in production.
|
||||||
|
- **Auth is cookie-based (MFA work, ported from GP_Master).** `AuthController.CreateTokenNew` puts the
|
||||||
|
real JWT in the HttpOnly, `Secure`, `SameSite=Strict` `JWToken` cookie and returns only a
|
||||||
|
dummy-key copy, which the SPA keeps in `localStorage`/`ApiService.AuthenticationToken` for reading
|
||||||
|
claims and sends as the Bearer header. `Startup`'s `JwtBearerEvents.OnMessageReceived` lets the
|
||||||
|
cookie win over that header; `CredentialsInterceptor` (`app.api.service.ts`) adds
|
||||||
|
`withCredentials`; month-end (`SalaryController.GenerateTokenAfterMonthEnd`) re-sets the cookie;
|
||||||
|
`api/Authentication/logOut` deletes it. So the API must stay **same-origin** with the page: never
|
||||||
|
point the dev base URL back at an absolute `https://localhost:44374`, or every call after login is 401.
|
||||||
- Shared code: `_guards/`, `_models/<Module>/`, `_services/<module>/`, `picker/`/`_picker/`/
|
- Shared code: `_guards/`, `_models/<Module>/`, `_services/<module>/`, `picker/`/`_picker/`/
|
||||||
`dynamic-picker/`, `custom-components/`, `form-builder/`, `helper/`, `SharedIconModule/`.
|
`dynamic-picker/`, `custom-components/`, `form-builder/`, `helper/`, `SharedIconModule/`.
|
||||||
- The UI kit is **Kendo UI Angular** (`@progress/kendo-angular-*`, licensed) plus chart.js, sweetalert2, the two
|
- The UI kit is **Kendo UI Angular** (`@progress/kendo-angular-*`, licensed) plus chart.js, sweetalert2, the two
|
||||||
|
|
|
||||||
|
|
@ -91,7 +91,8 @@
|
||||||
"serve": {
|
"serve": {
|
||||||
"builder": "@angular/build:dev-server",
|
"builder": "@angular/build:dev-server",
|
||||||
"options": {
|
"options": {
|
||||||
"buildTarget": "babylon:build"
|
"buildTarget": "babylon:build",
|
||||||
|
"proxyConfig": "proxy.json"
|
||||||
},
|
},
|
||||||
"configurations": {
|
"configurations": {
|
||||||
"production": {
|
"production": {
|
||||||
|
|
|
||||||
8
HRM.UI/ClientApp/proxy.json
Normal file
8
HRM.UI/ClientApp/proxy.json
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"/api": {
|
||||||
|
"target": "https://localhost:44374",
|
||||||
|
"secure": false,
|
||||||
|
"changeOrigin": true,
|
||||||
|
"logLevel": "debug"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -121,6 +121,8 @@ export class AuthService {
|
||||||
sessionStorage.removeItem('employeeCommonData');
|
sessionStorage.removeItem('employeeCommonData');
|
||||||
this.logoutSource.next();
|
this.logoutSource.next();
|
||||||
this.router.navigate(['/login']);
|
this.router.navigate(['/login']);
|
||||||
|
// Clear the HttpOnly JWToken cookie on the server; logout must never fail because of it.
|
||||||
|
this.apiService.httpPost<any>('/Authentication/logOut', {}).subscribe({ error: () => {} });
|
||||||
}
|
}
|
||||||
SaveUser(oUser: User): any {
|
SaveUser(oUser: User): any {
|
||||||
return this.apiService.httpPost<User>('/Authentication/SaveUser', oUser);
|
return this.apiService.httpPost<User>('/Authentication/SaveUser', oUser);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { Observable, Subject, throwError } from 'rxjs';
|
import { Observable, Subject, throwError } from 'rxjs';
|
||||||
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
|
import { HttpClient, HttpHeaders, HttpErrorResponse, HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';
|
||||||
import { catchError, retry } from 'rxjs/operators';
|
import { catchError, retry } from 'rxjs/operators';
|
||||||
import { DatePipe } from '@angular/common';
|
import { DatePipe } from '@angular/common';
|
||||||
import { GlobalfunctionExtension } from './_models/globalFunctions';
|
import { GlobalfunctionExtension } from './_models/globalFunctions';
|
||||||
|
|
@ -285,3 +285,12 @@ export class PubSubService {
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class BaseHrefService {
|
export class BaseHrefService {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sends cookies with every request (including AuthService's direct HttpClient calls), so the
|
||||||
|
// HttpOnly "JWToken" cookie set at login is stored and sent back to the API.
|
||||||
|
@Injectable()
|
||||||
|
export class CredentialsInterceptor implements HttpInterceptor {
|
||||||
|
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
||||||
|
return next.handle(req.clone({ withCredentials: true }));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -71,6 +71,7 @@ import {NgxExtendedPdfViewerModule} from 'ngx-extended-pdf-viewer';
|
||||||
import {ProgressBarModule} from '@progress/kendo-angular-progressbar';
|
import {ProgressBarModule} from '@progress/kendo-angular-progressbar';
|
||||||
import {MsalGuard, MsalInterceptor, MsalModule, MsalRedirectComponent, MsalService} from '@azure/msal-angular';
|
import {MsalGuard, MsalInterceptor, MsalModule, MsalRedirectComponent, MsalService} from '@azure/msal-angular';
|
||||||
import {InteractionType, PublicClientApplication} from '@azure/msal-browser';
|
import {InteractionType, PublicClientApplication} from '@azure/msal-browser';
|
||||||
|
import { CredentialsInterceptor } from './app.api.service';
|
||||||
import { BackgroundCanvasModule } from './BackgroundCanvas/BackgroundCanvas.module';
|
import { BackgroundCanvasModule } from './BackgroundCanvas/BackgroundCanvas.module';
|
||||||
import { VersionCheckService } from './version-check.service';
|
import { VersionCheckService } from './version-check.service';
|
||||||
import { EssModule } from './ess/ess.module';
|
import { EssModule } from './ess/ess.module';
|
||||||
|
|
@ -206,6 +207,11 @@ import { EmpCommonInformationModule } from './ess/emp-common-information/EmpComm
|
||||||
useClass: MsalInterceptor,
|
useClass: MsalInterceptor,
|
||||||
multi: true
|
multi: true
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
provide: HTTP_INTERCEPTORS,
|
||||||
|
useClass: CredentialsInterceptor,
|
||||||
|
multi: true
|
||||||
|
},
|
||||||
MsalService,
|
MsalService,
|
||||||
MenuService, NotificationTriggerService,
|
MenuService, NotificationTriggerService,
|
||||||
BreadcrumbService,
|
BreadcrumbService,
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,9 @@ export const environment = {
|
||||||
production: false,
|
production: false,
|
||||||
// Used by ApiService only in dev mode (ng serve). In production the API
|
// Used by ApiService only in dev mode (ng serve). In production the API
|
||||||
// base URL is derived from <base href> in index.html instead.
|
// base URL is derived from <base href> in index.html instead.
|
||||||
apiBaseUrl: 'https://localhost:44374/api'
|
// Relative on purpose: on ng serve, proxy.json forwards /api to https://localhost:44374, so the
|
||||||
|
// API is same-origin and the HttpOnly SameSite=Strict "JWToken" auth cookie is stored and sent.
|
||||||
|
apiBaseUrl: '/api'
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
||||||
|
|
@ -701,6 +701,21 @@ namespace HRM.UI.Controllers
|
||||||
return Ok(new { userToken });
|
return Ok(new { userToken });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clears the HttpOnly "JWToken" cookie the API authenticates from (see Startup), so the
|
||||||
|
// session really ends on logout. Anonymous so an invalid/expired cookie can still be removed.
|
||||||
|
[HttpPost("logOut")]
|
||||||
|
[AllowAnonymous]
|
||||||
|
public IActionResult logOut()
|
||||||
|
{
|
||||||
|
Response.Cookies.Delete("JWToken", new CookieOptions
|
||||||
|
{
|
||||||
|
HttpOnly = true,
|
||||||
|
Secure = true,
|
||||||
|
SameSite = SameSiteMode.Strict
|
||||||
|
});
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
|
||||||
[HttpPost("resend-otp")]
|
[HttpPost("resend-otp")]
|
||||||
[EnableRateLimiting("ResendOtpPolicy")]
|
[EnableRateLimiting("ResendOtpPolicy")]
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
|
|
|
||||||
|
|
@ -1408,6 +1408,15 @@ namespace HRM.UI.Controllers.Payroll
|
||||||
var token = tokenHandler.CreateToken(tokenDescriptor);
|
var token = tokenHandler.CreateToken(tokenDescriptor);
|
||||||
string tokenString = tokenHandler.WriteToken(token);
|
string tokenString = tokenHandler.WriteToken(token);
|
||||||
HttpContext.Session.SetString("JWToken", tokenString);
|
HttpContext.Session.SetString("JWToken", tokenString);
|
||||||
|
// The API authenticates from the "JWToken" cookie first (Startup), so the new
|
||||||
|
// process date only takes effect once the cookie carries the new token too.
|
||||||
|
Response.Cookies.Append("JWToken", tokenString, new CookieOptions
|
||||||
|
{
|
||||||
|
HttpOnly = true,
|
||||||
|
Secure = true,
|
||||||
|
SameSite = SameSiteMode.Strict,
|
||||||
|
Expires = DateTimeOffset.Now.AddDays(1)
|
||||||
|
});
|
||||||
return tokenString;
|
return tokenString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -242,6 +242,18 @@ namespace HRM.UI.Api
|
||||||
ValidateIssuer = false,
|
ValidateIssuer = false,
|
||||||
ValidateAudience = false
|
ValidateAudience = false
|
||||||
};
|
};
|
||||||
|
options.Events = new JwtBearerEvents
|
||||||
|
{
|
||||||
|
OnMessageReceived = context =>
|
||||||
|
{
|
||||||
|
var cookieToken = context.Request.Cookies["JWToken"];
|
||||||
|
if (!string.IsNullOrEmpty(cookieToken))
|
||||||
|
{
|
||||||
|
context.Token = cookieToken;
|
||||||
|
}
|
||||||
|
return System.Threading.Tasks.Task.CompletedTask;
|
||||||
|
}
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user