Skip to content

Commit

Permalink
secrecy: Adjust to breaking API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Turbo87 committed Sep 18, 2024
1 parent 8940a4b commit 86a9715
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/controllers/krate/owners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use diesel::prelude::*;
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;
use http::request::Parts;
use http::StatusCode;
use secrecy::{ExposeSecret, Secret};
use secrecy::{ExposeSecret, SecretString};
use serde_json::Value;
use tokio::runtime::Handle;

Expand Down Expand Up @@ -253,7 +253,7 @@ pub struct OwnerInviteEmail {
inviter: String,
domain: String,
crate_name: String,
token: Secret<String>,
token: SecretString,
}

impl OwnerInviteEmail {
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/user/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ pub async fn update_user(
.do_update()
.set(&new_email)
.returning(emails::token)
.get_result(conn)
.map(SecretString::new)
.get_result::<String>(conn)
.map(SecretString::from)
.map_err(|_| server_error("Error in creating token"))?;

// This swallows any errors that occur while attempting to send the email. Some users have
Expand Down
4 changes: 2 additions & 2 deletions src/models/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use diesel::dsl;
use diesel::pg::Pg;
use diesel::prelude::*;
use diesel::sql_types::{Bool, Text};
use secrecy::Secret;
use secrecy::SecretString;
use thiserror::Error;

use crate::controllers::helpers::pagination::*;
Expand Down Expand Up @@ -522,7 +522,7 @@ impl Crate {
pub enum NewOwnerInvite {
/// The invitee was a [`User`], and they must accept the invite through the
/// UI or via the provided invite token.
User(User, Secret<String>),
User(User, SecretString),

/// The invitee was a [`Team`], and they were immediately added as an owner.
Team(Team),
Expand Down
4 changes: 2 additions & 2 deletions src/models/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ impl<'a> NewUser<'a> {
.values(&new_email)
.on_conflict_do_nothing()
.returning(emails::token)
.get_result(conn)
.get_result::<String>(conn)
.optional()?
.map(SecretString::new);
.map(SecretString::from);

if let Some(token) = token {
// Swallows any error. Some users might insert an invalid email address here.
Expand Down
8 changes: 4 additions & 4 deletions src/util/token.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use diesel::{deserialize::FromSql, pg::Pg, serialize::ToSql, sql_types::Bytea};
use rand::{distributions::Uniform, rngs::OsRng, Rng};
use secrecy::{ExposeSecret, SecretString, SecretVec};
use secrecy::{ExposeSecret, SecretSlice, SecretString};
use sha2::{Digest, Sha256};

const TOKEN_LENGTH: usize = 32;
Expand All @@ -19,7 +19,7 @@ pub struct InvalidTokenError;

#[derive(FromSqlRow, AsExpression)]
#[diesel(sql_type = Bytea)]
pub struct HashedToken(SecretVec<u8>);
pub struct HashedToken(SecretSlice<u8>);

impl HashedToken {
pub fn parse(plaintext: &str) -> Result<Self, InvalidTokenError> {
Expand Down Expand Up @@ -77,8 +77,8 @@ impl PlainToken {
}
}

impl ExposeSecret<String> for PlainToken {
fn expose_secret(&self) -> &String {
impl ExposeSecret<str> for PlainToken {
fn expose_secret(&self) -> &str {
self.0.expose_secret()
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ impl From<CreatedApiToken> for EncodableApiTokenWithToken {
fn from(token: CreatedApiToken) -> Self {
EncodableApiTokenWithToken {
token: token.model,
plaintext: token.plaintext.expose_secret().clone(),
plaintext: token.plaintext.expose_secret().to_string(),
}
}
}
Expand Down

0 comments on commit 86a9715

Please sign in to comment.