mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-02-23 04:52:28 +00:00
move security auth openapi stuff into the path
This commit is contained in:
parent
b4a7b4d810
commit
640bb8263c
3 changed files with 48 additions and 21 deletions
|
@ -89,9 +89,13 @@ fn delete(_state : &mut State, id : u64)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rest_read_all(Auth)]
|
#[rest_read_all(Auth)]
|
||||||
fn auth_read_all(auth : AuthStatus<()>) -> Success<String>
|
fn auth_read_all(auth : AuthStatus<()>) -> AuthResult<Success<String>>
|
||||||
{
|
{
|
||||||
format!("{:?}", auth).into()
|
let str : Success<String> = match auth {
|
||||||
|
AuthStatus::Authenticated(data) => format!("{:?}", data).into(),
|
||||||
|
_ => return AuthErr
|
||||||
|
};
|
||||||
|
str.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
const ADDR : &str = "127.0.0.1:18080";
|
const ADDR : &str = "127.0.0.1:18080";
|
||||||
|
|
|
@ -23,7 +23,7 @@ use mime::{Mime, APPLICATION_JSON, TEXT_PLAIN};
|
||||||
use openapiv3::{
|
use openapiv3::{
|
||||||
APIKeyLocation, Components, MediaType, OpenAPI, Operation, Parameter, ParameterData, ParameterSchemaOrContent, PathItem,
|
APIKeyLocation, Components, MediaType, OpenAPI, Operation, Parameter, ParameterData, ParameterSchemaOrContent, PathItem,
|
||||||
Paths, ReferenceOr, ReferenceOr::Item, ReferenceOr::Reference, RequestBody as OARequestBody, Response, Responses, Schema,
|
Paths, ReferenceOr, ReferenceOr::Item, ReferenceOr::Reference, RequestBody as OARequestBody, Response, Responses, Schema,
|
||||||
SchemaKind, SecurityRequirement, SecurityScheme, Server, StatusCode, Type
|
SchemaKind, SecurityScheme, Server, StatusCode, Type
|
||||||
};
|
};
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use std::panic::RefUnwindSafe;
|
use std::panic::RefUnwindSafe;
|
||||||
|
@ -149,7 +149,7 @@ impl NewHandler for OpenapiHandler
|
||||||
const SECURITY_NAME : &'static str = "authToken";
|
const SECURITY_NAME : &'static str = "authToken";
|
||||||
|
|
||||||
#[cfg(feature = "auth")]
|
#[cfg(feature = "auth")]
|
||||||
fn get_security(state : &mut State) -> (Vec<SecurityRequirement>, IndexMap<String, ReferenceOr<SecurityScheme>>)
|
fn get_security(state : &mut State) -> IndexMap<String, ReferenceOr<SecurityScheme>>
|
||||||
{
|
{
|
||||||
use crate::AuthSource;
|
use crate::AuthSource;
|
||||||
use gotham::state::FromState;
|
use gotham::state::FromState;
|
||||||
|
@ -159,10 +159,6 @@ fn get_security(state : &mut State) -> (Vec<SecurityRequirement>, IndexMap<Strin
|
||||||
None => return Default::default()
|
None => return Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut security : IndexMap<String, Vec<String>> = Default::default();
|
|
||||||
security.insert(SECURITY_NAME.to_owned(), Vec::new());
|
|
||||||
let security = vec![security];
|
|
||||||
|
|
||||||
let security_scheme = match source {
|
let security_scheme = match source {
|
||||||
AuthSource::Cookie(name) => SecurityScheme::APIKey {
|
AuthSource::Cookie(name) => SecurityScheme::APIKey {
|
||||||
location: APIKeyLocation::Cookie,
|
location: APIKeyLocation::Cookie,
|
||||||
|
@ -181,7 +177,7 @@ fn get_security(state : &mut State) -> (Vec<SecurityRequirement>, IndexMap<Strin
|
||||||
let mut security_schemes : IndexMap<String, ReferenceOr<SecurityScheme>> = Default::default();
|
let mut security_schemes : IndexMap<String, ReferenceOr<SecurityScheme>> = Default::default();
|
||||||
security_schemes.insert(SECURITY_NAME.to_owned(), ReferenceOr::Item(security_scheme));
|
security_schemes.insert(SECURITY_NAME.to_owned(), ReferenceOr::Item(security_scheme));
|
||||||
|
|
||||||
(security, security_schemes)
|
security_schemes
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "auth"))]
|
#[cfg(not(feature = "auth"))]
|
||||||
|
@ -195,8 +191,7 @@ impl Handler for OpenapiHandler
|
||||||
fn handle(self, mut state : State) -> Box<HandlerFuture>
|
fn handle(self, mut state : State) -> Box<HandlerFuture>
|
||||||
{
|
{
|
||||||
let mut openapi = self.0;
|
let mut openapi = self.0;
|
||||||
let (security, security_schemes) = get_security(&mut state);
|
let security_schemes = get_security(&mut state);
|
||||||
openapi.security = security;
|
|
||||||
let mut components = openapi.components.unwrap_or_default();
|
let mut components = openapi.components.unwrap_or_default();
|
||||||
components.security_schemes = security_schemes;
|
components.security_schemes = security_schemes;
|
||||||
openapi.components = Some(components);
|
openapi.components = Some(components);
|
||||||
|
@ -318,7 +313,15 @@ impl<'a> OperationParams<'a>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_operation(default_status : hyper::StatusCode, accepted_types : Option<Vec<Mime>>, schema : ReferenceOr<Schema>, params : OperationParams, body_schema : Option<ReferenceOr<Schema>>, supported_types : Option<Vec<Mime>>) -> Operation
|
fn new_operation(
|
||||||
|
default_status : hyper::StatusCode,
|
||||||
|
accepted_types : Option<Vec<Mime>>,
|
||||||
|
schema : ReferenceOr<Schema>,
|
||||||
|
params : OperationParams,
|
||||||
|
body_schema : Option<ReferenceOr<Schema>>,
|
||||||
|
supported_types : Option<Vec<Mime>>,
|
||||||
|
requires_auth : bool
|
||||||
|
) -> Operation
|
||||||
{
|
{
|
||||||
let content = schema_to_content(accepted_types.unwrap_or_default(), schema);
|
let content = schema_to_content(accepted_types.unwrap_or_default(), schema);
|
||||||
|
|
||||||
|
@ -336,6 +339,14 @@ fn new_operation(default_status : hyper::StatusCode, accepted_types : Option<Vec
|
||||||
required: true
|
required: true
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
let mut security = Vec::new();
|
||||||
|
if requires_auth
|
||||||
|
{
|
||||||
|
let mut sec = IndexMap::new();
|
||||||
|
sec.insert(SECURITY_NAME.to_owned(), Vec::new());
|
||||||
|
security.push(sec);
|
||||||
|
}
|
||||||
|
|
||||||
Operation {
|
Operation {
|
||||||
tags: Vec::new(),
|
tags: Vec::new(),
|
||||||
summary: None,
|
summary: None,
|
||||||
|
@ -349,7 +360,7 @@ fn new_operation(default_status : hyper::StatusCode, accepted_types : Option<Vec
|
||||||
responses
|
responses
|
||||||
},
|
},
|
||||||
deprecated: false,
|
deprecated: false,
|
||||||
security: Vec::new(),
|
security,
|
||||||
servers: Vec::new()
|
servers: Vec::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -393,7 +404,7 @@ macro_rules! implOpenapiRouter {
|
||||||
|
|
||||||
let path = format!("/{}", &self.1);
|
let path = format!("/{}", &self.1);
|
||||||
let mut item = (self.0).1.remove_path(&path);
|
let mut item = (self.0).1.remove_path(&path);
|
||||||
item.get = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::default(), None, None));
|
item.get = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::default(), None, None, Res::requires_auth()));
|
||||||
(self.0).1.add_path(path, item);
|
(self.0).1.add_path(path, item);
|
||||||
|
|
||||||
(&mut *(self.0).0, self.1.to_string()).read_all::<Handler, Res>()
|
(&mut *(self.0).0, self.1.to_string()).read_all::<Handler, Res>()
|
||||||
|
@ -409,7 +420,7 @@ macro_rules! implOpenapiRouter {
|
||||||
|
|
||||||
let path = format!("/{}/{{id}}", &self.1);
|
let path = format!("/{}/{{id}}", &self.1);
|
||||||
let mut item = (self.0).1.remove_path(&path);
|
let mut item = (self.0).1.remove_path(&path);
|
||||||
item.get = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), None, None));
|
item.get = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), None, None, Res::requires_auth()));
|
||||||
(self.0).1.add_path(path, item);
|
(self.0).1.add_path(path, item);
|
||||||
|
|
||||||
(&mut *(self.0).0, self.1.to_string()).read::<Handler, ID, Res>()
|
(&mut *(self.0).0, self.1.to_string()).read::<Handler, ID, Res>()
|
||||||
|
@ -425,7 +436,7 @@ macro_rules! implOpenapiRouter {
|
||||||
|
|
||||||
let path = format!("/{}/search", &self.1);
|
let path = format!("/{}/search", &self.1);
|
||||||
let mut item = (self.0).1.remove_path(&self.1);
|
let mut item = (self.0).1.remove_path(&self.1);
|
||||||
item.get = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::from_query_params(Query::schema()), None, None));
|
item.get = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::from_query_params(Query::schema()), None, None, Res::requires_auth()));
|
||||||
(self.0).1.add_path(path, item);
|
(self.0).1.add_path(path, item);
|
||||||
|
|
||||||
(&mut *(self.0).0, self.1.to_string()).search::<Handler, Query, Res>()
|
(&mut *(self.0).0, self.1.to_string()).search::<Handler, Query, Res>()
|
||||||
|
@ -442,7 +453,7 @@ macro_rules! implOpenapiRouter {
|
||||||
|
|
||||||
let path = format!("/{}", &self.1);
|
let path = format!("/{}", &self.1);
|
||||||
let mut item = (self.0).1.remove_path(&path);
|
let mut item = (self.0).1.remove_path(&path);
|
||||||
item.post = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::default(), Some(body_schema), Body::supported_types()));
|
item.post = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::default(), Some(body_schema), Body::supported_types(), Res::requires_auth()));
|
||||||
(self.0).1.add_path(path, item);
|
(self.0).1.add_path(path, item);
|
||||||
|
|
||||||
(&mut *(self.0).0, self.1.to_string()).create::<Handler, Body, Res>()
|
(&mut *(self.0).0, self.1.to_string()).create::<Handler, Body, Res>()
|
||||||
|
@ -459,7 +470,7 @@ macro_rules! implOpenapiRouter {
|
||||||
|
|
||||||
let path = format!("/{}", &self.1);
|
let path = format!("/{}", &self.1);
|
||||||
let mut item = (self.0).1.remove_path(&path);
|
let mut item = (self.0).1.remove_path(&path);
|
||||||
item.put = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::default(), Some(body_schema), Body::supported_types()));
|
item.put = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::default(), Some(body_schema), Body::supported_types(), Res::requires_auth()));
|
||||||
(self.0).1.add_path(path, item);
|
(self.0).1.add_path(path, item);
|
||||||
|
|
||||||
(&mut *(self.0).0, self.1.to_string()).update_all::<Handler, Body, Res>()
|
(&mut *(self.0).0, self.1.to_string()).update_all::<Handler, Body, Res>()
|
||||||
|
@ -477,7 +488,7 @@ macro_rules! implOpenapiRouter {
|
||||||
|
|
||||||
let path = format!("/{}/{{id}}", &self.1);
|
let path = format!("/{}/{{id}}", &self.1);
|
||||||
let mut item = (self.0).1.remove_path(&path);
|
let mut item = (self.0).1.remove_path(&path);
|
||||||
item.put = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), Some(body_schema), Body::supported_types()));
|
item.put = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), Some(body_schema), Body::supported_types(), Res::requires_auth()));
|
||||||
(self.0).1.add_path(path, item);
|
(self.0).1.add_path(path, item);
|
||||||
|
|
||||||
(&mut *(self.0).0, self.1.to_string()).update::<Handler, ID, Body, Res>()
|
(&mut *(self.0).0, self.1.to_string()).update::<Handler, ID, Body, Res>()
|
||||||
|
@ -492,7 +503,7 @@ macro_rules! implOpenapiRouter {
|
||||||
|
|
||||||
let path = format!("/{}", &self.1);
|
let path = format!("/{}", &self.1);
|
||||||
let mut item = (self.0).1.remove_path(&path);
|
let mut item = (self.0).1.remove_path(&path);
|
||||||
item.delete = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::default(), None, None));
|
item.delete = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::default(), None, None, Res::requires_auth()));
|
||||||
(self.0).1.add_path(path, item);
|
(self.0).1.add_path(path, item);
|
||||||
|
|
||||||
(&mut *(self.0).0, self.1.to_string()).delete_all::<Handler, Res>()
|
(&mut *(self.0).0, self.1.to_string()).delete_all::<Handler, Res>()
|
||||||
|
@ -508,7 +519,7 @@ macro_rules! implOpenapiRouter {
|
||||||
|
|
||||||
let path = format!("/{}/{{id}}", &self.1);
|
let path = format!("/{}/{{id}}", &self.1);
|
||||||
let mut item = (self.0).1.remove_path(&path);
|
let mut item = (self.0).1.remove_path(&path);
|
||||||
item.delete = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), None, None));
|
item.delete = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), None, None, Res::requires_auth()));
|
||||||
(self.0).1.add_path(path, item);
|
(self.0).1.add_path(path, item);
|
||||||
|
|
||||||
(&mut *(self.0).0, self.1.to_string()).delete::<Handler, ID, Res>()
|
(&mut *(self.0).0, self.1.to_string()).delete::<Handler, ID, Res>()
|
||||||
|
|
|
@ -93,6 +93,12 @@ pub trait ResourceResult
|
||||||
{
|
{
|
||||||
StatusCode::OK
|
StatusCode::OK
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "openapi")]
|
||||||
|
fn requires_auth() -> bool
|
||||||
|
{
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "openapi")]
|
#[cfg(feature = "openapi")]
|
||||||
|
@ -326,6 +332,12 @@ impl<T : ResourceResult> ResourceResult for AuthResult<T>
|
||||||
{
|
{
|
||||||
T::default_status()
|
T::default_status()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "openapi")]
|
||||||
|
fn requires_auth() -> bool
|
||||||
|
{
|
||||||
|
true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue