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