1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! An example code to verify an ID token of the Firebase Auth for signing in user.
//!
//! ```shell
//! $ cargo run --example verify_id_token --features verify -- --email <email> --password <password>
//! ```
use clap::Parser;
#[derive(Parser)]
struct Arguments {
#[arg(short, long)]
email: String,
#[arg(short, long)]
password: String,
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
#[cfg(not(feature = "verify"))]
{
return Err(anyhow::anyhow!(
"Feature \"verify\" is not enabled.",
));
}
#[cfg(feature = "verify")]
{
use fars::ApiKey;
use fars::Config;
use fars::Email;
use fars::Password;
use fars::ProjectId;
use fars::VerificationConfig;
// Parse the command line arguments.
let arguments = Arguments::parse();
// Read API key from the environment variable.
let api_key = ApiKey::new(std::env::var("FIREBASE_API_KEY")?);
// Read project ID from the environment variable.
let project_id = ProjectId::new(std::env::var("FIREBASE_PROJECT_ID")?);
// Create a config.
let config = Config::new(api_key);
// Get a session by signing in with email and password.
let session = config
.sign_in_with_email_password(
Email::new(arguments.email.clone()),
Password::new(arguments.password.clone()),
)
.await?;
// Create a verification config.
let config = VerificationConfig::new(project_id);
// Verify the ID token.
let claims = config
.verify_id_token(&session.id_token)
.await?;
println!(
"Token ID verification succeeded: {:?}",
claims
);
return Ok(());
}
}