Skip to content

🔒 Secure Firebase authentication middleware for Go HTTP servers. Easy integration with any Go web framework (Chi, Gin, Echo, Gorilla Mux). Validate Firebase ID tokens and access user claims in your handlers.

License

Notifications You must be signed in to change notification settings

kw510/http-firebase-auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HTTP Firebase Auth Middleware 🔐

Go Report Card GoDoc License: MIT

A lightweight, secure, and easy-to-use HTTP middleware for Go applications that integrates Firebase Authentication. This middleware helps you protect your HTTP endpoints by validating Firebase ID tokens and making user claims available in your request context.

✨ Features

  • 🔒 Secure Firebase ID token validation
  • 🚀 Simple integration with any Go HTTP server
  • 🔑 Easy access to Firebase user claims
  • ⚡️ Minimal overhead
  • 🛡️ Built-in error handling
  • 🔄 Context-based token storage

📦 Installation

go get github.com/kw510/http-firebase-auth

🚀 Quick Start

package main

import (
    "context"
    "log"
    "net/http"

    firebase "firebase.google.com/go/v4"
    "github.com/kw510/http-firebase-auth"
)

func main() {
    // Initialize Firebase app
    app, err := firebase.NewApp(context.Background(), nil)
    if err != nil {
        log.Fatal(err)
    }

    // Create middleware
    authMiddleware := firebaseauth.New(context.Background(), app)

    // Create your handler
    handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        // Get user claims from context
        claims := firebaseauth.ClaimsFromContext(r.Context())
        if claims != nil {
            // Access user information
            userID := claims.UID
            // ... handle authenticated request
        }
    })

    // Apply middleware
    http.Handle("/protected", authMiddleware(handler))
    log.Fatal(http.ListenAndServe(":8080", nil))
}

🔑 Usage

1. Initialize Firebase

First, ensure you have Firebase credentials set up in your environment or configuration.

2. Create Middleware

authMiddleware := firebaseauth.New(context.Background(), app)

3. Apply to Routes

// Apply to specific route
http.Handle("/protected", authMiddleware(handler))

// Or apply to all routes
http.Handle("/", authMiddleware(mux))

4. Access User Claims

claims := firebaseauth.ClaimsFromContext(r.Context())
if claims != nil {
    userID := claims.UID
    email := claims.Claims["email"].(string)
    // ... use claims
}

🔒 Security

The middleware:

  • Validates Firebase ID tokens
  • Handles token expiration
  • Provides secure context-based storage of claims
  • Returns 401 Unauthorized for invalid tokens

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📝 License

This project is licensed under the MIT License - see the LICENSE file for details.

⭐️ Show your support

Give a ⭐️ if this project helped you!

📚 Related Projects

🔗 Links

About

🔒 Secure Firebase authentication middleware for Go HTTP servers. Easy integration with any Go web framework (Chi, Gin, Echo, Gorilla Mux). Validate Firebase ID tokens and access user claims in your handlers.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages