Compare commits
3
Commits
247cfb0476
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4fb8d0cb5c | ||
|
|
9a0cdec646 | ||
|
|
818dadb84f |
No files matched your search
@@ -193,17 +193,17 @@ If the API key is insecure, a warning will be outputted along with a generated A
|
|||||||
|
|
||||||
To add a link:
|
To add a link:
|
||||||
``` bash
|
``` bash
|
||||||
curl -X POST -H "Chhoto-Api-Key: <YOUR_API_KEY>" -d '{"shortlink":"<shortlink>", "longlink":"<longlink>"}' http://localhost:4567/api/new
|
curl -X POST -H "X-API-Key: <YOUR_API_KEY>" -d '{"shortlink":"<shortlink>", "longlink":"<longlink>"}' http://localhost:4567/api/new
|
||||||
```
|
```
|
||||||
|
|
||||||
To get a list of all the currently available links:
|
To get a list of all the currently available links:
|
||||||
``` bash
|
``` bash
|
||||||
curl -H "Chhoto-Api-Key: <YOUR_API_KEY>" http://localhost:4567/api/all
|
curl -H "X-API-Key: <YOUR_API_KEY>" http://localhost:4567/api/all
|
||||||
```
|
```
|
||||||
|
|
||||||
To delete a link:
|
To delete a link:
|
||||||
``` bash
|
``` bash
|
||||||
curl -X DELETE -H "Chhoto-Api-Key: <YOUR_API_KEY>" http://localhost:4567/api/del/<shortlink>
|
curl -X DELETE -H "X-API-Key: <YOUR_API_KEY>" http://localhost:4567/api/del/<shortlink>
|
||||||
```
|
```
|
||||||
Where `<shortlink>` is name of the shortened link you would like to delete. For example, if the shortened link is `http://localhost:4567/example`, `<shortlink>` would be `example`.
|
Where `<shortlink>` is name of the shortened link you would like to delete. For example, if the shortened link is `http://localhost:4567/example`, `<shortlink>` would be `example`.
|
||||||
|
|
||||||
|
|||||||
+3
-3
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
use actix_session::Session;
|
use actix_session::Session;
|
||||||
use std::{env, time::SystemTime};
|
use std::{env, time::SystemTime};
|
||||||
use actix_web::{HttpRequest};
|
use actix_web::HttpRequest;
|
||||||
|
|
||||||
// API key generation and scoring
|
// API key generation and scoring
|
||||||
use passwords::{PasswordGenerator, scorer, analyzer};
|
use passwords::{PasswordGenerator, scorer, analyzer};
|
||||||
@@ -42,13 +42,13 @@ pub fn gen_key() -> String {
|
|||||||
|
|
||||||
// Check if the API key header exists
|
// Check if the API key header exists
|
||||||
pub fn api_header(req: &HttpRequest) -> Option<&str> {
|
pub fn api_header(req: &HttpRequest) -> Option<&str> {
|
||||||
req.headers().get("Chhoto-Api-Key")?.to_str().ok()
|
req.headers().get("X-API-Key")?.to_str().ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine whether the inputted API key is sufficiently secure
|
// Determine whether the inputted API key is sufficiently secure
|
||||||
pub fn is_key_secure() -> bool {
|
pub fn is_key_secure() -> bool {
|
||||||
let score = scorer::score(&analyzer::analyze(env::var("api_key").unwrap()));
|
let score = scorer::score(&analyzer::analyze(env::var("api_key").unwrap()));
|
||||||
if score < 90.0 { false } else { true }
|
score >= 90.0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate a given password
|
// Validate a given password
|
||||||
|
|||||||
@@ -30,7 +30,6 @@ async fn main() -> Result<()> {
|
|||||||
.filter(|s| !s.trim().is_empty())
|
.filter(|s| !s.trim().is_empty())
|
||||||
.unwrap_or(String::from("urls.sqlite"));
|
.unwrap_or(String::from("urls.sqlite"));
|
||||||
|
|
||||||
|
|
||||||
let port = env::var("port")
|
let port = env::var("port")
|
||||||
.unwrap_or(String::from("4567"))
|
.unwrap_or(String::from("4567"))
|
||||||
.parse::<u16>()
|
.parse::<u16>()
|
||||||
|
|||||||
@@ -158,11 +158,8 @@ pub async fn link_handler(
|
|||||||
// Handle login
|
// Handle login
|
||||||
#[post("/api/login")]
|
#[post("/api/login")]
|
||||||
pub async fn login(req: String, session: Session) -> HttpResponse {
|
pub async fn login(req: String, session: Session) -> HttpResponse {
|
||||||
// Someone's API may be listening for the plain HTML body response of "Correct password!"
|
// Keep this function backwards compatible
|
||||||
// rather than a 200 OK HTTP response. Because of that, a check is performed to see whether
|
if env::var("api_key").is_ok() {
|
||||||
// the api_key environment variable is set. If it is set, then it is assumed the user will expect a JSON response for all API routes.
|
|
||||||
// *If this is not a concern, this can be removed.*
|
|
||||||
if let Ok(_) = env::var("api_key") {
|
|
||||||
if let Ok(password) = env::var("password") {
|
if let Ok(password) = env::var("password") {
|
||||||
if password != req {
|
if password != req {
|
||||||
eprintln!("Failed login attempt!");
|
eprintln!("Failed login attempt!");
|
||||||
|
|||||||
+6
-11
@@ -18,7 +18,6 @@ struct URLPair {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Define JSON struct for response
|
// Define JSON struct for response
|
||||||
// Named "ReturnResponse" rather than "Response" because of the previous import.
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
pub struct Response {
|
pub struct Response {
|
||||||
pub(crate) success: bool,
|
pub(crate) success: bool,
|
||||||
@@ -35,26 +34,22 @@ pub fn is_api_ok(http: HttpRequest) -> Response {
|
|||||||
if let Some(header) = auth::api_header(&http) {
|
if let Some(header) = auth::api_header(&http) {
|
||||||
// If the header is correct
|
// If the header is correct
|
||||||
if auth::validate_key(header.to_string()) {
|
if auth::validate_key(header.to_string()) {
|
||||||
let result = Response { success: true, error: false, reason: "".to_string(), pass: false };
|
Response { success: true, error: false, reason: "Correct API key".to_string(), pass: false }
|
||||||
result
|
|
||||||
} else {
|
} else {
|
||||||
let result = Response { success: false, error: true, reason: "Incorrect API key".to_string(), pass: false };
|
Response { success: false, error: true, reason: "Incorrect API key".to_string(), pass: false }
|
||||||
result
|
|
||||||
}
|
}
|
||||||
// The header may not exist when the user logs in through the web interface, so allow a request with no header.
|
// The header may not exist when the user logs in through the web interface, so allow a request with no header.
|
||||||
// Further authentication checks will be conducted in services.rs
|
// Further authentication checks will be conducted in services.rs
|
||||||
} else {
|
} else {
|
||||||
let result = Response { success: false, error: false, reason: "Chhoto-Api-Key header not found".to_string(), pass: true };
|
// Due to the implementation of this result in services.rs, this JSON object will not be outputted.
|
||||||
result
|
Response { success: false, error: false, reason: "X-API-Key header was not found".to_string(), pass: true }
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// If the API key isn't set, but an API Key header is provided
|
// If the API key isn't set, but an API Key header is provided
|
||||||
if let Some(_) = auth::api_header(&http) {
|
if let Some(_) = auth::api_header(&http) {
|
||||||
let result = Response {success: false, error: true, reason: "API key access was attempted, but no API key is configured".to_string(), pass: false};
|
Response {success: false, error: true, reason: "An API key was provided, but the 'api_key' environment variable is not configured in the Chhoto URL instance".to_string(), pass: false}
|
||||||
result
|
|
||||||
} else {
|
} else {
|
||||||
let result = Response {success: false, error: false, reason: "".to_string(), pass: true};
|
Response {success: false, error: false, reason: "".to_string(), pass: true}
|
||||||
result
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user