base44-etoro-backend-functions-infra 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +27 -11
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
import { createClient } from '@base44/sdk';
|
|
2
2
|
import { v4 as uuidv4 } from 'uuid';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Custom exception for missing eToro API credentials
|
|
6
|
+
*/
|
|
7
|
+
export class eToroCredentialsMissingError extends Error {
|
|
8
|
+
constructor(missingKey) {
|
|
9
|
+
super(`No SSO access token available. Falling back to admin role authentication, but ${missingKey} is not configured. Please set ${missingKey} in your application's secrets.`);
|
|
10
|
+
this.name = 'EtoroCredentialsMissingError';
|
|
11
|
+
this.missingKey = missingKey;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
4
15
|
/**
|
|
5
16
|
* Creates an eToro API client instance
|
|
6
17
|
* @param {string} appId - Base44 application ID from environment
|
|
@@ -16,31 +27,36 @@ function createBase44Client(appId) {
|
|
|
16
27
|
* Generates eToro API headers for requests
|
|
17
28
|
* @param {Request} req - The incoming request object
|
|
18
29
|
* @param {Object} user - The authenticated user object
|
|
19
|
-
* @param {Object} options - Configuration options
|
|
20
|
-
* @param {string} options.appId - Base44 application ID (defaults to Deno.env.get('BASE44_APP_ID'))
|
|
21
|
-
* @param {string} options.apiKey - eToro API key (defaults to Deno.env.get('ETORO_API_KEY'))
|
|
22
|
-
* @param {string} options.userKey - eToro user key (defaults to Deno.env.get('ETORO_USER_KEY'))
|
|
23
30
|
* @returns {Promise<Object>} Headers object for eToro API requests
|
|
24
31
|
*/
|
|
25
|
-
export async function getEtoroHeaders(
|
|
32
|
+
export async function getEtoroHeaders(base44, user) {
|
|
26
33
|
const headers = {
|
|
27
34
|
'x-request-id': uuidv4(),
|
|
28
35
|
'Content-Type': 'application/json',
|
|
29
36
|
};
|
|
30
37
|
|
|
31
|
-
|
|
38
|
+
console.log(base44.asServiceRole);
|
|
39
|
+
|
|
40
|
+
const ssoToken = await base44.asServiceRole.sso.getAccessToken(user.id);
|
|
32
41
|
if (ssoToken) {
|
|
33
42
|
const arr = ssoToken.split(' ');
|
|
34
43
|
const token = arr.length > 1 ? arr[1] : arr[0];
|
|
35
44
|
headers['Authorization'] = `Bearer ${token}`;
|
|
36
45
|
} else {
|
|
37
46
|
if (user.role == 'admin') {
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
|
|
47
|
+
const apiKey = Deno.env.get('ETORO_API_KEY');
|
|
48
|
+
const userKey = Deno.env.get('ETORO_USER_KEY');
|
|
49
|
+
|
|
50
|
+
if (!apiKey || apiKey === null || apiKey === undefined || apiKey === '') {
|
|
51
|
+
throw new eToroCredentialsMissingError('ETORO_API_KEY');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!userKey || userKey === null || userKey === undefined || userKey === '') {
|
|
55
|
+
throw new eToroCredentialsMissingError('ETORO_USER_KEY');
|
|
56
|
+
}
|
|
41
57
|
|
|
42
|
-
headers['x-api-key'] = apiKey
|
|
43
|
-
headers['x-user-key'] = userKey
|
|
58
|
+
headers['x-api-key'] = apiKey;
|
|
59
|
+
headers['x-user-key'] = userKey;
|
|
44
60
|
} else {
|
|
45
61
|
throw new Error('Unauthorized: Admin role required when SSO token is not provided');
|
|
46
62
|
}
|
package/package.json
CHANGED