-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow bearer tokens in client, not just typed socket
- Loading branch information
Showing
4 changed files
with
171 additions
and
95 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use url::Url; | ||
|
||
/// An authorized address combines a URL with an optional bearer token. | ||
#[derive(Clone, Debug)] | ||
pub struct AuthorizedAddress { | ||
pub url: Url, | ||
pub bearer_token: Option<String>, | ||
} | ||
|
||
impl AuthorizedAddress { | ||
pub fn join(&self, path: &str) -> AuthorizedAddress { | ||
let url = self.url.clone(); | ||
let url = url.join(path).expect("URL is always valid"); | ||
|
||
Self { | ||
url, | ||
bearer_token: self.bearer_token.clone(), | ||
} | ||
} | ||
|
||
pub fn to_websocket_address(mut self) -> AuthorizedAddress { | ||
if self.url.scheme() == "http" { | ||
self.url | ||
.set_scheme("ws") | ||
.expect("should always be able to set URL scheme to static value ws"); | ||
} else if self.url.scheme() == "https" { | ||
self.url | ||
.set_scheme("wss") | ||
.expect("should always be able to set URL scheme to static value wss"); | ||
} | ||
|
||
self | ||
} | ||
|
||
pub fn bearer_header(&self) -> Option<String> { | ||
self.bearer_token | ||
.as_ref() | ||
.map(|token| format!("Bearer {}", token)) | ||
} | ||
} | ||
|
||
impl From<Url> for AuthorizedAddress { | ||
fn from(url: Url) -> Self { | ||
let bearer_token = match url.username() { | ||
"" => None, | ||
username => Some(username.to_string()), | ||
}; | ||
|
||
let mut url = url; | ||
url.set_username("").expect("URL is always valid"); | ||
|
||
Self { url, bearer_token } | ||
} | ||
} |
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.