Encoding our token
We can encode our token with the following function:
// glue/src/token.rs
pub fn encode(self) -> Result<String, NanoServiceError> {
let key_str = Self::get_key()?;
let key = EncodingKey::from_secret(key_str.as_ref());
return match encode(&Header::default(), &self, &key) {
Ok(token) => Ok(token),
Err(error) => Err(
NanoServiceError::new(
error.to_string(),
NanoServiceErrorStatus::Unauthorized
)
)
};
}
Here we can see that we initially get the key for encoding our token. We then parse this into an EncodingKey
struct which can then be used to encode our HeaderToken
struct into a token.
With our encoded token, the only thing left is to decode our encoded token into an HeaderToken
struct.