@oxyhq/core 1.11.10 → 1.11.11
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/dist/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/HttpService.js +13 -0
- package/dist/cjs/OxyServices.base.js +21 -0
- package/dist/cjs/mixins/OxyServices.managedAccounts.js +117 -0
- package/dist/cjs/mixins/OxyServices.utility.js +81 -2
- package/dist/cjs/mixins/index.js +2 -0
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/HttpService.js +13 -0
- package/dist/esm/OxyServices.base.js +21 -0
- package/dist/esm/mixins/OxyServices.managedAccounts.js +114 -0
- package/dist/esm/mixins/OxyServices.utility.js +81 -2
- package/dist/esm/mixins/index.js +2 -0
- package/dist/types/.tsbuildinfo +1 -1
- package/dist/types/HttpService.d.ts +3 -0
- package/dist/types/OxyServices.base.d.ts +17 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/mixins/OxyServices.analytics.d.ts +2 -0
- package/dist/types/mixins/OxyServices.assets.d.ts +2 -0
- package/dist/types/mixins/OxyServices.auth.d.ts +2 -0
- package/dist/types/mixins/OxyServices.developer.d.ts +2 -0
- package/dist/types/mixins/OxyServices.devices.d.ts +2 -0
- package/dist/types/mixins/OxyServices.features.d.ts +5 -1
- package/dist/types/mixins/OxyServices.fedcm.d.ts +2 -0
- package/dist/types/mixins/OxyServices.karma.d.ts +2 -0
- package/dist/types/mixins/OxyServices.language.d.ts +2 -0
- package/dist/types/mixins/OxyServices.location.d.ts +2 -0
- package/dist/types/mixins/OxyServices.managedAccounts.d.ts +125 -0
- package/dist/types/mixins/OxyServices.payment.d.ts +2 -0
- package/dist/types/mixins/OxyServices.popup.d.ts +2 -0
- package/dist/types/mixins/OxyServices.privacy.d.ts +2 -0
- package/dist/types/mixins/OxyServices.redirect.d.ts +2 -0
- package/dist/types/mixins/OxyServices.security.d.ts +2 -0
- package/dist/types/mixins/OxyServices.topics.d.ts +2 -0
- package/dist/types/mixins/OxyServices.user.d.ts +2 -0
- package/dist/types/mixins/OxyServices.utility.d.ts +22 -0
- package/dist/types/models/interfaces.d.ts +2 -0
- package/package.json +1 -1
- package/src/HttpService.ts +17 -0
- package/src/OxyServices.base.ts +23 -0
- package/src/index.ts +1 -0
- package/src/mixins/OxyServices.managedAccounts.ts +147 -0
- package/src/mixins/OxyServices.utility.ts +103 -2
- package/src/mixins/index.ts +2 -0
- package/src/models/interfaces.ts +3 -0
|
@@ -138,6 +138,27 @@ export class OxyServicesBase {
|
|
|
138
138
|
getAccessToken() {
|
|
139
139
|
return this.httpService.getAccessToken();
|
|
140
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* Set the acting-as identity for managed accounts.
|
|
143
|
+
*
|
|
144
|
+
* When set, all subsequent API requests will include the `X-Acting-As` header,
|
|
145
|
+
* causing the server to attribute actions to the managed account. The
|
|
146
|
+
* authenticated user must be an authorized manager of the target account.
|
|
147
|
+
*
|
|
148
|
+
* Pass `null` to clear and revert to the authenticated user's own identity.
|
|
149
|
+
*
|
|
150
|
+
* @param userId - The managed account user ID, or null to clear
|
|
151
|
+
*/
|
|
152
|
+
setActingAs(userId) {
|
|
153
|
+
this.httpService.setActingAs(userId);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Get the current acting-as identity (managed account user ID), or null
|
|
157
|
+
* if operating as the authenticated user's own identity.
|
|
158
|
+
*/
|
|
159
|
+
getActingAs() {
|
|
160
|
+
return this.httpService.getActingAs();
|
|
161
|
+
}
|
|
141
162
|
/**
|
|
142
163
|
* Wait for authentication to be ready
|
|
143
164
|
*
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
export function OxyServicesManagedAccountsMixin(Base) {
|
|
2
|
+
return class extends Base {
|
|
3
|
+
constructor(...args) {
|
|
4
|
+
super(...args);
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Create a new managed account (sub-account).
|
|
8
|
+
*
|
|
9
|
+
* The server creates a User document with `isManagedAccount: true` and links
|
|
10
|
+
* it to the authenticated user as owner.
|
|
11
|
+
*/
|
|
12
|
+
async createManagedAccount(data) {
|
|
13
|
+
try {
|
|
14
|
+
return await this.makeRequest('POST', '/managed-accounts', data, {
|
|
15
|
+
cache: false,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
throw this.handleError(error);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* List all accounts the authenticated user manages.
|
|
24
|
+
*/
|
|
25
|
+
async getManagedAccounts() {
|
|
26
|
+
try {
|
|
27
|
+
return await this.makeRequest('GET', '/managed-accounts', undefined, {
|
|
28
|
+
cache: true,
|
|
29
|
+
cacheTTL: 2 * 60 * 1000, // 2 minutes cache
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
throw this.handleError(error);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Get details for a specific managed account.
|
|
38
|
+
*/
|
|
39
|
+
async getManagedAccountDetails(accountId) {
|
|
40
|
+
try {
|
|
41
|
+
return await this.makeRequest('GET', `/managed-accounts/${accountId}`, undefined, {
|
|
42
|
+
cache: true,
|
|
43
|
+
cacheTTL: 2 * 60 * 1000,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
throw this.handleError(error);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Update a managed account's profile data.
|
|
52
|
+
* Requires owner or admin role.
|
|
53
|
+
*/
|
|
54
|
+
async updateManagedAccount(accountId, data) {
|
|
55
|
+
try {
|
|
56
|
+
return await this.makeRequest('PUT', `/managed-accounts/${accountId}`, data, {
|
|
57
|
+
cache: false,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
throw this.handleError(error);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Delete a managed account permanently.
|
|
66
|
+
* Requires owner role.
|
|
67
|
+
*/
|
|
68
|
+
async deleteManagedAccount(accountId) {
|
|
69
|
+
try {
|
|
70
|
+
await this.makeRequest('DELETE', `/managed-accounts/${accountId}`, undefined, {
|
|
71
|
+
cache: false,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
throw this.handleError(error);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Add a manager to a managed account.
|
|
80
|
+
* Requires owner or admin role on the account.
|
|
81
|
+
*
|
|
82
|
+
* @param accountId - The managed account to add the manager to
|
|
83
|
+
* @param userId - The user to grant management access
|
|
84
|
+
* @param role - The role to assign: 'admin' or 'editor'
|
|
85
|
+
*/
|
|
86
|
+
async addManager(accountId, userId, role) {
|
|
87
|
+
try {
|
|
88
|
+
await this.makeRequest('POST', `/managed-accounts/${accountId}/managers`, { userId, role }, {
|
|
89
|
+
cache: false,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
throw this.handleError(error);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Remove a manager from a managed account.
|
|
98
|
+
* Requires owner role.
|
|
99
|
+
*
|
|
100
|
+
* @param accountId - The managed account
|
|
101
|
+
* @param userId - The manager to remove
|
|
102
|
+
*/
|
|
103
|
+
async removeManager(accountId, userId) {
|
|
104
|
+
try {
|
|
105
|
+
await this.makeRequest('DELETE', `/managed-accounts/${accountId}/managers/${userId}`, undefined, {
|
|
106
|
+
cache: false,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
throw this.handleError(error);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
}
|
|
@@ -10,6 +10,41 @@ export function OxyServicesUtilityMixin(Base) {
|
|
|
10
10
|
return class extends Base {
|
|
11
11
|
constructor(...args) {
|
|
12
12
|
super(...args);
|
|
13
|
+
/** @internal In-memory cache for acting-as verification results (TTL: 5 min) */
|
|
14
|
+
this._actingAsCache = new Map();
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Verify that a user is authorized to act as a managed account.
|
|
18
|
+
* Results are cached in-memory for 5 minutes to avoid repeated API calls.
|
|
19
|
+
*
|
|
20
|
+
* @internal Used by the auth() middleware — not part of the public API
|
|
21
|
+
*/
|
|
22
|
+
async verifyActingAs(userId, accountId) {
|
|
23
|
+
const cacheKey = `${userId}:${accountId}`;
|
|
24
|
+
const now = Date.now();
|
|
25
|
+
// Check cache
|
|
26
|
+
const cached = this._actingAsCache.get(cacheKey);
|
|
27
|
+
if (cached && cached.expiresAt > now) {
|
|
28
|
+
return cached.result;
|
|
29
|
+
}
|
|
30
|
+
// Query the API
|
|
31
|
+
try {
|
|
32
|
+
const result = await this.makeRequest('GET', '/managed-accounts/verify', { accountId, userId }, { cache: false, retry: false, timeout: 5000 });
|
|
33
|
+
// Cache successful result for 5 minutes
|
|
34
|
+
this._actingAsCache.set(cacheKey, {
|
|
35
|
+
result: result && result.authorized ? result : null,
|
|
36
|
+
expiresAt: now + 5 * 60 * 1000,
|
|
37
|
+
});
|
|
38
|
+
return result && result.authorized ? result : null;
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
// Cache negative result for 1 minute to avoid hammering on transient errors
|
|
42
|
+
this._actingAsCache.set(cacheKey, {
|
|
43
|
+
result: null,
|
|
44
|
+
expiresAt: now + 1 * 60 * 1000,
|
|
45
|
+
});
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
13
48
|
}
|
|
14
49
|
/**
|
|
15
50
|
* Fetch link metadata
|
|
@@ -72,6 +107,45 @@ export function OxyServicesUtilityMixin(Base) {
|
|
|
72
107
|
const oxyInstance = this;
|
|
73
108
|
// Return an async middleware function
|
|
74
109
|
return async (req, res, next) => {
|
|
110
|
+
// Process X-Acting-As header for managed account identity delegation.
|
|
111
|
+
// Called after successful authentication, before next(). If the header
|
|
112
|
+
// is present, verifies authorization and swaps the request identity to
|
|
113
|
+
// the managed account, preserving the original user for audit trails.
|
|
114
|
+
const processActingAs = async () => {
|
|
115
|
+
const actingAsUserId = req.headers['x-acting-as'];
|
|
116
|
+
if (!actingAsUserId)
|
|
117
|
+
return true; // No header, proceed normally
|
|
118
|
+
const verification = await oxyInstance.verifyActingAs(req.userId, actingAsUserId);
|
|
119
|
+
if (!verification) {
|
|
120
|
+
const error = {
|
|
121
|
+
error: 'ACTING_AS_UNAUTHORIZED',
|
|
122
|
+
message: 'Not authorized to act as this account',
|
|
123
|
+
code: 'ACTING_AS_UNAUTHORIZED',
|
|
124
|
+
status: 403,
|
|
125
|
+
};
|
|
126
|
+
if (onError) {
|
|
127
|
+
onError(error);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
res.status(403).json(error);
|
|
131
|
+
}
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
// Preserve original user for audit trails
|
|
135
|
+
req.originalUser = { id: req.userId, ...req.user };
|
|
136
|
+
req.actingAs = { userId: actingAsUserId, role: verification.role };
|
|
137
|
+
// Swap user identity to the managed account
|
|
138
|
+
req.userId = actingAsUserId;
|
|
139
|
+
req.user = { id: actingAsUserId };
|
|
140
|
+
// Also set _id for routes that use Pattern B (req.user._id)
|
|
141
|
+
if (req.user) {
|
|
142
|
+
req.user._id = actingAsUserId;
|
|
143
|
+
}
|
|
144
|
+
if (debug) {
|
|
145
|
+
console.log(`[oxy.auth] Acting as ${actingAsUserId} (role=${verification.role}) original=${req.originalUser.id}`);
|
|
146
|
+
}
|
|
147
|
+
return true;
|
|
148
|
+
};
|
|
75
149
|
try {
|
|
76
150
|
// Extract token from Authorization header or query params
|
|
77
151
|
const authHeader = req.headers['authorization'];
|
|
@@ -294,7 +368,10 @@ export function OxyServicesUtilityMixin(Base) {
|
|
|
294
368
|
if (debug) {
|
|
295
369
|
console.log(`[oxy.auth] OK user=${userId} session=${decoded.sessionId}`);
|
|
296
370
|
}
|
|
297
|
-
|
|
371
|
+
// Process X-Acting-As header before proceeding
|
|
372
|
+
if (await processActingAs())
|
|
373
|
+
return next();
|
|
374
|
+
return;
|
|
298
375
|
}
|
|
299
376
|
catch (validationError) {
|
|
300
377
|
if (debug) {
|
|
@@ -345,7 +422,9 @@ export function OxyServicesUtilityMixin(Base) {
|
|
|
345
422
|
if (debug) {
|
|
346
423
|
console.log(`[oxy.auth] OK user=${userId} (no session)`);
|
|
347
424
|
}
|
|
348
|
-
|
|
425
|
+
// Process X-Acting-As header before proceeding
|
|
426
|
+
if (await processActingAs())
|
|
427
|
+
next();
|
|
349
428
|
}
|
|
350
429
|
catch (error) {
|
|
351
430
|
const apiError = oxyInstance.handleError(error);
|
package/dist/esm/mixins/index.js
CHANGED
|
@@ -23,6 +23,7 @@ import { OxyServicesSecurityMixin } from './OxyServices.security.js';
|
|
|
23
23
|
import { OxyServicesUtilityMixin } from './OxyServices.utility.js';
|
|
24
24
|
import { OxyServicesFeaturesMixin } from './OxyServices.features.js';
|
|
25
25
|
import { OxyServicesTopicsMixin } from './OxyServices.topics.js';
|
|
26
|
+
import { OxyServicesManagedAccountsMixin } from './OxyServices.managedAccounts.js';
|
|
26
27
|
/**
|
|
27
28
|
* Mixin pipeline - applied in order from first to last.
|
|
28
29
|
*
|
|
@@ -60,6 +61,7 @@ const MIXIN_PIPELINE = [
|
|
|
60
61
|
OxyServicesSecurityMixin,
|
|
61
62
|
OxyServicesFeaturesMixin,
|
|
62
63
|
OxyServicesTopicsMixin,
|
|
64
|
+
OxyServicesManagedAccountsMixin,
|
|
63
65
|
// Utility (last, can use all above)
|
|
64
66
|
OxyServicesUtilityMixin,
|
|
65
67
|
];
|