-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
102 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** @param {number} ms */ | ||
const msToUnixTimestamp = (ms) => Math.floor(ms / 1000); | ||
|
||
/** | ||
* Check if secure token is expired. It uses a threshold of 10 seconds by default. i.e. if the token is not expired yet | ||
* but will expire in the next 10 seconds, it will return false. | ||
* | ||
* @param {import('../types').SecureUploadsSignatureAndExpire} secureToken | ||
* @param {{ threshold?: number }} [options] | ||
*/ | ||
export const isSecureTokenExpired = (secureToken, { threshold } = { threshold: 10 * 1000 }) => { | ||
const { secureExpire } = secureToken; | ||
const nowUnix = msToUnixTimestamp(Date.now()); | ||
const expireUnix = Number(secureExpire); | ||
const thresholdUnix = msToUnixTimestamp(threshold); | ||
return nowUnix + thresholdUnix >= expireUnix; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { isSecureTokenExpired } from './isSecureTokenExpired'; | ||
import { expect } from '@esm-bundle/chai'; | ||
import * as sinon from 'sinon'; | ||
|
||
const DATE_NOW = 60 * 1000; | ||
|
||
describe('isSecureTokenExpired', () => { | ||
let clock; | ||
beforeEach(() => { | ||
clock = sinon.useFakeTimers(DATE_NOW); | ||
}); | ||
|
||
afterEach(() => { | ||
clock.restore(); | ||
}); | ||
|
||
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); | ||
}); | ||
|
||
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); | ||
}); | ||
|
||
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); | ||
}); | ||
}); |