Ver código fonte

Require unchecked token on ls, info and dl endpoints

mmeisson 6 anos atrás
pai
commit
cf35233343
1 arquivos alterados com 33 adições e 5 exclusões
  1. 33 5
      src/main.rs

+ 33 - 5
src/main.rs

@@ -7,7 +7,10 @@ extern crate rocket_contrib;
 extern crate serde_derive;
 extern crate serde_derive;
 
 
 use std::fs;
 use std::fs;
+use rocket::Outcome;
+use rocket::http::Status;
 use rocket::response::NamedFile;
 use rocket::response::NamedFile;
+use rocket::request::{self, Request, FromRequest};
 
 
 use rocket_contrib::json::Json;
 use rocket_contrib::json::Json;
 use serde::Serialize;
 use serde::Serialize;
@@ -67,8 +70,8 @@ fn login_session(payload: Json<LoginSessionPayload>)
     Json(FbxCmdResult {
     Json(FbxCmdResult {
         success: true,
         success: true,
         result: LoginSessionResult {
         result: LoginSessionResult {
-            session_token: "".into(),
-            challenge: "".into(),
+            session_token: "SomeSessionToken".into(),
+            challenge: "SomeChallenge".into(),
             permissions: [("explorer".into(), true)].iter().cloned().collect(),
             permissions: [("explorer".into(), true)].iter().cloned().collect(),
         }})
         }})
 }
 }
@@ -90,8 +93,33 @@ struct FbxFileInfo
     filecount: i32,
     filecount: i32,
 }
 }
 
 
+struct AuthToken(String);
+
+#[derive(Debug)]
+enum AuthTokenError {
+    NoAuthentication,
+    BadAuthentication,
+}
+
+impl<'a, 'r> FromRequest<'a, 'r> for AuthToken {
+    type Error = AuthTokenError;
+
+    fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
+        let keys: Vec<_> = request.headers().get("X-Fbx-App-Auth").collect();
+
+        match keys.len() {
+            0 => Outcome::Failure((Status::Unauthorized, AuthTokenError::NoAuthentication)),
+            1 => {
+                println!("Coucou :: {}", keys[0]);
+                Outcome::Success(AuthToken(keys[0].to_string()))
+            },
+            _ => Outcome::Failure((Status::BadRequest, AuthTokenError::BadAuthentication)),
+        }
+    }
+}
+
 #[get("/api/v6/fs/info/<file_path>")]
 #[get("/api/v6/fs/info/<file_path>")]
-fn fs_info(file_path: String)
+fn fs_info(_token: AuthToken, file_path: String)
     -> Json<FbxCmdResult<FbxFileInfo>>
     -> Json<FbxCmdResult<FbxFileInfo>>
 {
 {
     let name: String = match file_path.as_ref() {
     let name: String = match file_path.as_ref() {
@@ -134,7 +162,7 @@ fn fs_info(file_path: String)
 }
 }
 
 
 #[get("/api/v6/fs/ls/<file_path>")]
 #[get("/api/v6/fs/ls/<file_path>")]
-fn fs_ls(file_path: String)
+fn fs_ls(_token: AuthToken, file_path: String)
     -> Json<FbxCmdResult<[FbxFileInfo; 4]>>
     -> Json<FbxCmdResult<[FbxFileInfo; 4]>>
 {
 {
     if file_path == "L2Rpcg==" {
     if file_path == "L2Rpcg==" {
@@ -270,7 +298,7 @@ fn fs_ls(file_path: String)
 }
 }
 
 
 #[get("/api/v6/dl/<file_path>")]
 #[get("/api/v6/dl/<file_path>")]
-fn fs_dl(file_path: String) -> NamedFile
+fn fs_dl(_token: AuthToken, file_path: String) -> NamedFile
 {
 {
     println!("{}", file_path);
     println!("{}", file_path);