Skip to content

Commit d47a5b5

Browse files
committed
trustpub/exchange: Save trustpub_data from the trustpub_tokens table to the versions table
1 parent 91b763f commit d47a5b5

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

src/controllers/krate/publish.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use crate::util::errors::{AppResult, BoxedAppError, bad_request, custom, forbidd
4444
use crate::views::{
4545
EncodableCrate, EncodableCrateDependency, GoodCrate, PublishMetadata, PublishWarnings,
4646
};
47-
use crates_io_database::models::{User, versions_published_by};
47+
use crates_io_database::models::{TrustpubData, User, versions_published_by};
4848
use crates_io_diesel_helpers::canon_crate_name;
4949
use crates_io_trustpub::access_token::AccessToken;
5050

@@ -57,20 +57,27 @@ const MAX_DESCRIPTION_LENGTH: usize = 1000;
5757

5858
enum AuthType {
5959
Regular(Box<Authentication>),
60-
TrustPub,
60+
TrustPub(Option<TrustpubData>),
6161
}
6262

6363
impl AuthType {
6464
fn user(&self) -> Option<&User> {
6565
match self {
6666
AuthType::Regular(auth) => Some(auth.user()),
67-
AuthType::TrustPub => None,
67+
AuthType::TrustPub(_) => None,
6868
}
6969
}
7070

7171
fn user_id(&self) -> Option<i32> {
7272
self.user().map(|u| u.id)
7373
}
74+
75+
fn trustpub_data(&self) -> Option<&TrustpubData> {
76+
match self {
77+
AuthType::Regular(_) => None,
78+
AuthType::TrustPub(data) => data.as_ref(),
79+
}
80+
}
7481
}
7582

7683
/// Publish a new crate/version.
@@ -173,22 +180,23 @@ pub async fn publish(app: AppState, req: Parts, body: Body) -> AppResult<Json<Go
173180

174181
let hashed_token = trustpub_token.sha256();
175182

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"))?;
184192

185193
if !crate_ids.contains(&Some(existing_crate.id)) {
186194
let name = &existing_crate.name;
187195
let error = format!("The provided access token is not valid for crate `{name}`");
188196
return Err(forbidden(error));
189197
}
190198

191-
AuthType::TrustPub
199+
AuthType::TrustPub(trustpub_data)
192200
} else {
193201
let endpoint_scope = match existing_crate {
194202
Some(_) => EndpointScope::PublishUpdate,
@@ -502,6 +510,7 @@ pub async fn publish(app: AppState, req: Parts, body: Body) -> AppResult<Json<Go
502510
.maybe_repository(repository.as_deref())
503511
.categories(&categories)
504512
.keywords(&keywords)
513+
.maybe_trustpub_data(auth.trustpub_data())
505514
.build();
506515

507516
let version = new_version.save(conn).await.map_err(|error| {

0 commit comments

Comments
 (0)