diff options
author | Jesse Luehrs <doy@tozt.net> | 2019-11-15 13:11:07 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2019-11-15 13:11:07 -0500 |
commit | bbf15cfef8134da720a27bd71a93efcb8467025b (patch) | |
tree | aa58a5d7c1862fcdd6c8629651f664aa12c70f66 /teleterm/src/oauth/recurse_center.rs | |
parent | fe4fa53dbbb6030beae2094e33d1db008532ae3c (diff) | |
download | teleterm-bbf15cfef8134da720a27bd71a93efcb8467025b.tar.gz teleterm-bbf15cfef8134da720a27bd71a93efcb8467025b.zip |
use workspaces
Diffstat (limited to 'teleterm/src/oauth/recurse_center.rs')
-rw-r--r-- | teleterm/src/oauth/recurse_center.rs | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/teleterm/src/oauth/recurse_center.rs b/teleterm/src/oauth/recurse_center.rs new file mode 100644 index 0000000..2b9f7f7 --- /dev/null +++ b/teleterm/src/oauth/recurse_center.rs @@ -0,0 +1,87 @@ +use crate::prelude::*; + +pub struct RecurseCenter { + client: oauth2::basic::BasicClient, + user_id: String, +} + +impl RecurseCenter { + pub fn new(config: super::Config, user_id: &str) -> Self { + Self { + client: config.into_basic_client(), + user_id: user_id.to_string(), + } + } + + pub fn config(client_id: &str, client_secret: &str) -> super::Config { + super::Config { + client_id: client_id.to_string(), + client_secret: client_secret.to_string(), + auth_url: url::Url::parse( + "https://www.recurse.com/oauth/authorize", + ) + .unwrap(), + token_url: url::Url::parse("https://www.recurse.com/oauth/token") + .unwrap(), + redirect_url: url::Url::parse(super::REDIRECT_URL).unwrap(), + } + } +} + +impl super::Oauth for RecurseCenter { + fn client(&self) -> &oauth2::basic::BasicClient { + &self.client + } + + fn user_id(&self) -> &str { + &self.user_id + } + + fn name(&self) -> &str { + crate::protocol::AuthType::RecurseCenter.name() + } + + fn get_username_from_access_token( + self: Box<Self>, + token: &str, + ) -> Box<dyn futures::future::Future<Item = String, Error = Error> + Send> + { + let fut = reqwest::r#async::Client::new() + .get("https://www.recurse.com/api/v1/profiles/me") + .bearer_auth(token) + .send() + .context(crate::error::GetRecurseCenterProfile) + .and_then(|mut res| res.json().context(crate::error::ParseJson)) + .map(|user: User| user.name()); + Box::new(fut) + } +} + +#[derive(serde::Deserialize)] +struct User { + name: String, + stints: Vec<Stint>, +} + +#[derive(serde::Deserialize)] +struct Stint { + batch: Option<Batch>, + start_date: String, +} + +#[derive(serde::Deserialize)] +struct Batch { + short_name: String, +} + +impl User { + fn name(&self) -> String { + let latest_stint = + self.stints.iter().max_by_key(|s| &s.start_date).unwrap(); + if let Some(batch) = &latest_stint.batch { + format!("{} ({})", self.name, batch.short_name) + } else { + self.name.to_string() + } + } +} |