-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref(common): Make the DSN public key Copy (#795)
Creates a type for DSN public keys that implements copy. Every public key is a 32-character hexadecimal string. Storage requirements could be halved by parsing the hexadecimal contents of the key, however this is currently not enforced anywhere in Sentry. For this reason, we're taking the conservative approach using 32 bytes. The type is called `ProjectKey` to distinguish it from Relay's own public key.
- Loading branch information
Showing
11 changed files
with
203 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use std::borrow::Cow; | ||
use std::fmt; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
#[doc(inline)] | ||
pub use sentry_types::ProjectId; | ||
|
||
/// An error parsing [`ProjectKey`](struct.ProjectKey.html). | ||
#[derive(Clone, Copy, Debug)] | ||
pub struct ParseProjectKeyError; | ||
|
||
impl fmt::Display for ParseProjectKeyError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "invalid project key") | ||
} | ||
} | ||
|
||
impl std::error::Error for ParseProjectKeyError {} | ||
|
||
/// The public key used in a DSN to identify and authenticate for a project at Sentry. | ||
/// | ||
/// Project keys are always 32-character hexadecimal strings. | ||
#[derive(Clone, Copy, Eq, Hash, Ord, PartialOrd, PartialEq)] | ||
pub struct ProjectKey([u8; 32]); | ||
|
||
impl Serialize for ProjectKey { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
serializer.serialize_str(self.as_str()) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for ProjectKey { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
let cow = Cow::<str>::deserialize(deserializer)?; | ||
Self::parse(&cow).map_err(serde::de::Error::custom) | ||
} | ||
} | ||
|
||
impl ProjectKey { | ||
/// Parses a `ProjectKey` from a string. | ||
pub fn parse(key: &str) -> Result<Self, ParseProjectKeyError> { | ||
if key.len() != 32 || !key.is_ascii() { | ||
return Err(ParseProjectKeyError); | ||
} | ||
|
||
let mut project_key = Self(Default::default()); | ||
project_key.0.copy_from_slice(key.as_bytes()); | ||
Ok(project_key) | ||
} | ||
|
||
/// Returns the string representation of the project key. | ||
#[inline] | ||
pub fn as_str(&self) -> &str { | ||
// Safety: The string is already validated to be of length 32 and valid ASCII when | ||
// constructing `ProjectKey`. | ||
unsafe { std::str::from_utf8_unchecked(&self.0) } | ||
} | ||
} | ||
|
||
impl fmt::Debug for ProjectKey { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "ProjectKey(\"{}\")", self.as_str()) | ||
} | ||
} | ||
|
||
impl fmt::Display for ProjectKey { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
self.as_str().fmt(f) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.