Skip to content

Commit 7e88377

Browse files
committed
session: add RevokedAt field
This commit adds a RevokedAt field to the Session structure and sets the value of the field to the current time when RevokeSession is called.
1 parent 7b1b8e3 commit 7e88377

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

session/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type Session struct {
4747
Type Type
4848
Expiry time.Time
4949
CreatedAt time.Time
50+
RevokedAt time.Time
5051
ServerAddr string
5152
DevServer bool
5253
MacaroonRootKey uint64

session/store.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package session
33
import (
44
"bytes"
55
"errors"
6+
"time"
67

78
"github.com/btcsuite/btcd/btcec/v2"
89
"go.etcd.io/bbolt"
@@ -100,5 +101,7 @@ func (db *DB) RevokeSession(key *btcec.PublicKey) error {
100101
}
101102

102103
session.State = StateRevoked
104+
session.RevokedAt = time.Now()
105+
103106
return db.StoreSession(session)
104107
}

session/tlv.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const (
2525
typeRemotePublicKey tlv.Type = 11
2626
typeMacaroonRecipe tlv.Type = 12
2727
typeCreatedAt tlv.Type = 13
28+
typeRevokedAt tlv.Type = 14
2829

2930
// typeMacaroon is no longer used, but we leave it defined for backwards
3031
// compatibility.
@@ -58,8 +59,13 @@ func SerializeSession(w io.Writer, session *Session) error {
5859
pairingSecret = session.PairingSecret[:]
5960
privateKey = session.LocalPrivateKey.Serialize()
6061
createdAt = uint64(session.CreatedAt.Unix())
62+
revokedAt uint64
6163
)
6264

65+
if !session.RevokedAt.IsZero() {
66+
revokedAt = uint64(session.RevokedAt.Unix())
67+
}
68+
6369
if session.DevServer {
6470
devServer = 1
6571
}
@@ -105,6 +111,10 @@ func SerializeSession(w io.Writer, session *Session) error {
105111
tlvRecords, tlv.MakePrimitiveRecord(typeCreatedAt, &createdAt),
106112
)
107113

114+
tlvRecords = append(
115+
tlvRecords, tlv.MakePrimitiveRecord(typeRevokedAt, &revokedAt),
116+
)
117+
108118
tlvStream, err := tlv.NewStream(tlvRecords...)
109119
if err != nil {
110120
return err
@@ -117,12 +127,12 @@ func SerializeSession(w io.Writer, session *Session) error {
117127
// the data to be encoded in the tlv format.
118128
func DeserializeSession(r io.Reader) (*Session, error) {
119129
var (
120-
session = &Session{}
121-
label, serverAddr []byte
122-
pairingSecret, privateKey []byte
123-
state, typ, devServer uint8
124-
expiry, createdAt uint64
125-
macRecipe MacaroonRecipe
130+
session = &Session{}
131+
label, serverAddr []byte
132+
pairingSecret, privateKey []byte
133+
state, typ, devServer uint8
134+
expiry, createdAt, revokedAt uint64
135+
macRecipe MacaroonRecipe
126136
)
127137
tlvStream, err := tlv.NewStream(
128138
tlv.MakePrimitiveRecord(typeLabel, &label),
@@ -144,6 +154,7 @@ func DeserializeSession(r io.Reader) (*Session, error) {
144154
macaroonRecipeEncoder, macaroonRecipeDecoder,
145155
),
146156
tlv.MakePrimitiveRecord(typeCreatedAt, &createdAt),
157+
tlv.MakePrimitiveRecord(typeRevokedAt, &revokedAt),
147158
)
148159
if err != nil {
149160
return nil, err
@@ -162,6 +173,10 @@ func DeserializeSession(r io.Reader) (*Session, error) {
162173
session.ServerAddr = string(serverAddr)
163174
session.DevServer = devServer == 1
164175

176+
if revokedAt != 0 {
177+
session.RevokedAt = time.Unix(int64(revokedAt), 0)
178+
}
179+
165180
if t, ok := parsedTypes[typeMacaroonRecipe]; ok && t == nil {
166181
session.MacaroonRecipe = &macRecipe
167182
}

session/tlv_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,18 @@ var (
5151
// and deserialized from and to the tlv binary format successfully.
5252
func TestSerializeDeserializeSession(t *testing.T) {
5353
tests := []struct {
54-
name string
55-
sessType Type
56-
perms []bakery.Op
57-
caveats []macaroon.Caveat
54+
name string
55+
sessType Type
56+
revokedAt time.Time
57+
perms []bakery.Op
58+
caveats []macaroon.Caveat
5859
}{
5960
{
6061
name: "session 1",
6162
sessType: TypeMacaroonCustom,
63+
revokedAt: time.Date(
64+
2023, 1, 10, 10, 10, 0, 0, time.UTC,
65+
),
6266
},
6367
{
6468
name: "session 2",
@@ -78,6 +82,8 @@ func TestSerializeDeserializeSession(t *testing.T) {
7882
)
7983
require.NoError(t, err)
8084

85+
session.RevokedAt = test.revokedAt
86+
8187
_, remotePubKey := btcec.PrivKeyFromBytes(testRootKey)
8288
session.RemotePublicKey = remotePubKey
8389

@@ -95,10 +101,16 @@ func TestSerializeDeserializeSession(t *testing.T) {
95101
t, session.Expiry.Unix(),
96102
deserializedSession.Expiry.Unix(),
97103
)
104+
require.Equal(
105+
t, session.RevokedAt.Unix(),
106+
deserializedSession.RevokedAt.Unix(),
107+
)
98108
session.Expiry = time.Time{}
99109
deserializedSession.Expiry = time.Time{}
100110
session.CreatedAt = time.Time{}
101111
deserializedSession.CreatedAt = time.Time{}
112+
session.RevokedAt = time.Time{}
113+
deserializedSession.RevokedAt = time.Time{}
102114

103115
require.Equal(t, session, deserializedSession)
104116
})

0 commit comments

Comments
 (0)