@@ -7,6 +7,28 @@ import axios from 'axios'
7
7
import { Session } from 'express-session'
8
8
9
9
@Service ( )
10
+ /**
11
+ * Service for managing Open Banking Project (OBP) consents functionality.
12
+ * This class handles the creation of consent clients, consent creation, and retrieval
13
+ * based on user sessions.
14
+ *
15
+ * @class OBPConsentsService
16
+ * @description Provides methods to interact with OBP Consent APIs, allowing the application
17
+ * to create and manage consents that permit access to user accounts via API Explorer II.
18
+ *
19
+ * Key functionalities:
20
+ * - Creating consent API clients based on user sessions
21
+ * - Creating implicit consents for access delegation i.e. for opey
22
+ * - Retrieving existing consents by ID
23
+ * - Finding consents associated with specific consumers (e.g., Opey)
24
+ *
25
+ * @requires OBPClientService
26
+ * @requires Configuration
27
+ * @requires ConsentApi
28
+ * @requires InlineResponse2017
29
+ * @requires ConsentsIMPLICITBody1
30
+ * @requires axios
31
+ */
10
32
export default class OBPConsentsService {
11
33
private consentApiConfig : Configuration
12
34
public obpClientService : OBPClientService // This needs to be changed once we migrate away from the old OBP SDK
@@ -106,31 +128,58 @@ export default class OBPConsentsService {
106
128
107
129
}
108
130
109
- async getExistingConsent ( session : Session ) : Promise < any > {
131
+
132
+
133
+ /**
134
+ * Retrieves a consent by consent ID for the current user.
135
+ *
136
+ * This method fetches a specific consent using its ID and updates the session
137
+ * with the retrieved consent data under the opeyConfig property.
138
+ *
139
+ * @param session - The user's session object, which must contain clientConfig with valid OAuth tokens
140
+ * @param consentId - The unique identifier of the consent to retrieve
141
+ * @returns Promise resolving to the consent data retrieved from OBP API
142
+ * @throws Error if the user is not logged in (no valid clientConfig or accessToken)
143
+ * @throws Error if the request to get the consent fails
144
+ */
145
+ async getConsentByConsentId ( session : Session , consentId : string ) : Promise < any > {
146
+
147
+ const clientConfig = session [ 'clientConfig' ]
148
+ if ( ! clientConfig || ! clientConfig . oauthConfig . accessToken ) {
149
+ throw new Error ( 'User is not logged in' )
150
+ }
151
+
152
+ try {
153
+ const response = await this . _sendOBPRequest ( `/obp/v5.1.0/user/current/consents/${ consentId } ` , 'GET' , clientConfig )
154
+
155
+ session [ 'opeyConfig' ] = {
156
+ authConfig : {
157
+ obpConsent : response . data
158
+ }
159
+ }
160
+
161
+ return response . data
162
+ } catch ( error ) {
163
+ console . error ( error )
164
+ throw new Error ( `Consent with ID ${ consentId } not retrieved: ${ error } ` )
165
+ }
166
+ }
167
+
168
+ async getExistingOpeyConsentId ( session : Session ) : Promise < any > {
110
169
// Get Consents for the current user, check if any of them are for Opey
111
170
// If so, return the consent
112
171
113
172
// I.e. this is done by iterating and finding the consent with the correct consumer ID
114
173
115
174
// Get the Consents API client from the OBP SDK
116
- // The OBP SDK is fucked here, so we'll need to use Fetch until the SWAGGER WILL ACTUALLY WORK
175
+ // The OBP SDK is messed up here, so we'll need to use Fetch until the SWAGGER WILL ACTUALLY WORK
117
176
// const client = await this.createUserConsentsClient(session, '/obp/v5.1.0/my/consents/IMPLICIT', 'POST')
118
177
// if (!client) {
119
178
// throw new Error('Could not create Consents API client')
120
179
// }
121
180
122
181
123
182
// Function to send an OBP request using the logged in user's OAuth1 headers
124
- const sendOBPRequest = async ( path : string , method : string , clientConfig : any ) => {
125
- const oauth1Headers = await this . obpClientService . getOAuthHeader ( path , method , clientConfig )
126
- const config = {
127
- headers : {
128
- 'Authorization' : oauth1Headers ,
129
- 'Content-Type' : 'application/json' ,
130
- }
131
- }
132
- return axios . get ( `${ clientConfig . baseUri } ${ path } ` , config )
133
- }
134
183
135
184
const clientConfig = session [ 'clientConfig' ]
136
185
if ( ! clientConfig || ! clientConfig . oauthConfig . accessToken ) {
@@ -142,19 +191,19 @@ export default class OBPConsentsService {
142
191
143
192
let opeyConsentId : string | null = null
144
193
try {
145
- const response = await sendOBPRequest ( consentInfosPath , 'GET' , clientConfig )
194
+ const response = await this . _sendOBPRequest ( consentInfosPath , 'GET' , clientConfig )
146
195
const consents = response . data . consents
147
196
148
197
const opeyConsumerID = process . env . VITE_OPEY_CONSUMER_ID
149
198
if ( ! opeyConsumerID ) {
150
199
throw new Error ( 'Opey Consumer ID is missing, please set VITE_OPEY_CONSUMER_ID' )
151
200
}
152
201
153
-
202
+ console . log ( 'consents data: \n' , response . data ) //DEBUG
154
203
155
204
for ( const consent of consents ) {
156
- console . log ( 'consent ' , consent )
157
- if ( consent . consumer_id === opeyConsumerID && consent . staus === 'ACCEPTED' ) {
205
+ console . log ( `consent_consumer_id: ${ consent . consumer_id } , opey_consumer_id: ${ opeyConsumerID } \n consent_status: ${ consent . status } ` ) //DEBUG
206
+ if ( consent . consumer_id === opeyConsumerID && consent . status === 'ACCEPTED' ) {
158
207
opeyConsentId = consent . consent_id
159
208
break
160
209
}
@@ -163,30 +212,26 @@ export default class OBPConsentsService {
163
212
if ( ! opeyConsentId ) {
164
213
console . log ( 'getExistingConsent: No consent found for Opey for current user' )
165
214
return null
215
+ } else {
216
+ return opeyConsentId
166
217
}
167
218
168
219
} catch ( error ) {
169
220
console . error ( error )
170
221
throw new Error ( `Could not get existing consent info, ${ error } ` )
171
222
}
172
223
173
- // Now try to get the consent using the consent ID
174
- try {
175
- const response = await sendOBPRequest ( `/obp/v5.1.0/user/current/consents/ ${ opeyConsentId } ` , 'GET' , clientConfig )
176
-
177
- session [ 'opeyConfig' ] = {
178
- authConfig : {
179
- obpConsent : response . data
180
- }
224
+ }
225
+
226
+ async _sendOBPRequest ( path : string , method : string , clientConfig : any ) {
227
+ const oauth1Headers = await this . obpClientService . getOAuthHeader ( path , method , clientConfig )
228
+ const config = {
229
+ headers : {
230
+ 'Authorization' : oauth1Headers ,
231
+ 'Content-Type' : 'application/json' ,
181
232
}
182
-
183
- return response . data
184
- } catch ( error ) {
185
- console . error ( error )
186
- throw new Error ( `Could not get existing consent, ${ error } ` )
187
233
}
188
-
189
-
234
+ return axios . get ( `${ clientConfig . baseUri } ${ path } ` , config )
190
235
}
191
236
192
237
0 commit comments