Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit 09d27b0

Browse files
authoredNov 2, 2020
Merge pull request #277 from mozilla/issue276
Do not require @hapi/sntp anymore
2 parents cf105e0 + 51c08e2 commit 09d27b0

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed
 

‎API.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,19 @@ Request(requestOptions, function (error, response, body) {
158158
});
159159
```
160160

161-
**Hawk** utilized the [**SNTP**](https://github.com/hueniverse/sntp) module for time sync management. By default, the local
162-
machine time is used. To automatically retrieve and synchronize the clock within the application, use the SNTP 'start()' method.
161+
## Time Sync
162+
163+
**Hawk** can utilize the [`@hapi/sntp`](https://github.com/outmoded/sntp) module for time sync management.
164+
By default, the local machine time is used.
165+
To automatically retrieve and synchronize the clock within the application, add `@hapi/sntp` to your application and initialize it at the top level:
163166

164167
```js
165-
Hawk.sntp.start();
166-
```
168+
const Sntp = require('@hapi/sntp');
169+
const Hawk = require('hawk');
167170

171+
Sntp.start();
172+
Hawk.utils.setTimeFunction(Sntp.now)
173+
```
168174

169175
## Protocol Example
170176

‎lib/index.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
'use strict';
22

33

4-
exports.sntp = require('@hapi/sntp');
5-
64
exports.server = require('./server');
75

86
exports.client = require('./client');

‎lib/utils.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const Boom = require('@hapi/boom');
4-
const Sntp = require('@hapi/sntp');
54

65

76
const internals = {};
@@ -93,9 +92,20 @@ exports.parseRequest = function (req, options) {
9392
};
9493

9594

95+
let _now = Date.now;
96+
97+
98+
// override the `now` function, e.g., to use sntp
99+
100+
exports.setTimeFunction = function (fn) {
101+
102+
_now = fn;
103+
};
104+
105+
96106
exports.now = function (localtimeOffsetMsec) {
97107

98-
return Sntp.now() + (localtimeOffsetMsec || 0);
108+
return _now() + (localtimeOffsetMsec || 0);
99109
};
100110

101111

‎package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
"@hapi/b64": "5.x.x",
1818
"@hapi/boom": "9.x.x",
1919
"@hapi/cryptiles": "5.x.x",
20-
"@hapi/hoek": "9.x.x",
21-
"@hapi/sntp": "4.x.x"
20+
"@hapi/hoek": "9.x.x"
2221
},
2322
"devDependencies": {
2423
"@hapi/code": "8.x.x",

‎test/utils.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const Package = require('../package.json');
1010
const internals = {};
1111

1212

13-
const { describe, it } = exports.lab = Lab.script();
13+
const { describe, it, after } = exports.lab = Lab.script();
1414
const expect = Code.expect;
1515

1616

@@ -119,6 +119,20 @@ describe('Utils', () => {
119119
});
120120
});
121121

122+
describe('setTimeFunction()', () => {
123+
124+
after(() => {
125+
126+
Hawk.utils.setTimeFunction(Date.now);
127+
});
128+
129+
it('creates the value that now() will return', () => {
130+
131+
Hawk.utils.setTimeFunction(() => 269323200000);
132+
expect(Hawk.utils.now()).to.equal(269323200000);
133+
});
134+
});
135+
122136
describe('unauthorized()', () => {
123137

124138
it('returns a hawk 401', () => {

0 commit comments

Comments
 (0)
This repository has been archived.