1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use std::fmt;
use std::str::FromStr;
use chrono::{DateTime, Utc};
use protocol;
use utils::{datetime_to_timestamp, timestamp_to_datetime};
use dsn::Dsn;
#[derive(Debug, Fail)]
pub enum AuthParseError {
#[fail(display = "non sentry auth")]
NonSentryAuth,
#[fail(display = "invalid value for timestamp")]
InvalidTimestamp,
#[fail(display = "invalid value for version")]
InvalidVersion,
#[fail(display = "no valid version defined")]
MissingVersion,
#[fail(display = "missing public key in auth header")]
MissingPublicKey,
}
#[derive(Debug)]
pub struct Auth {
timestamp: Option<DateTime<Utc>>,
client: Option<String>,
version: u16,
key: String,
secret: Option<String>,
}
impl Auth {
pub fn timestamp(&self) -> Option<DateTime<Utc>> {
self.timestamp
}
pub fn version(&self) -> u16 {
self.version
}
pub fn public_key(&self) -> &str {
&self.key
}
pub fn secret_key(&self) -> Option<&str> {
self.secret.as_ref().map(|x| x.as_str())
}
pub fn is_public(&self) -> bool {
self.secret.is_none()
}
pub fn client_agent(&self) -> Option<&str> {
self.client.as_ref().map(|x| x.as_str())
}
}
impl fmt::Display for Auth {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"Sentry sentry_key={}, sentry_version={}",
self.key, self.version
)?;
if let Some(ts) = self.timestamp {
write!(f, ", sentry_timestamp={}", datetime_to_timestamp(&ts))?;
}
if let Some(ref client) = self.client {
write!(f, ", sentry_client={}", client)?;
}
if let Some(ref secret) = self.secret {
write!(f, ", sentry_secret={}", secret)?;
}
Ok(())
}
}
impl FromStr for Auth {
type Err = AuthParseError;
fn from_str(s: &str) -> Result<Auth, AuthParseError> {
let mut rv = Auth {
timestamp: None,
client: None,
version: protocol::LATEST,
key: "".into(),
secret: None,
};
let mut base_iter = s.splitn(2, ' ');
if !base_iter
.next()
.unwrap_or("")
.eq_ignore_ascii_case("sentry")
{
return Err(AuthParseError::NonSentryAuth);
}
let items = base_iter.next().unwrap_or("");
for item in items.split(',') {
let mut kviter = item.trim().split('=');
match (kviter.next(), kviter.next()) {
(Some("sentry_timestamp"), Some(ts)) => {
let f: f64 = ts.parse().map_err(|_| AuthParseError::InvalidTimestamp)?;
rv.timestamp = Some(timestamp_to_datetime(f));
}
(Some("sentry_client"), Some(client)) => {
rv.client = Some(client.into());
}
(Some("sentry_version"), Some(version)) => {
rv.version = version.parse().map_err(|_| AuthParseError::InvalidVersion)?;
}
(Some("sentry_key"), Some(key)) => {
rv.key = key.into();
}
(Some("sentry_secret"), Some(secret)) => {
rv.secret = Some(secret.into());
}
_ => {}
}
}
if rv.key.is_empty() {
return Err(AuthParseError::MissingPublicKey);
}
if rv.version == 0 {
return Err(AuthParseError::MissingVersion);
}
Ok(rv)
}
}
pub(crate) fn auth_from_dsn_and_client(dsn: &Dsn, client: Option<&str>) -> Auth {
Auth {
timestamp: Some(Utc::now()),
client: client.map(|x| x.to_string()),
version: protocol::LATEST,
key: dsn.public_key().to_string(),
secret: dsn.secret_key().map(|x| x.to_string()),
}
}