@oxyhq/core 2.4.1 → 3.1.0

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.
@@ -20,7 +20,7 @@ import { composeOxyServices } from './mixins/index.js';
20
20
  * - **Payment**: Payment processing
21
21
  * - **Karma**: Karma system
22
22
  * - **Assets**: File upload and asset management
23
- * - **Developer**: Developer API management
23
+ * - **Applications**: Application, membership, and credential management
24
24
  * - **Location**: Location-based features
25
25
  * - **Analytics**: Analytics tracking
26
26
  * - **Devices**: Device management
@@ -0,0 +1,211 @@
1
+ import { CACHE_TIMES } from './mixinHelpers.js';
2
+ export function OxyServicesApplicationsMixin(Base) {
3
+ return class extends Base {
4
+ constructor(...args) {
5
+ super(...args);
6
+ }
7
+ /**
8
+ * List applications the current user is an active member of.
9
+ */
10
+ async getApplications() {
11
+ try {
12
+ const res = await this.makeRequest('GET', '/applications', undefined, { cache: true, cacheTTL: CACHE_TIMES.MEDIUM });
13
+ return res.applications ?? [];
14
+ }
15
+ catch (error) {
16
+ throw this.handleError(error);
17
+ }
18
+ }
19
+ /**
20
+ * Create a new application. The caller becomes its `owner`.
21
+ * @param data - Application configuration. Staff-only fields are ignored.
22
+ */
23
+ async createApplication(data) {
24
+ try {
25
+ const res = await this.makeRequest('POST', '/applications', data, { cache: false });
26
+ return res.application;
27
+ }
28
+ catch (error) {
29
+ throw this.handleError(error);
30
+ }
31
+ }
32
+ /**
33
+ * Fetch a single application by id.
34
+ * @param applicationId - The application's Mongo `_id`.
35
+ */
36
+ async getApplication(applicationId) {
37
+ try {
38
+ const res = await this.makeRequest('GET', `/applications/${applicationId}`, undefined, { cache: true, cacheTTL: CACHE_TIMES.LONG });
39
+ return res.application;
40
+ }
41
+ catch (error) {
42
+ throw this.handleError(error);
43
+ }
44
+ }
45
+ /**
46
+ * Update an application's mutable fields.
47
+ * @param applicationId - The application's Mongo `_id`.
48
+ * @param data - Subset of updatable fields. Staff-only fields are ignored.
49
+ */
50
+ async updateApplication(applicationId, data) {
51
+ try {
52
+ const res = await this.makeRequest('PATCH', `/applications/${applicationId}`, data, { cache: false });
53
+ return res.application;
54
+ }
55
+ catch (error) {
56
+ throw this.handleError(error);
57
+ }
58
+ }
59
+ /**
60
+ * Soft-delete an application (owner only).
61
+ * @param applicationId - The application's Mongo `_id`.
62
+ */
63
+ async deleteApplication(applicationId) {
64
+ try {
65
+ return await this.makeRequest('DELETE', `/applications/${applicationId}`, undefined, { cache: false });
66
+ }
67
+ catch (error) {
68
+ throw this.handleError(error);
69
+ }
70
+ }
71
+ /**
72
+ * List members of an application.
73
+ * @param applicationId - The application's Mongo `_id`.
74
+ */
75
+ async getApplicationMembers(applicationId) {
76
+ try {
77
+ const res = await this.makeRequest('GET', `/applications/${applicationId}/members`, undefined, { cache: true, cacheTTL: CACHE_TIMES.MEDIUM });
78
+ return res.members ?? [];
79
+ }
80
+ catch (error) {
81
+ throw this.handleError(error);
82
+ }
83
+ }
84
+ /**
85
+ * Add a member to an application.
86
+ * @param applicationId - The application's Mongo `_id`.
87
+ * @param data - Target user id and role (never `owner`).
88
+ */
89
+ async inviteApplicationMember(applicationId, data) {
90
+ try {
91
+ const res = await this.makeRequest('POST', `/applications/${applicationId}/members`, data, { cache: false });
92
+ return res.member;
93
+ }
94
+ catch (error) {
95
+ throw this.handleError(error);
96
+ }
97
+ }
98
+ /**
99
+ * Change a member's role.
100
+ * @param applicationId - The application's Mongo `_id`.
101
+ * @param memberId - The member's Mongo `_id`.
102
+ * @param data - New role.
103
+ */
104
+ async updateApplicationMember(applicationId, memberId, data) {
105
+ try {
106
+ const res = await this.makeRequest('PATCH', `/applications/${applicationId}/members/${memberId}`, data, { cache: false });
107
+ return res.member;
108
+ }
109
+ catch (error) {
110
+ throw this.handleError(error);
111
+ }
112
+ }
113
+ /**
114
+ * Remove a member from an application.
115
+ * @param applicationId - The application's Mongo `_id`.
116
+ * @param memberId - The member's Mongo `_id`.
117
+ */
118
+ async removeApplicationMember(applicationId, memberId) {
119
+ try {
120
+ return await this.makeRequest('DELETE', `/applications/${applicationId}/members/${memberId}`, undefined, { cache: false });
121
+ }
122
+ catch (error) {
123
+ throw this.handleError(error);
124
+ }
125
+ }
126
+ /**
127
+ * Transfer ownership of an application to another member (owner only).
128
+ * Demotes the current owner to `admin` and promotes the target to `owner`.
129
+ * @param applicationId - The application's Mongo `_id`.
130
+ * @param data - Target user id.
131
+ */
132
+ async transferApplicationOwnership(applicationId, data) {
133
+ try {
134
+ return await this.makeRequest('POST', `/applications/${applicationId}/transfer-ownership`, data, { cache: false });
135
+ }
136
+ catch (error) {
137
+ throw this.handleError(error);
138
+ }
139
+ }
140
+ /**
141
+ * List an application's credentials. The response NEVER includes secrets.
142
+ * @param applicationId - The application's Mongo `_id`.
143
+ */
144
+ async getApplicationCredentials(applicationId) {
145
+ try {
146
+ const res = await this.makeRequest('GET', `/applications/${applicationId}/credentials`, undefined, { cache: true, cacheTTL: CACHE_TIMES.MEDIUM });
147
+ return res.credentials ?? [];
148
+ }
149
+ catch (error) {
150
+ throw this.handleError(error);
151
+ }
152
+ }
153
+ /**
154
+ * Create a credential. The plaintext `secret` is returned exactly ONCE;
155
+ * the server stores only a hash and will never return it again.
156
+ * @param applicationId - The application's Mongo `_id`.
157
+ * @param data - Credential configuration.
158
+ */
159
+ async createApplicationCredential(applicationId, data) {
160
+ try {
161
+ return await this.makeRequest('POST', `/applications/${applicationId}/credentials`, data, { cache: false });
162
+ }
163
+ catch (error) {
164
+ throw this.handleError(error);
165
+ }
166
+ }
167
+ /**
168
+ * Rotate a credential's secret. The new plaintext `secret` is returned
169
+ * exactly ONCE, along with audit fields: `rotatedFrom` (the previous
170
+ * credentialId) and `graceExpiresAt` (ISO string for the grace window during
171
+ * which the old credential is still honoured).
172
+ * @param applicationId - The application's Mongo `_id`.
173
+ * @param credentialId - The credential's Mongo `_id`.
174
+ */
175
+ async rotateApplicationCredential(applicationId, credentialId) {
176
+ try {
177
+ return await this.makeRequest('POST', `/applications/${applicationId}/credentials/${credentialId}/rotate`, undefined, { cache: false });
178
+ }
179
+ catch (error) {
180
+ throw this.handleError(error);
181
+ }
182
+ }
183
+ /**
184
+ * Revoke a credential (`status='revoked'`). Revoked credentials can no
185
+ * longer authenticate.
186
+ * @param applicationId - The application's Mongo `_id`.
187
+ * @param credentialId - The credential's Mongo `_id`.
188
+ */
189
+ async revokeApplicationCredential(applicationId, credentialId) {
190
+ try {
191
+ return await this.makeRequest('DELETE', `/applications/${applicationId}/credentials/${credentialId}`, undefined, { cache: false });
192
+ }
193
+ catch (error) {
194
+ throw this.handleError(error);
195
+ }
196
+ }
197
+ /**
198
+ * Fetch usage statistics for an application.
199
+ * @param applicationId - The application's Mongo `_id`.
200
+ * @param period - Time window (defaults to the server default).
201
+ */
202
+ async getApplicationUsage(applicationId, period) {
203
+ try {
204
+ return await this.makeRequest('GET', `/applications/${applicationId}/usage`, period ? { period } : undefined, { cache: true, cacheTTL: CACHE_TIMES.SHORT });
205
+ }
206
+ catch (error) {
207
+ throw this.handleError(error);
208
+ }
209
+ }
210
+ };
211
+ }
@@ -60,8 +60,8 @@ export function OxyServicesAuthMixin(Base) {
60
60
  * legitimate multi-tenant hosts that need to switch credentials cannot leak
61
61
  * one tenant's token to another tenant on the same instance.
62
62
  *
63
- * @param apiKey - DeveloperApp API key (oxy_dk_*)
64
- * @param apiSecret - DeveloperApp API secret
63
+ * @param apiKey - Application credential public key (oxy_dk_*)
64
+ * @param apiSecret - Application credential secret
65
65
  */
66
66
  configureServiceAuth(apiKey, apiSecret) {
67
67
  this._serviceApiKey = apiKey;
@@ -83,8 +83,8 @@ export function OxyServicesAuthMixin(Base) {
83
83
  * This prevents an attacker who learned a peer's apiKey from extracting
84
84
  * their service token by polling with a wrong secret.
85
85
  *
86
- * @param apiKey - DeveloperApp API key (optional if configureServiceAuth was called)
87
- * @param apiSecret - DeveloperApp API secret (optional if configureServiceAuth was called)
86
+ * @param apiKey - Application credential public key (optional if configureServiceAuth was called)
87
+ * @param apiSecret - Application credential secret (optional if configureServiceAuth was called)
88
88
  */
89
89
  async getServiceToken(apiKey, apiSecret) {
90
90
  const key = apiKey || this._serviceApiKey;
@@ -472,6 +472,9 @@ export function OxyServicesUtilityMixin(Base) {
472
472
  appId,
473
473
  appName: decoded.appName || 'unknown',
474
474
  scopes: Array.isArray(decoded.scopes) ? decoded.scopes : [],
475
+ ...(typeof decoded.credentialId === 'string' && decoded.credentialId.length > 0
476
+ ? { credentialId: decoded.credentialId }
477
+ : {}),
475
478
  };
476
479
  if (debug) {
477
480
  logger.debug(`[oxy.auth] Service token OK app=${decoded.appName} delegateUser=${oxyUserId || '(none)'}`, {
@@ -16,7 +16,7 @@ import { OxyServicesLanguageMixin } from './OxyServices.language.js';
16
16
  import { OxyServicesPaymentMixin } from './OxyServices.payment.js';
17
17
  import { OxyServicesKarmaMixin } from './OxyServices.karma.js';
18
18
  import { OxyServicesAssetsMixin } from './OxyServices.assets.js';
19
- import { OxyServicesDeveloperMixin } from './OxyServices.developer.js';
19
+ import { OxyServicesApplicationsMixin } from './OxyServices.applications.js';
20
20
  import { OxyServicesLocationMixin } from './OxyServices.location.js';
21
21
  import { OxyServicesAnalyticsMixin } from './OxyServices.analytics.js';
22
22
  import { OxyServicesDevicesMixin } from './OxyServices.devices.js';
@@ -60,7 +60,7 @@ const MIXIN_PIPELINE = [
60
60
  OxyServicesPaymentMixin,
61
61
  OxyServicesKarmaMixin,
62
62
  OxyServicesAssetsMixin,
63
- OxyServicesDeveloperMixin,
63
+ OxyServicesApplicationsMixin,
64
64
  OxyServicesLocationMixin,
65
65
  OxyServicesAnalyticsMixin,
66
66
  OxyServicesDevicesMixin,