routes.go raw

   1  package handler
   2  
   3  import (
   4  	"net/http"
   5  
   6  	"github.com/go-chi/chi/v5"
   7  	"github.com/go-chi/chi/v5/middleware"
   8  	"github.com/go-chi/cors"
   9  
  10  	"github.com/mlekudev/gitea-nostr-auth/internal/config"
  11  	"github.com/mlekudev/gitea-nostr-auth/internal/nostr"
  12  	"github.com/mlekudev/gitea-nostr-auth/internal/oauth2"
  13  )
  14  
  15  func NewRouter(cfg *config.Config, store oauth2.Store, fetcher *nostr.Fetcher) http.Handler {
  16  	r := chi.NewRouter()
  17  
  18  	// Middleware
  19  	r.Use(middleware.Logger)
  20  	r.Use(middleware.Recoverer)
  21  	r.Use(middleware.RealIP)
  22  	r.Use(cors.Handler(cors.Options{
  23  		AllowedOrigins:   []string{"*"},
  24  		AllowedMethods:   []string{"GET", "POST", "OPTIONS"},
  25  		AllowedHeaders:   []string{"Accept", "Authorization", "Content-Type"},
  26  		AllowCredentials: true,
  27  		MaxAge:           300,
  28  	}))
  29  
  30  	h := &Handler{
  31  		cfg:     cfg,
  32  		store:   store,
  33  		fetcher: fetcher,
  34  	}
  35  
  36  	// OIDC Discovery
  37  	r.Get("/.well-known/openid-configuration", h.OIDCDiscovery)
  38  
  39  	// OAuth2 endpoints
  40  	r.Get("/authorize", h.Authorize)
  41  	r.Post("/verify", h.Verify)
  42  	r.Post("/token", h.Token)
  43  	r.Get("/userinfo", h.UserInfo)
  44  
  45  	// JWKS endpoint (required for OIDC)
  46  	r.Get("/.well-known/jwks.json", h.JWKS)
  47  
  48  	return r
  49  }
  50  
  51  type Handler struct {
  52  	cfg     *config.Config
  53  	store   oauth2.Store
  54  	fetcher *nostr.Fetcher
  55  }
  56