Run 'cargo fmt' to format code (#489)
This commit is contained in:
committed by
Baptiste Gelez
parent
732f514da7
commit
b945d1f602
+1
-5
@@ -3,11 +3,7 @@ use rocket_contrib::json::Json;
|
||||
use serde_json;
|
||||
|
||||
use plume_api::apps::AppEndpoint;
|
||||
use plume_models::{
|
||||
Connection,
|
||||
db_conn::DbConn,
|
||||
apps::App,
|
||||
};
|
||||
use plume_models::{apps::App, db_conn::DbConn, Connection};
|
||||
|
||||
#[post("/apps", data = "<data>")]
|
||||
pub fn create(conn: DbConn, data: Json<AppEndpoint>) -> Json<serde_json::Value> {
|
||||
|
||||
+15
-12
@@ -1,10 +1,10 @@
|
||||
use plume_models::{self, api_tokens::ApiToken};
|
||||
use rocket::{
|
||||
Outcome,
|
||||
http::Status,
|
||||
request::{self, FromRequest, Request}
|
||||
request::{self, FromRequest, Request},
|
||||
Outcome,
|
||||
};
|
||||
use std::marker::PhantomData;
|
||||
use plume_models::{self, api_tokens::ApiToken};
|
||||
|
||||
// Actions
|
||||
pub trait Action {
|
||||
@@ -33,22 +33,25 @@ impl Scope for plume_models::posts::Post {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Authorization<A, S> (pub ApiToken, PhantomData<(A, S)>);
|
||||
pub struct Authorization<A, S>(pub ApiToken, PhantomData<(A, S)>);
|
||||
|
||||
impl<'a, 'r, A, S> FromRequest<'a, 'r> for Authorization<A, S>
|
||||
where A: Action,
|
||||
S: Scope
|
||||
where
|
||||
A: Action,
|
||||
S: Scope,
|
||||
{
|
||||
type Error = ();
|
||||
|
||||
fn from_request(request: &'a Request<'r>) -> request::Outcome<Authorization<A, S>, ()> {
|
||||
request.guard::<ApiToken>()
|
||||
request
|
||||
.guard::<ApiToken>()
|
||||
.map_failure(|_| (Status::Unauthorized, ()))
|
||||
.and_then(|token| if token.can(A::to_str(), S::to_str()) {
|
||||
Outcome::Success(Authorization(token, PhantomData))
|
||||
} else {
|
||||
Outcome::Failure((Status::Unauthorized, ()))
|
||||
.and_then(|token| {
|
||||
if token.can(A::to_str(), S::to_str()) {
|
||||
Outcome::Success(Authorization(token, PhantomData))
|
||||
} else {
|
||||
Outcome::Failure((Status::Unauthorized, ()))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+20
-17
@@ -1,16 +1,13 @@
|
||||
#![warn(clippy::too_many_arguments)]
|
||||
use rocket::{response::{self, Responder}, request::{Form, Request}};
|
||||
use rocket::{
|
||||
request::{Form, Request},
|
||||
response::{self, Responder},
|
||||
};
|
||||
use rocket_contrib::json::Json;
|
||||
use serde_json;
|
||||
|
||||
use plume_common::utils::random_hex;
|
||||
use plume_models::{
|
||||
Error,
|
||||
apps::App,
|
||||
api_tokens::*,
|
||||
db_conn::DbConn,
|
||||
users::User,
|
||||
};
|
||||
use plume_models::{api_tokens::*, apps::App, db_conn::DbConn, users::User, Error};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ApiError(Error);
|
||||
@@ -26,13 +23,16 @@ impl<'r> Responder<'r> for ApiError {
|
||||
match self.0 {
|
||||
Error::NotFound => Json(json!({
|
||||
"error": "Not found"
|
||||
})).respond_to(req),
|
||||
}))
|
||||
.respond_to(req),
|
||||
Error::Unauthorized => Json(json!({
|
||||
"error": "You are not authorized to access this resource"
|
||||
})).respond_to(req),
|
||||
}))
|
||||
.respond_to(req),
|
||||
_ => Json(json!({
|
||||
"error": "Server error"
|
||||
})).respond_to(req)
|
||||
}))
|
||||
.respond_to(req),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,12 +52,15 @@ pub fn oauth(query: Form<OAuthRequest>, conn: DbConn) -> Result<Json<serde_json:
|
||||
if app.client_secret == query.client_secret {
|
||||
if let Ok(user) = User::find_by_fqn(&*conn, &query.username) {
|
||||
if user.auth(&query.password) {
|
||||
let token = ApiToken::insert(&*conn, NewApiToken {
|
||||
app_id: app.id,
|
||||
user_id: user.id,
|
||||
value: random_hex(),
|
||||
scopes: query.scopes.clone(),
|
||||
})?;
|
||||
let token = ApiToken::insert(
|
||||
&*conn,
|
||||
NewApiToken {
|
||||
app_id: app.id,
|
||||
user_id: user.id,
|
||||
value: random_hex(),
|
||||
scopes: query.scopes.clone(),
|
||||
},
|
||||
)?;
|
||||
Ok(Json(json!({
|
||||
"token": token.value
|
||||
})))
|
||||
|
||||
+61
-25
@@ -5,43 +5,79 @@ use scheduled_thread_pool::ScheduledThreadPool;
|
||||
use serde_json;
|
||||
use serde_qs;
|
||||
|
||||
use api::authorization::*;
|
||||
use plume_api::posts::PostEndpoint;
|
||||
use plume_models::{
|
||||
Connection,
|
||||
db_conn::DbConn,
|
||||
posts::Post,
|
||||
search::Searcher as UnmanagedSearcher,
|
||||
db_conn::DbConn, posts::Post, search::Searcher as UnmanagedSearcher, Connection,
|
||||
};
|
||||
use api::authorization::*;
|
||||
use {Searcher, Worker};
|
||||
|
||||
#[get("/posts/<id>")]
|
||||
pub fn get(id: i32, conn: DbConn, worker: Worker, auth: Option<Authorization<Read, Post>>, search: Searcher) -> Json<serde_json::Value> {
|
||||
let post = <Post as Provider<(&Connection, &ScheduledThreadPool, &UnmanagedSearcher, Option<i32>)>>
|
||||
::get(&(&*conn, &worker, &search, auth.map(|a| a.0.user_id)), id).ok();
|
||||
pub fn get(
|
||||
id: i32,
|
||||
conn: DbConn,
|
||||
worker: Worker,
|
||||
auth: Option<Authorization<Read, Post>>,
|
||||
search: Searcher,
|
||||
) -> Json<serde_json::Value> {
|
||||
let post = <Post as Provider<(
|
||||
&Connection,
|
||||
&ScheduledThreadPool,
|
||||
&UnmanagedSearcher,
|
||||
Option<i32>,
|
||||
)>>::get(&(&*conn, &worker, &search, auth.map(|a| a.0.user_id)), id)
|
||||
.ok();
|
||||
Json(json!(post))
|
||||
}
|
||||
|
||||
#[get("/posts")]
|
||||
pub fn list(conn: DbConn, uri: &Origin, worker: Worker, auth: Option<Authorization<Read, Post>>, search: Searcher) -> Json<serde_json::Value> {
|
||||
let query: PostEndpoint = serde_qs::from_str(uri.query().unwrap_or("")).expect("api::list: invalid query error");
|
||||
let post = <Post as Provider<(&Connection, &ScheduledThreadPool, &UnmanagedSearcher, Option<i32>)>>
|
||||
::list(&(&*conn, &worker, &search, auth.map(|a| a.0.user_id)), query);
|
||||
pub fn list(
|
||||
conn: DbConn,
|
||||
uri: &Origin,
|
||||
worker: Worker,
|
||||
auth: Option<Authorization<Read, Post>>,
|
||||
search: Searcher,
|
||||
) -> Json<serde_json::Value> {
|
||||
let query: PostEndpoint =
|
||||
serde_qs::from_str(uri.query().unwrap_or("")).expect("api::list: invalid query error");
|
||||
let post = <Post as Provider<(
|
||||
&Connection,
|
||||
&ScheduledThreadPool,
|
||||
&UnmanagedSearcher,
|
||||
Option<i32>,
|
||||
)>>::list(
|
||||
&(&*conn, &worker, &search, auth.map(|a| a.0.user_id)),
|
||||
query,
|
||||
);
|
||||
Json(json!(post))
|
||||
}
|
||||
|
||||
#[post("/posts", data = "<payload>")]
|
||||
pub fn create(conn: DbConn, payload: Json<PostEndpoint>, worker: Worker, auth: Authorization<Write, Post>, search: Searcher) -> Json<serde_json::Value> {
|
||||
let new_post = <Post as Provider<(&Connection, &ScheduledThreadPool, &UnmanagedSearcher, Option<i32>)>>
|
||||
::create(&(&*conn, &worker, &search, Some(auth.0.user_id)), (*payload).clone());
|
||||
Json(new_post.map(|p| json!(p)).unwrap_or_else(|e| json!({
|
||||
"error": "Invalid data, couldn't create new post",
|
||||
"details": match e {
|
||||
ApiError::Fetch(msg) => msg,
|
||||
ApiError::SerDe(msg) => msg,
|
||||
ApiError::NotFound(msg) => msg,
|
||||
ApiError::Authorization(msg) => msg,
|
||||
}
|
||||
})))
|
||||
pub fn create(
|
||||
conn: DbConn,
|
||||
payload: Json<PostEndpoint>,
|
||||
worker: Worker,
|
||||
auth: Authorization<Write, Post>,
|
||||
search: Searcher,
|
||||
) -> Json<serde_json::Value> {
|
||||
let new_post = <Post as Provider<(
|
||||
&Connection,
|
||||
&ScheduledThreadPool,
|
||||
&UnmanagedSearcher,
|
||||
Option<i32>,
|
||||
)>>::create(
|
||||
&(&*conn, &worker, &search, Some(auth.0.user_id)),
|
||||
(*payload).clone(),
|
||||
);
|
||||
Json(new_post.map(|p| json!(p)).unwrap_or_else(|e| {
|
||||
json!({
|
||||
"error": "Invalid data, couldn't create new post",
|
||||
"details": match e {
|
||||
ApiError::Fetch(msg) => msg,
|
||||
ApiError::SerDe(msg) => msg,
|
||||
ApiError::NotFound(msg) => msg,
|
||||
ApiError::Authorization(msg) => msg,
|
||||
}
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user