Sign out upon receiving an Unauthorized response when acquiring an LLM token (#49661)
This PR makes it so the user gets signed out upon receiving an Unauthorized response when acquiring an LLM token. Closes CLO-324. Release Notes: - N/A
This commit is contained in:
1 parent
5a8bbfea81
commit
e90302e1fd
6 files changed
+100
-26
No files matched your search
@@ -11,6 +11,7 @@ use gpui_tokio::Tokio;
|
||||
use http_client::http::request;
|
||||
use http_client::{AsyncBody, HttpClientWithUrl, HttpRequestExt, Method, Request, StatusCode};
|
||||
use parking_lot::RwLock;
|
||||
use thiserror::Error;
|
||||
use yawc::WebSocket;
|
||||
|
||||
use crate::websocket::Connection;
|
||||
@@ -20,6 +21,14 @@ struct Credentials {
|
||||
access_token: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ClientApiError {
|
||||
#[error("Unauthorized")]
|
||||
Unauthorized,
|
||||
#[error(transparent)]
|
||||
Other(#[from] anyhow::Error),
|
||||
}
|
||||
|
||||
pub struct CloudApiClient {
|
||||
credentials: RwLock<Option<Credentials>>,
|
||||
http_client: Arc<HttpClientWithUrl>,
|
||||
@@ -58,7 +67,9 @@ impl CloudApiClient {
|
||||
build_request(req, body, credentials)
|
||||
}
|
||||
|
||||
pub async fn get_authenticated_user(&self) -> Result<GetAuthenticatedUserResponse> {
|
||||
pub async fn get_authenticated_user(
|
||||
&self,
|
||||
) -> Result<GetAuthenticatedUserResponse, ClientApiError> {
|
||||
let request = self.build_request(
|
||||
Request::builder().method(Method::GET).uri(
|
||||
self.http_client
|
||||
@@ -71,19 +82,31 @@ impl CloudApiClient {
|
||||
let mut response = self.http_client.send(request).await?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
let mut body = String::new();
|
||||
response.body_mut().read_to_string(&mut body).await?;
|
||||
if response.status() == StatusCode::UNAUTHORIZED {
|
||||
return Err(ClientApiError::Unauthorized);
|
||||
}
|
||||
|
||||
anyhow::bail!(
|
||||
let mut body = String::new();
|
||||
response
|
||||
.body_mut()
|
||||
.read_to_string(&mut body)
|
||||
.await
|
||||
.context("failed to read response body")?;
|
||||
|
||||
return Err(ClientApiError::Other(anyhow::anyhow!(
|
||||
"Failed to get authenticated user.\nStatus: {:?}\nBody: {body}",
|
||||
response.status()
|
||||
)
|
||||
)));
|
||||
}
|
||||
|
||||
let mut body = String::new();
|
||||
response.body_mut().read_to_string(&mut body).await?;
|
||||
response
|
||||
.body_mut()
|
||||
.read_to_string(&mut body)
|
||||
.await
|
||||
.context("failed to read response body")?;
|
||||
|
||||
Ok(serde_json::from_str(&body)?)
|
||||
Ok(serde_json::from_str(&body).context("failed to parse response body")?)
|
||||
}
|
||||
|
||||
pub fn connect(&self, cx: &App) -> Result<Task<Result<Connection>>> {
|
||||
@@ -118,7 +141,7 @@ impl CloudApiClient {
|
||||
pub async fn create_llm_token(
|
||||
&self,
|
||||
system_id: Option<String>,
|
||||
) -> Result<CreateLlmTokenResponse> {
|
||||
) -> Result<CreateLlmTokenResponse, ClientApiError> {
|
||||
let request_builder = Request::builder()
|
||||
.method(Method::POST)
|
||||
.uri(
|
||||
@@ -135,19 +158,31 @@ impl CloudApiClient {
|
||||
let mut response = self.http_client.send(request).await?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
let mut body = String::new();
|
||||
response.body_mut().read_to_string(&mut body).await?;
|
||||
if response.status() == StatusCode::UNAUTHORIZED {
|
||||
return Err(ClientApiError::Unauthorized);
|
||||
}
|
||||
|
||||
anyhow::bail!(
|
||||
let mut body = String::new();
|
||||
response
|
||||
.body_mut()
|
||||
.read_to_string(&mut body)
|
||||
.await
|
||||
.context("failed to read response body")?;
|
||||
|
||||
return Err(ClientApiError::Other(anyhow::anyhow!(
|
||||
"Failed to create LLM token.\nStatus: {:?}\nBody: {body}",
|
||||
response.status()
|
||||
)
|
||||
)));
|
||||
}
|
||||
|
||||
let mut body = String::new();
|
||||
response.body_mut().read_to_string(&mut body).await?;
|
||||
response
|
||||
.body_mut()
|
||||
.read_to_string(&mut body)
|
||||
.await
|
||||
.context("failed to read response body")?;
|
||||
|
||||
Ok(serde_json::from_str(&body)?)
|
||||
Ok(serde_json::from_str(&body).context("failed to parse response body")?)
|
||||
}
|
||||
|
||||
pub async fn validate_credentials(&self, user_id: u32, access_token: &str) -> Result<bool> {
|
||||
|
||||
Reference in new issue
Block a user