-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.rs
More file actions
174 lines (146 loc) · 4.95 KB
/
auth.rs
File metadata and controls
174 lines (146 loc) · 4.95 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use axum::http::HeaderMap;
use crate::encoding::percent_decode;
#[derive(Debug, Clone)]
pub struct SentryAuth {
pub sentry_key: String,
}
/// Pulls the sentry key out of request headers -- tries X-Sentry-Auth first, then Authorization.
pub fn extract_from_header(headers: &HeaderMap) -> Option<SentryAuth> {
let header_val = headers
.get("X-Sentry-Auth")
.or_else(|| headers.get("Authorization"))
.and_then(|v| v.to_str().ok())?;
parse_auth_header(header_val)
}
pub fn extract_from_query(query: Option<&str>) -> Option<SentryAuth> {
let query = query?;
for pair in query.split('&') {
if let Some(key) = pair.strip_prefix("sentry_key=") {
return Some(SentryAuth {
sentry_key: percent_decode(key),
});
}
}
None
}
/// Cracks open a DSN string to get the auth key and project ID out of it.
pub fn extract_from_dsn(dsn: &str) -> Option<(SentryAuth, u64)> {
let without_scheme = dsn
.strip_prefix("https://")
.or_else(|| dsn.strip_prefix("http://"))?;
let (key, rest) = without_scheme.split_once('@')?;
let project_str = rest.rsplit('/').find(|s| !s.is_empty())?;
let project_id: u64 = project_str.parse().ok()?;
Some((
SentryAuth {
sentry_key: key.to_string(),
},
project_id,
))
}
fn parse_auth_header(value: &str) -> Option<SentryAuth> {
let payload = value
.strip_prefix("Sentry ")
.or_else(|| value.strip_prefix("sentry "))?;
let mut sentry_key = None;
for part in payload.split(',') {
let part = part.trim();
if let Some(val) = part.strip_prefix("sentry_key=") {
sentry_key = Some(val.to_string());
}
}
Some(SentryAuth {
sentry_key: sentry_key?,
})
}
#[cfg(test)]
mod tests {
use super::*;
use axum::http::HeaderMap;
#[test]
fn parse_auth_header_with_key_and_version() {
let auth = parse_auth_header("Sentry sentry_key=abc123, sentry_version=7").unwrap();
assert_eq!(auth.sentry_key, "abc123");
}
#[test]
fn parse_auth_header_lowercase_prefix() {
let auth = parse_auth_header("sentry sentry_key=key1").unwrap();
assert_eq!(auth.sentry_key, "key1");
}
#[test]
fn parse_auth_header_missing_prefix_returns_none() {
assert!(parse_auth_header("Bearer token123").is_none());
}
#[test]
fn parse_auth_header_missing_key_returns_none() {
assert!(parse_auth_header("Sentry sentry_version=7").is_none());
}
#[test]
fn extract_from_header_x_sentry_auth() {
let mut headers = HeaderMap::new();
headers.insert("X-Sentry-Auth", "Sentry sentry_key=abc".parse().unwrap());
let auth = extract_from_header(&headers).unwrap();
assert_eq!(auth.sentry_key, "abc");
}
#[test]
fn extract_from_header_authorization_fallback() {
let mut headers = HeaderMap::new();
headers.insert("Authorization", "Sentry sentry_key=xyz".parse().unwrap());
let auth = extract_from_header(&headers).unwrap();
assert_eq!(auth.sentry_key, "xyz");
}
#[test]
fn extract_from_header_missing_returns_none() {
let headers = HeaderMap::new();
assert!(extract_from_header(&headers).is_none());
}
#[test]
fn extract_from_query_valid() {
let auth = extract_from_query(Some("sentry_key=mykey&other=1")).unwrap();
assert_eq!(auth.sentry_key, "mykey");
}
#[test]
fn extract_from_query_url_encoded_key() {
let auth = extract_from_query(Some("sentry_key=abc%3D123%26key")).unwrap();
assert_eq!(auth.sentry_key, "abc=123&key");
}
#[test]
fn extract_from_query_no_key() {
assert!(extract_from_query(Some("foo=bar&baz=1")).is_none());
}
#[test]
fn extract_from_query_none_input() {
assert!(extract_from_query(None).is_none());
}
#[test]
fn extract_from_dsn_https() {
let (auth, project_id) =
extract_from_dsn("https://[email protected]/456").unwrap();
assert_eq!(auth.sentry_key, "abc123");
assert_eq!(project_id, 456);
}
#[test]
fn extract_from_dsn_http() {
let (auth, project_id) = extract_from_dsn("http://key@localhost:3000/42").unwrap();
assert_eq!(auth.sentry_key, "key");
assert_eq!(project_id, 42);
}
#[test]
fn extract_from_dsn_invalid_scheme() {
assert!(extract_from_dsn("ftp://key@host/1").is_none());
}
#[test]
fn extract_from_dsn_no_project_id() {
assert!(extract_from_dsn("https://key@host/notanumber").is_none());
}
#[test]
fn extract_from_dsn_no_at_sign() {
assert!(extract_from_dsn("https://noatsign/1").is_none());
}
#[test]
fn extract_from_dsn_trailing_slash() {
let (auth, project_id) = extract_from_dsn("https://key@host/42/").unwrap();
assert_eq!(auth.sentry_key, "key");
assert_eq!(project_id, 42);
}
}