Skip to content

A simple and small Rust Actix web framework Extractor for verifing JWT token from Firebase Authentication.

Notifications You must be signed in to change notification settings

D4ntin/firebase-auth

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Firebase Auth

A simple and small Rust Actix web framework Extractor for verifing JWT token from Firebase Authentication.

Example

Dependencies:

[dependencies]
actix-web = "4"
firebase-auth = "0.1"

Code:

Basic

use actix_web::{get, middleware::Logger, web::Data, App, HttpServer, Responder};
use env_logger::Env;
use firebase_auth::{FirebaseAuth, FirebaseUser};

// Use `FirebaseUser` extractor to verify the user token and decode the claims
#[get("/hello")]
async fn greet(user: FirebaseUser) -> impl Responder {
    format!("Hello {}!", user.email)
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    env_logger::init_from_env(Env::default().default_filter_or("debug"));

    // Create Application State for the `FirebaseAuth` it will refresh the public keys
    // automatically.
    // Change project_id to your Firebase Project ID
    // We put this in blocking because the first time it run, it will try to get the public keys
    // from Google endpoint, if it failed it will panic.
    let firebase_auth = tokio::task::spawn_blocking(|| FirebaseAuth::new("my-project-id"))
        .await
        .expect("panic init FirebaseAuth");

    let app_data = Data::new(firebase_auth);

    HttpServer::new(move || {
        App::new()
            .wrap(Logger::default())
            .app_data(app_data.clone())
            .service(greet)
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}

Request the endpoint

Obtain the Bearer token

Use firebase sdk to get the User Token.

For example: getIdToken()

Request the endpoint with Authorization Bearer

Make the request using the User's token. Note that it will expire so you will need to get it again if expired.

TOKEN="<paste your token here>"

curl --header 'Authorization: Bearer $TOKEN' http://127.0.0.1:8080/hello

License

MIT

Copyright (c) 2022-, Quang Tran.

About

A simple and small Rust Actix web framework Extractor for verifing JWT token from Firebase Authentication.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 100.0%