We'll use a JWT in the `Authorization` request header to identify the user saving a page. The token will need to be set in the _authorization token_ field in the SingleFile configuration so it will be included when uploading.
38 lines
935 B
Rust
38 lines
935 B
Rust
use std::io::Read;
|
|
use std::time::SystemTime;
|
|
|
|
use jsonwebtoken::{encode, EncodingKey, Header};
|
|
use serde::Serialize;
|
|
|
|
#[derive(Debug, Serialize)]
|
|
struct UserClaims {
|
|
aud: String,
|
|
exp: u64,
|
|
iat: u64,
|
|
iss: String,
|
|
nbf: u64,
|
|
sub: String,
|
|
}
|
|
|
|
fn main() {
|
|
let args: Vec<_> = std::env::args().collect();
|
|
let mut secret = vec![];
|
|
let mut f = std::fs::File::open(&args[1]).unwrap();
|
|
f.read_to_end(&mut secret).unwrap();
|
|
let k = EncodingKey::from_secret(&secret);
|
|
let now = SystemTime::now()
|
|
.duration_since(SystemTime::UNIX_EPOCH)
|
|
.unwrap()
|
|
.as_secs();
|
|
let claims = UserClaims {
|
|
aud: env!("CARGO_PKG_NAME").into(),
|
|
exp: now + 604800,
|
|
iat: now,
|
|
iss: env!("CARGO_PKG_NAME").into(),
|
|
nbf: now - 60,
|
|
sub: args[2].to_string(),
|
|
};
|
|
let jwt = encode(&Header::default(), &claims, &k).unwrap();
|
|
println!("{}", jwt);
|
|
}
|