@avakado.ai/schemas 1.1.14 → 1.1.15

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@avakado.ai/schemas",
3
- "version": "1.1.14",
3
+ "version": "1.1.15",
4
4
  "description": "Canonical Mongo schemas, models, enums and index declarations shared by the Avakado services.",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",
@@ -140,8 +140,16 @@ ApiAuthenticationSchema.methods._resolveProvider = async function () {
140
140
  ApiAuthenticationSchema.methods._plain = function (value) {
141
141
  return value?.toObject?.() ?? value ?? {};
142
142
  };
143
+ ApiAuthenticationSchema.methods._tokenFields = function (value) {
144
+ const plain = { ...this._plain(value) };
145
+ const nested = this._plain(plain.credentials);
146
+ const nestedIsToken = Boolean(nested && (nested.accessToken || nested.refreshToken || nested.apiToken || nested.basicToken));
147
+ if (!nestedIsToken) return plain;
148
+ delete plain.credentials;
149
+ return { ...plain, ...nested };
150
+ };
143
151
  ApiAuthenticationSchema.methods._enrichedCredentials = function () {
144
- const creds = { ...this._plain(this.credentials) };
152
+ const creds = this._tokenFields(this.credentials);
145
153
  const config = this._plain(this.config);
146
154
  if (this.authType === "apiKey") {
147
155
  creds.accountSid ??= config.AccountSid;
@@ -166,7 +174,8 @@ ApiAuthenticationSchema.methods.refreshToken = async function () {
166
174
  }
167
175
  const newTokensRes = await serviceProvider.refreshToken(this._enrichedCredentials());
168
176
  if (!newTokensRes.success) throw new Error(newTokensRes.error.message);
169
- this.credentials = { ...this.credentials, ...newTokensRes.data };
177
+ this.credentials = { ...this._enrichedCredentials(), ...this._tokenFields(newTokensRes.data) };
178
+ this.markModified("credentials");
170
179
  await this.save();
171
180
  return this;
172
181
  };
@@ -174,11 +183,19 @@ ApiAuthenticationSchema.methods.ensureValidToken = async function () {
174
183
  const serviceProvider = await this._resolveProvider();
175
184
  const providerSupportsRefresh = requireDep("providerSupportsRefresh", "ApiAuthenticators.ensureValidToken");
176
185
 
177
- if (this._isAccessTokenFresh()) return this;
186
+ if (this._plain(this.credentials).credentials) {
187
+ this.credentials = this._enrichedCredentials();
188
+ this.markModified("credentials");
189
+ await this.save();
190
+ }
191
+
192
+ const canRefresh = providerSupportsRefresh(serviceProvider);
193
+ const expiresAt = this._enrichedCredentials().expiresAt;
194
+ if (expiresAt && !this._isAccessTokenFresh() && canRefresh) return this.refreshToken();
178
195
 
179
- if (await serviceProvider.validateToken(this.credentials)) return this;
196
+ if (await serviceProvider.validateToken(this._enrichedCredentials())) return this;
180
197
 
181
- if (!providerSupportsRefresh(serviceProvider)) {
198
+ if (!canRefresh) {
182
199
  throw new Error(`Invalid or expired credentials for ${this.provider.name}; re-authentication required`);
183
200
  }
184
201