@@ -12,7 +12,7 @@ use piquel_common::{
1212 events:: { DomainEvent , EventContext } ,
1313} ;
1414use piquel_crypto:: { ClientSecret , CryptoError } ;
15- use piquel_repository:: { Database , DatabaseError } ;
15+ use piquel_repository:: { Database , DatabaseError , DatabaseTransaction , StoredOidcClient } ;
1616use thiserror:: Error ;
1717
1818/// Creates, updates, enables, disables and rotates the secrets of clients.
@@ -40,6 +40,10 @@ pub enum ClientRegistryError {
4040 /// Public clients authenticate without a secret.
4141 #[ error( "client {0} is public, so it has no secret" ) ]
4242 Public ( ClientId ) ,
43+ /// An update changed the client's ID or kind, which would orphan its grants
44+ /// or secrets.
45+ #[ error( "client {0}'s ID and kind cannot change; register a new client instead" ) ]
46+ Immutable ( ClientId ) ,
4347 /// A secret could not be generated.
4448 #[ error( "could not generate a client secret" ) ]
4549 Crypto ( #[ from] CryptoError ) ,
@@ -109,20 +113,40 @@ impl ClientRegistry {
109113 Ok ( secret)
110114 }
111115
112- /// Replaces a client's registration. Its enabled state and secrets stay.
116+ /// Replaces client `id`'s registration with what `edit` makes of the stored one.
117+ /// The client stays locked from the read to the write, so concurrent updates
118+ /// cannot undo each other. Its ID, kind, enabled state and secrets stay.
113119 ///
114120 /// # Errors
115- /// Fails if the client does not exist or the database fails.
116- pub async fn update (
121+ /// Fails if the client does not exist, `edit` fails or changes the ID or kind,
122+ /// or the database fails.
123+ pub async fn update < E : From < ClientRegistryError > > (
117124 & self ,
118125 context : & EventContext ,
126+ id : & ClientId ,
127+ edit : impl FnOnce ( StoredOidcClient ) -> Result < ClientRegistration , E > ,
128+ ) -> Result < ( ) , E > {
129+ let mut tx = self
130+ . database
131+ . begin ( )
132+ . await
133+ . map_err ( ClientRegistryError :: from) ?;
134+ let current = Self :: locked ( & mut tx, id) . await ?;
135+ let kind = current. kind ;
136+ let registration = edit ( current) ?;
137+ if registration. id != * id || registration. kind != kind {
138+ return Err ( ClientRegistryError :: Immutable ( id. clone ( ) ) . into ( ) ) ;
139+ }
140+ Ok ( Self :: replace ( tx, context, & registration) . await ?)
141+ }
142+
143+ /// Writes an update checked by [`Self::update`] and records it.
144+ async fn replace (
145+ mut tx : DatabaseTransaction < ' _ > ,
146+ context : & EventContext ,
119147 registration : & ClientRegistration ,
120148 ) -> Result < ( ) , ClientRegistryError > {
121- let mut tx = self . database . begin ( ) . await ?;
122- tx. oidc ( )
123- . update_client ( registration)
124- . await
125- . map_err ( ClientRegistryError :: lookup ( & registration. id ) ) ?;
149+ tx. oidc ( ) . update_client ( registration) . await ?;
126150 tx. record (
127151 context,
128152 None ,
@@ -178,11 +202,8 @@ impl ClientRegistry {
178202 grace : Duration ,
179203 ) -> Result < ClientSecret , ClientRegistryError > {
180204 let mut tx = self . database . begin ( ) . await ?;
181- let client = tx
182- . oidc ( )
183- . find_client ( id. as_str ( ) )
184- . await ?
185- . ok_or_else ( || ClientRegistryError :: Unknown ( id. clone ( ) ) ) ?;
205+ // Locked, so a concurrent rotation cannot insert a secret this cap misses.
206+ let client = Self :: locked ( & mut tx, id) . await ?;
186207 if client. kind == ClientKind :: Public {
187208 return Err ( ClientRegistryError :: Public ( client. id ) ) ;
188209 }
@@ -203,6 +224,21 @@ impl ClientRegistry {
203224 tx. commit ( ) . await ?;
204225 Ok ( secret)
205226 }
227+
228+ /// Locks client `id` for the rest of `tx`, then reads it.
229+ async fn locked (
230+ tx : & mut DatabaseTransaction < ' _ > ,
231+ id : & ClientId ,
232+ ) -> Result < StoredOidcClient , ClientRegistryError > {
233+ tx. oidc ( )
234+ . lock_client ( id)
235+ . await
236+ . map_err ( ClientRegistryError :: lookup ( id) ) ?;
237+ tx. oidc ( )
238+ . find_client ( id. as_str ( ) )
239+ . await ?
240+ . ok_or_else ( || ClientRegistryError :: Unknown ( id. clone ( ) ) )
241+ }
206242}
207243
208244/// Changes made while auth serves requests apply to the next request.
0 commit comments