Skip to content

Commit 0653424

Browse files
authored
Merge pull request #475 from ellemouton/storeRevokedAt
multi: add RevokedAt field to Session
2 parents 7b1b8e3 + 46745ce commit 0653424

File tree

7 files changed

+124
-63
lines changed

7 files changed

+124
-63
lines changed

litrpc/lit-sessions.pb.go

Lines changed: 70 additions & 53 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

litrpc/lit-sessions.proto

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ message Session {
9292
MacaroonRecipe macaroon_recipe = 12;
9393

9494
string account_id = 13;
95+
96+
/*
97+
The unix timestamp indicating the time at which the session was revoked.
98+
Note that this field has not been around since the beginning and so it
99+
could be the case that a session has been revoked but that this field
100+
will not have been set for that session. Therefore, it is suggested that
101+
readers should not assume that if this field is zero that the session is
102+
not revoked. Readers should instead first check the session_state field.
103+
*/
104+
uint64 revoked_at = 14 [jstype = JS_STRING];
95105
}
96106

97107
message MacaroonRecipe {

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: 18 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
}
@@ -103,6 +109,7 @@ func SerializeSession(w io.Writer, session *Session) error {
103109

104110
tlvRecords = append(
105111
tlvRecords, tlv.MakePrimitiveRecord(typeCreatedAt, &createdAt),
112+
tlv.MakePrimitiveRecord(typeRevokedAt, &revokedAt),
106113
)
107114

108115
tlvStream, err := tlv.NewStream(tlvRecords...)
@@ -117,12 +124,12 @@ func SerializeSession(w io.Writer, session *Session) error {
117124
// the data to be encoded in the tlv format.
118125
func DeserializeSession(r io.Reader) (*Session, error) {
119126
var (
120-
session = &Session{}
121-
label, serverAddr []byte
122-
pairingSecret, privateKey []byte
123-
state, typ, devServer uint8
124-
expiry, createdAt uint64
125-
macRecipe MacaroonRecipe
127+
session = &Session{}
128+
label, serverAddr []byte
129+
pairingSecret, privateKey []byte
130+
state, typ, devServer uint8
131+
expiry, createdAt, revokedAt uint64
132+
macRecipe MacaroonRecipe
126133
)
127134
tlvStream, err := tlv.NewStream(
128135
tlv.MakePrimitiveRecord(typeLabel, &label),
@@ -144,6 +151,7 @@ func DeserializeSession(r io.Reader) (*Session, error) {
144151
macaroonRecipeEncoder, macaroonRecipeDecoder,
145152
),
146153
tlv.MakePrimitiveRecord(typeCreatedAt, &createdAt),
154+
tlv.MakePrimitiveRecord(typeRevokedAt, &revokedAt),
147155
)
148156
if err != nil {
149157
return nil, err
@@ -162,6 +170,10 @@ func DeserializeSession(r io.Reader) (*Session, error) {
162170
session.ServerAddr = string(serverAddr)
163171
session.DevServer = devServer == 1
164172

173+
if revokedAt != 0 {
174+
session.RevokedAt = time.Unix(int64(revokedAt), 0)
175+
}
176+
165177
if t, ok := parsedTypes[typeMacaroonRecipe]; ok && t == nil {
166178
session.MacaroonRecipe = &macRecipe
167179
}

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
})

session_rpcserver.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,11 @@ func marshalRPCSession(sess *session.Session) (*litrpc.Session, error) {
519519

520520
macRecipe := marshalRPCMacaroonRecipe(sess.MacaroonRecipe)
521521

522+
var revokedAt uint64
523+
if !sess.RevokedAt.IsZero() {
524+
revokedAt = uint64(sess.RevokedAt.Unix())
525+
}
526+
522527
return &litrpc.Session{
523528
Label: sess.Label,
524529
SessionState: rpcState,
@@ -531,6 +536,7 @@ func marshalRPCSession(sess *session.Session) (*litrpc.Session, error) {
531536
LocalPublicKey: sess.LocalPublicKey.SerializeCompressed(),
532537
RemotePublicKey: remotePubKey,
533538
CreatedAt: uint64(sess.CreatedAt.Unix()),
539+
RevokedAt: revokedAt,
534540
MacaroonRecipe: macRecipe,
535541
}, nil
536542
}

0 commit comments

Comments
 (0)