start on local token creation

This commit is contained in:
Cadey Ratio 2020-10-27 21:07:25 -04:00
parent 512080c131
commit 5d273dbf1c
10 changed files with 105 additions and 26 deletions

3
Cargo.lock generated
View File

@ -240,6 +240,7 @@ dependencies = [
"libc",
"num-integer",
"num-traits",
"serde",
"time 0.1.44",
"winapi 0.3.9",
]
@ -393,6 +394,7 @@ checksum = "3e2de9deab977a153492a1468d1b1c0662c1cf39e5ea87d0c060ecd59ef18d8c"
dependencies = [
"bitflags",
"byteorder",
"chrono",
"diesel_derives",
"pq-sys",
"r2d2",
@ -2157,6 +2159,7 @@ checksum = "1d649a3145108d7d3fbcde896a468d1bd636791823c9921135218ad89be08307"
name = "wasmcloud-api"
version = "0.1.0"
dependencies = [
"chrono",
"color-eyre",
"diesel",
"log 0.4.11",

View File

@ -7,8 +7,9 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
chrono = { version = "0.4", features = ["serde"] }
color-eyre = "0.5"
diesel = { version = "1", features = ["postgres", "r2d2", "uuidv07"] }
diesel = { version = "1", features = ["postgres", "r2d2", "uuidv07", "chrono"] }
log = "0"
rocket = "0.4"
rocket_oauth2 = "0.4"

View File

@ -1,5 +1,13 @@
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE OR REPLACE FUNCTION trigger_set_timestamp()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TABLE IF NOT EXISTS users
( id UUID DEFAULT uuid_generate_v4() NOT NULL
, email VARCHAR UNIQUE NOT NULL
@ -7,5 +15,12 @@ CREATE TABLE IF NOT EXISTS users
, is_admin BOOLEAN DEFAULT false NOT NULL
, is_locked BOOLEAN DEFAULT false NOT NULL
, tier INTEGER DEFAULT 0 NOT NULL
, created_at TIMESTAMP NOT NULL DEFAULT NOW()
, updated_at TIMESTAMP NOT NULL DEFAULT NOW()
, PRIMARY KEY (id)
);
CREATE TRIGGER set_timestamp_users
BEFORE UPDATE ON users
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();

View File

@ -3,8 +3,15 @@ CREATE TABLE IF NOT EXISTS gitea_tokens
, user_id UUID NOT NULL
, access_token VARCHAR NOT NULL
, refresh_token VARCHAR NOT NULL
, created_at TIMESTAMP NOT NULL DEFAULT NOW()
, updated_at TIMESTAMP NOT NULL DEFAULT NOW()
, PRIMARY KEY (id)
, CONSTRAINT fk_user_id
FOREIGN KEY (user_id)
REFERENCES users(id)
);
CREATE TRIGGER set_timestamp_gitea_tokens
BEFORE UPDATE ON gitea_tokens
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();

View File

@ -0,0 +1 @@
-- This file should undo anything in `up.sql`

View File

@ -0,0 +1,13 @@
CREATE TABLE IF NOT EXISTS tokens
( id UUID DEFAULT uuid_generate_v4() NOT NULL
, user_id UUID NOT NULL
, created_at TIMESTAMP NOT NULL DEFAULT NOW()
, updated_at TIMESTAMP NOT NULL DEFAULT NOW()
, deleted_at TIMESTAMP
, PRIMARY KEY (id)
);
CREATE TRIGGER set_timestamp_tokens
BEFORE UPDATE ON tokens
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();

15
src/api.rs Normal file
View File

@ -0,0 +1,15 @@
use crate::{schema, models, MainDatabase};
use diesel::prelude::*;
use rocket_contrib::{json::Json, uuid::Uuid};
#[tracing::instrument(skip(conn))]
#[get("/user/<uuid>")]
pub fn get_user(conn: MainDatabase, uuid: Uuid) -> Json<models::User> {
use schema::users::dsl::users;
let result = users
.find(uuid.into_inner())
.get_result::<models::User>(&*conn)
.expect("to find user");
Json(result)
}

View File

@ -14,30 +14,19 @@ use rocket::{
http::{Cookie, Cookies, SameSite},
response::Redirect,
};
use rocket_contrib::{helmet::SpaceHelmet, json::Json, uuid::Uuid};
use rocket_contrib::{helmet::SpaceHelmet};
use rocket_oauth2::{OAuth2, TokenResponse};
pub mod api;
pub mod gitea;
pub mod models;
pub mod schema;
#[database("main_data")]
struct MainDatabase(PgConnection);
pub struct MainDatabase(PgConnection);
struct Gitea;
#[tracing::instrument(skip(conn))]
#[get("/user/<uuid>")]
fn get_user(conn: MainDatabase, uuid: Uuid) -> Json<models::User> {
use schema::users::dsl::users;
let result = users
.find(uuid.into_inner())
.get_result::<models::User>(&*conn)
.expect("to find user");
Json(result)
}
#[tracing::instrument(skip(oauth2, cookies))]
#[get("/login/gitea")]
fn gitea_login(oauth2: OAuth2<Gitea>, mut cookies: Cookies<'_>) -> Redirect {
@ -69,8 +58,7 @@ fn gitea_callback(
.load::<models::User>(&*conn)
{
Ok(u) => if u.len() == 0 {
let u = models::User {
id: uuid::Uuid::new_v4(),
let u = models::NewUser {
salutation: gitea_user.full_name,
email: gitea_user.email,
is_admin: gitea_user.is_admin,
@ -83,8 +71,7 @@ fn gitea_callback(
.get_result(&*conn)
.expect("able to insert user");
let tok = models::GiteaToken {
id: uuid::Uuid::new_v4(),
let tok = models::NewGiteaToken {
user_id: u.id.clone(),
access_token: tok,
refresh_token: refresh,
@ -124,7 +111,7 @@ fn main() -> Result<()> {
.attach(OAuth2::<Gitea>::fairing("gitea"))
.attach(MainDatabase::fairing())
.attach(SpaceHelmet::default())
.mount("/api", routes![get_user])
.mount("/api", routes![api::get_user])
.mount("/", routes![gitea_login, gitea_callback])
.launch();

View File

@ -1,11 +1,11 @@
use crate::schema::{gitea_tokens, users};
use chrono::NaiveDateTime;
use serde::Serialize;
use uuid::Uuid;
use crate::schema::{gitea_tokens, users};
#[derive(Insertable, Queryable, Serialize, Debug, Clone)]
#[table_name="users"]
pub struct User {
pub id: Uuid,
#[derive(Insertable)]
#[table_name = "users"]
pub struct NewUser {
pub email: String,
pub salutation: String,
pub is_admin: bool,
@ -13,11 +13,33 @@ pub struct User {
pub tier: i32,
}
#[derive(Insertable, Queryable, Debug, Clone)]
#[derive(Queryable, Serialize, Debug, Clone)]
pub struct User {
pub id: Uuid,
pub email: String,
pub salutation: String,
pub is_admin: bool,
pub is_locked: bool,
pub tier: i32,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
}
#[derive(Insertable)]
#[table_name="gitea_tokens"]
pub struct NewGiteaToken {
pub user_id: Uuid,
pub access_token: String,
pub refresh_token: String,
}
#[derive(Insertable, Queryable, Debug, Clone)]
#[table_name = "gitea_tokens"]
pub struct GiteaToken {
pub id: Uuid,
pub user_id: Uuid,
pub access_token: String,
pub refresh_token: String,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
}

View File

@ -4,6 +4,18 @@ table! {
user_id -> Uuid,
access_token -> Varchar,
refresh_token -> Varchar,
created_at -> Timestamp,
updated_at -> Timestamp,
}
}
table! {
tokens (id) {
id -> Uuid,
user_id -> Uuid,
created_at -> Timestamp,
updated_at -> Timestamp,
deleted_at -> Nullable<Timestamp>,
}
}
@ -15,6 +27,8 @@ table! {
is_admin -> Bool,
is_locked -> Bool,
tier -> Int4,
created_at -> Timestamp,
updated_at -> Timestamp,
}
}
@ -22,5 +36,6 @@ joinable!(gitea_tokens -> users (user_id));
allow_tables_to_appear_in_same_query!(
gitea_tokens,
tokens,
users,
);