-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
heimdall-v2: add
state
module and JWT settings
- Loading branch information
Showing
6 changed files
with
91 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,4 @@ | |
pub mod bootstrap; | ||
pub mod error; | ||
pub mod settings; | ||
mod state; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//! # AppState | ||
//! | ||
//! This module defines the application state type. | ||
use jsonwebtoken::DecodingKey; | ||
use secrecy::{ExposeSecret, Secret}; | ||
|
||
use crate::error::{Error, Result}; | ||
|
||
/// Application state type. | ||
#[derive(Clone)] | ||
pub struct AppState { | ||
/// JWT decoding key. | ||
pub jwt_decoding_key: DecodingKey, | ||
} | ||
|
||
impl AppState { | ||
/// Create new application state. | ||
pub fn new(jwt: Secret<String>) -> Result<Self> { | ||
let jwt_decoding_key = | ||
DecodingKey::from_ec_pem(jwt.expose_secret().as_bytes()).map_err(Error::JwtError)?; | ||
Ok(Self { jwt_decoding_key }) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use secrecy::Secret; | ||
|
||
#[test] | ||
fn state() { | ||
let secret = concat!( | ||
"-----BEGIN PUBLIC KEY-----\n", | ||
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEMMkapa1mVNQtUdWP9B61OpMcuBHmw+\n", | ||
"LwS66RkRJ3gYlrXCisZwWaNQo3nkNjRujIVVI9jEGCWYRdECga9lUjrg=\n", | ||
"-----END PUBLIC KEY-----", | ||
); | ||
|
||
let secret = Secret::new(secret.into()); | ||
let state = AppState::new(secret); | ||
assert!(state.is_ok()) | ||
} | ||
} |