-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(@aws-amplify/core): Support clock skew (#4844)
* fix(@aws-amplify/core): AWS.config.systemClockOffset for signing requests * Add Util/Date to support new Date() with clock offset * Add tests for Utils.Date * Signer users Util.Date * Remove unused "date" for mocking * Rename Date to DateUtils to allow "import { DateUtils }" * Reference DateUtils directly vs. Utils.DateUtils * Move DateUtils.test.ts so that core can build * Add annotation to clockOffset for documentation & intellisense * Fix test path * Improve JSDoc Co-authored-by: Thiago Hirata <hirata@devcase.com.br> Co-authored-by: Sam Martinez <sammartinez19@gmail.com>
- Loading branch information
1 parent
1ed9f0a
commit 8156cc9
Showing
5 changed files
with
83 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { DateUtils } from '../src/Util/DateUtils'; | ||
|
||
// Mock Date (https://github.com/facebook/jest/issues/2234#issuecomment-308121037) | ||
const staticDate = new Date('2020-01-01'); | ||
// @ts-ignore Type 'typeof Date' is not assignable to type 'DateConstructor'. | ||
Date = class extends Date { | ||
// @ts-ignore Constructors for derived classes must contain a 'super' call.ts(2377) | ||
constructor() { | ||
return staticDate; | ||
} | ||
}; | ||
|
||
describe('Date', () => { | ||
describe('getDateWithClockOffset()', () => { | ||
it('should return a new Date()', () => { | ||
expect(DateUtils.getDateWithClockOffset()).toEqual(new Date()); | ||
}); | ||
}); | ||
|
||
describe('getClockOffset()', () => { | ||
it('should default to 0', () => { | ||
expect(DateUtils.getClockOffset()).toEqual(0); | ||
}); | ||
}); | ||
|
||
describe('with setClockOffset()', () => { | ||
beforeAll(() => { | ||
DateUtils.setClockOffset(1000); | ||
}); | ||
|
||
describe('getDate()', () => { | ||
expect(DateUtils.getDateWithClockOffset()).toEqual( | ||
new Date(new Date().getTime() + 1000) | ||
); | ||
}); | ||
}); | ||
}); |
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,35 @@ | ||
/** | ||
* Date & time utility functions to abstract the `aws-sdk` away from users. | ||
* (v2 => v3 modularization is a breaking change) | ||
* | ||
* @see https://github.com/aws/aws-sdk-js/blob/6edf586dcc1de7fe8fbfbbd9a0d2b1847921e6e1/lib/util.js#L262 | ||
*/ | ||
|
||
export const DateUtils = { | ||
/** | ||
* Milliseconds to offset the date to compensate for clock skew between device & services | ||
*/ | ||
clockOffset: 0, | ||
|
||
getDateWithClockOffset() { | ||
if (DateUtils.clockOffset) { | ||
return new Date(new Date().getTime() + DateUtils.clockOffset); | ||
} else { | ||
return new Date(); | ||
} | ||
}, | ||
|
||
/** | ||
* @returns {number} Clock offset in milliseconds | ||
*/ | ||
getClockOffset() { | ||
return DateUtils.clockOffset; | ||
}, | ||
|
||
/** | ||
* @param {number} offset Clock offset in milliseconds | ||
*/ | ||
setClockOffset(offset: number) { | ||
DateUtils.clockOffset = offset; | ||
}, | ||
}; |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './Retry'; | ||
export { default as Mutex } from './Mutex'; | ||
export { default as Reachability } from './Reachability'; | ||
export * from './DateUtils'; |