@@ -44,7 +44,7 @@ use crate::util::errors::{AppResult, BoxedAppError, bad_request, custom, forbidd
44
44
use crate :: views:: {
45
45
EncodableCrate , EncodableCrateDependency , GoodCrate , PublishMetadata , PublishWarnings ,
46
46
} ;
47
- use crates_io_database:: models:: { User , versions_published_by} ;
47
+ use crates_io_database:: models:: { TrustpubData , User , versions_published_by} ;
48
48
use crates_io_diesel_helpers:: canon_crate_name;
49
49
use crates_io_trustpub:: access_token:: AccessToken ;
50
50
@@ -57,20 +57,27 @@ const MAX_DESCRIPTION_LENGTH: usize = 1000;
57
57
58
58
enum AuthType {
59
59
Regular ( Box < Authentication > ) ,
60
- TrustPub ,
60
+ TrustPub ( Option < TrustpubData > ) ,
61
61
}
62
62
63
63
impl AuthType {
64
64
fn user ( & self ) -> Option < & User > {
65
65
match self {
66
66
AuthType :: Regular ( auth) => Some ( auth. user ( ) ) ,
67
- AuthType :: TrustPub => None ,
67
+ AuthType :: TrustPub ( _ ) => None ,
68
68
}
69
69
}
70
70
71
71
fn user_id ( & self ) -> Option < i32 > {
72
72
self . user ( ) . map ( |u| u. id )
73
73
}
74
+
75
+ fn trustpub_data ( & self ) -> Option < & TrustpubData > {
76
+ match self {
77
+ AuthType :: Regular ( _) => None ,
78
+ AuthType :: TrustPub ( data) => data. as_ref ( ) ,
79
+ }
80
+ }
74
81
}
75
82
76
83
/// Publish a new crate/version.
@@ -173,22 +180,23 @@ pub async fn publish(app: AppState, req: Parts, body: Body) -> AppResult<Json<Go
173
180
174
181
let hashed_token = trustpub_token. sha256 ( ) ;
175
182
176
- let crate_ids: Vec < Option < i32 > > = trustpub_tokens:: table
177
- . filter ( trustpub_tokens:: hashed_token. eq ( hashed_token. as_slice ( ) ) )
178
- . filter ( trustpub_tokens:: expires_at. gt ( now) )
179
- . select ( trustpub_tokens:: crate_ids)
180
- . get_result ( & mut conn)
181
- . await
182
- . optional ( ) ?
183
- . ok_or_else ( || forbidden ( "Invalid authentication token" ) ) ?;
183
+ let ( crate_ids, trustpub_data) : ( Vec < Option < i32 > > , Option < TrustpubData > ) =
184
+ trustpub_tokens:: table
185
+ . filter ( trustpub_tokens:: hashed_token. eq ( hashed_token. as_slice ( ) ) )
186
+ . filter ( trustpub_tokens:: expires_at. gt ( now) )
187
+ . select ( ( trustpub_tokens:: crate_ids, trustpub_tokens:: trustpub_data) )
188
+ . get_result ( & mut conn)
189
+ . await
190
+ . optional ( ) ?
191
+ . ok_or_else ( || forbidden ( "Invalid authentication token" ) ) ?;
184
192
185
193
if !crate_ids. contains ( & Some ( existing_crate. id ) ) {
186
194
let name = & existing_crate. name ;
187
195
let error = format ! ( "The provided access token is not valid for crate `{name}`" ) ;
188
196
return Err ( forbidden ( error) ) ;
189
197
}
190
198
191
- AuthType :: TrustPub
199
+ AuthType :: TrustPub ( trustpub_data )
192
200
} else {
193
201
let endpoint_scope = match existing_crate {
194
202
Some ( _) => EndpointScope :: PublishUpdate ,
@@ -502,6 +510,7 @@ pub async fn publish(app: AppState, req: Parts, body: Body) -> AppResult<Json<Go
502
510
. maybe_repository ( repository. as_deref ( ) )
503
511
. categories ( & categories)
504
512
. keywords ( & keywords)
513
+ . maybe_trustpub_data ( auth. trustpub_data ( ) )
505
514
. build ( ) ;
506
515
507
516
let version = new_version. save ( conn) . await . map_err ( |error| {
0 commit comments