Skip to content

Commit 37f92f6

Browse files
committed
session: add ability to start in Created State
TODO: test
1 parent 5a28d56 commit 37f92f6

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

session/interface.go

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ const (
2727
type State uint8
2828

2929
/*
30-
/---> StateExpired
31-
StateReserved ---> StateCreated ---
32-
\---> StateRevoked
30+
31+
--> ------------------\ /---> StateExpired
32+
--> StateReserved ---> StateCreated ---
33+
\---> StateRevoked
3334
*/
3435

3536
const (
@@ -102,7 +103,12 @@ func buildSession(id ID, localPrivKey *btcec.PrivateKey, label string, typ Type,
102103
created, expiry time.Time, serverAddr string, devServer bool,
103104
perms []bakery.Op, caveats []macaroon.Caveat,
104105
featureConfig FeaturesConfig, privacy bool, linkedGroupID *ID,
105-
flags PrivacyFlags) (*Session, error) {
106+
flags PrivacyFlags, options ...Option) (*Session, error) {
107+
108+
opts := defaultSessionOptions()
109+
for _, o := range options {
110+
o(opts)
111+
}
106112

107113
_, pairingSecret, err := mailbox.NewPassphraseEntropy()
108114
if err != nil {
@@ -139,6 +145,10 @@ func buildSession(id ID, localPrivKey *btcec.PrivateKey, label string, typ Type,
139145
GroupID: groupID,
140146
}
141147

148+
if opts.immediateActivation {
149+
sess.State = StateCreated
150+
}
151+
142152
if perms != nil || caveats != nil {
143153
sess.MacaroonRecipe = &MacaroonRecipe{
144154
Permissions: perms,
@@ -163,6 +173,34 @@ type IDToGroupIndex interface {
163173
GetSessionIDs(groupID ID) ([]ID, error)
164174
}
165175

176+
// sessionOptions defines various options that can be tweaked via functional
177+
// parameters for session creation.
178+
type sessionOptions struct {
179+
// immediateActivation can be used to immediately move a session to
180+
// the StateCreated state on creation.
181+
immediateActivation bool
182+
}
183+
184+
// defaultSessionOptions returns a new sessionOptions struct with default
185+
// values set.
186+
func defaultSessionOptions() *sessionOptions {
187+
return &sessionOptions{
188+
immediateActivation: false,
189+
}
190+
}
191+
192+
// Option defines the signature of a functional option that can be used to
193+
// tweak various session creation options.
194+
type Option func(*sessionOptions)
195+
196+
// WithImmediateActivation can be used to immediately move a session to the
197+
// StateCreated state on creation.
198+
func WithImmediateActivation() Option {
199+
return func(o *sessionOptions) {
200+
o.immediateActivation = true
201+
}
202+
}
203+
166204
// Store is the interface a persistent storage must implement for storing and
167205
// retrieving Terminal Connect sessions.
168206
type Store interface {
@@ -172,7 +210,7 @@ type Store interface {
172210
NewSession(label string, typ Type, expiry time.Time, serverAddr string,
173211
devServer bool, perms []bakery.Op, caveats []macaroon.Caveat,
174212
featureConfig FeaturesConfig, privacy bool, linkedGroupID *ID,
175-
flags PrivacyFlags) (*Session, error)
213+
flags PrivacyFlags, opts ...Option) (*Session, error)
176214

177215
// CreateSession moves the given session from the StateReserved state to
178216
// the StateCreated state.

session/kvdb_store.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ func getSessionKey(session *Session) []byte {
190190
func (db *BoltStore) NewSession(label string, typ Type, expiry time.Time,
191191
serverAddr string, devServer bool, perms []bakery.Op,
192192
caveats []macaroon.Caveat, featureConfig FeaturesConfig, privacy bool,
193-
linkedGroupID *ID, flags PrivacyFlags) (*Session, error) {
193+
linkedGroupID *ID, flags PrivacyFlags, options ...Option) (*Session,
194+
error) {
194195

195196
var session *Session
196197
err := db.Update(func(tx *bbolt.Tx) error {
@@ -207,7 +208,7 @@ func (db *BoltStore) NewSession(label string, typ Type, expiry time.Time,
207208
session, err = buildSession(
208209
id, localPrivKey, label, typ, db.clock.Now(), expiry,
209210
serverAddr, devServer, perms, caveats, featureConfig,
210-
privacy, linkedGroupID, flags,
211+
privacy, linkedGroupID, flags, options...,
211212
)
212213
if err != nil {
213214
return err

0 commit comments

Comments
 (0)