add create token function (#52)
All checks were successful
Rust Build / Check (push) Successful in 31s
Release Tagging / release (push) Successful in 35s
Rust Build / Rustfmt (push) Successful in 30s
Rust Build / Test Suite (push) Successful in 40s
Rust Build / Clippy (push) Successful in 32s
Rust Build / build (push) Successful in 30s
Rust Build / Check (pull_request) Successful in 36s
Rust Build / Test Suite (pull_request) Successful in 44s
Rust Build / Rustfmt (pull_request) Successful in 26s
Rust Build / build (pull_request) Successful in 30s
Rust Build / Clippy (pull_request) Successful in 45s
All checks were successful
Rust Build / Check (push) Successful in 31s
Release Tagging / release (push) Successful in 35s
Rust Build / Rustfmt (push) Successful in 30s
Rust Build / Test Suite (push) Successful in 40s
Rust Build / Clippy (push) Successful in 32s
Rust Build / build (push) Successful in 30s
Rust Build / Check (pull_request) Successful in 36s
Rust Build / Test Suite (pull_request) Successful in 44s
Rust Build / Rustfmt (pull_request) Successful in 26s
Rust Build / build (pull_request) Successful in 30s
Rust Build / Clippy (pull_request) Successful in 45s
Reviewed-on: #52 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
49
src/token.rs
49
src/token.rs
@@ -62,6 +62,55 @@ impl Token {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_issued() -> time::Result<time::OffsetDateTime> {
|
||||
Ok(time::OffsetDateTime::now_utc())
|
||||
}
|
||||
|
||||
pub fn get_expiration(issued: &time::OffsetDateTime) -> Result<time::OffsetDateTime, time::Error> {
|
||||
let duration_expire = time::Duration::hours(4);
|
||||
Ok(*issued + duration_expire)
|
||||
}
|
||||
|
||||
mod util {
|
||||
pub fn time_to_std_time(
|
||||
provided_time: &time::OffsetDateTime,
|
||||
) -> Result<std::time::SystemTime, std::time::SystemTimeError> {
|
||||
let converted = std::time::SystemTime::from(*provided_time);
|
||||
Ok(converted)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_token(
|
||||
key: &String,
|
||||
message: &String,
|
||||
issuer: &String,
|
||||
audience: &String,
|
||||
) -> Result<(String, i64), josekit::JoseError> {
|
||||
let mut header = josekit::jws::JwsHeader::new();
|
||||
header.set_token_type("JWT");
|
||||
|
||||
let mut payload = josekit::jwt::JwtPayload::new();
|
||||
payload.set_subject(message);
|
||||
payload.set_issuer(issuer);
|
||||
payload.set_audience(vec![audience]);
|
||||
match get_issued() {
|
||||
Ok(issued) => {
|
||||
let expire = get_expiration(&issued).unwrap();
|
||||
payload.set_issued_at(&util::time_to_std_time(&issued).unwrap());
|
||||
payload.set_expires_at(&util::time_to_std_time(&expire).unwrap());
|
||||
|
||||
let signer = josekit::jws::alg::hmac::HmacJwsAlgorithm::Hs256
|
||||
.signer_from_bytes(key.as_bytes())
|
||||
.unwrap();
|
||||
Ok((
|
||||
josekit::jwt::encode_with_signer(&payload, &header, &signer).unwrap(),
|
||||
(expire - time::OffsetDateTime::UNIX_EPOCH).whole_seconds(),
|
||||
))
|
||||
}
|
||||
Err(e) => Err(josekit::JoseError::InvalidClaim(e.into())),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
Reference in New Issue
Block a user