diff --git a/session/interface.go b/session/interface.go index d29ee567..fa85f073 100644 --- a/session/interface.go +++ b/session/interface.go @@ -71,8 +71,8 @@ type Session struct { GroupID ID } -// NewSession creates a new session with the given user-defined parameters. -func NewSession(id ID, localPrivKey *btcec.PrivateKey, label string, typ Type, +// buildSession creates a new session with the given user-defined parameters. +func buildSession(id ID, localPrivKey *btcec.PrivateKey, label string, typ Type, expiry time.Time, serverAddr string, devServer bool, perms []bakery.Op, caveats []macaroon.Caveat, featureConfig FeaturesConfig, privacy bool, linkedGroupID *ID, flags PrivacyFlags) (*Session, error) { @@ -139,6 +139,18 @@ type IDToGroupIndex interface { // Store is the interface a persistent storage must implement for storing and // retrieving Terminal Connect sessions. type Store interface { + // NewSession creates a new session with the given user-defined + // parameters. + // + // NOTE: currently this purely a constructor of the Session type and + // does not make any database calls. This will be changed in a future + // commit. + NewSession(id ID, localPrivKey *btcec.PrivateKey, label string, + typ Type, expiry time.Time, serverAddr string, devServer bool, + perms []bakery.Op, caveats []macaroon.Caveat, + featureConfig FeaturesConfig, privacy bool, linkedGroupID *ID, + flags PrivacyFlags) (*Session, error) + // CreateSession adds a new session to the store. If a session with the // same local public key already exists an error is returned. This // can only be called with a Session with an ID that the Store has diff --git a/session/kvdb_store.go b/session/kvdb_store.go index 67b1003c..1c335d97 100644 --- a/session/kvdb_store.go +++ b/session/kvdb_store.go @@ -11,6 +11,8 @@ import ( "github.com/btcsuite/btcd/btcec/v2" "go.etcd.io/bbolt" + "gopkg.in/macaroon-bakery.v2/bakery" + "gopkg.in/macaroon.v2" ) var ( @@ -173,6 +175,24 @@ func getSessionKey(session *Session) []byte { return session.LocalPublicKey.SerializeCompressed() } +// NewSession creates a new session with the given user-defined parameters. +// +// NOTE: currently this purely a constructor of the Session type and does not +// make any database calls. This will be changed in a future commit. +// +// NOTE: this is part of the Store interface. +func (db *BoltStore) NewSession(id ID, localPrivKey *btcec.PrivateKey, + label string, typ Type, expiry time.Time, serverAddr string, + devServer bool, perms []bakery.Op, caveats []macaroon.Caveat, + featureConfig FeaturesConfig, privacy bool, linkedGroupID *ID, + flags PrivacyFlags) (*Session, error) { + + return buildSession( + id, localPrivKey, label, typ, expiry, serverAddr, devServer, + perms, caveats, featureConfig, privacy, linkedGroupID, flags, + ) +} + // CreateSession adds a new session to the store. If a session with the same // local public key already exists an error is returned. // diff --git a/session/store_test.go b/session/store_test.go index 1ebde58e..bc3962bd 100644 --- a/session/store_test.go +++ b/session/store_test.go @@ -285,7 +285,7 @@ func newSession(t *testing.T, db Store, label string, id, priv, err := db.GetUnusedIDAndKeyPair() require.NoError(t, err) - session, err := NewSession( + session, err := buildSession( id, priv, label, TypeMacaroonAdmin, time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC), "foo.bar.baz:1234", true, nil, nil, nil, true, linkedGroupID, diff --git a/session/tlv_test.go b/session/tlv_test.go index 6b46d1fb..2d62a841 100644 --- a/session/tlv_test.go +++ b/session/tlv_test.go @@ -129,7 +129,7 @@ func TestSerializeDeserializeSession(t *testing.T) { priv, id, err := NewSessionPrivKeyAndID() require.NoError(t, err) - session, err := NewSession( + session, err := buildSession( id, priv, test.name, test.sessType, time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC), "foo.bar.baz:1234", true, test.perms, @@ -183,7 +183,7 @@ func TestGroupIDForOlderSessions(t *testing.T) { priv, id, err := NewSessionPrivKeyAndID() require.NoError(t, err) - session, err := NewSession( + session, err := buildSession( id, priv, "test-session", TypeMacaroonAdmin, time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC), "foo.bar.baz:1234", true, nil, nil, nil, false, nil, @@ -218,7 +218,7 @@ func TestGroupID(t *testing.T) { require.NoError(t, err) // Create session 1 which is not linked to any previous session. - session1, err := NewSession( + session1, err := buildSession( id, priv, "test-session", TypeMacaroonAdmin, time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC), "foo.bar.baz:1234", true, nil, nil, nil, false, nil, @@ -232,7 +232,7 @@ func TestGroupID(t *testing.T) { // Create session 2 and link it to session 1. priv, id, err = NewSessionPrivKeyAndID() require.NoError(t, err) - session2, err := NewSession( + session2, err := buildSession( id, priv, "test-session", TypeMacaroonAdmin, time.Date(99999, 1, 1, 0, 0, 0, 0, time.UTC), "foo.bar.baz:1234", true, nil, nil, nil, false, diff --git a/session_rpcserver.go b/session_rpcserver.go index 203455d1..666744cd 100644 --- a/session_rpcserver.go +++ b/session_rpcserver.go @@ -318,7 +318,7 @@ func (s *sessionRpcServer) AddSession(ctx context.Context, return nil, err } - sess, err := session.NewSession( + sess, err := s.cfg.db.NewSession( id, localPrivKey, req.Label, typ, expiry, req.MailboxServerAddr, req.DevServer, uniquePermissions, caveats, nil, false, nil, session.PrivacyFlags{}, @@ -1148,7 +1148,7 @@ func (s *sessionRpcServer) AddAutopilotSession(ctx context.Context, return nil, err } - sess, err := session.NewSession( + sess, err := s.cfg.db.NewSession( id, localPrivKey, req.Label, session.TypeAutopilot, expiry, req.MailboxServerAddr, req.DevServer, perms, caveats, clientConfig, privacy, linkedGroupID, privacyFlags,