start on local token creation
This commit is contained in:
parent
512080c131
commit
5d273dbf1c
|
@ -240,6 +240,7 @@ dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"num-integer",
|
"num-integer",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
|
"serde",
|
||||||
"time 0.1.44",
|
"time 0.1.44",
|
||||||
"winapi 0.3.9",
|
"winapi 0.3.9",
|
||||||
]
|
]
|
||||||
|
@ -393,6 +394,7 @@ checksum = "3e2de9deab977a153492a1468d1b1c0662c1cf39e5ea87d0c060ecd59ef18d8c"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags",
|
||||||
"byteorder",
|
"byteorder",
|
||||||
|
"chrono",
|
||||||
"diesel_derives",
|
"diesel_derives",
|
||||||
"pq-sys",
|
"pq-sys",
|
||||||
"r2d2",
|
"r2d2",
|
||||||
|
@ -2157,6 +2159,7 @@ checksum = "1d649a3145108d7d3fbcde896a468d1bd636791823c9921135218ad89be08307"
|
||||||
name = "wasmcloud-api"
|
name = "wasmcloud-api"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"chrono",
|
||||||
"color-eyre",
|
"color-eyre",
|
||||||
"diesel",
|
"diesel",
|
||||||
"log 0.4.11",
|
"log 0.4.11",
|
||||||
|
|
|
@ -7,8 +7,9 @@ edition = "2018"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
chrono = { version = "0.4", features = ["serde"] }
|
||||||
color-eyre = "0.5"
|
color-eyre = "0.5"
|
||||||
diesel = { version = "1", features = ["postgres", "r2d2", "uuidv07"] }
|
diesel = { version = "1", features = ["postgres", "r2d2", "uuidv07", "chrono"] }
|
||||||
log = "0"
|
log = "0"
|
||||||
rocket = "0.4"
|
rocket = "0.4"
|
||||||
rocket_oauth2 = "0.4"
|
rocket_oauth2 = "0.4"
|
||||||
|
|
|
@ -1,5 +1,13 @@
|
||||||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
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
|
CREATE TABLE IF NOT EXISTS users
|
||||||
( id UUID DEFAULT uuid_generate_v4() NOT NULL
|
( id UUID DEFAULT uuid_generate_v4() NOT NULL
|
||||||
, email VARCHAR UNIQUE 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_admin BOOLEAN DEFAULT false NOT NULL
|
||||||
, is_locked BOOLEAN DEFAULT false NOT NULL
|
, is_locked BOOLEAN DEFAULT false NOT NULL
|
||||||
, tier INTEGER DEFAULT 0 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)
|
, PRIMARY KEY (id)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
CREATE TRIGGER set_timestamp_users
|
||||||
|
BEFORE UPDATE ON users
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE PROCEDURE trigger_set_timestamp();
|
||||||
|
|
|
@ -3,8 +3,15 @@ CREATE TABLE IF NOT EXISTS gitea_tokens
|
||||||
, user_id UUID NOT NULL
|
, user_id UUID NOT NULL
|
||||||
, access_token VARCHAR NOT NULL
|
, access_token VARCHAR NOT NULL
|
||||||
, refresh_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)
|
, PRIMARY KEY (id)
|
||||||
, CONSTRAINT fk_user_id
|
, CONSTRAINT fk_user_id
|
||||||
FOREIGN KEY (user_id)
|
FOREIGN KEY (user_id)
|
||||||
REFERENCES users(id)
|
REFERENCES users(id)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
CREATE TRIGGER set_timestamp_gitea_tokens
|
||||||
|
BEFORE UPDATE ON gitea_tokens
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE PROCEDURE trigger_set_timestamp();
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
-- This file should undo anything in `up.sql`
|
|
@ -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();
|
|
@ -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)
|
||||||
|
}
|
25
src/main.rs
25
src/main.rs
|
@ -14,30 +14,19 @@ use rocket::{
|
||||||
http::{Cookie, Cookies, SameSite},
|
http::{Cookie, Cookies, SameSite},
|
||||||
response::Redirect,
|
response::Redirect,
|
||||||
};
|
};
|
||||||
use rocket_contrib::{helmet::SpaceHelmet, json::Json, uuid::Uuid};
|
use rocket_contrib::{helmet::SpaceHelmet};
|
||||||
use rocket_oauth2::{OAuth2, TokenResponse};
|
use rocket_oauth2::{OAuth2, TokenResponse};
|
||||||
|
|
||||||
|
pub mod api;
|
||||||
pub mod gitea;
|
pub mod gitea;
|
||||||
pub mod models;
|
pub mod models;
|
||||||
pub mod schema;
|
pub mod schema;
|
||||||
|
|
||||||
#[database("main_data")]
|
#[database("main_data")]
|
||||||
struct MainDatabase(PgConnection);
|
pub struct MainDatabase(PgConnection);
|
||||||
|
|
||||||
struct Gitea;
|
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))]
|
#[tracing::instrument(skip(oauth2, cookies))]
|
||||||
#[get("/login/gitea")]
|
#[get("/login/gitea")]
|
||||||
fn gitea_login(oauth2: OAuth2<Gitea>, mut cookies: Cookies<'_>) -> Redirect {
|
fn gitea_login(oauth2: OAuth2<Gitea>, mut cookies: Cookies<'_>) -> Redirect {
|
||||||
|
@ -69,8 +58,7 @@ fn gitea_callback(
|
||||||
.load::<models::User>(&*conn)
|
.load::<models::User>(&*conn)
|
||||||
{
|
{
|
||||||
Ok(u) => if u.len() == 0 {
|
Ok(u) => if u.len() == 0 {
|
||||||
let u = models::User {
|
let u = models::NewUser {
|
||||||
id: uuid::Uuid::new_v4(),
|
|
||||||
salutation: gitea_user.full_name,
|
salutation: gitea_user.full_name,
|
||||||
email: gitea_user.email,
|
email: gitea_user.email,
|
||||||
is_admin: gitea_user.is_admin,
|
is_admin: gitea_user.is_admin,
|
||||||
|
@ -83,8 +71,7 @@ fn gitea_callback(
|
||||||
.get_result(&*conn)
|
.get_result(&*conn)
|
||||||
.expect("able to insert user");
|
.expect("able to insert user");
|
||||||
|
|
||||||
let tok = models::GiteaToken {
|
let tok = models::NewGiteaToken {
|
||||||
id: uuid::Uuid::new_v4(),
|
|
||||||
user_id: u.id.clone(),
|
user_id: u.id.clone(),
|
||||||
access_token: tok,
|
access_token: tok,
|
||||||
refresh_token: refresh,
|
refresh_token: refresh,
|
||||||
|
@ -124,7 +111,7 @@ fn main() -> Result<()> {
|
||||||
.attach(OAuth2::<Gitea>::fairing("gitea"))
|
.attach(OAuth2::<Gitea>::fairing("gitea"))
|
||||||
.attach(MainDatabase::fairing())
|
.attach(MainDatabase::fairing())
|
||||||
.attach(SpaceHelmet::default())
|
.attach(SpaceHelmet::default())
|
||||||
.mount("/api", routes![get_user])
|
.mount("/api", routes![api::get_user])
|
||||||
.mount("/", routes![gitea_login, gitea_callback])
|
.mount("/", routes![gitea_login, gitea_callback])
|
||||||
.launch();
|
.launch();
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
|
use crate::schema::{gitea_tokens, users};
|
||||||
|
use chrono::NaiveDateTime;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use crate::schema::{gitea_tokens, users};
|
|
||||||
|
|
||||||
#[derive(Insertable, Queryable, Serialize, Debug, Clone)]
|
#[derive(Insertable)]
|
||||||
#[table_name="users"]
|
#[table_name = "users"]
|
||||||
pub struct User {
|
pub struct NewUser {
|
||||||
pub id: Uuid,
|
|
||||||
pub email: String,
|
pub email: String,
|
||||||
pub salutation: String,
|
pub salutation: String,
|
||||||
pub is_admin: bool,
|
pub is_admin: bool,
|
||||||
|
@ -13,11 +13,33 @@ pub struct User {
|
||||||
pub tier: i32,
|
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"]
|
#[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 struct GiteaToken {
|
||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
pub user_id: Uuid,
|
pub user_id: Uuid,
|
||||||
pub access_token: String,
|
pub access_token: String,
|
||||||
pub refresh_token: String,
|
pub refresh_token: String,
|
||||||
|
pub created_at: NaiveDateTime,
|
||||||
|
pub updated_at: NaiveDateTime,
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,18 @@ table! {
|
||||||
user_id -> Uuid,
|
user_id -> Uuid,
|
||||||
access_token -> Varchar,
|
access_token -> Varchar,
|
||||||
refresh_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_admin -> Bool,
|
||||||
is_locked -> Bool,
|
is_locked -> Bool,
|
||||||
tier -> Int4,
|
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!(
|
allow_tables_to_appear_in_same_query!(
|
||||||
gitea_tokens,
|
gitea_tokens,
|
||||||
|
tokens,
|
||||||
users,
|
users,
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue