mirror of
https://github.com/zed-industries/zed.git
synced 2024-11-08 07:35:01 +03:00
Add an endpoint for creating an access token for a GitHub login
This commit is contained in:
parent
323e1f7367
commit
61b806e485
@ -38,6 +38,7 @@ rand = "0.8"
|
||||
rust-embed = { version = "6.2", features = ["include-exclude"] }
|
||||
scrypt = "0.7"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
sha-1 = "0.9"
|
||||
surf = "2.2.0"
|
||||
tide = "0.16.0"
|
||||
|
@ -1,9 +1,12 @@
|
||||
use crate::{AppState, Request, RequestExt as _};
|
||||
use crate::{auth, AppState, Request, RequestExt as _};
|
||||
use async_trait::async_trait;
|
||||
use serde_json::json;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub fn add_routes(app: &mut tide::Server<Arc<AppState>>) {
|
||||
app.at("/users/:github_login").get(get_user);
|
||||
app.at("/users/:github_login/access_tokens")
|
||||
.post(create_access_token);
|
||||
}
|
||||
|
||||
async fn get_user(request: Request) -> tide::Result {
|
||||
@ -20,6 +23,21 @@ async fn get_user(request: Request) -> tide::Result {
|
||||
.build())
|
||||
}
|
||||
|
||||
async fn create_access_token(request: Request) -> tide::Result {
|
||||
request.require_token().await?;
|
||||
|
||||
let user = request
|
||||
.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(tide::Response::builder(200)
|
||||
.body(json!({"user_id": user.id, "access_token": token}))
|
||||
.build())
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait RequestExt {
|
||||
async fn require_token(&self) -> tide::Result<()>;
|
||||
|
@ -11,6 +11,7 @@ use oauth2::{
|
||||
TokenResponse as _, TokenUrl,
|
||||
};
|
||||
use rand::thread_rng;
|
||||
use rpc::auth as zed_auth;
|
||||
use scrypt::{
|
||||
password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
|
||||
Scrypt,
|
||||
@ -19,7 +20,6 @@ use serde::{Deserialize, Serialize};
|
||||
use std::{borrow::Cow, convert::TryFrom, sync::Arc};
|
||||
use surf::{StatusCode, Url};
|
||||
use tide::{log, Error, Server};
|
||||
use rpc::auth as zed_auth;
|
||||
|
||||
static CURRENT_GITHUB_USER: &'static str = "current_github_user";
|
||||
static GITHUB_AUTH_URL: &'static str = "https://github.com/login/oauth/authorize";
|
||||
|
Loading…
Reference in New Issue
Block a user