googleoidcmiddleware

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 20, 2022 License: MIT Imports: 18 Imported by: 1

README

google-oidc-middleware

Documentation Latest GitHub tag Github Actions test Go Report Card License

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)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IDTokenClaims

func IDTokenClaims(ctx context.Context) (map[string]interface{}, bool)

func New

func New(cfg *Config) (func(next http.Handler) http.Handler, error)

func WrapGoogleOIDC

func WrapGoogleOIDC(next http.Handler, cfg *Config) http.Handler

Types

type Config

type Config struct {
	ClientID          string
	ClientSecret      string
	SessionEncryptKey []byte
	CookieName        string
	LoginPath         string
	CallbackPath      string
	BaseURL           *url.URL
	Scopes            []string
	Logger
}

func (*Config) WithBaseURL

func (cfg *Config) WithBaseURL(rawURL string) *Config

type Logger

type Logger interface {
	Println(v ...any)
}

type Session

type Session struct {
	IDToken    string `json:"id_token,omitempty"`
	RedirectTo string `json:"redirect_to,omitempty"`
	S          string `json:"s,omitempty"`
}

func (*Session) MarshalCookie

func (s *Session) MarshalCookie(w http.ResponseWriter, cookieName string, encryptKey []byte, optFns ...func(*http.Cookie)) error

func (*Session) UnmarshalCookie

func (s *Session) UnmarshalCookie(r *http.Request, cookieName string, encryptKey []byte) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL