2828import { Controller , Session , Req , Res , Get , Delete , Post , Put } from 'routing-controllers'
2929import type { Request , Response } from 'express'
3030import OBPClientService from '../services/OBPClientService.js'
31+ import { OAuth2Service } from '../services/OAuth2Service.js'
3132import { Service , Container } from 'typedi'
3233
3334@Service ( )
3435@Controller ( )
3536export class OBPController {
3637 private obpClientService : OBPClientService
38+ private oauth2Service : OAuth2Service
3739
3840 constructor ( ) {
39- // Explicitly get OBPClientService from the container to avoid injection issues
41+ // Explicitly get services from the container to avoid injection issues
4042 this . obpClientService = Container . get ( OBPClientService )
43+ this . oauth2Service = Container . get ( OAuth2Service )
44+ }
45+
46+ /**
47+ * Check if access token is expired and refresh it if needed
48+ * This ensures API calls always use a valid token
49+ */
50+ private async ensureValidToken ( session : any ) : Promise < boolean > {
51+ const accessToken = session [ 'oauth2_access_token' ]
52+ const refreshToken = session [ 'oauth2_refresh_token' ]
53+
54+ // If no access token, user is not authenticated
55+ if ( ! accessToken ) {
56+ return false
57+ }
58+
59+ // Check if token is expired
60+ if ( this . oauth2Service . isTokenExpired ( accessToken ) ) {
61+ console . log ( 'RequestController: Access token expired, attempting refresh' )
62+
63+ if ( ! refreshToken ) {
64+ console . log ( 'RequestController: No refresh token available' )
65+ return false
66+ }
67+
68+ try {
69+ const newTokens = await this . oauth2Service . refreshAccessToken ( refreshToken )
70+
71+ // Update session with new tokens
72+ session [ 'oauth2_access_token' ] = newTokens . accessToken
73+ session [ 'oauth2_refresh_token' ] = newTokens . refreshToken || refreshToken
74+ session [ 'oauth2_id_token' ] = newTokens . idToken
75+ session [ 'oauth2_token_timestamp' ] = Date . now ( )
76+ session [ 'oauth2_expires_in' ] = newTokens . expiresIn
77+
78+ // CRITICAL: Update clientConfig with new access token
79+ if ( session [ 'clientConfig' ] && session [ 'clientConfig' ] . oauth2 ) {
80+ session [ 'clientConfig' ] . oauth2 . accessToken = newTokens . accessToken
81+ console . log ( 'RequestController: Updated clientConfig with refreshed token' )
82+ }
83+
84+ console . log ( 'RequestController: Token refresh successful' )
85+ return true
86+ } catch ( error ) {
87+ console . error ( 'RequestController: Token refresh failed:' , error )
88+ return false
89+ }
90+ }
91+
92+ // Token is still valid
93+ return true
4194 }
4295
4396 @Get ( '/get' )
4497 async get ( @Session ( ) session : any , @Req ( ) request : Request , @Res ( ) response : Response ) : Response {
4598 const path = request . query . path
99+
100+ // Ensure token is valid before making the request
101+ const tokenValid = await this . ensureValidToken ( session )
102+ if ( ! tokenValid && session [ 'oauth2_user' ] ) {
103+ console . log ( 'RequestController: Token expired and refresh failed' )
104+ return response . status ( 401 ) . json ( {
105+ code : 401 ,
106+ message : 'Session expired. Please log in again.'
107+ } )
108+ }
109+
46110 const oauthConfig = session [ 'clientConfig' ]
47111
48112 try {
@@ -72,6 +136,17 @@ export class OBPController {
72136 ) : Response {
73137 const path = request . query . path
74138 const data = request . body
139+
140+ // Ensure token is valid before making the request
141+ const tokenValid = await this . ensureValidToken ( session )
142+ if ( ! tokenValid && session [ 'oauth2_user' ] ) {
143+ console . log ( 'RequestController: Token expired and refresh failed' )
144+ return response . status ( 401 ) . json ( {
145+ code : 401 ,
146+ message : 'Session expired. Please log in again.'
147+ } )
148+ }
149+
75150 const oauthConfig = session [ 'clientConfig' ]
76151
77152 // Debug logging to diagnose authentication issues
@@ -104,6 +179,17 @@ export class OBPController {
104179 ) : Response {
105180 const path = request . query . path
106181 const data = request . body
182+
183+ // Ensure token is valid before making the request
184+ const tokenValid = await this . ensureValidToken ( session )
185+ if ( ! tokenValid && session [ 'oauth2_user' ] ) {
186+ console . log ( 'RequestController: Token expired and refresh failed' )
187+ return response . status ( 401 ) . json ( {
188+ code : 401 ,
189+ message : 'Session expired. Please log in again.'
190+ } )
191+ }
192+
107193 const oauthConfig = session [ 'clientConfig' ]
108194
109195 try {
@@ -119,12 +205,23 @@ export class OBPController {
119205 }
120206
121207 @Delete ( '/delete' )
122- async delete (
208+ async discard (
123209 @Session ( ) session : any ,
124210 @Req ( ) request : Request ,
125211 @Res ( ) response : Response
126212 ) : Response {
127213 const path = request . query . path
214+
215+ // Ensure token is valid before making the request
216+ const tokenValid = await this . ensureValidToken ( session )
217+ if ( ! tokenValid && session [ 'oauth2_user' ] ) {
218+ console . log ( 'RequestController: Token expired and refresh failed' )
219+ return response . status ( 401 ) . json ( {
220+ code : 401 ,
221+ message : 'Session expired. Please log in again.'
222+ } )
223+ }
224+
128225 const oauthConfig = session [ 'clientConfig' ]
129226
130227 try {
0 commit comments