Finish adding API routes
We haven't tested them yet. Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
parent
cb9d608e53
commit
35bec69fa4
3 changed files with 156 additions and 161 deletions
|
@ -1,18 +1,10 @@
|
|||
// use super::{
|
||||
// db::{self, UserId},
|
||||
// errors::TideResultExt,
|
||||
// };
|
||||
// use crate::Request;
|
||||
// use anyhow::{anyhow, Context};
|
||||
// use rand::thread_rng;
|
||||
// use rpc::auth as zed_auth;
|
||||
// use scrypt::{
|
||||
// password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
|
||||
// Scrypt,
|
||||
// };
|
||||
// use std::convert::TryFrom;
|
||||
// use surf::StatusCode;
|
||||
// use tide::Error;
|
||||
use super::db::{self, UserId};
|
||||
use anyhow::{Context, Result};
|
||||
use rand::thread_rng;
|
||||
use scrypt::{
|
||||
password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
|
||||
Scrypt,
|
||||
};
|
||||
|
||||
// pub async fn process_auth_header(request: &Request) -> tide::Result<UserId> {
|
||||
// let mut auth_header = request
|
||||
|
@ -58,45 +50,45 @@
|
|||
// Ok(user_id)
|
||||
// }
|
||||
|
||||
// const MAX_ACCESS_TOKENS_TO_STORE: usize = 8;
|
||||
const MAX_ACCESS_TOKENS_TO_STORE: usize = 8;
|
||||
|
||||
// pub async fn create_access_token(db: &dyn db::Db, user_id: UserId) -> tide::Result<String> {
|
||||
// let access_token = zed_auth::random_token();
|
||||
// let access_token_hash =
|
||||
// hash_access_token(&access_token).context("failed to hash access token")?;
|
||||
// db.create_access_token_hash(user_id, &access_token_hash, MAX_ACCESS_TOKENS_TO_STORE)
|
||||
// .await?;
|
||||
// Ok(access_token)
|
||||
// }
|
||||
pub async fn create_access_token(db: &dyn db::Db, user_id: UserId) -> Result<String> {
|
||||
let access_token = rpc::auth::random_token();
|
||||
let access_token_hash =
|
||||
hash_access_token(&access_token).context("failed to hash access token")?;
|
||||
db.create_access_token_hash(user_id, &access_token_hash, MAX_ACCESS_TOKENS_TO_STORE)
|
||||
.await?;
|
||||
Ok(access_token)
|
||||
}
|
||||
|
||||
// fn hash_access_token(token: &str) -> tide::Result<String> {
|
||||
// // Avoid slow hashing in debug mode.
|
||||
// let params = if cfg!(debug_assertions) {
|
||||
// scrypt::Params::new(1, 1, 1).unwrap()
|
||||
// } else {
|
||||
// scrypt::Params::recommended()
|
||||
// };
|
||||
fn hash_access_token(token: &str) -> Result<String> {
|
||||
// Avoid slow hashing in debug mode.
|
||||
let params = if cfg!(debug_assertions) {
|
||||
scrypt::Params::new(1, 1, 1).unwrap()
|
||||
} else {
|
||||
scrypt::Params::recommended()
|
||||
};
|
||||
|
||||
// Ok(Scrypt
|
||||
// .hash_password(
|
||||
// token.as_bytes(),
|
||||
// None,
|
||||
// params,
|
||||
// &SaltString::generate(thread_rng()),
|
||||
// )?
|
||||
// .to_string())
|
||||
// }
|
||||
Ok(Scrypt
|
||||
.hash_password(
|
||||
token.as_bytes(),
|
||||
None,
|
||||
params,
|
||||
&SaltString::generate(thread_rng()),
|
||||
)?
|
||||
.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 encrypt_access_token(access_token: &str, public_key: String) -> Result<String> {
|
||||
let native_app_public_key =
|
||||
rpc::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())
|
||||
// }
|
||||
pub fn verify_access_token(token: &str, hash: &str) -> 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