Skip to content

Commit 10241e3

Browse files
authored
Merge pull request #10573 from Turbo87/secret-token
models/user: Change `gh_access_token` field to `SecretString`
2 parents 1a33394 + fc8f1be commit 10241e3

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

src/models/team.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::util::errors::{bad_request, custom, AppResult};
77

88
use crates_io_github::{GitHubClient, GitHubError};
99
use oauth2::AccessToken;
10+
use secrecy::ExposeSecret;
1011

1112
use crate::models::{Crate, CrateOwner, Owner, OwnerKind, User};
1213
use crate::schema::{crate_owners, teams};
@@ -125,7 +126,7 @@ impl Team {
125126
)));
126127
}
127128

128-
let token = AccessToken::new(req_user.gh_access_token.clone());
129+
let token = AccessToken::new(req_user.gh_access_token.expose_secret().to_string());
129130
let team = gh_client.team_by_name(org_name, team_name, &token).await
130131
.map_err(|_| {
131132
bad_request(format_args!(
@@ -211,7 +212,7 @@ async fn is_gh_org_owner(
211212
org_id: i32,
212213
user: &User,
213214
) -> AppResult<bool> {
214-
let token = AccessToken::new(user.gh_access_token.clone());
215+
let token = AccessToken::new(user.gh_access_token.expose_secret().to_string());
215216
match gh_client
216217
.org_membership(org_id, &user.gh_login, &token)
217218
.await
@@ -231,7 +232,7 @@ async fn team_with_gh_id_contains_user(
231232
// GET /organizations/:org_id/team/:team_id/memberships/:username
232233
// check that "state": "active"
233234

234-
let token = AccessToken::new(user.gh_access_token.clone());
235+
let token = AccessToken::new(user.gh_access_token.expose_secret().to_string());
235236
let membership = match gh_client
236237
.team_membership(github_org_id, github_team_id, &user.gh_login, &token)
237238
.await

src/models/user.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use diesel::prelude::*;
55
use diesel::sql_types::Integer;
66
use diesel::upsert::excluded;
77
use diesel_async::{AsyncPgConnection, RunQueryDsl};
8+
use secrecy::SecretString;
89

910
use crate::util::errors::AppResult;
1011

@@ -14,10 +15,11 @@ use crates_io_diesel_helpers::lower;
1415
use crates_io_github::GitHubClient;
1516

1617
/// The model representing a row in the `users` database table.
17-
#[derive(Clone, Debug, PartialEq, Eq, Queryable, Identifiable, AsChangeset, Selectable)]
18+
#[derive(Clone, Debug, Queryable, Identifiable, Selectable)]
1819
pub struct User {
1920
pub id: i32,
20-
pub gh_access_token: String,
21+
#[diesel(deserialize_as = String)]
22+
pub gh_access_token: SecretString,
2123
pub gh_login: String,
2224
pub name: Option<String>,
2325
pub gh_avatar: Option<String>,

src/tests/user.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ async fn updating_existing_user_doesnt_change_api_token() -> anyhow::Result<()>
4444
let user = assert_ok!(User::find(&mut conn, api_token.user_id).await);
4545

4646
assert_eq!(user.gh_login, "bar");
47-
assert_eq!(user.gh_access_token, "bar_token");
47+
assert_eq!(user.gh_access_token.expose_secret(), "bar_token");
4848

4949
Ok(())
5050
}

0 commit comments

Comments
 (0)