#firebase #actix #web #framework #authentication

firebase-auth

Firebase authentication extractor for Actix Web

3 releases

0.1.6 Mar 7, 2023
0.1.5 Dec 22, 2022
0.1.2 Aug 11, 2022

#178 in HTTP server

Download history 9/week @ 2023-02-05 14/week @ 2023-02-12 19/week @ 2023-02-19 20/week @ 2023-02-26 41/week @ 2023-03-05 8/week @ 2023-03-12 25/week @ 2023-03-26 11/week @ 2023-04-02 22/week @ 2023-04-09 6/week @ 2023-04-16 7/week @ 2023-04-23 6/week @ 2023-04-30 32/week @ 2023-05-07 8/week @ 2023-05-14 32/week @ 2023-05-21

81 downloads per month

MIT license

18KB
278 lines

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.

Dependencies

~21–30MB
~718K SLoC