Added expiration pop
Some checks failed
Rust Build / Check (pull_request) Successful in 47s
Rust Build / Test Suite (pull_request) Successful in 56s
Rust Build / Rustfmt (pull_request) Failing after 33s
Rust Build / Clippy (pull_request) Successful in 45s
Rust Build / build (pull_request) Successful in 1m8s
Some checks failed
Rust Build / Check (pull_request) Successful in 47s
Rust Build / Test Suite (pull_request) Successful in 56s
Rust Build / Rustfmt (pull_request) Failing after 33s
Rust Build / Clippy (pull_request) Successful in 45s
Rust Build / build (pull_request) Successful in 1m8s
This commit is contained in:
@@ -70,7 +70,7 @@ pub mod endpoint {
|
||||
if hashing::verify_password(&usr.password, hash_password.clone()).unwrap() {
|
||||
// Create token
|
||||
let key = token_stuff::get_key().unwrap();
|
||||
let token_literal = token_stuff::create_token(&key).unwrap();
|
||||
let (token_literal, duration) = token_stuff::create_token(&key).unwrap();
|
||||
|
||||
if token_stuff::verify_token(&key, &token_literal) {
|
||||
(
|
||||
@@ -82,7 +82,7 @@ pub mod endpoint {
|
||||
username: user.username,
|
||||
token: token_literal,
|
||||
token_type: String::from(token_stuff::TOKENTYPE),
|
||||
expiration: -1,
|
||||
expiration: duration as i32,
|
||||
}],
|
||||
}),
|
||||
)
|
||||
|
@@ -25,7 +25,7 @@ pub fn get_expiration() -> time::Result<time::Duration> {
|
||||
Ok(since_the_epoch)
|
||||
}
|
||||
|
||||
pub fn create_token(provided_key: &String) -> Result<String, josekit::JoseError> {
|
||||
pub fn create_token(provided_key: &String) -> Result<(String, i64), josekit::JoseError> {
|
||||
let mut header = JwsHeader::new();
|
||||
header.set_token_type(TOKENTYPE);
|
||||
|
||||
@@ -40,9 +40,6 @@ pub fn create_token(provided_key: &String) -> Result<String, josekit::JoseError>
|
||||
"expiration",
|
||||
Some(serde_json::to_value(expire.to_string()).unwrap()),
|
||||
);
|
||||
}
|
||||
Err(_) => {}
|
||||
};
|
||||
|
||||
let key: String = if provided_key.is_empty() {
|
||||
get_key().unwrap()
|
||||
@@ -51,7 +48,12 @@ pub fn create_token(provided_key: &String) -> Result<String, josekit::JoseError>
|
||||
};
|
||||
|
||||
let signer = Hs256.signer_from_bytes(key.as_bytes()).unwrap();
|
||||
Ok(josekit::jwt::encode_with_signer(&payload, &header, &signer).unwrap())
|
||||
Ok((josekit::jwt::encode_with_signer(&payload, &header, &signer).unwrap(), duration.whole_seconds()))
|
||||
}
|
||||
Err(e) => {
|
||||
Err(josekit::JoseError::InvalidClaim(e.into()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn verify_token(key: &String, token: &String) -> bool {
|
||||
@@ -72,7 +74,7 @@ mod tests {
|
||||
fn test_tokenize() {
|
||||
let special_key = get_key().unwrap();
|
||||
match create_token(&special_key) {
|
||||
Ok(token) => {
|
||||
Ok((token, _duration)) => {
|
||||
let result = verify_token(&special_key, &token);
|
||||
assert!(result, "Token not verified");
|
||||
}
|
||||
|
Reference in New Issue
Block a user