Refactored function
This commit is contained in:
+13
-5
@@ -70,11 +70,17 @@ pub struct TokenResource {
|
|||||||
/// Token type
|
/// Token type
|
||||||
pub const TOKEN_TYPE: &str = "JWT";
|
pub const TOKEN_TYPE: &str = "JWT";
|
||||||
|
|
||||||
|
pub struct CreateTokenResult {
|
||||||
|
pub access_token: String,
|
||||||
|
pub issued: i64,
|
||||||
|
pub expires_in: i64,
|
||||||
|
}
|
||||||
|
|
||||||
pub fn create_token(
|
pub fn create_token(
|
||||||
key: &String,
|
key: &String,
|
||||||
token_resource: &TokenResource,
|
token_resource: &TokenResource,
|
||||||
duration: time::Duration,
|
duration: time::Duration,
|
||||||
) -> Result<(String, i64), josekit::JoseError> {
|
) -> Result<CreateTokenResult, josekit::JoseError> {
|
||||||
let mut header = josekit::jws::JwsHeader::new();
|
let mut header = josekit::jws::JwsHeader::new();
|
||||||
header.set_token_type(TOKEN_TYPE);
|
header.set_token_type(TOKEN_TYPE);
|
||||||
|
|
||||||
@@ -102,10 +108,12 @@ pub fn create_token(
|
|||||||
let signer = josekit::jws::alg::hmac::HmacJwsAlgorithm::Hs256
|
let signer = josekit::jws::alg::hmac::HmacJwsAlgorithm::Hs256
|
||||||
.signer_from_bytes(key.as_bytes())
|
.signer_from_bytes(key.as_bytes())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
Ok((
|
|
||||||
josekit::jwt::encode_with_signer(&payload, &header, &signer).unwrap(),
|
Ok(CreateTokenResult {
|
||||||
(expire - time::OffsetDateTime::UNIX_EPOCH).whole_seconds(),
|
access_token: josekit::jwt::encode_with_signer(&payload, &header, &signer).unwrap(),
|
||||||
))
|
issued: (expire - time::OffsetDateTime::UNIX_EPOCH).whole_seconds(),
|
||||||
|
expires_in: (issued - time::OffsetDateTime::UNIX_EPOCH).whole_seconds(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
Err(e) => Err(josekit::JoseError::InvalidClaim(e.into())),
|
Err(e) => Err(josekit::JoseError::InvalidClaim(e.into())),
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user