Add an endpoint for creating an access token for a GitHub login

This commit is contained in:
Nathan Sobo 2021-12-21 13:05:32 -07:00
parent 323e1f7367
commit 61b806e485
3 changed files with 21 additions and 2 deletions

View File

@ -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"

View File

@ -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<()>;

View File

@ -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";