google-oidc-middleware
Google OIDC Middleware for golang
Requirements
- Go 1.19 or higher. support the 3 latest versions of Go.
Usage
sample go code
package main
import (
"encoding/json"
"log"
"net/http"
"os"
googleoidcmiddleware "github.com/mashiike/google-oidc-middleware"
"github.com/thanhpk/randstr"
)
func main() {
log.Println("access http://localhost:8080")
err := http.ListenAndServe("localhost:8080", googleoidcmiddleware.WrapGoogleOIDC(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
claims, ok := googleoidcmiddleware.IDTokenClaims(r.Context())
if !ok {
w.WriteHeader(http.StatusForbidden)
return
}
w.WriteHeader(http.StatusOK)
e := json.NewEncoder(w)
e.SetEscapeHTML(true)
e.SetIndent("", " ")
e.Encode(claims)
}),
(&googleoidcmiddleware.Config{
ClientID: os.Getenv("GOOGLE_CLIENT_ID"),
ClientSecret: os.Getenv("GOOGLE_CLIENT_SECRET"),
SessionEncryptKey: randstr.Bytes(32),
Scopes: []string{"email"},
}).WithBaseURL("http://localhost:8080"),
))
if err != nil {
log.Fatalln(err)
}
}