2022-04-25 00:02:14 +00:00
|
|
|
use crate::{
|
2022-04-25 23:51:13 +00:00
|
|
|
auth,
|
|
|
|
db::{User, UserId},
|
2022-05-25 23:42:25 +00:00
|
|
|
rpc::{self, ResultExt},
|
2022-04-25 23:51:13 +00:00
|
|
|
AppState, Error, Result,
|
2022-04-25 00:02:14 +00:00
|
|
|
};
|
|
|
|
use anyhow::anyhow;
|
2022-04-24 17:08:25 +00:00
|
|
|
use axum::{
|
|
|
|
body::Body,
|
2022-04-25 23:51:13 +00:00
|
|
|
extract::{Path, Query},
|
2022-04-26 02:05:09 +00:00
|
|
|
http::{self, Request, StatusCode},
|
|
|
|
middleware::{self, Next},
|
|
|
|
response::IntoResponse,
|
|
|
|
routing::{get, post, put},
|
|
|
|
Extension, Json, Router,
|
2022-04-24 17:08:25 +00:00
|
|
|
};
|
2022-05-26 07:34:39 +00:00
|
|
|
use axum_extra::response::ErasedJson;
|
2022-04-25 23:51:13 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-12-19 16:43:13 +00:00
|
|
|
use std::sync::Arc;
|
2022-04-26 02:05:09 +00:00
|
|
|
use tower::ServiceBuilder;
|
2022-05-02 13:17:43 +00:00
|
|
|
use tracing::instrument;
|
2022-04-24 16:45:20 +00:00
|
|
|
|
2022-05-25 23:42:25 +00:00
|
|
|
pub fn routes(rpc_server: &Arc<rpc::Server>, state: Arc<AppState>) -> Router<Body> {
|
2022-04-26 02:05:09 +00:00
|
|
|
Router::new()
|
|
|
|
.route("/users", get(get_users).post(create_user))
|
2022-04-26 17:15:41 +00:00
|
|
|
.route(
|
|
|
|
"/users/:id",
|
|
|
|
put(update_user).delete(destroy_user).get(get_user),
|
|
|
|
)
|
|
|
|
.route("/users/:id/access_tokens", post(create_access_token))
|
2022-05-19 18:35:07 +00:00
|
|
|
.route("/invite_codes/:code", get(get_user_for_invite_code))
|
2022-05-02 15:36:56 +00:00
|
|
|
.route("/panic", post(trace_panic))
|
2022-05-25 23:42:25 +00:00
|
|
|
.route("/rpc_server_snapshot", get(get_rpc_server_snapshot))
|
2022-04-26 02:05:09 +00:00
|
|
|
.layer(
|
|
|
|
ServiceBuilder::new()
|
|
|
|
.layer(Extension(state))
|
2022-05-20 10:02:31 +00:00
|
|
|
.layer(Extension(rpc_server.clone()))
|
2022-04-26 02:05:09 +00:00
|
|
|
.layer(middleware::from_fn(validate_api_token)),
|
|
|
|
)
|
2022-04-24 16:45:20 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 02:05:09 +00:00
|
|
|
pub async fn validate_api_token<B>(req: Request<B>, next: Next<B>) -> impl IntoResponse {
|
2022-04-26 02:10:14 +00:00
|
|
|
let token = req
|
2022-04-26 02:05:09 +00:00
|
|
|
.headers()
|
|
|
|
.get(http::header::AUTHORIZATION)
|
|
|
|
.and_then(|header| header.to_str().ok())
|
|
|
|
.ok_or_else(|| {
|
|
|
|
Error::Http(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
"missing authorization header".to_string(),
|
|
|
|
)
|
2022-04-26 02:10:14 +00:00
|
|
|
})?
|
|
|
|
.strip_prefix("token ")
|
|
|
|
.ok_or_else(|| {
|
|
|
|
Error::Http(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
"invalid authorization header".to_string(),
|
|
|
|
)
|
2022-04-26 02:05:09 +00:00
|
|
|
})?;
|
|
|
|
|
2022-04-26 02:10:14 +00:00
|
|
|
let state = req.extensions().get::<Arc<AppState>>().unwrap();
|
|
|
|
|
|
|
|
if token != state.api_token {
|
|
|
|
Err(Error::Http(
|
|
|
|
StatusCode::UNAUTHORIZED,
|
|
|
|
"invalid authorization token".to_string(),
|
|
|
|
))?
|
|
|
|
}
|
|
|
|
|
2022-04-26 02:05:09 +00:00
|
|
|
Ok::<_, Error>(next.run(req).await)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_users(Extension(app): Extension<Arc<AppState>>) -> Result<Json<Vec<User>>> {
|
2022-04-25 00:02:14 +00:00
|
|
|
let users = app.db.get_all_users().await?;
|
|
|
|
Ok(Json(users))
|
2022-04-24 17:08:25 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 02:24:02 +00:00
|
|
|
#[derive(Deserialize, Debug)]
|
2022-04-25 23:51:13 +00:00
|
|
|
struct CreateUserParams {
|
2022-04-25 00:02:14 +00:00
|
|
|
github_login: String,
|
2022-05-18 17:51:47 +00:00
|
|
|
invite_code: Option<String>,
|
2022-05-21 02:24:02 +00:00
|
|
|
email_address: Option<String>,
|
2022-04-25 00:02:14 +00:00
|
|
|
admin: bool,
|
|
|
|
}
|
2022-04-22 19:33:19 +00:00
|
|
|
|
2022-04-25 23:51:13 +00:00
|
|
|
async fn create_user(
|
|
|
|
Json(params): Json<CreateUserParams>,
|
2022-04-26 02:05:09 +00:00
|
|
|
Extension(app): Extension<Arc<AppState>>,
|
2022-05-25 23:42:25 +00:00
|
|
|
Extension(rpc_server): Extension<Arc<rpc::Server>>,
|
2022-04-25 23:51:13 +00:00
|
|
|
) -> Result<Json<User>> {
|
2022-05-18 22:14:58 +00:00
|
|
|
let user_id = if let Some(invite_code) = params.invite_code {
|
2022-05-20 10:02:31 +00:00
|
|
|
let invitee_id = app
|
|
|
|
.db
|
2022-05-21 02:24:02 +00:00
|
|
|
.redeem_invite_code(
|
|
|
|
&invite_code,
|
|
|
|
¶ms.github_login,
|
|
|
|
params.email_address.as_deref(),
|
|
|
|
)
|
2022-05-20 10:02:31 +00:00
|
|
|
.await?;
|
|
|
|
rpc_server
|
|
|
|
.invite_code_redeemed(&invite_code, invitee_id)
|
|
|
|
.await
|
|
|
|
.trace_err();
|
|
|
|
invitee_id
|
2022-05-18 17:51:47 +00:00
|
|
|
} else {
|
2022-05-18 22:14:58 +00:00
|
|
|
app.db
|
2022-05-21 02:24:02 +00:00
|
|
|
.create_user(
|
|
|
|
¶ms.github_login,
|
|
|
|
params.email_address.as_deref(),
|
|
|
|
params.admin,
|
|
|
|
)
|
2022-05-18 17:51:47 +00:00
|
|
|
.await?
|
|
|
|
};
|
2022-04-22 19:33:19 +00:00
|
|
|
|
2022-05-18 22:14:58 +00:00
|
|
|
let user = app
|
|
|
|
.db
|
|
|
|
.get_user_by_id(user_id)
|
|
|
|
.await?
|
|
|
|
.ok_or_else(|| anyhow!("couldn't find the user we just created"))?;
|
|
|
|
|
2022-04-25 00:02:14 +00:00
|
|
|
Ok(Json(user))
|
|
|
|
}
|
2022-04-22 19:33:19 +00:00
|
|
|
|
2022-04-25 00:02:14 +00:00
|
|
|
#[derive(Deserialize)]
|
2022-04-25 23:51:13 +00:00
|
|
|
struct UpdateUserParams {
|
2022-05-18 16:23:08 +00:00
|
|
|
admin: Option<bool>,
|
|
|
|
invite_count: Option<u32>,
|
2022-04-25 00:02:14 +00:00
|
|
|
}
|
2022-04-22 19:33:19 +00:00
|
|
|
|
2022-04-25 00:02:14 +00:00
|
|
|
async fn update_user(
|
|
|
|
Path(user_id): Path<i32>,
|
2022-04-25 23:51:13 +00:00
|
|
|
Json(params): Json<UpdateUserParams>,
|
2022-04-26 02:05:09 +00:00
|
|
|
Extension(app): Extension<Arc<AppState>>,
|
2022-06-06 18:14:01 +00:00
|
|
|
Extension(rpc_server): Extension<Arc<rpc::Server>>,
|
2022-04-25 23:51:13 +00:00
|
|
|
) -> Result<()> {
|
2022-06-06 18:14:01 +00:00
|
|
|
let user_id = UserId(user_id);
|
|
|
|
|
2022-05-18 16:23:08 +00:00
|
|
|
if let Some(admin) = params.admin {
|
2022-06-06 18:14:01 +00:00
|
|
|
app.db.set_user_is_admin(user_id, admin).await?;
|
2022-05-18 16:23:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(invite_count) = params.invite_count {
|
2022-06-06 18:14:01 +00:00
|
|
|
app.db.set_invite_count(user_id, invite_count).await?;
|
|
|
|
rpc_server.invite_count_updated(user_id).await.trace_err();
|
2022-05-18 16:23:08 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 00:02:14 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-04-22 19:33:19 +00:00
|
|
|
|
2022-04-26 02:05:09 +00:00
|
|
|
async fn destroy_user(
|
|
|
|
Path(user_id): Path<i32>,
|
|
|
|
Extension(app): Extension<Arc<AppState>>,
|
|
|
|
) -> Result<()> {
|
2022-04-25 23:51:13 +00:00
|
|
|
app.db.destroy_user(UserId(user_id)).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-04-22 19:33:19 +00:00
|
|
|
|
2022-04-26 02:05:09 +00:00
|
|
|
async fn get_user(
|
|
|
|
Path(login): Path<String>,
|
|
|
|
Extension(app): Extension<Arc<AppState>>,
|
|
|
|
) -> Result<Json<User>> {
|
2022-04-25 23:51:13 +00:00
|
|
|
let user = app
|
|
|
|
.db
|
|
|
|
.get_user_by_github_login(&login)
|
|
|
|
.await?
|
2022-05-18 16:32:58 +00:00
|
|
|
.ok_or_else(|| Error::Http(StatusCode::NOT_FOUND, "User not found".to_string()))?;
|
2022-04-25 23:51:13 +00:00
|
|
|
Ok(Json(user))
|
|
|
|
}
|
2022-04-22 19:33:19 +00:00
|
|
|
|
2022-05-02 13:17:43 +00:00
|
|
|
#[derive(Debug, Deserialize)]
|
2022-05-02 15:36:56 +00:00
|
|
|
struct Panic {
|
2022-05-02 13:17:43 +00:00
|
|
|
version: String,
|
|
|
|
text: String,
|
|
|
|
}
|
|
|
|
|
2022-05-02 15:36:56 +00:00
|
|
|
#[instrument(skip(panic))]
|
|
|
|
async fn trace_panic(panic: Json<Panic>) -> Result<()> {
|
|
|
|
tracing::error!(version = %panic.version, text = %panic.text, "panic report");
|
2022-05-02 13:17:43 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-05-26 07:34:39 +00:00
|
|
|
async fn get_rpc_server_snapshot(
|
2022-05-25 23:42:25 +00:00
|
|
|
Extension(rpc_server): Extension<Arc<rpc::Server>>,
|
2022-05-26 07:34:39 +00:00
|
|
|
) -> Result<ErasedJson> {
|
|
|
|
Ok(ErasedJson::pretty(rpc_server.snapshot().await))
|
2022-05-25 23:42:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 23:51:13 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct CreateAccessTokenQueryParams {
|
|
|
|
public_key: String,
|
|
|
|
impersonate: Option<String>,
|
|
|
|
}
|
2022-04-22 19:33:19 +00:00
|
|
|
|
2022-04-25 23:51:13 +00:00
|
|
|
#[derive(Serialize)]
|
|
|
|
struct CreateAccessTokenResponse {
|
|
|
|
user_id: UserId,
|
|
|
|
encrypted_access_token: String,
|
|
|
|
}
|
2022-04-22 19:33:19 +00:00
|
|
|
|
2022-04-25 23:51:13 +00:00
|
|
|
async fn create_access_token(
|
|
|
|
Path(login): Path<String>,
|
|
|
|
Query(params): Query<CreateAccessTokenQueryParams>,
|
2022-04-26 02:05:09 +00:00
|
|
|
Extension(app): Extension<Arc<AppState>>,
|
2022-04-25 23:51:13 +00:00
|
|
|
) -> Result<Json<CreateAccessTokenResponse>> {
|
|
|
|
// request.require_token().await?;
|
2022-04-22 19:33:19 +00:00
|
|
|
|
2022-04-25 23:51:13 +00:00
|
|
|
let user = app
|
|
|
|
.db
|
|
|
|
.get_user_by_github_login(&login)
|
|
|
|
.await?
|
|
|
|
.ok_or_else(|| anyhow!("user not found"))?;
|
|
|
|
|
|
|
|
let mut user_id = user.id;
|
|
|
|
if let Some(impersonate) = params.impersonate {
|
|
|
|
if user.admin {
|
|
|
|
if let Some(impersonated_user) = app.db.get_user_by_github_login(&impersonate).await? {
|
|
|
|
user_id = impersonated_user.id;
|
|
|
|
} else {
|
|
|
|
return Err(Error::Http(
|
|
|
|
StatusCode::UNPROCESSABLE_ENTITY,
|
|
|
|
format!("user {impersonate} does not exist"),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(Error::Http(
|
|
|
|
StatusCode::UNAUTHORIZED,
|
|
|
|
format!("you do not have permission to impersonate other users"),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let access_token = auth::create_access_token(app.db.as_ref(), user_id).await?;
|
|
|
|
let encrypted_access_token =
|
|
|
|
auth::encrypt_access_token(&access_token, params.public_key.clone())?;
|
|
|
|
|
|
|
|
Ok(Json(CreateAccessTokenResponse {
|
|
|
|
user_id,
|
|
|
|
encrypted_access_token,
|
|
|
|
}))
|
|
|
|
}
|
2022-05-19 18:35:07 +00:00
|
|
|
|
|
|
|
async fn get_user_for_invite_code(
|
|
|
|
Path(code): Path<String>,
|
|
|
|
Extension(app): Extension<Arc<AppState>>,
|
|
|
|
) -> Result<Json<User>> {
|
|
|
|
Ok(Json(app.db.get_user_for_invite_code(&code).await?))
|
|
|
|
}
|