Compare commits

...
3 Commits
Author SHA1 Message Date
aya 4fb8d0cb5c Edited the API Key header to comply with OpenAPI v3 specs 2025-01-03 00:25:55 +10:00
aya 9a0cdec646 Improved API error codes 2025-01-01 19:08:35 +10:00
aya 818dadb84f Made code more Rust-like 2025-01-01 17:34:09 +10:00
5 changed files with 14 additions and 23 deletions

No files matched your search

+3 -3
View File
@@ -193,17 +193,17 @@ If the API key is insecure, a warning will be outputted along with a generated A
To add a link:
``` 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:
``` 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:
``` 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`.
+3 -3
View File
@@ -3,7 +3,7 @@
use actix_session::Session;
use std::{env, time::SystemTime};
use actix_web::{HttpRequest};
use actix_web::HttpRequest;
// API key generation and scoring
use passwords::{PasswordGenerator, scorer, analyzer};
@@ -42,13 +42,13 @@ pub fn gen_key() -> String {
// Check if the API key header exists
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
pub fn is_key_secure() -> bool {
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
-1
View File
@@ -30,7 +30,6 @@ async fn main() -> Result<()> {
.filter(|s| !s.trim().is_empty())
.unwrap_or(String::from("urls.sqlite"));
let port = env::var("port")
.unwrap_or(String::from("4567"))
.parse::<u16>()
+2 -5
View File
@@ -158,11 +158,8 @@ pub async fn link_handler(
// Handle login
#[post("/api/login")]
pub async fn login(req: String, session: Session) -> HttpResponse {
// Someone's API may be listening for the plain HTML body response of "Correct password!"
// rather than a 200 OK HTTP response. Because of that, a check is performed to see whether
// 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") {
// Keep this function backwards compatible
if env::var("api_key").is_ok() {
if let Ok(password) = env::var("password") {
if password != req {
eprintln!("Failed login attempt!");
+6 -11
View File
@@ -18,7 +18,6 @@ struct URLPair {
}
// Define JSON struct for response
// Named "ReturnResponse" rather than "Response" because of the previous import.
#[derive(Serialize)]
pub struct Response {
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 the header is correct
if auth::validate_key(header.to_string()) {
let result = Response { success: true, error: false, reason: "".to_string(), pass: false };
result
Response { success: true, error: false, reason: "Correct API key".to_string(), pass: false }
} else {
let result = Response { success: false, error: true, reason: "Incorrect API key".to_string(), pass: false };
result
Response { success: false, error: true, reason: "Incorrect API key".to_string(), pass: false }
}
// 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
} else {
let result = Response { success: false, error: false, reason: "Chhoto-Api-Key header not found".to_string(), pass: true };
result
// Due to the implementation of this result in services.rs, this JSON object will not be outputted.
Response { success: false, error: false, reason: "X-API-Key header was not found".to_string(), pass: true }
}
} else {
// If the API key isn't set, but an API Key header is provided
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};
result
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}
} else {
let result = Response {success: false, error: false, reason: "".to_string(), pass: true};
result
Response {success: false, error: false, reason: "".to_string(), pass: true}
}
}
}