Skip to content

Commit 6b2ef5f

Browse files
committed
add consent expiry checking
1 parent 1393105 commit 6b2ef5f

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

server/controllers/OpeyIIController.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ export class OpeyController {
310310
// If we have a consent id, we can get the consent from OBP
311311
const consent = await this.obpConsentsService.getConsentByConsentId(session, consentId)
312312

313-
return response.status(200).json({consent_id: consent.consent_id});
313+
return response.status(200).json({consent_id: consent.consent_id, jwt: consent.jwt});
314314
} else {
315315
console.log("No existing consent ID found")
316316
}
@@ -322,7 +322,7 @@ export class OpeyController {
322322

323323
const authConfig = session['opeyConfig']['authConfig']
324324

325-
return response.status(200).json({consent_id: authConfig?.obpConsent.consent_id});
325+
return response.status(200).json({consent_id: authConfig?.obpConsent.consent_id, jwt: authConfig?.obpConsent.jwt});
326326

327327
} catch (error) {
328328
console.error("Error in consent endpoint: ", error);

server/services/OBPConsentsService.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,15 @@ export default class OBPConsentsService {
165165
}
166166
}
167167

168+
async checkConsentExpired(consent: any): Promise<boolean> { //DEBUG
169+
// Check if the consent is expired
170+
// Decode the JWT and check the exp field
171+
172+
const exp = consent.jwt_payload.exp
173+
const now = Math.floor(Date.now() / 1000)
174+
return exp < now
175+
}
176+
168177
async getExistingOpeyConsentId(session: Session): Promise<any> {
169178
// Get Consents for the current user, check if any of them are for Opey
170179
// If so, return the consent
@@ -186,8 +195,10 @@ export default class OBPConsentsService {
186195
throw new Error('User is not logged in')
187196
}
188197

189-
190-
const consentInfosPath = '/obp/v5.1.0/my/consent-infos'
198+
// We need to change this back to consent infos once OBP shows 'EXPIRED' in the status
199+
// Right now we have to check the JWT ourselves
200+
const consentInfosPath = '/obp/v5.1.0/my/consents'
201+
//const consentInfosPath = '/obp/v5.1.0/my/consent-infos'
191202

192203
let opeyConsentId: string | null = null
193204
try {
@@ -199,11 +210,15 @@ export default class OBPConsentsService {
199210
throw new Error('Opey Consumer ID is missing, please set VITE_OPEY_CONSUMER_ID')
200211
}
201212

202-
console.log('consents data: \n', response.data) //DEBUG
203-
204213
for (const consent of consents) {
205214
console.log(`consent_consumer_id: ${consent.consumer_id}, opey_consumer_id: ${opeyConsumerID}\n consent_status: ${consent.status}`) //DEBUG
206215
if (consent.consumer_id === opeyConsumerID && consent.status === 'ACCEPTED') {
216+
// Check if the consent is expired
217+
const isExpired = await this.checkConsentExpired(consent)
218+
if (isExpired) {
219+
console.log('getExistingConsent: Consent is expired')
220+
continue
221+
}
207222
opeyConsentId = consent.consent_id
208223
break
209224
}

src/stores/chat.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,34 @@ export const useChat = defineStore('chat', {
152152

153153
if (consentResponse) {
154154
const consentId = consentResponse.consent_id
155-
if (consentId) {
156-
this.userIsAuthenticated = true
157-
} else {
158-
throw new Error('Failed to grant consent. Please try again.')
159-
}
155+
160156
} else {
161157
throw new Error('Failed to grant consent. Please try again.')
162158
}
159+
160+
const consentJwt = consentResponse.jwt
161+
162+
const opeyBaseUri = import.meta.env.VITE_CHATBOT_URL
163+
// Get a session from opey
164+
try {
165+
const sessionResponse = await fetch(`${opeyBaseUri}/create-session`, {
166+
method: 'POST',
167+
credentials: 'include',
168+
headers: {
169+
'Content-Type': 'application/json',
170+
'Consent-JWT': consentJwt
171+
},
172+
})
173+
174+
if (!sessionResponse.ok) {
175+
throw new Error(`Failed to create session: ${sessionResponse.statusText}`);
176+
} else if (sessionResponse.status === 200) {
177+
this.userIsAuthenticated = true
178+
}
179+
180+
} catch (error) {
181+
console.error('Error creating session:', error);
182+
}
163183
},
164184

165185
async stream(input: ChatStreamInput): Promise<void> {
@@ -181,11 +201,12 @@ export const useChat = defineStore('chat', {
181201
this.addMessage(this.currentAssistantMessage)
182202

183203
// Set the status to 'loading' before we fetch the stream
184-
204+
const opeyBaseUri = import.meta.env.VITE_CHATBOT_URL
185205
// Handle stream
186206
try {
187-
const response = await fetch('/api/opey/stream', {
207+
const response = await fetch(`${opeyBaseUri}/stream`, {
188208
method: 'POST',
209+
credentials: 'include',
189210
headers: {
190211
'Content-Type': 'application/json'
191212
},

0 commit comments

Comments
 (0)