@@ -19,6 +19,7 @@ use nexus_db_schema::schema::device_access_token;
1919use omicron_common:: api:: external:: CreateResult ;
2020use omicron_common:: api:: external:: DataPageParams ;
2121use omicron_common:: api:: external:: Error ;
22+ use omicron_common:: api:: external:: InternalContext ;
2223use omicron_common:: api:: external:: ListResultVec ;
2324use omicron_common:: api:: external:: LookupResult ;
2425use omicron_common:: api:: external:: LookupType ;
@@ -181,19 +182,25 @@ impl DataStore {
181182 } )
182183 }
183184
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 (
185192 & self ,
186193 opctx : & OpContext ,
187- authz_user : & authz:: SiloUser ,
188194 pagparams : & DataPageParams < ' _ , Uuid > ,
189195 ) -> 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" ) ?;
193200
194201 use nexus_db_schema:: schema:: device_access_token:: dsl;
195202 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 ( ) ) )
197204 // we don't have time_deleted on tokens. unfortunately this is not
198205 // indexed well. maybe it can be!
199206 . filter (
@@ -207,19 +214,20 @@ impl DataStore {
207214 . map_err ( |e| public_error_from_diesel ( e, ErrorHandler :: Server ) )
208215 }
209216
210- pub async fn device_access_token_delete (
217+ pub async fn current_user_token_delete (
211218 & self ,
212219 opctx : & OpContext ,
213- authz_user : & authz:: SiloUser ,
214220 token_id : Uuid ,
215221 ) -> 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" ) ?;
218226
219227 use nexus_db_schema:: schema:: device_access_token:: dsl;
220228 let num_deleted = diesel:: delete ( dsl:: device_access_token)
229+ . filter ( dsl:: silo_user_id. eq ( actor. actor_id ( ) ) )
221230 . filter ( dsl:: id. eq ( token_id) )
222- . filter ( dsl:: silo_user_id. eq ( authz_user. id ( ) ) )
223231 . execute_async ( & * self . pool_connection_authorized ( opctx) . await ?)
224232 . await
225233 . map_err ( |e| public_error_from_diesel ( e, ErrorHandler :: Server ) ) ?;
0 commit comments