diff options
| author | Teddy Wing | 2021-05-22 03:06:07 +0200 |
|---|---|---|
| committer | Teddy Wing | 2021-05-22 03:06:07 +0200 |
| commit | e40ea0fc324b1a0b47a7b3433ee3abe2b5171b40 (patch) | |
| tree | 00ff017d7bc6fd6aafb57a047c6fabc2211468a8 /src/main.rs | |
| parent | 21ef6f0cc8ca7f6e9690cdefd1f7ee02d93c38b9 (diff) | |
| download | google-calendar-rsvp-e40ea0fc324b1a0b47a7b3433ee3abe2b5171b40.tar.bz2 | |
event_id_from_base64(): Assume `event_id` is base64-encoded
A base64-encoded string I tested with didn't pass the regex test, and
was treated as an event ID even though it was an `eid`.
Instead of testing whether the input is a base64-encoded string with
regex, assume it's a base64-encoded string and try to decode it. If that
fails, assume it's a regular event ID.
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 20 |
1 files changed, 5 insertions, 15 deletions
diff --git a/src/main.rs b/src/main.rs index 9f4ad6a..a5e7a05 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,6 @@ use google_calendar3::CalendarHub; use home; use hyper; use hyper_rustls; -use regex::Regex; use tokio; use yup_oauth2 as oauth2; @@ -153,20 +152,11 @@ fn secret_from_file() -> oauth2::ApplicationSecret { } fn event_id_from_base64(event_id: &str) -> String { - // Base64-matching regex from Xuanyuanzhiyuan - // (https://stackoverflow.com/users/1076906/xuanyuanzhiyuan) on Stack - // Overflow: - // https://stackoverflow.com/questions/8571501/how-to-check-whether-a-string-is-base64-encoded-or-not/8571649#8571649 - let re = Regex::new( - "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$", - ).unwrap(); - - if !re.is_match(event_id) { - return event_id.to_owned(); - } - - let decoded = &base64::decode(event_id).unwrap(); - let id_email_pair = str::from_utf8(decoded).unwrap(); + let decoded = match base64::decode(event_id) { + Ok(d) => d, + Err(_) => return event_id.to_owned(), + }; + let id_email_pair = str::from_utf8(&decoded).unwrap(); let values = id_email_pair.split(" ").collect::<Vec<_>>(); let id = values.first().unwrap().to_string(); |
