| 1 | use serde_json::{Value, json}; |
| 2 | use session::trusted_url::may_carry_credentials; |
| 3 | use session::{ApiResponse, App, Request, Scope, Session}; |
| 4 | |
| 5 | use crate::error::{Error, Result}; |
| 6 | |
| 7 | pub const CSA_RESOURCE: &str = "https://chatsvcagg.teams.microsoft.com"; |
| 8 | pub const CSA_SCOPE: &str = "user_impersonation"; |
| 9 | |
| 10 | pub trait CsaTransport { |
| 11 | fn send(&self, request: Request) -> impl std::future::Future<Output = Result<ApiResponse>>; |
| 12 | } |
| 13 | |
| 14 | pub struct SessionTransport { |
| 15 | session: Session, |
| 16 | scope: Scope, |
| 17 | } |
| 18 | |
| 19 | impl SessionTransport { |
| 20 | pub fn new(session: &Session) -> Self { |
| 21 | SessionTransport::with_scope(session, Scope::new(CSA_RESOURCE, CSA_SCOPE)) |
| 22 | } |
| 23 | |
| 24 | pub fn with_scope(session: &Session, scope: Scope) -> Self { |
| 25 | SessionTransport { |
| 26 | session: session.clone(), |
| 27 | scope, |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | impl CsaTransport for SessionTransport { |
| 33 | async fn send(&self, request: Request) -> Result<ApiResponse> { |
| 34 | let mut answers = self.session.batch(&[request], &self.scope).await?; |
| 35 | Ok(answers.remove(0)) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | const SKYPE_FETCH_BODY: &str = concat!( |
| 40 | include_str!("../../assets/skype_token.js"), |
| 41 | include_str!("../../assets/csa_skype_fetch.js") |
| 42 | ); |
| 43 | |
| 44 | /// Teams sends the skype token and the chat service endpoint next to the CSA bearer on the pinned post routes. |
| 45 | pub struct SkypeTokenTransport { |
| 46 | session: Session, |
| 47 | scope: Scope, |
| 48 | } |
| 49 | |
| 50 | impl SkypeTokenTransport { |
| 51 | pub fn new(session: &Session) -> Self { |
| 52 | SkypeTokenTransport { |
| 53 | session: session.clone(), |
| 54 | scope: Scope::new(CSA_RESOURCE, CSA_SCOPE), |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | pub fn skype_fetch_args(request: &Request) -> Value { |
| 60 | let headers: serde_json::Map<String, Value> = request |
| 61 | .headers |
| 62 | .iter() |
| 63 | .map(|(name, value)| (name.clone(), Value::String(value.clone()))) |
| 64 | .collect(); |
| 65 | json!({ |
| 66 | "method": request.method.as_str(), |
| 67 | "url": request.url, |
| 68 | "headers": headers, |
| 69 | "body": request.body, |
| 70 | }) |
| 71 | } |
| 72 | |
| 73 | pub fn skype_fetch_answer(answer: Value) -> ApiResponse { |
| 74 | ApiResponse { |
| 75 | status: answer |
| 76 | .get("status") |
| 77 | .and_then(Value::as_u64) |
| 78 | .and_then(|status| u16::try_from(status).ok()) |
| 79 | .unwrap_or(0), |
| 80 | retry_after: answer |
| 81 | .get("retryAfter") |
| 82 | .and_then(Value::as_str) |
| 83 | .map(str::to_owned), |
| 84 | body: answer.get("body").cloned().unwrap_or(Value::Null), |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | impl CsaTransport for SkypeTokenTransport { |
| 89 | async fn send(&self, request: Request) -> Result<ApiResponse> { |
| 90 | if !may_carry_credentials(&request.url) { |
| 91 | return Err(Error::UnexpectedAnswer(format!( |
| 92 | "refusing skype token request to {}", |
| 93 | request.url |
| 94 | ))); |
| 95 | } |
| 96 | let answer = self |
| 97 | .session |
| 98 | .run_with_token( |
| 99 | App::Teams, |
| 100 | &self.scope, |
| 101 | SKYPE_FETCH_BODY, |
| 102 | &skype_fetch_args(&request), |
| 103 | false, |
| 104 | ) |
| 105 | .await?; |
| 106 | Ok(skype_fetch_answer(answer)) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | #[cfg(test)] |
| 111 | mod tests { |
| 112 | use session::Method; |
| 113 | |
| 114 | use super::*; |
| 115 | |
| 116 | #[test] |
| 117 | fn skype_fetch_args_carry_method_url_headers_and_body() { |
| 118 | let mut request = Request::delete("https://csa/pin"); |
| 119 | request |
| 120 | .headers |
| 121 | .push(("x-ms-continuation".to_owned(), "next".to_owned())); |
| 122 | assert_eq!( |
| 123 | skype_fetch_args(&request), |
| 124 | json!({ |
| 125 | "method": "DELETE", |
| 126 | "url": "https://csa/pin", |
| 127 | "headers": {"x-ms-continuation": "next"}, |
| 128 | "body": null, |
| 129 | }) |
| 130 | ); |
| 131 | let post = Request { |
| 132 | method: Method::Post, |
| 133 | ..Request::get("https://csa/pin") |
| 134 | }; |
| 135 | assert_eq!(skype_fetch_args(&post)["method"], "POST"); |
| 136 | } |
| 137 | |
| 138 | #[test] |
| 139 | fn page_answers_become_api_responses() { |
| 140 | let answer = skype_fetch_answer(json!({"status": 204, "retryAfter": null, "body": null})); |
| 141 | assert_eq!(answer.status, 204); |
| 142 | assert!(answer.is_success()); |
| 143 | let failed = skype_fetch_answer(json!({"status": 0, "body": "authz_http_401"})); |
| 144 | assert!(!failed.is_success()); |
| 145 | assert_eq!(failed.body, json!("authz_http_401")); |
| 146 | } |
| 147 | } |