-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(session): add create session cookie feature
* Add free standing function sessions::create_session_cookie(credentials, id_token, duration) * Add example Signed-off-by: Stephane Eintrazi <[email protected]> Signed-off-by: David Graeff <[email protected]>
- Loading branch information
1 parent
42e2d17
commit a70727f
Showing
4 changed files
with
211 additions
and
8 deletions.
There are no files selected for viewing
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,41 @@ | ||
use firestore_db_and_auth::{ | ||
errors::FirebaseError, Credentials, FirebaseAuthBearer, sessions::session_cookie | ||
}; | ||
|
||
use chrono::Duration; | ||
|
||
mod utils; | ||
|
||
fn main() -> Result<(), FirebaseError> { | ||
// Search for a credentials file in the root directory | ||
use std::path::PathBuf; | ||
|
||
let mut credential_file = PathBuf::from(env!("CARGO_MANIFEST_DIR")); | ||
credential_file.push("firebase-service-account.json"); | ||
let mut cred = Credentials::from_file(credential_file.to_str().unwrap())?; | ||
|
||
// Only download the public keys once, and cache them. | ||
let jwkset = utils::from_cache_file(credential_file.with_file_name("cached_jwks.jwks").as_path(), &cred)?; | ||
cred.add_jwks_public_keys(&jwkset); | ||
cred.verify()?; | ||
|
||
let cookie = session_cookie::create(&cred, user_session.access_token(), Duration::seconds(3600))?; | ||
println!("Created session cookie: {}", cookie); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn create_session_cookie_test() -> Result<(), FirebaseError> { | ||
let cred = utils::valid_test_credentials()?; | ||
let user_session = utils::user_session_with_cached_refresh_token(&cred)?; | ||
|
||
assert_eq!(user_session.user_id, utils::TEST_USER_ID); | ||
assert_eq!(user_session.project_id(), cred.project_id); | ||
|
||
use chrono::Duration; | ||
let cookie = session_cookie::create(&cred, user_session.access_token(), Duration::seconds(3600))?; | ||
|
||
assert!(cookie.len() > 0); | ||
Ok(()) | ||
} |
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