@@ -120,10 +120,13 @@ type Session struct {
120
120
121
121
// buildSession creates a new session with the given user-defined parameters.
122
122
func buildSession (id ID , localPrivKey * btcec.PrivateKey , label string , typ Type ,
123
- created , expiry time.Time , serverAddr string , devServer bool ,
124
- perms []bakery.Op , caveats []macaroon.Caveat ,
125
- featureConfig FeaturesConfig , privacy bool , linkedGroupID * ID ,
126
- flags PrivacyFlags ) (* Session , error ) {
123
+ created , expiry time.Time , serverAddr string ,
124
+ options ... Option ) (* Session , error ) {
125
+
126
+ opts := defaultSessionOptions ()
127
+ for _ , o := range options {
128
+ o (opts )
129
+ }
127
130
128
131
_ , pairingSecret , err := mailbox .NewPassphraseEntropy ()
129
132
if err != nil {
@@ -135,10 +138,10 @@ func buildSession(id ID, localPrivKey *btcec.PrivateKey, label string, typ Type,
135
138
// The group ID will by default be the same as the Session ID
136
139
// unless this session links to a previous session.
137
140
groupID := id
138
- if linkedGroupID != nil {
141
+ if opts . linkedGroupID != nil {
139
142
// If this session is linked to a previous session, then the
140
143
// group ID is the same as the linked session's group ID.
141
- groupID = * linkedGroupID
144
+ groupID = * opts . linkedGroupID
142
145
}
143
146
144
147
sess := & Session {
@@ -149,29 +152,109 @@ func buildSession(id ID, localPrivKey *btcec.PrivateKey, label string, typ Type,
149
152
Expiry : expiry .UTC (),
150
153
CreatedAt : created .UTC (),
151
154
ServerAddr : serverAddr ,
152
- DevServer : devServer ,
155
+ DevServer : opts . devServer ,
153
156
MacaroonRootKey : macRootKey ,
154
157
PairingSecret : pairingSecret ,
155
158
LocalPrivateKey : localPrivKey ,
156
159
LocalPublicKey : localPrivKey .PubKey (),
157
160
RemotePublicKey : nil ,
158
- WithPrivacyMapper : privacy ,
159
- PrivacyFlags : flags ,
161
+ WithPrivacyMapper : opts . privacy ,
162
+ PrivacyFlags : opts . privacyFlags ,
160
163
GroupID : groupID ,
164
+ MacaroonRecipe : opts .macaroonRecipe ,
161
165
}
162
166
163
- if perms != nil || caveats != nil {
164
- sess .MacaroonRecipe = & MacaroonRecipe {
165
- Permissions : perms ,
166
- Caveats : caveats ,
167
- }
167
+ if len (opts .featureConfig ) != 0 {
168
+ sess .FeatureConfig = & opts .featureConfig
168
169
}
169
170
170
- if len (featureConfig ) != 0 {
171
- sess .FeatureConfig = & featureConfig
171
+ return sess , nil
172
+ }
173
+
174
+ // sessionOptions defines various options that can be tweaked via functional
175
+ // parameters for session creation.
176
+ type sessionOptions struct {
177
+ // privacy indicates if a privacy map should be used with this session.
178
+ privacy bool
179
+
180
+ // privacyFlags to use in combination with the session's privacy mapper.
181
+ privacyFlags PrivacyFlags
182
+
183
+ // featureConfig holds any feature configuration bytes to use for this
184
+ // session.
185
+ featureConfig FeaturesConfig
186
+
187
+ // linkedGroupID is the ID of the group that this session is linked
188
+ // to. By default, a session is not linked to another group.
189
+ linkedGroupID * ID
190
+
191
+ // devServer is true if TLS should be skipped when connecting to the
192
+ // mailbox server.
193
+ devServer bool
194
+
195
+ // macaroonRecipe holds the permissions and caveats that should be used
196
+ // to bake the macaroon to be used with this session.
197
+ macaroonRecipe * MacaroonRecipe
198
+ }
199
+
200
+ // defaultSessionOptions returns a new sessionOptions struct with default
201
+ // values set.
202
+ func defaultSessionOptions () * sessionOptions {
203
+ return & sessionOptions {
204
+ privacy : false ,
205
+ privacyFlags : PrivacyFlags {},
206
+ featureConfig : FeaturesConfig {},
207
+ linkedGroupID : nil ,
208
+ devServer : false ,
172
209
}
210
+ }
173
211
174
- return sess , nil
212
+ // Option defines the signature of a functional option that can be used to
213
+ // tweak various session creation options.
214
+ type Option func (* sessionOptions )
215
+
216
+ // WithPrivacy can be used to enable the privacy mapper for this session.
217
+ // An optional set of privacy flags can be provided to further customize the
218
+ // privacy mapper.
219
+ func WithPrivacy (flags PrivacyFlags ) Option {
220
+ return func (o * sessionOptions ) {
221
+ o .privacy = true
222
+ o .privacyFlags = flags
223
+ }
224
+ }
225
+
226
+ // WithFeatureConfig can be used to set the feature configuration bytes for
227
+ // this session.
228
+ func WithFeatureConfig (config FeaturesConfig ) Option {
229
+ return func (o * sessionOptions ) {
230
+ o .featureConfig = config
231
+ }
232
+ }
233
+
234
+ // WithLinkedGroupID can be used to link this session to a previous session.
235
+ func WithLinkedGroupID (groupID * ID ) Option {
236
+ return func (o * sessionOptions ) {
237
+ o .linkedGroupID = groupID
238
+ }
239
+ }
240
+
241
+ // WithDevServer can be used to set if TLS verification should be skipped when
242
+ // connecting to the mailbox server.
243
+ func WithDevServer () Option {
244
+ return func (o * sessionOptions ) {
245
+ o .devServer = true
246
+ }
247
+ }
248
+
249
+ // WithMacaroonRecipe can be used to set the permissions and caveats that
250
+ // should be used to bake the macaroon for a session.
251
+ func WithMacaroonRecipe (caveats []macaroon.Caveat , perms []bakery.Op ) Option {
252
+ return func (o * sessionOptions ) {
253
+ o .macaroonRecipe = & MacaroonRecipe {
254
+ Permissions : perms ,
255
+ Caveats : caveats ,
256
+ }
257
+ }
175
258
}
176
259
177
260
// IDToGroupIndex defines an interface for the session ID to group ID index.
@@ -191,9 +274,7 @@ type Store interface {
191
274
// parameters. The session will remain in the StateReserved state until
192
275
// ShiftState is called to update the state.
193
276
NewSession (label string , typ Type , expiry time.Time , serverAddr string ,
194
- devServer bool , perms []bakery.Op , caveats []macaroon.Caveat ,
195
- featureConfig FeaturesConfig , privacy bool , linkedGroupID * ID ,
196
- flags PrivacyFlags ) (* Session , error )
277
+ opts ... Option ) (* Session , error )
197
278
198
279
// GetSession fetches the session with the given key.
199
280
GetSession (key * btcec.PublicKey ) (* Session , error )
0 commit comments