(
key: &str,
code: String,
verifier: OAuth2Verifier,
app: &AppHandle,
)
| 175 | |
| 176 | #[tracing::instrument(level = "debug", skip(key, code, verifier, app))] |
| 177 | pub async fn authorize( |
| 178 | key: &str, |
| 179 | code: String, |
| 180 | verifier: OAuth2Verifier, |
| 181 | app: &AppHandle, |
| 182 | ) -> Result<TokenHolder> { |
| 183 | if verifier.is_none() { |
| 184 | return Err("OAuth not initiated".into()); |
| 185 | } |
| 186 | |
| 187 | let parsed_code = if code.starts_with("?") { |
| 188 | Url::parse(format!("https://moosync.app/login{}", code).as_str()).unwrap() |
| 189 | } else { |
| 190 | Url::parse(code.as_str()).unwrap() |
| 191 | }; |
| 192 | let code = parsed_code |
| 193 | .query_pairs() |
| 194 | .find(|p| p.0 == "code") |
| 195 | .unwrap() |
| 196 | .1 |
| 197 | .to_string(); |
| 198 | |
| 199 | let (client, verifier, _csrf) = verifier.unwrap(); |
| 200 | |
| 201 | let res = client |
| 202 | .exchange_code(AuthorizationCode::new(code)) |
| 203 | .set_pkce_verifier(verifier) |
| 204 | .request_async(async_http_client) |
| 205 | .await |
| 206 | .map_err(|err| match err { |
| 207 | oauth2::RequestTokenError::ServerResponse(e) => MoosyncError::String(format!( |
| 208 | "{:?}: {:?} {:?}", |
| 209 | e.error(), |
| 210 | e.error_description(), |
| 211 | serde_json::to_string(&e) |
| 212 | )), |
| 213 | oauth2::RequestTokenError::Request(_) => todo!(), |
| 214 | oauth2::RequestTokenError::Parse(_, _) => todo!(), |
| 215 | oauth2::RequestTokenError::Other(_) => todo!(), |
| 216 | })?; |
| 217 | |
| 218 | set_tokens(key, app, res, None) |
| 219 | } |
no test coverage detected