-
Notifications
You must be signed in to change notification settings - Fork 645
Implement PUT /api/v1/trusted_publishing/tokens
API endpoint
#11131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e9c82d0
34fbba3
609333c
aaff3cd
7fb7948
7a7cbb9
81261b0
fe581b6
0af18d3
be6ef1c
eb2381c
9f21064
688f462
11623e3
afb3438
a598135
f06f2d5
11c4dda
713e5fc
3afc69d
95c15f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
mod github_config; | ||
mod token; | ||
mod used_jti; | ||
|
||
pub use self::github_config::{GitHubConfig, NewGitHubConfig}; | ||
pub use self::token::NewToken; | ||
pub use self::used_jti::NewUsedJti; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use crate::schema::trustpub_tokens; | ||
use chrono::{DateTime, Utc}; | ||
use diesel::prelude::*; | ||
use diesel_async::{AsyncPgConnection, RunQueryDsl}; | ||
|
||
#[derive(Debug, Insertable)] | ||
#[diesel(table_name = trustpub_tokens, check_for_backend(diesel::pg::Pg))] | ||
pub struct NewToken<'a> { | ||
pub expires_at: DateTime<Utc>, | ||
pub hashed_token: &'a [u8], | ||
pub crate_ids: &'a [i32], | ||
} | ||
|
||
impl NewToken<'_> { | ||
pub async fn insert(&self, conn: &mut AsyncPgConnection) -> QueryResult<()> { | ||
self.insert_into(trustpub_tokens::table) | ||
.execute(conn) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use crate::schema::trustpub_used_jtis; | ||
use chrono::{DateTime, Utc}; | ||
use diesel::prelude::*; | ||
use diesel_async::{AsyncPgConnection, RunQueryDsl}; | ||
|
||
#[derive(Debug, Insertable)] | ||
#[diesel(table_name = trustpub_used_jtis, check_for_backend(diesel::pg::Pg))] | ||
pub struct NewUsedJti<'a> { | ||
pub jti: &'a str, | ||
pub expires_at: DateTime<Utc>, | ||
} | ||
|
||
impl<'a> NewUsedJti<'a> { | ||
pub fn new(jti: &'a str, expires_at: DateTime<Utc>) -> Self { | ||
Self { jti, expires_at } | ||
} | ||
|
||
pub async fn insert(&self, conn: &mut AsyncPgConnection) -> QueryResult<usize> { | ||
diesel::insert_into(trustpub_used_jtis::table) | ||
.values(self) | ||
.execute(conn) | ||
.await | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
use rand::distr::{Alphanumeric, SampleString}; | ||
use secrecy::{ExposeSecret, SecretString}; | ||
use sha2::digest::Output; | ||
use sha2::{Digest, Sha256}; | ||
|
||
/// A temporary access token used to publish crates to crates.io using | ||
/// the "Trusted Publishing" feature. | ||
/// | ||
/// The token consists of a prefix, a random alphanumeric string (31 characters), | ||
/// and a single-character checksum. | ||
#[derive(Debug)] | ||
pub struct AccessToken(SecretString); | ||
|
||
impl AccessToken { | ||
/// The prefix used for the temporary access token. | ||
/// | ||
/// This overlaps with the `cio` prefix used for other tokens, but since | ||
/// the regular tokens don't use `_` characters, they can easily be | ||
/// distinguished. | ||
pub const PREFIX: &str = "cio_tp_"; | ||
|
||
/// The length of the random alphanumeric string in the token, without | ||
/// the checksum. | ||
const RAW_LENGTH: usize = 31; | ||
|
||
/// Generate a new random access token. | ||
pub fn generate() -> Self { | ||
let raw = Alphanumeric.sample_string(&mut rand::rng(), Self::RAW_LENGTH); | ||
Self(raw.into()) | ||
} | ||
|
||
/// Parse a byte string into an access token. | ||
/// | ||
/// This can be used to convert an HTTP header value into an access token. | ||
pub fn from_byte_str(byte_str: &[u8]) -> Result<Self, AccessTokenError> { | ||
let suffix = byte_str | ||
.strip_prefix(Self::PREFIX.as_bytes()) | ||
.ok_or(AccessTokenError::MissingPrefix)?; | ||
|
||
if suffix.len() != Self::RAW_LENGTH + 1 { | ||
return Err(AccessTokenError::InvalidLength); | ||
} | ||
|
||
let suffix = std::str::from_utf8(suffix).map_err(|_| AccessTokenError::InvalidCharacter)?; | ||
if !suffix.chars().all(|c| char::is_ascii_alphanumeric(&c)) { | ||
return Err(AccessTokenError::InvalidCharacter); | ||
} | ||
|
||
let raw = suffix.chars().take(Self::RAW_LENGTH).collect::<String>(); | ||
let claimed_checksum = suffix.chars().nth(Self::RAW_LENGTH).unwrap(); | ||
let actual_checksum = checksum(raw.as_bytes()); | ||
if claimed_checksum != actual_checksum { | ||
return Err(AccessTokenError::InvalidChecksum { | ||
claimed: claimed_checksum, | ||
actual: actual_checksum, | ||
}); | ||
} | ||
|
||
Ok(Self(raw.into())) | ||
} | ||
|
||
/// Wrap the raw access token with the token prefix and a checksum. | ||
/// | ||
/// This turns e.g. `ABC` into `cio_tp_ABC{checksum}`. | ||
pub fn finalize(&self) -> SecretString { | ||
let raw = self.0.expose_secret(); | ||
let checksum = checksum(raw.as_bytes()); | ||
format!("{}{raw}{checksum}", Self::PREFIX).into() | ||
} | ||
|
||
/// Generate a SHA256 hash of the access token. | ||
/// | ||
/// This is used to create a hashed version of the token for storage in | ||
/// the database to avoid storing the plaintext token. | ||
pub fn sha256(&self) -> Output<Sha256> { | ||
Sha256::digest(self.0.expose_secret()) | ||
} | ||
} | ||
|
||
/// The error type for parsing access tokens. | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub enum AccessTokenError { | ||
MissingPrefix, | ||
InvalidLength, | ||
InvalidCharacter, | ||
InvalidChecksum { claimed: char, actual: char }, | ||
} | ||
|
||
/// Generate a single-character checksum for the given raw token. | ||
/// | ||
/// Note that this checksum is not cryptographically secure and should not be | ||
/// used for security purposes. It should only be used to detect invalid tokens. | ||
fn checksum(raw: &[u8]) -> char { | ||
Turbo87 marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also went ahead and checked the distribution on this checksum with a Python script: (The vertical axis is the ASCII codepoint, so this suggests that a basic XOR here produces a pretty good distribution -- the only gaps are in the non-alphanumeric ranges, which is exactly what's expected.) The script I used: #!/usr/bin/env python3
import random
ALPHANUMERIC = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
def checksum(raw: str) -> str:
checksum_value = 0
for b in raw:
checksum_value ^= ord(b)
return ALPHANUMERIC[checksum_value % len(ALPHANUMERIC)]
def token() -> str:
# Generate a random token of 31 characters
return "".join(random.choices(ALPHANUMERIC, k=31))
for _ in range(10_000):
raw = token()
cksum = checksum(raw)
print(f"{raw} {ord(cksum)}") and then for plotting: ./checksum-distribution.py | awk '{ print $2 }' | gnuplot -p -e 'plot "/dev/stdin"' |
||
const ALPHANUMERIC: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||
|
||
let checksum = raw.iter().fold(0, |acc, &b| acc ^ b); | ||
|
||
ALPHANUMERIC | ||
.chars() | ||
.nth(checksum as usize % ALPHANUMERIC.len()) | ||
.unwrap_or('0') | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use claims::{assert_err_eq, assert_ok}; | ||
use insta::{assert_compact_debug_snapshot, assert_snapshot}; | ||
|
||
const EXAMPLE_TOKEN: &str = "gGK6jurSwKyl9V3Az19z7YEFQI9aoOO"; | ||
|
||
#[test] | ||
fn test_generate() { | ||
let token = AccessToken::generate(); | ||
assert_eq!(token.0.expose_secret().len(), AccessToken::RAW_LENGTH); | ||
} | ||
|
||
#[test] | ||
fn test_finalize() { | ||
let token = AccessToken(SecretString::from(EXAMPLE_TOKEN)); | ||
assert_snapshot!(token.finalize().expose_secret(), @"cio_tp_gGK6jurSwKyl9V3Az19z7YEFQI9aoOOd"); | ||
} | ||
|
||
#[test] | ||
fn test_sha256() { | ||
let token = AccessToken(SecretString::from(EXAMPLE_TOKEN)); | ||
let hash = token.sha256(); | ||
assert_compact_debug_snapshot!(hash.as_slice(), @"[11, 102, 58, 175, 81, 174, 38, 227, 173, 48, 158, 96, 20, 130, 99, 78, 7, 16, 241, 211, 195, 166, 110, 74, 193, 126, 53, 125, 42, 21, 23, 124]"); | ||
} | ||
|
||
#[test] | ||
fn test_from_byte_str() { | ||
let token = AccessToken::generate().finalize(); | ||
let token = token.expose_secret(); | ||
let token2 = assert_ok!(AccessToken::from_byte_str(token.as_bytes())); | ||
assert_eq!(token2.finalize().expose_secret(), token); | ||
|
||
let bytes = b"cio_tp_0000000000000000000000000000000w"; | ||
assert_ok!(AccessToken::from_byte_str(bytes)); | ||
|
||
let bytes = b"invalid_token"; | ||
assert_err_eq!( | ||
AccessToken::from_byte_str(bytes), | ||
AccessTokenError::MissingPrefix | ||
); | ||
|
||
let bytes = b"cio_tp_invalid_token"; | ||
assert_err_eq!( | ||
AccessToken::from_byte_str(bytes), | ||
AccessTokenError::InvalidLength | ||
); | ||
|
||
let bytes = b"cio_tp_00000000000000000000000000"; | ||
assert_err_eq!( | ||
AccessToken::from_byte_str(bytes), | ||
AccessTokenError::InvalidLength | ||
); | ||
|
||
let bytes = b"cio_tp_000000@0000000000000000000000000"; | ||
assert_err_eq!( | ||
AccessToken::from_byte_str(bytes), | ||
AccessTokenError::InvalidCharacter | ||
); | ||
|
||
let bytes = b"cio_tp_00000000000000000000000000000000"; | ||
assert_err_eq!( | ||
AccessToken::from_byte_str(bytes), | ||
AccessTokenError::InvalidChecksum { | ||
claimed: '0', | ||
actual: 'w', | ||
} | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just noting for cross-checking purposes:
log2(62) * 31
is approximately 185 bytes of entropy, so more than sufficient 🙂There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, we're using roughly the same pattern for the regular API keys, though without the checksum, so I figured it should be sufficient :)