@@ -134,7 +134,21 @@ func (s *sessionRpcServer) AddSession(_ context.Context,
134
134
return nil , err
135
135
}
136
136
137
- var permissions []bakery.Op
137
+ // Store the entity-action permission pairs in a map in order to
138
+ // de-dup any repeat perms.
139
+ permissions := make (map [string ]map [string ]struct {})
140
+
141
+ // addPerm is a closure that can be used to add entity-action pairs to
142
+ // the permissions map.
143
+ addPerm := func (entity , action string ) {
144
+ _ , ok := permissions [entity ]
145
+ if ! ok {
146
+ permissions [entity ] = make (map [string ]struct {})
147
+ }
148
+
149
+ permissions [entity ][action ] = struct {}{}
150
+ }
151
+
138
152
switch typ {
139
153
// For the default session types we use empty caveats and permissions,
140
154
// the macaroons are baked correctly when creating the session.
@@ -152,10 +166,7 @@ func (s *sessionRpcServer) AddSession(_ context.Context,
152
166
153
167
for _ , op := range req .MacaroonCustomPermissions {
154
168
if op .Entity != macaroons .PermissionEntityCustomURI {
155
- permissions = append (permissions , bakery.Op {
156
- Entity : op .Entity ,
157
- Action : op .Action ,
158
- })
169
+ addPerm (op .Entity , op .Action )
159
170
160
171
continue
161
172
}
@@ -169,7 +180,9 @@ func (s *sessionRpcServer) AddSession(_ context.Context,
169
180
true ,
170
181
)
171
182
172
- permissions = append (permissions , readPerms ... )
183
+ for _ , p := range readPerms {
184
+ addPerm (p .Entity , p .Action )
185
+ }
173
186
174
187
continue
175
188
}
@@ -181,12 +194,7 @@ func (s *sessionRpcServer) AddSession(_ context.Context,
181
194
// the matching URIs returned from the
182
195
// permissions' manager.
183
196
for _ , uri := range uris {
184
- permissions = append (
185
- permissions , bakery.Op {
186
- Entity : op .Entity ,
187
- Action : uri ,
188
- },
189
- )
197
+ addPerm (op .Entity , uri )
190
198
}
191
199
continue
192
200
}
@@ -199,10 +207,7 @@ func (s *sessionRpcServer) AddSession(_ context.Context,
199
207
"LiT" , op .Action )
200
208
}
201
209
202
- permissions = append (permissions , bakery.Op {
203
- Entity : op .Entity ,
204
- Action : op .Action ,
205
- })
210
+ addPerm (op .Entity , op .Action )
206
211
}
207
212
208
213
// No other types are currently supported.
@@ -211,9 +216,20 @@ func (s *sessionRpcServer) AddSession(_ context.Context,
211
216
"readonly and custom macaroon types supported in LiT" )
212
217
}
213
218
219
+ // Collect the de-duped permissions.
220
+ var perms []bakery.Op
221
+ for entity , actions := range permissions {
222
+ for action := range actions {
223
+ perms = append (perms , bakery.Op {
224
+ Entity : entity ,
225
+ Action : action ,
226
+ })
227
+ }
228
+ }
229
+
214
230
sess , err := session .NewSession (
215
231
req .Label , typ , expiry , req .MailboxServerAddr , req .DevServer ,
216
- permissions , nil ,
232
+ perms , nil ,
217
233
)
218
234
if err != nil {
219
235
return nil , fmt .Errorf ("error creating new session: %v" , err )
0 commit comments