Compare commits

..
10 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
aya 247cfb0476 Fixed compose.yaml 2024-12-31 20:32:46 +10:00
aya 6347a89725 Minor code clean up 2024-12-31 20:30:55 +10:00
aya 9ddf043c17 Fix typos, etc 2024-12-31 20:17:13 +10:00
aya a1f8700664 Change README.md 2024-12-31 20:15:06 +10:00
aya aab7a9b3d1 Change README.md and remove unneeded dependencies 2024-12-31 20:13:37 +10:00
aya 1ef5d539d5 Improve API error handling 2024-12-31 19:54:22 +10:00
aya 5c2886f651 Changes the API to use JSON data and results 2024-12-31 19:11:47 +10:00
9 changed files with 136 additions and 61 deletions

No files matched your search

+39
View File
@@ -128,6 +128,17 @@ docker run -p 4567:4567 \
-e site_url="https://www.example.com" \ -e site_url="https://www.example.com" \
-d chhoto-url:latest -d chhoto-url:latest
``` ```
1.c Optionally, set an API key to activate JSON result mode (optional)
```
docker run -p 4567:4567 \
-e password="password" \
-e api_key="SECURE_API_KEY" \
-v ./urls.sqlite:/urls.sqlite \
-e db_url=/urls.sqlite \
-e site_url="https://www.example.com" \
-d chhoto-url:latest
```
You can set the redirect method to Permanent 308 (default) or Temporary 307 by setting You can set the redirect method to Permanent 308 (default) or Temporary 307 by setting
the `redirect_method` variable to `TEMPORARY` or `PERMANENT` (it's matched exactly). By the `redirect_method` variable to `TEMPORARY` or `PERMANENT` (it's matched exactly). By
@@ -148,6 +159,7 @@ served through a proxy.
The application can be used from the terminal using something like `curl`. In all the examples The application can be used from the terminal using something like `curl`. In all the examples
below, replace `http://localhost:4567` with where your instance of `chhoto-url` is accessible. below, replace `http://localhost:4567` with where your instance of `chhoto-url` is accessible.
### Cookie validation
If you have set up If you have set up
a password, first do the following to get an authentication cookie and store it in a file. a password, first do the following to get an authentication cookie and store it in a file.
```bash ```bash
@@ -173,6 +185,33 @@ curl -X DELETE http://localhost:4567/api/del/<shortlink>
``` ```
The server will send a confirmation. The server will send a confirmation.
### API key validation
**This is required for programs that rely on a JSON response from Chhoto URL**
In order to use API key validation, set the `api_key` environment variable. If this is not set, the API will default to cookie validation (see section above).
If the API key is insecure, a warning will be outputted along with a generated API key which may be used.
To add a link:
``` bash
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 "X-API-Key: <YOUR_API_KEY>" http://localhost:4567/api/all
```
To delete a link:
``` bash
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`.
The server will output when the instance is accessed over API, when an incorrect API key is received, etc.
In both modes, these routes are accessible:
You can get the version of `chhoto-url` the server is running using `curl http://localhost:4567/api/version` and You can get the version of `chhoto-url` the server is running using `curl http://localhost:4567/api/version` and
get the siteurl using `curl http://localhost:4567/api/siteurl`. get the siteurl using `curl http://localhost:4567/api/siteurl`.
+47 -33
View File
@@ -19,21 +19,6 @@ dependencies = [
"tracing", "tracing",
] ]
[[package]]
name = "actix-cors"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9e772b3bcafe335042b5db010ab7c09013dad6eac4915c91d8d50902769f331"
dependencies = [
"actix-utils",
"actix-web",
"derive_more 0.99.18",
"futures-util",
"log",
"once_cell",
"smallvec",
]
[[package]] [[package]]
name = "actix-files" name = "actix-files"
version = "0.6.6" version = "0.6.6"
@@ -240,21 +225,6 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "actix-web-httpauth"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "456348ed9dcd72a13a1f4a660449fafdecee9ac8205552e286809eb5b0b29bd3"
dependencies = [
"actix-utils",
"actix-web",
"base64 0.22.1",
"futures-core",
"futures-util",
"log",
"pin-project-lite",
]
[[package]] [[package]]
name = "addr2line" name = "addr2line"
version = "0.24.2" version = "0.24.2"
@@ -508,13 +478,12 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
name = "chhoto-url" name = "chhoto-url"
version = "5.4.6" version = "5.4.6"
dependencies = [ dependencies = [
"actix-cors",
"actix-files", "actix-files",
"actix-session", "actix-session",
"actix-web", "actix-web",
"actix-web-httpauth",
"env_logger", "env_logger",
"nanoid", "nanoid",
"passwords",
"rand", "rand",
"regex", "regex",
"rusqlite", "rusqlite",
@@ -768,7 +737,6 @@ dependencies = [
"futures-task", "futures-task",
"pin-project-lite", "pin-project-lite",
"pin-utils", "pin-utils",
"slab",
] ]
[[package]] [[package]]
@@ -1260,6 +1228,15 @@ dependencies = [
"windows-targets", "windows-targets",
] ]
[[package]]
name = "passwords"
version = "3.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11407193a7c2bd14ec6b0ec3394da6fdcf7a4d5dcbc8c3cc38dfb17802c8d59c"
dependencies = [
"random-pick",
]
[[package]] [[package]]
name = "paste" name = "paste"
version = "1.0.15" version = "1.0.15"
@@ -1317,6 +1294,12 @@ dependencies = [
"zerocopy", "zerocopy",
] ]
[[package]]
name = "proc-macro-hack"
version = "0.5.20+deprecated"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.89" version = "1.0.89"
@@ -1365,6 +1348,37 @@ dependencies = [
"getrandom", "getrandom",
] ]
[[package]]
name = "random-number"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fc8cdd49be664772ffc3dbfa743bb8c34b78f9cc6a9f50e56ae878546796067"
dependencies = [
"proc-macro-hack",
"rand",
"random-number-macro-impl",
]
[[package]]
name = "random-number-macro-impl"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f5135143cb48d14289139e4615bffec0d59b4cbfd4ea2398a3770bd2abfc4aa2"
dependencies = [
"proc-macro-hack",
"quote",
"syn",
]
[[package]]
name = "random-pick"
version = "1.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c179499072da789afe44127d5f4aa6012de2c2f96ef759990196b37387a2a0f8"
dependencies = [
"random-number",
]
[[package]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.5.7" version = "0.5.7"
-2
View File
@@ -29,8 +29,6 @@ categories = ["web-programming"]
[dependencies] [dependencies]
actix-web = "4.5.1" actix-web = "4.5.1"
actix-files = "0.6.5" actix-files = "0.6.5"
actix-cors = "0.7.0"
actix-web-httpauth = "0.8.2"
rusqlite = { version = "0.32.0", features = ["bundled"] } rusqlite = { version = "0.32.0", features = ["bundled"] }
regex = "1.10.3" regex = "1.10.3"
rand = "0.8.5" rand = "0.8.5"
+3 -3
View File
@@ -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
+1 -1
View File
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: 2023 Sayantan Santra <sayantan.santra689@gmail.com> // SPDX-FileCopyrightText: 2023 Sayantan Santra <sayantan.santra689@gmail.com>
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
use rusqlite::{Connection}; use rusqlite::Connection;
use serde::Serialize; use serde::Serialize;
// Struct for encoding a DB row // Struct for encoding a DB row
-1
View File
@@ -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>()
+28 -8
View File
@@ -4,7 +4,7 @@
use actix_files::NamedFile; use actix_files::NamedFile;
use actix_session::Session; use actix_session::Session;
use actix_web::{delete, get, http::StatusCode, post, web::{self, Redirect}, Either, HttpRequest, HttpResponse, Responder}; use actix_web::{delete, get, http::StatusCode, post, web::{self, Redirect}, Either, HttpRequest, HttpResponse, Responder};
use std::{env}; use std::env;
// Serialize JSON data // Serialize JSON data
use serde::Serialize; use serde::Serialize;
@@ -25,6 +25,14 @@ struct Response {
reason: String, reason: String,
} }
// Needs to return the short URL to make it easier for programs leveraging the API
#[derive(Serialize)]
struct CreatedURL {
success: bool,
error: bool,
shorturl: String,
}
// Define the routes // Define the routes
// Add new links // Add new links
@@ -36,9 +44,24 @@ pub async fn add_link(req: String, data: web::Data<AppState>, session: Session,
if result.success { if result.success {
let out = utils::add_link(req, &data.db); let out = utils::add_link(req, &data.db);
if out.0 { if out.0 {
HttpResponse::Created().body(out.1) let port = env::var("port")
.unwrap_or(String::from("4567"))
.parse::<u16>()
.expect("Supplied port is not an integer");
let url = format!("{}:{}", env::var("site_url").unwrap_or(String::from("http://localhost")), port);
let response = CreatedURL {
success: true,
error: false,
shorturl: format!("{}/{}", url, out.1)
};
HttpResponse::Created().json(response)
} else { } else {
HttpResponse::Conflict().body(out.1) let response = Response {
success: false,
error: true,
reason: out.1
};
HttpResponse::Conflict().json(response)
} }
} else if result.error { } else if result.error {
HttpResponse::Unauthorized().json(result) HttpResponse::Unauthorized().json(result)
@@ -135,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!");
+10 -8
View File
@@ -34,21 +34,23 @@ 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 {
let result = Response {success: false, error: false, reason: "".to_string(), pass: true}; // If the API key isn't set, but an API Key header is provided
result if let Some(_) = auth::api_header(&http) {
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 {
Response {success: false, error: false, reason: "".to_string(), pass: true}
}
} }
} }
+8 -5
View File
@@ -3,7 +3,7 @@
services: services:
chhoto-url: chhoto-url:
image: chhoto-url image: sintan1729/chhoto-url:latest
restart: unless-stopped restart: unless-stopped
container_name: chhoto-url container_name: chhoto-url
ports: ports:
@@ -25,7 +25,10 @@ services:
- password=TopSecretPass - password=TopSecretPass
- api_key=test # This needs to be set in order to use programs that use the JSON interface of Chhoto URL.
# You will get a warning if this is insecure, and a generated value will be outputted
# You may use that value if you can't think of a secure key
# - api_key=SECURE_API_KEY
# Pass the redirect method, if needed. TEMPORARY and PERMANENT # Pass the redirect method, if needed. TEMPORARY and PERMANENT
# are accepted values, defaults to PERMANENT. # are accepted values, defaults to PERMANENT.
@@ -35,12 +38,12 @@ services:
# If you want UIDs, please change slug_style to UID. # If you want UIDs, please change slug_style to UID.
# Supported values for slug_style are Pair and UID. # Supported values for slug_style are Pair and UID.
# The length is 8 by default, and a minimum of 4 is allowed. # The length is 8 by default, and a minimum of 4 is allowed.
- slug_style=Pair # - slug_style=Pair
- slug_length=8 # - slug_length=8
# In case you want to provide public access to adding links (and not # In case you want to provide public access to adding links (and not
# delete, or listing), change the following option to Enable. # delete, or listing), change the following option to Enable.
- public_mode=Disable # - public_mode=Disable
# By default, the server sends no Cache-Control headers. You can supply a # By default, the server sends no Cache-Control headers. You can supply a
# comma separated list of valid header as per RFC 7234 §5.2 to send those # comma separated list of valid header as per RFC 7234 §5.2 to send those