Code cleanup
All checks were successful
Rust Build / Test Suite (pull_request) Successful in 46s
Rust Build / Rustfmt (pull_request) Successful in 25s
Rust Build / Check (pull_request) Successful in 1m50s
Rust Build / Clippy (pull_request) Successful in 36s
Rust Build / build (pull_request) Successful in 51s
All checks were successful
Rust Build / Test Suite (pull_request) Successful in 46s
Rust Build / Rustfmt (pull_request) Successful in 25s
Rust Build / Check (pull_request) Successful in 1m50s
Rust Build / Clippy (pull_request) Successful in 36s
Rust Build / build (pull_request) Successful in 51s
This commit is contained in:
@@ -62,7 +62,7 @@ pub mod endpoint {
|
|||||||
id: user.id,
|
id: user.id,
|
||||||
username: user.username.clone(),
|
username: user.username.clone(),
|
||||||
token: token_literal,
|
token: token_literal,
|
||||||
token_type: String::from(token_stuff::TOKENTYPE),
|
token_type: String::from(icarus_models::token::TOKEN_TYPE),
|
||||||
expiration: duration,
|
expiration: duration,
|
||||||
}],
|
}],
|
||||||
}),
|
}),
|
||||||
|
@@ -1,12 +1,11 @@
|
|||||||
use josekit::{
|
use josekit::{
|
||||||
self,
|
self,
|
||||||
jws::{alg::hmac::HmacJwsAlgorithm::Hs256},
|
jws::alg::hmac::HmacJwsAlgorithm::Hs256,
|
||||||
jwt::{self},
|
jwt::{self},
|
||||||
};
|
};
|
||||||
|
|
||||||
use time;
|
use time;
|
||||||
|
|
||||||
pub const TOKENTYPE: &str = "JWT";
|
|
||||||
pub const KEY_ENV: &str = "SECRET_KEY";
|
pub const KEY_ENV: &str = "SECRET_KEY";
|
||||||
pub const MESSAGE: &str = "Something random";
|
pub const MESSAGE: &str = "Something random";
|
||||||
pub const ISSUER: &str = "icarus_auth";
|
pub const ISSUER: &str = "icarus_auth";
|
||||||
@@ -21,54 +20,11 @@ pub fn get_expiration(issued: &time::OffsetDateTime) -> Result<time::OffsetDateT
|
|||||||
Ok(*issued + duration_expire)
|
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(provided_key: &String) -> Result<(String, i64), josekit::JoseError> {
|
pub fn create_token(provided_key: &String) -> Result<(String, i64), josekit::JoseError> {
|
||||||
/*
|
|
||||||
let mut header = JwsHeader::new();
|
|
||||||
header.set_token_type(TOKENTYPE);
|
|
||||||
|
|
||||||
let mut payload = 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 key: String = if provided_key.is_empty() {
|
|
||||||
let rt = tokio::runtime::Runtime::new().unwrap();
|
|
||||||
|
|
||||||
// Block on the async function to get the result
|
|
||||||
rt.block_on(icarus_envy::environment::get_secret_key())
|
|
||||||
} else {
|
|
||||||
provided_key.to_owned()
|
|
||||||
};
|
|
||||||
|
|
||||||
let signer = 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())),
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
let resource = icarus_models::token::TokenResource {
|
let resource = icarus_models::token::TokenResource {
|
||||||
message: String::from(MESSAGE),
|
message: String::from(MESSAGE),
|
||||||
issuer: String::from(ISSUER),
|
issuer: String::from(ISSUER),
|
||||||
audiences: vec![String::from(AUDIENCE)]
|
audiences: vec![String::from(AUDIENCE)],
|
||||||
};
|
};
|
||||||
icarus_models::token::create_token(provided_key, &resource, time::Duration::hours(4))
|
icarus_models::token::create_token(provided_key, &resource, time::Duration::hours(4))
|
||||||
}
|
}
|
||||||
@@ -84,7 +40,6 @@ pub fn verify_token(key: &String, token: &String) -> bool {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Reference in New Issue
Block a user