Skip to content

Fix API routing, default endpoints and login not working with chorus #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 35 additions & 32 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,41 +49,11 @@ pub async fn start_api(
));
}

let routes = Route::new()
.nest("/auth", auth::setup_routes())
.nest(
"/users",
users::setup_routes()
.with(AuthenticationMiddleware)
.with(CurrentUserMiddleware),
)
.nest(
"/guilds",
guilds::setup_routes()
.with(AuthenticationMiddleware)
.with(CurrentUserMiddleware),
)
.nest(
"/channels",
channels::setup_routes()
.with(AuthenticationMiddleware)
.with(CurrentUserMiddleware),
)
.nest(
"/invites",
routes::invites::setup_routes()
.with(AuthenticationMiddleware)
.with(CurrentUserMiddleware),
)
.nest("/policies", routes::policies::setup_routes())
.nest("/-", routes::health::setup_routes())
.at("/version", routes::version::setup_routes())
.at("/ping", routes::ping::setup_routes());

let v9_api = Route::new()
.at("/ping", routes::ping::setup_routes())
.at("/version", routes::version::setup_routes())
.nest("/api/v9", routes)
.nest("/api", setup_api_routes())
.nest("/api/v9", setup_api_routes())
.data(db)
.data(config)
.data(connected_users)
Expand Down Expand Up @@ -118,6 +88,39 @@ pub async fn start_api(
Ok(())
}

fn setup_api_routes() -> Route {
Route::new()
.nest("/auth", auth::setup_routes())
.nest(
"/users",
users::setup_routes()
.with(AuthenticationMiddleware)
.with(CurrentUserMiddleware),
)
.nest(
"/guilds",
guilds::setup_routes()
.with(AuthenticationMiddleware)
.with(CurrentUserMiddleware),
)
.nest(
"/channels",
channels::setup_routes()
.with(AuthenticationMiddleware)
.with(CurrentUserMiddleware),
)
.nest(
"/invites",
routes::invites::setup_routes()
.with(AuthenticationMiddleware)
.with(CurrentUserMiddleware),
)
.nest("/policies", routes::policies::setup_routes())
.nest("/-", routes::health::setup_routes())
.at("/version", routes::version::setup_routes())
.at("/ping", routes::ping::setup_routes())
}

async fn custom_error(err: poem::Error) -> impl IntoResponse {
Json(json! ({
"success": false,
Expand Down
4 changes: 1 addition & 3 deletions src/api/routes/auth/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,11 @@ pub async fn login(
&cfg.security.jwt_secret,
);

//let user_settings = user.get_settings()

Ok(Response::builder()
.body(
json!({
"token": token,
"settings": {}
"settings": user.settings
})
.to_string(),
)
Expand Down
4 changes: 2 additions & 2 deletions src/api/routes/policies/instance/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ pub async fn domain(
} else if let Some(endpoint) = &cfg.gateway.endpoint_public {
endpoint.to_owned()
} else {
"http://localhost:3003".to_string()
"ws://localhost:3003".to_string()
};

let api = if let Ok(endpoint) = std::env::var("API") {
endpoint
} else if let Some(endpoint) = &cfg.api.endpoint_public {
endpoint.to_owned()
} else {
"http://localhost:3001".to_string()
"http://localhost:3001/api".to_string()
};

Ok(Json(json!({
Expand Down
4 changes: 1 addition & 3 deletions src/util/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ pub async fn check_token(db: &PgPool, token: &str, jwt_secret: &str) -> Result<C
.unwrap()
.ok_or(Error::User(UserError::InvalidUser))?;

// TODO(bitfl0wer): I changed the direction of the comparison here (from < to >) to fix a bug. I don't know if this is correct,
// nor have I looked at this code at all to see if it's correct.
if chrono::DateTime::from_timestamp(token.claims.iat, 0).unwrap() > user.data.valid_tokens_since
if chrono::DateTime::from_timestamp(token.claims.iat, 0).unwrap() < user.data.valid_tokens_since
{
return Err(Error::User(UserError::InvalidToken));
}
Expand Down
Loading