Using functions for setting times on tokens
This commit is contained in:
@@ -18,11 +18,28 @@ pub fn get_key() -> Result<String, dotenvy::Error> {
|
|||||||
Ok(key)
|
Ok(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_expiration() -> time::Result<time::Duration> {
|
pub fn get_issued() -> time::Result<time::OffsetDateTime> {
|
||||||
let now = time::OffsetDateTime::now_utc();
|
let now = time::OffsetDateTime::now_utc();
|
||||||
let epoch = time::OffsetDateTime::UNIX_EPOCH;
|
// let epoch = time::OffsetDateTime::UNIX_EPOCH;
|
||||||
let since_the_epoch = now - epoch;
|
// let since_the_epoch = now - epoch;
|
||||||
Ok(since_the_epoch)
|
// Ok(since_the_epoch)
|
||||||
|
Ok(now)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_expiration(issued: &time::OffsetDateTime) -> Result<time::OffsetDateTime, time::Error> {
|
||||||
|
let duration_expire = time::Duration::hours(4);
|
||||||
|
let expiration = *issued + duration_expire;
|
||||||
|
Ok(expiration)
|
||||||
|
}
|
||||||
|
|
||||||
|
mod util {
|
||||||
|
// use std::time;
|
||||||
|
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> {
|
||||||
@@ -33,13 +50,19 @@ pub fn create_token(provided_key: &String) -> Result<(String, i64), josekit::Jos
|
|||||||
payload.set_subject(MESSAGE);
|
payload.set_subject(MESSAGE);
|
||||||
payload.set_issuer(ISSUER);
|
payload.set_issuer(ISSUER);
|
||||||
payload.set_audience(vec![AUDIENCE]);
|
payload.set_audience(vec![AUDIENCE]);
|
||||||
match get_expiration() {
|
match get_issued() {
|
||||||
Ok(duration) => {
|
Ok(issued) => {
|
||||||
|
/*
|
||||||
let expire = duration.whole_seconds();
|
let expire = duration.whole_seconds();
|
||||||
let _ = payload.set_claim(
|
let _ = payload.set_claim(
|
||||||
"expiration",
|
"issued",
|
||||||
Some(serde_json::to_value(expire.to_string()).unwrap()),
|
Some(serde_json::to_value(expire.to_string()).unwrap()),
|
||||||
);
|
);
|
||||||
|
*/
|
||||||
|
let expire = get_expiration(&issued).unwrap();
|
||||||
|
// payload.set_issued_at(std::time::Duration::try_from(duration).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 key: String = if provided_key.is_empty() {
|
||||||
get_key().unwrap()
|
get_key().unwrap()
|
||||||
@@ -47,6 +70,8 @@ pub fn create_token(provided_key: &String) -> Result<(String, i64), josekit::Jos
|
|||||||
provided_key.to_owned()
|
provided_key.to_owned()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let duration = expire - time::OffsetDateTime::UNIX_EPOCH;
|
||||||
|
|
||||||
let signer = Hs256.signer_from_bytes(key.as_bytes()).unwrap();
|
let signer = Hs256.signer_from_bytes(key.as_bytes()).unwrap();
|
||||||
Ok((
|
Ok((
|
||||||
josekit::jwt::encode_with_signer(&payload, &header, &signer).unwrap(),
|
josekit::jwt::encode_with_signer(&payload, &header, &signer).unwrap(),
|
||||||
|
Reference in New Issue
Block a user