@@ -52,6 +52,7 @@ import { type SessionService } from './session.server';
5252import { Tokens } from '@app-builder/routes/oidc+/auth' ;
5353import { OIDCStrategy } from 'remix-auth-openid' ;
5454import { AppConfigRepository } from '@app-builder/repositories/AppConfigRepository' ;
55+ import { MarbleOidcStrategy } from './oidc.server' ;
5556
5657interface AuthenticatedInfo {
5758 /**
@@ -130,6 +131,7 @@ interface MakeAuthenticationServerServiceArgs {
130131 getMarbleCoreAPIClientWithAuth : GetMarbleCoreAPIClientWithAuth ;
131132 getTransfercheckAPIClientWithAuth : GetTransfercheckAPIClientWithAuth ;
132133 getFeatureAccessAPIClientWithAuth : GetFeatureAccessAPIClientWithAuth ;
134+ getAppConfigRepository : ( marbleCoreApiClient : MarbleCoreApi ) => AppConfigRepository ;
133135 getUserRepository : ( marbleCoreApiClient : MarbleCoreApi ) => UserRepository ;
134136 getInboxRepository : ( marbleCoreApiClient : MarbleCoreApi ) => InboxRepository ;
135137 getEditorRepository : ( marbleCoreApiClient : MarbleCoreApi ) => EditorRepository ;
@@ -166,7 +168,7 @@ interface MakeAuthenticationServerServiceArgs {
166168 authSessionService : SessionService < AuthData , AuthFlashData > ;
167169 toastSessionService : SessionService < void , ToastFlashData > ;
168170 csrfService : CSRF ;
169- makeOidcService : ( configRepository : AppConfigRepository ) => Promise < OIDCStrategy < Tokens > > ;
171+ makeOidcService : ( configRepository : AppConfigRepository ) => Promise < MarbleOidcStrategy < Tokens > > ;
170172}
171173
172174function expectedErrors ( error : unknown ) {
@@ -177,6 +179,7 @@ export function makeAuthenticationServerService({
177179 getMarbleCoreAPIClientWithAuth,
178180 getTransfercheckAPIClientWithAuth,
179181 getFeatureAccessAPIClientWithAuth,
182+ getAppConfigRepository,
180183 getUserRepository,
181184 getInboxRepository,
182185 getEditorRepository,
@@ -209,6 +212,36 @@ export function makeAuthenticationServerService({
209212 return {
210213 getToken : ( ) => Promise . resolve ( marbleAccessToken ) ,
211214 refreshToken : async ( ) => {
215+ const appConfigRepository = getAppConfigRepository ( marblecoreApi ) ;
216+ const appConfig = await appConfigRepository . getAppConfig ( ) ;
217+
218+ if ( appConfig . auth . provider == 'oidc' ) {
219+ const oidc = await makeOidcService ( appConfigRepository ) ;
220+
221+ if ( request ) {
222+ const authSession = await authSessionService . getSession ( request ) ;
223+
224+ if ( authSession . data . refreshToken ) {
225+ const response = await oidc . refreshToken ( authSession . data . refreshToken ) ;
226+
227+ const marbleToken = await marblecoreApi . postToken (
228+ {
229+ authorization : `Bearer ${ response . idToken ( ) } ` ,
230+ } ,
231+ { baseUrl : getServerEnv ( 'MARBLE_API_URL_SERVER' ) } ,
232+ ) ;
233+
234+ authSession . set ( 'authToken' , marbleToken ) ;
235+
236+ if ( response . hasRefreshToken ( ) ) {
237+ authSession . set ( 'refreshToken' , response . refreshToken ( ) ) ;
238+ }
239+
240+ return marbleToken . access_token ;
241+ }
242+ }
243+ }
244+
212245 // We don't handle refresh for now, force a logout when 401 is returned instead
213246 throw redirect ( getRoute ( '/ressources/auth/logout' ) ) ;
214247 } ,
@@ -386,10 +419,10 @@ export function makeAuthenticationServerService({
386419 else return null ;
387420 }
388421
389- const tokenService = getTokenService ( marbleToken . access_token ) ;
422+ const tokenService = getTokenService ( marbleToken . access_token , request ) ;
390423 const marbleCoreApiClient = getMarbleCoreAPIClientWithAuth ( tokenService ) ;
391424 const featureAccessApiClient = getFeatureAccessAPIClientWithAuth (
392- getTokenService ( marbleToken . access_token ) ,
425+ getTokenService ( marbleToken . access_token , request ) ,
393426 ) ;
394427 const transfercheckAPIClient = getTransfercheckAPIClientWithAuth ( tokenService ) ;
395428
0 commit comments