import { Injectable } from '@nestjs/common' ; import { CryptographyService } from '@fc/cryptography' ; import { LoggerLevelNames , LoggerService } from '@fc/logger' ; import { OidcSession } from '@fc/oidc' ; import { OidcClientSession } from '@fc/oidc-client' ; import { ISessionService } from '@fc/session' ; todo author Hugues
 *  date ticket @ Injectable ( ) export class SessionCsrfService { constructor ( private readonly logger , private readonly cryptography ) { this . logger . setContext ( this . constructor . name ) ; } TODO see https get ( ) { const csrfTokenLength = 32 ; const csrfToken = this . cryptography . genRandomString ( csrfTokenLength ) ; this . logger . trace ( { csrfToken } ) ; return csrfToken ; } async save ( sessionOidc , csrfToken ) { this . logger . trace ( { csrfToken } ) ; return await sessionOidc . set ( { csrfToken } ) ; } async validate ( sessionOidc , csrfToken ) { const { csrfToken : csrfTokenSession } = await sessionOidc . get ( ) ; if ( csrfToken !== csrfTokenSession ) { this . logger . trace ( { csrfToken , csrfTokenSession } , LoggerLevelNames . WARN ) ; throw new Error ( 'Une erreur technique est survenue, fermez l’onglet de votre navigateur et reconnectez-vous.' ) ; } this . logger . trace ( { check : { csrfToken , csrfTokenSession } } ) ; return true ; } }