wip
This commit is contained in:
@@ -6,6 +6,8 @@ pub mod test;
|
||||
pub mod transport;
|
||||
pub mod types;
|
||||
|
||||
pub use transport::UnauthorizedError;
|
||||
|
||||
use collections::HashMap;
|
||||
use http_client::HttpClient;
|
||||
use std::path::Path;
|
||||
|
||||
@@ -4,7 +4,7 @@ mod stdio_transport;
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use futures::Stream;
|
||||
use std::pin::Pin;
|
||||
use std::{error::Error, fmt, pin::Pin};
|
||||
|
||||
pub use http::*;
|
||||
pub use stdio_transport::*;
|
||||
@@ -15,3 +15,16 @@ pub trait Transport: Send + Sync {
|
||||
fn receive(&self) -> Pin<Box<dyn Stream<Item = String> + Send>>;
|
||||
fn receive_err(&self) -> Pin<Box<dyn Stream<Item = String> + Send>>;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct UnauthorizedError {
|
||||
pub www_authenticate_header: Option<String>,
|
||||
}
|
||||
|
||||
impl Error for UnauthorizedError {}
|
||||
|
||||
impl fmt::Display for UnauthorizedError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "Unauthorized")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ use smol::channel;
|
||||
use std::{pin::Pin, sync::Arc};
|
||||
|
||||
use crate::transport::{
|
||||
Transport,
|
||||
Transport, UnauthorizedError,
|
||||
http::{auth::OAuthClient, www_authenticate::WwwAuthenticate},
|
||||
};
|
||||
|
||||
@@ -137,16 +137,14 @@ impl HttpTransport {
|
||||
log::debug!("Notification accepted");
|
||||
}
|
||||
status if status.as_u16() == 401 => {
|
||||
// todo! stateful
|
||||
let www_authenticate_header = response.headers().get("WWW-Authenticate");
|
||||
let www_authenticate_header = response
|
||||
.headers()
|
||||
.get("WWW-Authenticate")
|
||||
.and_then(|value| Some(value.to_str().ok()?.to_string()));
|
||||
|
||||
let www_authenticate = www_authenticate_header
|
||||
.and_then(|value| WwwAuthenticate::parse(value.to_str().ok()?));
|
||||
let client =
|
||||
OAuthClient::init(&self.endpoint, www_authenticate.as_ref(), &self.http_client)
|
||||
.await?;
|
||||
let (url, code_verifier) = client.authorize_url()?;
|
||||
dbg!(url);
|
||||
anyhow::bail!(UnauthorizedError {
|
||||
www_authenticate_header
|
||||
})
|
||||
}
|
||||
_ => {
|
||||
let mut error_body = String::new();
|
||||
|
||||
Reference in New Issue
Block a user