@@ -19,6 +19,7 @@ use nexus_db_schema::schema::device_access_token;
19
19
use omicron_common:: api:: external:: CreateResult ;
20
20
use omicron_common:: api:: external:: DataPageParams ;
21
21
use omicron_common:: api:: external:: Error ;
22
+ use omicron_common:: api:: external:: InternalContext ;
22
23
use omicron_common:: api:: external:: ListResultVec ;
23
24
use omicron_common:: api:: external:: LookupResult ;
24
25
use omicron_common:: api:: external:: LookupType ;
@@ -181,19 +182,25 @@ impl DataStore {
181
182
} )
182
183
}
183
184
184
- pub async fn device_access_tokens_list (
185
+ // Similar to session hard delete and silo group list, we do not do a
186
+ // typical authz check, instead effectively encoding the policy here that
187
+ // any user is allowed to list and delete their own tokens. When we add the
188
+ // ability for silo admins to list and delete tokens from any user, we will
189
+ // have to model these permissions properly in the polar policy.
190
+
191
+ pub async fn current_user_token_list (
185
192
& self ,
186
193
opctx : & OpContext ,
187
- authz_user : & authz:: SiloUser ,
188
194
pagparams : & DataPageParams < ' _ , Uuid > ,
189
195
) -> ListResultVec < DeviceAccessToken > {
190
- // TODO: this authz check can't be right can it? or at least, we
191
- // should probably handle this explicitly at the policy level
192
- opctx. authorize ( authz:: Action :: ListChildren , authz_user) . await ?;
196
+ let & actor = opctx
197
+ . authn
198
+ . actor_required ( )
199
+ . internal_context ( "listing current user's tokens" ) ?;
193
200
194
201
use nexus_db_schema:: schema:: device_access_token:: dsl;
195
202
paginated ( dsl:: device_access_token, dsl:: id, & pagparams)
196
- . filter ( dsl:: silo_user_id. eq ( authz_user . id ( ) ) )
203
+ . filter ( dsl:: silo_user_id. eq ( actor . actor_id ( ) ) )
197
204
// we don't have time_deleted on tokens. unfortunately this is not
198
205
// indexed well. maybe it can be!
199
206
. filter (
@@ -207,19 +214,20 @@ impl DataStore {
207
214
. map_err ( |e| public_error_from_diesel ( e, ErrorHandler :: Server ) )
208
215
}
209
216
210
- pub async fn device_access_token_delete (
217
+ pub async fn current_user_token_delete (
211
218
& self ,
212
219
opctx : & OpContext ,
213
- authz_user : & authz:: SiloUser ,
214
220
token_id : Uuid ,
215
221
) -> Result < ( ) , Error > {
216
- // TODO: surely this is the wrong permission
217
- opctx. authorize ( authz:: Action :: Modify , authz_user) . await ?;
222
+ let & actor = opctx
223
+ . authn
224
+ . actor_required ( )
225
+ . internal_context ( "deleting current user's token" ) ?;
218
226
219
227
use nexus_db_schema:: schema:: device_access_token:: dsl;
220
228
let num_deleted = diesel:: delete ( dsl:: device_access_token)
229
+ . filter ( dsl:: silo_user_id. eq ( actor. actor_id ( ) ) )
221
230
. filter ( dsl:: id. eq ( token_id) )
222
- . filter ( dsl:: silo_user_id. eq ( authz_user. id ( ) ) )
223
231
. execute_async ( & * self . pool_connection_authorized ( opctx) . await ?)
224
232
. await
225
233
. map_err ( |e| public_error_from_diesel ( e, ErrorHandler :: Server ) ) ?;
0 commit comments