Add an ApiToken model, and an endpoint to get one
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
use chrono::NaiveDateTime;
|
||||
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||
|
||||
use schema::api_tokens;
|
||||
|
||||
#[derive(Clone, Queryable)]
|
||||
pub struct ApiToken {
|
||||
pub id: i32,
|
||||
pub creation_date: NaiveDateTime,
|
||||
pub value: String,
|
||||
|
||||
/// Scopes, separated by +
|
||||
/// Global scopes are read and write
|
||||
/// and both can be limited to an endpoint by affixing them with :ENDPOINT
|
||||
///
|
||||
/// Examples :
|
||||
///
|
||||
/// read
|
||||
/// read+write
|
||||
/// read:posts
|
||||
/// read:posts+write:posts
|
||||
pub scopes: String,
|
||||
pub app_id: i32,
|
||||
pub user_id: i32,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[table_name = "api_tokens"]
|
||||
pub struct NewApiToken {
|
||||
pub value: String,
|
||||
pub scopes: String,
|
||||
pub app_id: i32,
|
||||
pub user_id: i32,
|
||||
}
|
||||
|
||||
impl ApiToken {
|
||||
get!(api_tokens);
|
||||
insert!(api_tokens, NewApiToken);
|
||||
find_by!(api_tokens, find_by_value, value as String);
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
use canapi::{Error, Provider};
|
||||
use chrono::NaiveDateTime;
|
||||
use diesel::{self, RunQueryDsl, QueryDsl, ExpressionMethods};
|
||||
use openssl::rand::rand_bytes;
|
||||
|
||||
use plume_api::apps::AppEndpoint;
|
||||
use plume_common::utils::random_hex;
|
||||
use Connection;
|
||||
use schema::apps;
|
||||
|
||||
@@ -14,7 +14,7 @@ pub struct App {
|
||||
pub client_id: String,
|
||||
pub client_secret: String,
|
||||
pub redirect_uri: Option<String>,
|
||||
pub website: Option<String>,
|
||||
pub website: Option<String>,
|
||||
pub creation_date: NaiveDateTime,
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ pub struct NewApp {
|
||||
pub client_id: String,
|
||||
pub client_secret: String,
|
||||
pub redirect_uri: Option<String>,
|
||||
pub website: Option<String>,
|
||||
pub website: Option<String>,
|
||||
}
|
||||
|
||||
impl Provider<Connection> for App {
|
||||
@@ -40,13 +40,9 @@ impl Provider<Connection> for App {
|
||||
}
|
||||
|
||||
fn create(conn: &Connection, data: AppEndpoint) -> Result<AppEndpoint, Error> {
|
||||
let mut id = [0; 32];
|
||||
rand_bytes(&mut id).expect("Error while generating client id");
|
||||
let client_id = id.into_iter().fold(String::new(), |res, byte| format!("{}{:x}", res, byte));
|
||||
|
||||
let mut secret = [0; 32];
|
||||
rand_bytes(&mut secret).expect("Error while generating client secret");
|
||||
let client_secret = secret.into_iter().fold(String::new(), |res, byte| format!("{}{:x}", res, byte));
|
||||
let client_id = random_hex();
|
||||
|
||||
let client_secret = random_hex();
|
||||
let app = App::insert(conn, NewApp {
|
||||
name: data.name.expect("App::create: name is required"),
|
||||
client_id: client_id,
|
||||
@@ -68,7 +64,7 @@ impl Provider<Connection> for App {
|
||||
fn update(conn: &Connection, id: i32, new_data: AppEndpoint) -> Result<AppEndpoint, Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
|
||||
fn delete(conn: &Connection, id: i32) {
|
||||
unimplemented!()
|
||||
}
|
||||
@@ -77,4 +73,5 @@ impl Provider<Connection> for App {
|
||||
impl App {
|
||||
get!(apps);
|
||||
insert!(apps, NewApp);
|
||||
}
|
||||
find_by!(apps, find_by_client_id, client_id as String);
|
||||
}
|
||||
|
||||
@@ -214,6 +214,7 @@ pub fn ap_url(url: String) -> String {
|
||||
}
|
||||
|
||||
pub mod admin;
|
||||
pub mod api_tokens;
|
||||
pub mod apps;
|
||||
pub mod blog_authors;
|
||||
pub mod blogs;
|
||||
|
||||
@@ -1,3 +1,14 @@
|
||||
table! {
|
||||
api_tokens (id) {
|
||||
id -> Int4,
|
||||
creation_date -> Timestamp,
|
||||
value -> Text,
|
||||
scopes -> Text,
|
||||
app_id -> Int4,
|
||||
user_id -> Int4,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
apps (id) {
|
||||
id -> Int4,
|
||||
@@ -184,6 +195,8 @@ table! {
|
||||
}
|
||||
}
|
||||
|
||||
joinable!(api_tokens -> apps (app_id));
|
||||
joinable!(api_tokens -> users (user_id));
|
||||
joinable!(blog_authors -> blogs (blog_id));
|
||||
joinable!(blog_authors -> users (author_id));
|
||||
joinable!(blogs -> instances (instance_id));
|
||||
@@ -204,6 +217,7 @@ joinable!(tags -> posts (post_id));
|
||||
joinable!(users -> instances (instance_id));
|
||||
|
||||
allow_tables_to_appear_in_same_query!(
|
||||
api_tokens,
|
||||
apps,
|
||||
blog_authors,
|
||||
blogs,
|
||||
|
||||
Reference in New Issue
Block a user