|
| 1 | +use hypr_nango::OwnedNangoProxy; |
| 2 | + |
| 3 | +#[derive(serde::Deserialize)] |
| 4 | +struct GoogleUserInfo { |
| 5 | + email: Option<String>, |
| 6 | + name: Option<String>, |
| 7 | +} |
| 8 | + |
| 9 | +#[derive(serde::Deserialize)] |
| 10 | +struct OutlookMe { |
| 11 | + mail: Option<String>, |
| 12 | + #[serde(rename = "userPrincipalName")] |
| 13 | + user_principal_name: Option<String>, |
| 14 | + #[serde(rename = "displayName")] |
| 15 | + display_name: Option<String>, |
| 16 | +} |
| 17 | + |
| 18 | +pub(crate) async fn fetch_identity( |
| 19 | + nango: &hypr_nango::NangoClient, |
| 20 | + integration_id: &str, |
| 21 | + connection_id: &str, |
| 22 | +) -> std::result::Result<(Option<String>, Option<String>), String> { |
| 23 | + let proxy = OwnedNangoProxy::new(nango, integration_id.to_string(), connection_id.to_string()); |
| 24 | + |
| 25 | + match integration_id { |
| 26 | + // https://docs.cloud.google.com/identity-platform/docs/reference/rest/v1/UserInfo |
| 27 | + "google-calendar" | "google-drive" => { |
| 28 | + let resp = proxy |
| 29 | + .get("/oauth2/v1/userinfo?alt=json") |
| 30 | + .map_err(|e| e.to_string())? |
| 31 | + .send() |
| 32 | + .await |
| 33 | + .map_err(|e| e.to_string())? |
| 34 | + .error_for_status() |
| 35 | + .map_err(|e| e.to_string())?; |
| 36 | + |
| 37 | + let me: GoogleUserInfo = resp.json().await.map_err(|e| e.to_string())?; |
| 38 | + Ok((me.email, me.name)) |
| 39 | + } |
| 40 | + |
| 41 | + // https://learn.microsoft.com/en-us/graph/api/user-get |
| 42 | + "outlook" => { |
| 43 | + let resp = proxy |
| 44 | + .get("/v1.0/me?$select=mail,userPrincipalName,displayName") |
| 45 | + .map_err(|e| e.to_string())? |
| 46 | + .send() |
| 47 | + .await |
| 48 | + .map_err(|e| e.to_string())? |
| 49 | + .error_for_status() |
| 50 | + .map_err(|e| e.to_string())?; |
| 51 | + |
| 52 | + let me: OutlookMe = resp.json().await.map_err(|e| e.to_string())?; |
| 53 | + Ok((me.mail.or(me.user_principal_name), me.display_name)) |
| 54 | + } |
| 55 | + |
| 56 | + other => Err(format!("unsupported integration: {other}")), |
| 57 | + } |
| 58 | +} |
0 commit comments