Skip to content
This repository has been archived by the owner on Apr 18, 2021. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
joxcat committed Jul 16, 2019
0 parents commit 1c169c0
Show file tree
Hide file tree
Showing 26 changed files with 1,050 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Cargo
# will have compiled files and executables
/target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

/.idea
/.vscode

/keys
user_db

**/*.exe
**/*.zip
**/*.pub
**/*.prv
8 changes: 8 additions & 0 deletions .whitesource
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"generalSettings": {
"shouldScanRepo": true
},
"checkRunSettings": {
"vulnerableCheckRunConclusionLevel": "failure"
}
}
20 changes: 20 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "hume-endpoints"
version = "0.1.0"
authors = ["Arthur Hugon <dev@planchon.xyz>"]
edition = "2018"

[dependencies]
actix-web = "1.0.2"
actix-web-codegen = "0.1"
log = "0.4"
badlog = "1.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
base64 = "0.10"
simple-error = "0.2"
futures = "0.1"
http = "0.1"
arthur-hugon-tools = { git = "https://github.com/joxcat/arthur-hugon-tools.git", features = ["security","urlenc","passwords","simple-db"] }
chrono = "0.4"
lazy_static = "1.3"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Planchon

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hume-endpoints
15 changes: 15 additions & 0 deletions src/library/defaults/consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#![allow(dead_code)]
pub const ADDR: &str = "127.0.0.1:3001";
pub const LOG_LEVEL: &str = "INFO";
#[cfg(target_os = "windows")]
pub const EXECUTABLE_PATH: &str = "\\bin";
#[cfg(not(target_os = "windows"))]
pub const EXECUTABLE_PATH: &str = "/bin";
#[cfg(target_os = "windows")]
pub const CERTIFICATOR: &str = "\\gsuite-cert.exe";
#[cfg(not(target_os = "windows"))]
pub const CERTIFICATOR: &str = "/gsuite-cert";
#[cfg(target_os = "windows")]
pub const NEXT: &str = "\\nextcloud-api-rs.exe";
#[cfg(not(target_os = "windows"))]
pub const NEXT: &str = "/nextcloud-api-rs";
13 changes: 13 additions & 0 deletions src/library/defaults/dynamics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![allow(dead_code)]

pub type ErrorChainResult<T> = Result<T, Box<dyn std::error::Error>>;

type CustomErrorResult = super::structs::ResponseTypes;
pub fn custom_404(reason: &str) -> super::structs::Response<CustomErrorResult> {
super::structs::Response {
status_code: 400,
err_short: Some("Bad Request"),
err_long: Some(reason),
value: None as super::structs::GenericResponse<CustomErrorResult>,
}
}
10 changes: 10 additions & 0 deletions src/library/defaults/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pub use super::structs;

mod consts;
pub use consts::*;

mod dynamics;
pub use dynamics::*;

mod prefab;
pub use prefab::*;
48 changes: 48 additions & 0 deletions src/library/defaults/prefab.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#![allow(dead_code)]
pub const ERROR_400: super::structs::Response<super::structs::ResponseTypes> =
super::structs::Response {
status_code: 400,
err_short: Some("Bad Request"),
err_long: Some("Bad parameters in the request!"),
value: None as super::structs::GenericResponse<super::structs::ResponseTypes>,
};

pub const ERROR_401: super::structs::Response<super::structs::ResponseTypes> =
super::structs::Response {
status_code: 401,
err_short: Some("Unauthorized"),
err_long: Some("Please authenticate yourself correctly"),
value: None as super::structs::GenericResponse<super::structs::ResponseTypes>,
};

pub const ERROR_403: super::structs::Response<super::structs::ResponseTypes> =
super::structs::Response {
status_code: 403,
err_short: Some("Forbidden"),
err_long: Some("Please make another request"),
value: None as super::structs::GenericResponse<super::structs::ResponseTypes>,
};

pub const ERROR_404: super::structs::Response<super::structs::ResponseTypes> =
super::structs::Response {
status_code: 404,
err_short: Some("Not Found"),
err_long: Some("Route not found!"),
value: None as super::structs::GenericResponse<super::structs::ResponseTypes>,
};

pub const ERROR_405: super::structs::Response<super::structs::ResponseTypes> =
super::structs::Response {
status_code: 405,
err_short: Some("Method Not Allowed"),
err_long: Some("This method is not allowed here!"),
value: None as super::structs::GenericResponse<super::structs::ResponseTypes>,
};

pub const ERROR_500: super::structs::Response<super::structs::ResponseTypes> =
super::structs::Response {
status_code: 500,
err_short: Some("Internal Server Error"),
err_long: Some("Internal server error please try again soon!"),
value: None as super::structs::GenericResponse<super::structs::ResponseTypes>,
};
42 changes: 42 additions & 0 deletions src/library/methods.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::process::Command;

pub fn execute(app: &str, args: &[&str]) -> Result<String, Box<dyn std::error::Error>> {
Ok(String::from_utf8(
Command::new(format!(
"{}{}{}",
std::env::current_exe()?
.parent()
.ok_or("Folder not found")?
.to_str()
.ok_or("Folder not found")?,
super::defaults::EXECUTABLE_PATH,
app
))
.args(args)
.output()?
.stdout,
)?)
}

pub fn execute_with_envs(
app: &str,
args: &[&str],
envs: Vec<(&str, &str)>,
) -> Result<String, Box<dyn std::error::Error>> {
Ok(String::from_utf8(
Command::new(format!(
"{}{}{}",
std::env::current_exe()?
.parent()
.ok_or("Folder not found")?
.to_str()
.ok_or("Folder not found")?,
super::defaults::EXECUTABLE_PATH,
app
))
.args(args)
.envs(envs)
.output()?
.stdout,
)?)
}
13 changes: 13 additions & 0 deletions src/library/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mod structures;
pub mod structs {
pub use super::structures::*;
}

mod methods;
pub use methods::*;

pub mod defaults;

pub mod routes;

pub mod security;
40 changes: 40 additions & 0 deletions src/library/routes/delete_nextcloud_user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use super::{
defaults, execute,
structs::{ExternalResponse, IdOnly, Response, ResponseTypes},
};
use crate::library::security::{get_token, validate_token};
use actix_web::web::Json;
use actix_web::{HttpRequest, HttpResponse};
use actix_web_codegen::delete;

/**
* @todo Create the method in nextcloud-rust-api
*/
#[delete("/nextcloud/user")]
pub fn sync_delete_nextcloud_user(
req: HttpRequest,
params: Json<IdOnly>,
) -> actix_web::Result<HttpResponse> {
match get_token(req.headers()) {
Ok(x) => {
if validate_token(x.as_str()).unwrap() {
let res = |id: String| -> Result<String, Box<dyn std::error::Error>> {
Ok(execute(defaults::NEXT, &["delete", "-i", id.as_str()])?)
};

Ok(match res(params.clone().id) {
Ok(x) => {
let x = ss!(ExternalResponse<bool>, x.as_str());
let x = Response::ok(200, ResponseTypes::Boolean(x.value.unwrap()));
response!(Ok, x)
}
Err(e) => response!(BadRequest, e.to_string()),
_ => response!(InternalServerError, defaults::ERROR_500),
})
} else {
Ok(response!(Unauthorized, defaults::ERROR_401))
}
}
Err(e) => Ok(response!(BadRequest, defaults::ERROR_400)),
}
}
45 changes: 45 additions & 0 deletions src/library/routes/delete_session.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use super::{
defaults,
structs::{Response, ResponseTypes},
};
use crate::library::security::{disconnect, get_token, mail_from_token, validate_token};
use actix_web::{HttpRequest, HttpResponse};
use actix_web_codegen::delete;

/**
* @todo Generate SA Certs
*/
#[delete("/session")]
pub fn sync_post_logout(req: HttpRequest) -> actix_web::Result<HttpResponse> {
Ok(match get_token(req.headers()) {
Ok(x) => match validate_token(x.clone().as_str()) {
Ok(v) => {
if v {
match mail_from_token(x.clone()) {
Ok(x) => match disconnect(x.as_str()) {
Ok(x) => {
if x {
response!(
Ok,
Response::ok(
200,
ResponseTypes::PlainText("disconnecting".to_owned())
)
)
} else {
response!(InternalServerError, defaults::ERROR_403)
}
}
Err(_) => response!(InternalServerError, defaults::ERROR_500),
},
Err(_) => response!(InternalServerError, defaults::ERROR_500),
}
} else {
response!(Unauthorized, defaults::ERROR_401)
}
}
Err(_) => response!(Unauthorized, defaults::ERROR_401),
},
Err(_) => response!(BadRequest, defaults::ERROR_400),
})
}
61 changes: 61 additions & 0 deletions src/library/routes/get_nextcloud_cookies.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use super::{
defaults, execute,
structs::{ExternalResponse, NextCloudCoke, NextCloudUser, Response, ResponseTypes},
};
use crate::library::security::{get_token, validate_token};
use actix_web::web::Json;
use actix_web::{HttpRequest, HttpResponse};
use actix_web_codegen::get;

#[get("/nextcloud/cookies")]
pub fn sync_get_nextcloud_cookies(
req: HttpRequest,
params: Json<NextCloudUser>,
) -> actix_web::Result<HttpResponse> {
match get_token(req.headers()) {
Ok(x) => {
if validate_token(x.as_str()).unwrap() {
let res = |(u, p): (String, String),
r: Option<String>|
-> Result<String, Box<dyn std::error::Error>> {
Ok(match r {
Some(x) => execute(
defaults::NEXT,
&[
"cookies",
"-u",
u.as_str(),
"-p",
p.as_str(),
"-e",
x.as_str(),
],
)?,
None => execute(
defaults::NEXT,
&["cookies", "-u", u.as_str(), "-p", p.as_str()],
)?,
})
};

Ok(
match res(
(params.clone().username, params.clone().password),
params.clone().response_type,
) {
Ok(x) => {
let x = ss!(ExternalResponse<NextCloudCoke>, x.as_str());
let x = Response::ok(200, ResponseTypes::Cookie(x.value.unwrap()));
response!(Ok, x)
}
Err(e) => response!(BadRequest, e.to_string()),
_ => response!(InternalServerError, defaults::ERROR_500),
},
)
} else {
Ok(response!(Unauthorized, defaults::ERROR_401))
}
}
Err(e) => Ok(response!(BadRequest, defaults::ERROR_400)),
}
}
10 changes: 10 additions & 0 deletions src/library/routes/get_sa.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use actix_web::HttpResponse;
use actix_web_codegen::get;

/**
* @todo Create the method in daemon-hume
*/
#[get("/sa")]
pub fn sync_get_sa() -> actix_web::Result<HttpResponse> {
Ok(HttpResponse::Ok().finish())
}

0 comments on commit 1c169c0

Please sign in to comment.