mirror of
https://github.com/lightninglabs/lightning-terminal.git
synced 2026-08-13 12:33:36 +02:00
session: replace RevokeSession with ShiftState
This commit is contained in:
parent
8f22fc98a0
commit
1ed4907097
4 changed files with 27 additions and 47 deletions
|
|
@ -215,10 +215,6 @@ type Store interface {
|
|||
// that are in the given states.
|
||||
ListSessionsByState(...State) ([]*Session, error)
|
||||
|
||||
// RevokeSession updates the state of the session with the given local
|
||||
// public key to be revoked.
|
||||
RevokeSession(*btcec.PublicKey) error
|
||||
|
||||
// UpdateSessionRemotePubKey can be used to add the given remote pub key
|
||||
// to the session with the given local pub key.
|
||||
UpdateSessionRemotePubKey(localPubKey,
|
||||
|
|
|
|||
|
|
@ -555,35 +555,6 @@ func (db *BoltStore) ShiftState(id ID, dest State) error {
|
|||
})
|
||||
}
|
||||
|
||||
// RevokeSession updates the state of the session with the given local
|
||||
// public key to be revoked.
|
||||
//
|
||||
// NOTE: this is part of the Store interface.
|
||||
func (db *BoltStore) RevokeSession(key *btcec.PublicKey) error {
|
||||
var session *Session
|
||||
return db.Update(func(tx *bbolt.Tx) error {
|
||||
sessionBucket, err := getBucket(tx, sessionBucketKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sessionBytes := sessionBucket.Get(key.SerializeCompressed())
|
||||
if len(sessionBytes) == 0 {
|
||||
return ErrSessionNotFound
|
||||
}
|
||||
|
||||
session, err = DeserializeSession(bytes.NewReader(sessionBytes))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
session.State = StateRevoked
|
||||
session.RevokedAt = db.clock.Now().UTC()
|
||||
|
||||
return putSession(sessionBucket, session)
|
||||
})
|
||||
}
|
||||
|
||||
// GetSessionByID fetches the session with the given ID.
|
||||
//
|
||||
// NOTE: this is part of the Store interface.
|
||||
|
|
|
|||
|
|
@ -106,7 +106,7 @@ func TestBasicSessionStore(t *testing.T) {
|
|||
require.Equal(t, session1.State, StateCreated)
|
||||
|
||||
// Now revoke the session and assert that the state is revoked.
|
||||
require.NoError(t, db.RevokeSession(s1.LocalPublicKey))
|
||||
require.NoError(t, db.ShiftState(s1.ID, StateRevoked))
|
||||
s1, err = db.GetSession(s1.LocalPublicKey)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, s1.State, StateRevoked)
|
||||
|
|
@ -225,7 +225,7 @@ func TestLinkingSessions(t *testing.T) {
|
|||
require.ErrorContains(t, db.CreateSession(s2), "is still active")
|
||||
|
||||
// Revoke the first session.
|
||||
require.NoError(t, db.RevokeSession(s1.LocalPublicKey))
|
||||
require.NoError(t, db.ShiftState(s1.ID, StateRevoked))
|
||||
|
||||
// Persisting the second linked session should now work.
|
||||
require.NoError(t, db.CreateSession(s2))
|
||||
|
|
@ -248,16 +248,20 @@ func TestLinkedSessions(t *testing.T) {
|
|||
// the same group. The group ID is equivalent to the session ID of the
|
||||
// first session.
|
||||
s1 := newSession(t, db, clock, "session 1")
|
||||
s2 := newSession(t, db, clock, "session 2", withLinkedGroupID(&s1.GroupID))
|
||||
s3 := newSession(t, db, clock, "session 3", withLinkedGroupID(&s2.GroupID))
|
||||
s2 := newSession(
|
||||
t, db, clock, "session 2", withLinkedGroupID(&s1.GroupID),
|
||||
)
|
||||
s3 := newSession(
|
||||
t, db, clock, "session 3", withLinkedGroupID(&s2.GroupID),
|
||||
)
|
||||
|
||||
// Persist the sessions.
|
||||
require.NoError(t, db.CreateSession(s1))
|
||||
|
||||
require.NoError(t, db.RevokeSession(s1.LocalPublicKey))
|
||||
require.NoError(t, db.ShiftState(s1.ID, StateRevoked))
|
||||
require.NoError(t, db.CreateSession(s2))
|
||||
|
||||
require.NoError(t, db.RevokeSession(s2.LocalPublicKey))
|
||||
require.NoError(t, db.ShiftState(s2.ID, StateRevoked))
|
||||
require.NoError(t, db.CreateSession(s3))
|
||||
|
||||
// Assert that the session ID to group ID index works as expected.
|
||||
|
|
@ -282,7 +286,7 @@ func TestLinkedSessions(t *testing.T) {
|
|||
|
||||
// Persist the sessions.
|
||||
require.NoError(t, db.CreateSession(s4))
|
||||
require.NoError(t, db.RevokeSession(s4.LocalPublicKey))
|
||||
require.NoError(t, db.ShiftState(s4.ID, StateRevoked))
|
||||
|
||||
require.NoError(t, db.CreateSession(s5))
|
||||
|
||||
|
|
@ -337,7 +341,7 @@ func TestCheckSessionGroupPredicate(t *testing.T) {
|
|||
require.False(t, ok)
|
||||
|
||||
// Revoke the first session.
|
||||
require.NoError(t, db.RevokeSession(s1.LocalPublicKey))
|
||||
require.NoError(t, db.ShiftState(s1.ID, StateRevoked))
|
||||
|
||||
// Add a new session to the same group as the first one.
|
||||
s2 := newSession(t, db, clock, "label 2", withLinkedGroupID(&s1.GroupID))
|
||||
|
|
|
|||
|
|
@ -154,8 +154,8 @@ func (s *sessionRpcServer) start(ctx context.Context) error {
|
|||
err)
|
||||
|
||||
if perm {
|
||||
err := s.cfg.db.RevokeSession(
|
||||
sess.LocalPublicKey,
|
||||
err := s.cfg.db.ShiftState(
|
||||
sess.ID, session.StateRevoked,
|
||||
)
|
||||
if err != nil {
|
||||
log.Errorf("error revoking "+
|
||||
|
|
@ -360,7 +360,8 @@ func (s *sessionRpcServer) resumeSession(ctx context.Context,
|
|||
log.Debugf("Not resuming session %x with expiry %s",
|
||||
pubKeyBytes, sess.Expiry)
|
||||
|
||||
if err := s.cfg.db.RevokeSession(pubKey); err != nil {
|
||||
err := s.cfg.db.ShiftState(sess.ID, session.StateRevoked)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error revoking session: %v", err)
|
||||
}
|
||||
|
||||
|
|
@ -436,7 +437,9 @@ func (s *sessionRpcServer) resumeSession(ctx context.Context,
|
|||
log.Debugf("Deadline for session %x has already "+
|
||||
"passed. Revoking session", pubKeyBytes)
|
||||
|
||||
return s.cfg.db.RevokeSession(pubKey)
|
||||
return s.cfg.db.ShiftState(
|
||||
sess.ID, session.StateRevoked,
|
||||
)
|
||||
}
|
||||
|
||||
// Start the deadline timer.
|
||||
|
|
@ -515,7 +518,7 @@ func (s *sessionRpcServer) resumeSession(ctx context.Context,
|
|||
log.Debugf("Error stopping session: %v", err)
|
||||
}
|
||||
|
||||
err = s.cfg.db.RevokeSession(pubKey)
|
||||
err = s.cfg.db.ShiftState(sess.ID, session.StateRevoked)
|
||||
if err != nil {
|
||||
log.Debugf("error revoking session: %v", err)
|
||||
}
|
||||
|
|
@ -557,7 +560,13 @@ func (s *sessionRpcServer) RevokeSession(ctx context.Context,
|
|||
return nil, fmt.Errorf("error parsing public key: %v", err)
|
||||
}
|
||||
|
||||
if err := s.cfg.db.RevokeSession(pubKey); err != nil {
|
||||
sess, err := s.cfg.db.GetSession(pubKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error fetching session: %v", err)
|
||||
}
|
||||
|
||||
err = s.cfg.db.ShiftState(sess.ID, session.StateRevoked)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error revoking session: %v", err)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue