Enable authentication via the NextJS site
This commit is contained in:
parent
6dd23c250b
commit
fe5465a265
3 changed files with 79 additions and 31 deletions
|
@ -1,7 +1,9 @@
|
|||
use crate::{auth, AppState, Request, RequestExt as _};
|
||||
use async_trait::async_trait;
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
use std::sync::Arc;
|
||||
use surf::StatusCode;
|
||||
|
||||
pub fn add_routes(app: &mut tide::Server<Arc<AppState>>) {
|
||||
app.at("/users/:github_login").get(get_user);
|
||||
|
@ -18,7 +20,7 @@ async fn get_user(request: Request) -> tide::Result {
|
|||
.await?
|
||||
.ok_or_else(|| surf::Error::from_str(404, "user not found"))?;
|
||||
|
||||
Ok(tide::Response::builder(200)
|
||||
Ok(tide::Response::builder(StatusCode::Ok)
|
||||
.body(tide::Body::from_json(&user)?)
|
||||
.build())
|
||||
}
|
||||
|
@ -30,11 +32,49 @@ async fn create_access_token(request: Request) -> tide::Result {
|
|||
.db()
|
||||
.get_user_by_github_login(request.param("github_login")?)
|
||||
.await?
|
||||
.ok_or_else(|| surf::Error::from_str(404, "user not found"))?;
|
||||
let token = auth::create_access_token(request.db(), user.id).await?;
|
||||
.ok_or_else(|| surf::Error::from_str(StatusCode::NotFound, "user not found"))?;
|
||||
let access_token = auth::create_access_token(request.db(), user.id).await?;
|
||||
|
||||
Ok(tide::Response::builder(200)
|
||||
.body(json!({"user_id": user.id, "access_token": token}))
|
||||
#[derive(Deserialize)]
|
||||
struct QueryParams {
|
||||
public_key: String,
|
||||
impersonate: Option<String>,
|
||||
}
|
||||
|
||||
let query_params: QueryParams = request.query().map_err(|_| {
|
||||
surf::Error::from_str(StatusCode::UnprocessableEntity, "invalid query params")
|
||||
})?;
|
||||
|
||||
let encrypted_access_token =
|
||||
auth::encrypt_access_token(&access_token, query_params.public_key.clone())?;
|
||||
|
||||
let mut user_id = user.id;
|
||||
if let Some(impersonate) = query_params.impersonate {
|
||||
if user.admin {
|
||||
if let Some(impersonated_user) =
|
||||
request.db().get_user_by_github_login(&impersonate).await?
|
||||
{
|
||||
user_id = impersonated_user.id;
|
||||
} else {
|
||||
return Ok(tide::Response::builder(StatusCode::UnprocessableEntity)
|
||||
.body(format!(
|
||||
"Can't impersonate non-existent user {}",
|
||||
impersonate
|
||||
))
|
||||
.build());
|
||||
}
|
||||
} else {
|
||||
return Ok(tide::Response::builder(StatusCode::Unauthorized)
|
||||
.body(format!(
|
||||
"Can't impersonate user {} because the real user isn't an admin",
|
||||
impersonate
|
||||
))
|
||||
.build());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(tide::Response::builder(StatusCode::Ok)
|
||||
.body(json!({"user_id": user_id, "encrypted_access_token": encrypted_access_token}))
|
||||
.build())
|
||||
}
|
||||
|
||||
|
|
|
@ -238,12 +238,10 @@ async fn get_auth_callback(mut request: Request) -> tide::Result {
|
|||
}
|
||||
|
||||
let access_token = create_access_token(request.db(), user_id).await?;
|
||||
let native_app_public_key =
|
||||
zed_auth::PublicKey::try_from(app_sign_in_params.native_app_public_key.clone())
|
||||
.context("failed to parse app public key")?;
|
||||
let encrypted_access_token = native_app_public_key
|
||||
.encrypt_string(&access_token)
|
||||
.context("failed to encrypt access token with public key")?;
|
||||
let encrypted_access_token = encrypt_access_token(
|
||||
&access_token,
|
||||
app_sign_in_params.native_app_public_key.clone(),
|
||||
)?;
|
||||
|
||||
return Ok(tide::Redirect::new(&format!(
|
||||
"http://127.0.0.1:{}?user_id={}&access_token={}",
|
||||
|
@ -289,6 +287,15 @@ fn hash_access_token(token: &str) -> tide::Result<String> {
|
|||
.to_string())
|
||||
}
|
||||
|
||||
pub fn encrypt_access_token(access_token: &str, public_key: String) -> tide::Result<String> {
|
||||
let native_app_public_key =
|
||||
zed_auth::PublicKey::try_from(public_key).context("failed to parse app public key")?;
|
||||
let encrypted_access_token = native_app_public_key
|
||||
.encrypt_string(&access_token)
|
||||
.context("failed to encrypt access token with public key")?;
|
||||
Ok(encrypted_access_token)
|
||||
}
|
||||
|
||||
pub fn verify_access_token(token: &str, hash: &str) -> tide::Result<bool> {
|
||||
let hash = PasswordHash::new(hash)?;
|
||||
Ok(Scrypt.verify_password(token.as_bytes(), &hash).is_ok())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue