Compare commits
10
Commits
2c56c68637
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4fb8d0cb5c | ||
|
|
9a0cdec646 | ||
|
|
818dadb84f | ||
|
|
247cfb0476 | ||
|
|
6347a89725 | ||
|
|
9ddf043c17 | ||
|
|
a1f8700664 | ||
|
|
aab7a9b3d1 | ||
|
|
1ef5d539d5 | ||
|
|
5c2886f651 |
No files matched your search
@@ -128,6 +128,17 @@ docker run -p 4567:4567 \
|
||||
-e site_url="https://www.example.com" \
|
||||
-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
|
||||
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
|
||||
below, replace `http://localhost:4567` with where your instance of `chhoto-url` is accessible.
|
||||
|
||||
### Cookie validation
|
||||
If you have set up
|
||||
a password, first do the following to get an authentication cookie and store it in a file.
|
||||
```bash
|
||||
@@ -173,6 +185,33 @@ curl -X DELETE http://localhost:4567/api/del/<shortlink>
|
||||
```
|
||||
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
|
||||
get the siteurl using `curl http://localhost:4567/api/siteurl`.
|
||||
|
||||
|
||||
Generated
+47
-33
@@ -19,21 +19,6 @@ dependencies = [
|
||||
"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]]
|
||||
name = "actix-files"
|
||||
version = "0.6.6"
|
||||
@@ -240,21 +225,6 @@ dependencies = [
|
||||
"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]]
|
||||
name = "addr2line"
|
||||
version = "0.24.2"
|
||||
@@ -508,13 +478,12 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
name = "chhoto-url"
|
||||
version = "5.4.6"
|
||||
dependencies = [
|
||||
"actix-cors",
|
||||
"actix-files",
|
||||
"actix-session",
|
||||
"actix-web",
|
||||
"actix-web-httpauth",
|
||||
"env_logger",
|
||||
"nanoid",
|
||||
"passwords",
|
||||
"rand",
|
||||
"regex",
|
||||
"rusqlite",
|
||||
@@ -768,7 +737,6 @@ dependencies = [
|
||||
"futures-task",
|
||||
"pin-project-lite",
|
||||
"pin-utils",
|
||||
"slab",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1260,6 +1228,15 @@ dependencies = [
|
||||
"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]]
|
||||
name = "paste"
|
||||
version = "1.0.15"
|
||||
@@ -1317,6 +1294,12 @@ dependencies = [
|
||||
"zerocopy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-hack"
|
||||
version = "0.5.20+deprecated"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.89"
|
||||
@@ -1365,6 +1348,37 @@ dependencies = [
|
||||
"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]]
|
||||
name = "redox_syscall"
|
||||
version = "0.5.7"
|
||||
|
||||
@@ -29,8 +29,6 @@ categories = ["web-programming"]
|
||||
[dependencies]
|
||||
actix-web = "4.5.1"
|
||||
actix-files = "0.6.5"
|
||||
actix-cors = "0.7.0"
|
||||
actix-web-httpauth = "0.8.2"
|
||||
rusqlite = { version = "0.32.0", features = ["bundled"] }
|
||||
regex = "1.10.3"
|
||||
rand = "0.8.5"
|
||||
|
||||
+3
-3
@@ -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,7 +1,7 @@
|
||||
// SPDX-FileCopyrightText: 2023 Sayantan Santra <sayantan.santra689@gmail.com>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use rusqlite::{Connection};
|
||||
use rusqlite::Connection;
|
||||
use serde::Serialize;
|
||||
|
||||
// Struct for encoding a DB row
|
||||
|
||||
@@ -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>()
|
||||
|
||||
+28
-8
@@ -4,7 +4,7 @@
|
||||
use actix_files::NamedFile;
|
||||
use actix_session::Session;
|
||||
use actix_web::{delete, get, http::StatusCode, post, web::{self, Redirect}, Either, HttpRequest, HttpResponse, Responder};
|
||||
use std::{env};
|
||||
use std::env;
|
||||
|
||||
// Serialize JSON data
|
||||
use serde::Serialize;
|
||||
@@ -25,6 +25,14 @@ struct Response {
|
||||
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
|
||||
|
||||
// Add new links
|
||||
@@ -36,9 +44,24 @@ pub async fn add_link(req: String, data: web::Data<AppState>, session: Session,
|
||||
if result.success {
|
||||
let out = utils::add_link(req, &data.db);
|
||||
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 {
|
||||
HttpResponse::Conflict().body(out.1)
|
||||
let response = Response {
|
||||
success: false,
|
||||
error: true,
|
||||
reason: out.1
|
||||
};
|
||||
HttpResponse::Conflict().json(response)
|
||||
}
|
||||
} else if result.error {
|
||||
HttpResponse::Unauthorized().json(result)
|
||||
@@ -135,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!");
|
||||
|
||||
+10
-8
@@ -34,21 +34,23 @@ 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 {
|
||||
let result = Response {success: false, error: false, reason: "".to_string(), pass: true};
|
||||
result
|
||||
// If the API key isn't set, but an API Key header is provided
|
||||
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
@@ -3,7 +3,7 @@
|
||||
|
||||
services:
|
||||
chhoto-url:
|
||||
image: chhoto-url
|
||||
image: sintan1729/chhoto-url:latest
|
||||
restart: unless-stopped
|
||||
container_name: chhoto-url
|
||||
ports:
|
||||
@@ -25,7 +25,10 @@ services:
|
||||
|
||||
- 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
|
||||
# are accepted values, defaults to PERMANENT.
|
||||
@@ -35,12 +38,12 @@ services:
|
||||
# If you want UIDs, please change slug_style to UID.
|
||||
# Supported values for slug_style are Pair and UID.
|
||||
# The length is 8 by default, and a minimum of 4 is allowed.
|
||||
- slug_style=Pair
|
||||
- slug_length=8
|
||||
# - slug_style=Pair
|
||||
# - slug_length=8
|
||||
|
||||
# In case you want to provide public access to adding links (and not
|
||||
# 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
|
||||
# comma separated list of valid header as per RFC 7234 §5.2 to send those
|
||||
|
||||
Reference in new issue
Block a user