Skip to content

Commit 64f7961

Browse files
committed
session: use clock for times
And be consistent with UTC conversions.
1 parent 8c4d17c commit 64f7961

File tree

5 files changed

+18
-11
lines changed

5 files changed

+18
-11
lines changed

session/interface.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,10 @@ type Session struct {
7373

7474
// buildSession creates a new session with the given user-defined parameters.
7575
func buildSession(id ID, localPrivKey *btcec.PrivateKey, label string, typ Type,
76-
expiry time.Time, serverAddr string, devServer bool, perms []bakery.Op,
77-
caveats []macaroon.Caveat, featureConfig FeaturesConfig,
78-
privacy bool, linkedGroupID *ID, flags PrivacyFlags) (*Session, error) {
76+
created, expiry time.Time, serverAddr string, devServer bool,
77+
perms []bakery.Op, caveats []macaroon.Caveat,
78+
featureConfig FeaturesConfig, privacy bool, linkedGroupID *ID,
79+
flags PrivacyFlags) (*Session, error) {
7980

8081
_, pairingSecret, err := mailbox.NewPassphraseEntropy()
8182
if err != nil {
@@ -98,8 +99,8 @@ func buildSession(id ID, localPrivKey *btcec.PrivateKey, label string, typ Type,
9899
Label: label,
99100
State: StateCreated,
100101
Type: typ,
101-
Expiry: expiry,
102-
CreatedAt: time.Now(),
102+
Expiry: expiry.UTC(),
103+
CreatedAt: created.UTC(),
103104
ServerAddr: serverAddr,
104105
DevServer: devServer,
105106
MacaroonRootKey: macRootKey,

session/kvdb_store.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,9 @@ func (db *BoltStore) NewSession(id ID, localPrivKey *btcec.PrivateKey,
194194
flags PrivacyFlags) (*Session, error) {
195195

196196
return buildSession(
197-
id, localPrivKey, label, typ, expiry, serverAddr, devServer,
198-
perms, caveats, featureConfig, privacy, linkedGroupID, flags,
197+
id, localPrivKey, label, typ, db.clock.Now(), expiry,
198+
serverAddr, devServer, perms, caveats, featureConfig, privacy,
199+
linkedGroupID, flags,
199200
)
200201
}
201202

@@ -424,7 +425,7 @@ func (db *BoltStore) RevokeSession(key *btcec.PublicKey) error {
424425
}
425426

426427
session.State = StateRevoked
427-
session.RevokedAt = time.Now()
428+
session.RevokedAt = db.clock.Now().UTC()
428429

429430
var buf bytes.Buffer
430431
if err := SerializeSession(&buf, session); err != nil {

session/store_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ func newSession(t *testing.T, db Store, label string,
290290

291291
session, err := buildSession(
292292
id, priv, label, TypeMacaroonAdmin,
293+
time.Now(),
293294
time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC),
294295
"foo.bar.baz:1234", true, nil, nil, nil, true, linkedGroupID,
295296
[]PrivacyFlag{ClearPubkeys},

session/tlv.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ func DeserializeSession(r io.Reader) (*Session, error) {
237237
session.Label = string(label)
238238
session.State = State(state)
239239
session.Type = Type(typ)
240-
session.Expiry = time.Unix(int64(expiry), 0)
241-
session.CreatedAt = time.Unix(int64(createdAt), 0)
240+
session.Expiry = time.Unix(int64(expiry), 0).UTC()
241+
session.CreatedAt = time.Unix(int64(createdAt), 0).UTC()
242242
session.ServerAddr = string(serverAddr)
243243
session.DevServer = devServer == 1
244244
session.WithPrivacyMapper = privacy == 1
@@ -248,7 +248,7 @@ func DeserializeSession(r io.Reader) (*Session, error) {
248248
}
249249

250250
if revokedAt != 0 {
251-
session.RevokedAt = time.Unix(int64(revokedAt), 0)
251+
session.RevokedAt = time.Unix(int64(revokedAt), 0).UTC()
252252
}
253253

254254
if t, ok := parsedTypes[typeMacaroonRecipe]; ok && t == nil {

session/tlv_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ func TestSerializeDeserializeSession(t *testing.T) {
131131

132132
session, err := buildSession(
133133
id, priv, test.name, test.sessType,
134+
time.Now(),
134135
time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC),
135136
"foo.bar.baz:1234", true, test.perms,
136137
test.caveats, test.featureConfig, true,
@@ -185,6 +186,7 @@ func TestGroupIDForOlderSessions(t *testing.T) {
185186

186187
session, err := buildSession(
187188
id, priv, "test-session", TypeMacaroonAdmin,
189+
time.Now(),
188190
time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC),
189191
"foo.bar.baz:1234", true, nil, nil, nil, false, nil,
190192
PrivacyFlags{},
@@ -220,6 +222,7 @@ func TestGroupID(t *testing.T) {
220222
// Create session 1 which is not linked to any previous session.
221223
session1, err := buildSession(
222224
id, priv, "test-session", TypeMacaroonAdmin,
225+
time.Now(),
223226
time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC),
224227
"foo.bar.baz:1234", true, nil, nil, nil, false, nil,
225228
PrivacyFlags{},
@@ -234,6 +237,7 @@ func TestGroupID(t *testing.T) {
234237
require.NoError(t, err)
235238
session2, err := buildSession(
236239
id, priv, "test-session", TypeMacaroonAdmin,
240+
time.Now(),
237241
time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC),
238242
"foo.bar.baz:1234", true, nil, nil, nil, false,
239243
&session1.GroupID, PrivacyFlags{},

0 commit comments

Comments
 (0)