import { encode } from 'querystring' ; import { Controller , Get , Query , Req , Res } from '@nestjs/common' ; import { ConfigService } from '@fc/config' ; import { CryptographyService } from '@fc/cryptography' ; import { IdentityProviderAdapterEnvService } from '@fc/identity-provider-adapter-env' ; import { LoggerLevelNames , LoggerService } from '@fc/logger' ; import { IOidcIdentity , OidcSession } from '@fc/oidc' ; import { OidcClientConfig , OidcClientService , OidcClientSession , } from '@fc/oidc-client' ; import { ISessionService , Session , SessionCsrfService , SessionNotFoundException , SessionService , } from '@fc/session' ; import { ExploitationRoutes } from '../enums' ; @ Controller ( ) export class ExploitationController { constructor ( private readonly config , private readonly crypto , private readonly oidcClient , private readonly logger , private readonly identityProvider , private readonly sessionService , private readonly csrfService ) { this . logger . setContext ( this . constructor . name ) ; } @ Get ( ExploitationRoutes . OIDC_CLIENT_GET_AUTHORIZE_URL ) async getAuthorizeUrl ( todo author Hugues
     *  date ticket @ Session ( 'OidcClient' ) sessionOidc ) { const [ provider ] = await this . identityProvider . getList ( ) ; const { acr , claims , scope } = this . config . get < OidcClientConfig > ( 'OidcClient' ) ; const { nonce , state } = await this . oidcClient . utils . buildAuthorizeParameters ( ) ; const authorizationUrl = await this . oidcClient . utils . getAuthorizeUrl ( { acr_values : acr , claims , nonce , idpId : provider . uid , scope , state , } ) ; await sessionOidc . set ( { idpId : provider . uid , idpState : state , idpNonce : nonce , } ) ; return authorizationUrl ; } TODO see https @ Get ( ExploitationRoutes . OIDC_CLIENT_CALLBACK ) async getOidcCallback ( @ Req ( ) req , @ Res ( ) res , @ Query ( ) query , todo author Hugues
     *  date ticket @ Session ( 'OidcClient' ) sessionOidc ) { todo Todo author Stéphane/Matthieu
         *  date ticket if ( query . error ) { const loginUri = `/login?${ encode ( query ) }` ; this . logger . trace ( { query , loginUri } , LoggerLevelNames . WARN ) ; return res . redirect ( loginUri ) ; } const session = await sessionOidc . get ( ) ; if ( ! session ) { throw new SessionNotFoundException ( 'OidcClient' ) ; } const { idpId , idpNonce , idpState } = session ; const tokenParams = { state : idpState , nonce : idpNonce , } ; const { accessToken , idToken , acr , amr } = await this . oidcClient . getTokenFromProvider ( idpId , tokenParams , req ) ; const userInfoParams = { accessToken , idpId , } ; const identity = await this . oidcClient . getUserInfosFromProvider ( userInfoParams , req ) ; todo author Arnaud
         *  date ticket const identityExchange = { idpIdentity : identity , idpAcr : acr , amr , idpAccessToken : accessToken , idpIdToken : idToken , } ; await sessionOidc . set ( { ... identityExchange } ) ; const redirect = '/' ; this . logger . trace ( { route : ExploitationRoutes . OIDC_CLIENT_CALLBACK , method : 'GET' , name : 'ExploitationRoutes.OIDC_CLIENT_CALLBACK' , identityExchange , redirect , } ) ; res . redirect ( redirect ) ; } @ Get ( ExploitationRoutes . OIDC_CLIENT_LOAD_USER_INFOS ) async loadUserInfos ( @ Session ( 'OidcClient' ) sessionOidc ) { const idpIdentity = await sessionOidc . get ( 'idpIdentity' ) ; return { connected : idpIdentity ? true : false , userinfos : idpIdentity , } ; } @ Get ( ExploitationRoutes . OIDC_CLIENT_GET_END_SESSION_URL ) async getEndSessionUrl ( todo author Hugues
     *  date ticket @ Session ( 'OidcClient' ) sessionOidc , @ Query ( 'post_logout_redirect_uri' ) postLogoutRedirectUri ? ) { const { idpIdToken , idpState , idpId } = await sessionOidc . get ( ) ; const endSessionUrl = await this . oidcClient . getEndSessionUrlFromProvider ( idpId , idpState , idpIdToken , postLogoutRedirectUri ) ; return endSessionUrl ; } @ Get ( ExploitationRoutes . OIDC_CLIENT_LOGOUT_CALLBACK ) async logoutCallback ( @ Req ( ) req , @ Res ( ) res ) { TODO see https this . logger . trace ( { route : ExploitationRoutes . OIDC_CLIENT_LOGOUT_CALLBACK , method : 'GET' , name : 'MockServiceProviderRoutes.LOGOUT_CALLBACK' , redirect : '/' , } ) ; await this . sessionService . reset ( req , res ) ; const redirect = '/' ; return res . redirect ( redirect ) ; } @ Get ( ExploitationRoutes . CSRF_TOKEN ) async getCsrfToken ( @ Session ( 'OidcClient' ) sessionOidc ) { const csrfToken = this . csrfService . get ( ) ; await this . csrfService . save ( sessionOidc , csrfToken ) ; return { csrfToken } ; } }