| 1 | use std::time::SystemTime; |
| 2 | |
| 3 | use super::RepositoryResult; |
| 4 | use crate::domain::{PersonalAccessToken, TokenHash, TokenId, UserId}; |
| 5 | |
| 6 | /// Persistence for [`PersonalAccessToken`]. |
| 7 | pub trait TokenRepository: Send + Sync { |
| 8 | /// Looks a token up by the hash of what a client presented. |
| 9 | /// |
| 10 | /// By hash rather than by id, because a client presents the token itself and never |
| 11 | /// its id — this is the lookup that authentication actually performs, so it is the |
| 12 | /// one the storage layer has to make fast. |
| 13 | fn find_by_hash( |
| 14 | &self, |
| 15 | hash: &TokenHash, |
| 16 | ) -> impl Future<Output = RepositoryResult<Option<PersonalAccessToken>>> + Send; |
| 17 | |
| 18 | /// Every token a user holds, newest first. |
| 19 | fn list_by_user( |
| 20 | &self, |
| 21 | user_id: &UserId, |
| 22 | ) -> impl Future<Output = RepositoryResult<Vec<PersonalAccessToken>>> + Send; |
| 23 | |
| 24 | /// Inserts or replaces a token. |
| 25 | fn save( |
| 26 | &self, |
| 27 | token: &PersonalAccessToken, |
| 28 | ) -> impl Future<Output = RepositoryResult<()>> + Send; |
| 29 | |
| 30 | /// Records that a token was used, succeeding if there was nothing to record against. |
| 31 | /// |
| 32 | /// Separate from [`save`](Self::save) rather than folded into it, because the |
| 33 | /// caller is the authentication path: it holds a record it read a moment ago and |
| 34 | /// must not write back the rest of it, and this is the one field a *read* is |
| 35 | /// allowed to move. Callers ask |
| 36 | /// [`should_record_use`](PersonalAccessToken::should_record_use) first, so a busy |
| 37 | /// clone does not turn every git request into a row write. |
| 38 | fn touch_last_used( |
| 39 | &self, |
| 40 | id: &TokenId, |
| 41 | at: SystemTime, |
| 42 | ) -> impl Future<Output = RepositoryResult<()>> + Send; |
| 43 | |
| 44 | /// Deletes a token, succeeding if there was nothing to delete. |
| 45 | /// |
| 46 | /// Revocation is a delete rather than a flag: a revoked token that lingers in the |
| 47 | /// table is a credential that stops working only as long as every read remembers to |
| 48 | /// check the flag. Nothing needs the history. |
| 49 | fn delete(&self, id: &TokenId) -> impl Future<Output = RepositoryResult<()>> + Send; |
| 50 | } |