Skip to content

Implement CrateScope struct #5569

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

Merged
merged 3 commits into from
Dec 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/models/token.rs
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ pub struct ApiToken {
pub revoked: bool,
/// `None` or a list of crate scope patterns (see RFC #2947)
#[serde(skip)]
pub crate_scopes: Option<Vec<String>>,
pub crate_scopes: Option<Vec<scopes::CrateScope>>,
/// A list of endpoint scopes or `None` for the `legacy` endpoint scope (see RFC #2947)
#[serde(skip)]
pub endpoint_scopes: Option<Vec<scopes::EndpointScope>>,
133 changes: 133 additions & 0 deletions src/models/token/scopes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::models::Crate;
use diesel::deserialize::{self, FromSql};
use diesel::pg::Pg;
use diesel::serialize::{self, IsNull, Output, ToSql};
@@ -50,3 +51,135 @@ impl FromSql<Text, Pg> for EndpointScope {
Ok(EndpointScope::try_from(not_none!(bytes))?)
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CrateScope {
pattern: String,
}

impl TryFrom<&str> for CrateScope {
type Error = String;

fn try_from(pattern: &str) -> Result<Self, Self::Error> {
match CrateScope::is_valid_pattern(pattern) {
true => Ok(CrateScope {
pattern: pattern.to_string(),
}),
false => Err("Invalid crate scope pattern".to_string()),
}
}
}

impl TryFrom<String> for CrateScope {
type Error = String;

fn try_from(pattern: String) -> Result<Self, Self::Error> {
match CrateScope::is_valid_pattern(&pattern) {
true => Ok(CrateScope { pattern }),
false => Err("Invalid crate scope pattern".to_string()),
}
}
}

impl FromSql<Text, Pg> for CrateScope {
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
let value = <String as FromSql<Text, Pg>>::from_sql(bytes)?;
Ok(CrateScope::try_from(value)?)
}
}

impl ToSql<Text, Pg> for CrateScope {
fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> serialize::Result {
<String as ToSql<Text, Pg>>::to_sql(&self.pattern, out)
}
}

impl CrateScope {
fn is_valid_pattern(pattern: &str) -> bool {
if pattern.is_empty() {
return false;
}

if pattern == "*" {
return true;
}

let name_without_wildcard = pattern.strip_suffix('*').unwrap_or(pattern);
Crate::valid_name(name_without_wildcard)
}

pub fn matches(&self, crate_name: &str) -> bool {
let canonicalize = |name: &str| name.replace('-', "_");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for further discussion: do we want this canonicalization? or do we want the users to be precise?


if self.pattern == "*" {
return true;
}

return match self.pattern.strip_suffix('*') {
Some(prefix) => {
crate_name.starts_with(prefix)
|| canonicalize(crate_name).starts_with(&canonicalize(prefix))
}
None => {
crate_name == self.pattern
|| canonicalize(crate_name) == canonicalize(&self.pattern)
}
};
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn crate_scope_validation() {
assert_ok!(CrateScope::try_from("foo"));

// wildcards
assert_ok!(CrateScope::try_from("foo*"));
assert_ok!(CrateScope::try_from("f*"));
assert_ok!(CrateScope::try_from("*"));
assert_err!(CrateScope::try_from("te*st"));

// hyphens and underscores
assert_ok!(CrateScope::try_from("foo-bar"));
assert_ok!(CrateScope::try_from("foo_bar"));

// empty string
assert_err!(CrateScope::try_from(""));

// invalid characters
assert_err!(CrateScope::try_from("test#"));
}

#[test]
fn crate_scope_matching() {
let scope = |pattern: &str| CrateScope::try_from(pattern).unwrap();

assert!(scope("foo").matches("foo"));
assert!(!scope("foo").matches("bar"));
assert!(!scope("foo").matches("fo"));
assert!(!scope("foo").matches("fooo"));

// wildcards
assert!(scope("foo*").matches("foo"));
assert!(!scope("foo*").matches("bar"));
assert!(scope("foo*").matches("foo-bar"));
assert!(scope("foo*").matches("foo_bar"));
assert!(scope("f*").matches("foo"));
assert!(scope("*").matches("foo"));

// hyphens and underscores
assert!(!scope("foo").matches("foo-bar"));
assert!(!scope("foo").matches("foo_bar"));
assert!(scope("foo-bar").matches("foo-bar"));
assert!(scope("foo-bar").matches("foo_bar"));
assert!(scope("foo_bar").matches("foo-bar"));
assert!(scope("foo_bar").matches("foo_bar"));
assert!(scope("foo-*").matches("foo-bar"));
assert!(scope("foo-*").matches("foo_bar"));
assert!(scope("foo_*").matches("foo-bar"));
assert!(scope("foo_*").matches("foo_bar"));
}
}