Skip to content

Commit

Permalink
feat: add secureUploadsExpireThreshold option
Browse files Browse the repository at this point in the history
  • Loading branch information
nd0ut committed May 24, 2024
1 parent 0574d8b commit 0d9205d
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 12 deletions.
9 changes: 8 additions & 1 deletion abstract/SecureUploadsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ export class SecureUploadsManager {
}

if (secureUploadsSignatureResolver) {
if (!this._secureToken || isSecureTokenExpired(this._secureToken)) {
if (
!this._secureToken ||
isSecureTokenExpired(this._secureToken, { threshold: this._block.cfg.secureUploadsExpireThreshold })
) {
if (!this._secureToken) {
this._debugPrint('Secure signature is not set yet.');
} else {
Expand All @@ -53,6 +56,10 @@ export class SecureUploadsManager {
console.error('Secure signature resolver returned an invalid result:', result);
} else {
this._debugPrint('Secure signature resolved:', result);
this._debugPrint(
'Secure signature will expire in',
new Date(Number(result.secureExpire) * 1000).toISOString(),
);
this._secureToken = result;
}
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion abstract/UploaderBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ export class UploaderBlock extends ActivityBlock {
async getUploadClientOptions() {
/** @type {SecureUploadsManager} */
const secureUploadsManager = this.$['*secureUploadsManager'];
const secureToken = await secureUploadsManager.getSecureToken();
const secureToken = await secureUploadsManager.getSecureToken().catch(() => null);

let options = {
store: this.cfg.store,
Expand Down
1 change: 1 addition & 0 deletions blocks/Config/initialConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const initialConfig = {
metadata: null,
localeName: 'en',
localeDefinitionOverride: null,
secureUploadsExpireThreshold: 10 * 60 * 1000,
secureUploadsSignatureResolver: null,
secureDeliveryProxyUrlResolver: null,
iconHrefResolver: null,
Expand Down
1 change: 1 addition & 0 deletions blocks/Config/normalizeConfigValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ const mapping = {
localeName: asString,

metadata: asMetadata,
secureUploadsExpireThreshold: asNumber,
localeDefinitionOverride: /** @type {typeof asObject<import('../../types').LocaleDefinitionOverride>} */ (asObject),
secureUploadsSignatureResolver:
/** @type {typeof asFunction<import('../../types').SecureUploadsSignatureResolver>} */ (asFunction),
Expand Down
3 changes: 2 additions & 1 deletion types/exported.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export type ConfigType = {
userAgentIntegration: string;
debug: boolean;
localeName: string;

secureUploadsExpireThreshold: number;

// Complex types
metadata: Metadata | MetadataCallback | null;
localeDefinitionOverride: LocaleDefinitionOverride | null;
Expand Down
4 changes: 2 additions & 2 deletions utils/isSecureTokenExpired.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const msToUnixTimestamp = (ms) => Math.floor(ms / 1000);
* but will expire in the next 10 seconds, it will return false.
*
* @param {import('../types').SecureUploadsSignatureAndExpire} secureToken
* @param {{ threshold?: number }} [options]
* @param {{ threshold?: number }} options
*/
export const isSecureTokenExpired = (secureToken, { threshold } = { threshold: 10 * 1000 }) => {
export const isSecureTokenExpired = (secureToken, { threshold }) => {
const { secureExpire } = secureToken;
const nowUnix = msToUnixTimestamp(Date.now());
const expireUnix = Number(secureExpire);
Expand Down
15 changes: 8 additions & 7 deletions utils/isSecureTokenExpired.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { expect } from '@esm-bundle/chai';
import * as sinon from 'sinon';

const DATE_NOW = 60 * 1000;
const THRESHOLD = 10 * 1000;

describe('isSecureTokenExpired', () => {
let clock;
Expand All @@ -15,18 +16,18 @@ describe('isSecureTokenExpired', () => {
});

it('should return true if the token is expired', () => {
expect(isSecureTokenExpired({ secureExpire: '0', secureSignature: '' })).to.equal(true);
expect(isSecureTokenExpired({ secureExpire: '59', secureSignature: '' })).to.equal(true);
expect(isSecureTokenExpired({ secureExpire: '0', secureSignature: '' }, { threshold: THRESHOLD })).to.equal(true);
expect(isSecureTokenExpired({ secureExpire: '59', secureSignature: '' }, { threshold: THRESHOLD })).to.equal(true);
});

it('should return true if the token will expire in the next 10 seconds', () => {
expect(isSecureTokenExpired({ secureExpire: '60', secureSignature: '' })).to.equal(true);
expect(isSecureTokenExpired({ secureExpire: '61', secureSignature: '' })).to.equal(true);
expect(isSecureTokenExpired({ secureExpire: '70', secureSignature: '' })).to.equal(true);
expect(isSecureTokenExpired({ secureExpire: '60', secureSignature: '' }, { threshold: THRESHOLD })).to.equal(true);
expect(isSecureTokenExpired({ secureExpire: '61', secureSignature: '' }, { threshold: THRESHOLD })).to.equal(true);
expect(isSecureTokenExpired({ secureExpire: '70', secureSignature: '' }, { threshold: THRESHOLD })).to.equal(true);
});

it("should return false if the token is not expired and won't expire in next 10 seconds", () => {
expect(isSecureTokenExpired({ secureExpire: '71', secureSignature: '' })).to.equal(false);
expect(isSecureTokenExpired({ secureExpire: '80', secureSignature: '' })).to.equal(false);
expect(isSecureTokenExpired({ secureExpire: '71', secureSignature: '' }, { threshold: THRESHOLD })).to.equal(false);
expect(isSecureTokenExpired({ secureExpire: '80', secureSignature: '' }, { threshold: THRESHOLD })).to.equal(false);
});
});

0 comments on commit 0d9205d

Please sign in to comment.