From 396c3ecdf7085fecee1077d65dea388f34fe2b44 Mon Sep 17 00:00:00 2001 From: Juan Font Alonso Date: Mon, 20 Jun 2022 12:31:19 +0200 Subject: [PATCH] Remove Gin from the OIDC handlers --- app.go | 8 ++++---- oidc.go | 21 ++++++++++++++------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/app.go b/app.go index d8a4a609..0587813c 100644 --- a/app.go +++ b/app.go @@ -411,12 +411,12 @@ func (h *Headscale) createRouter(grpcMux *runtime.ServeMux) *mux.Router { router.HandleFunc("/key", h.KeyHandler).Methods(http.MethodGet) router.HandleFunc("/register", h.RegisterWebAPI).Methods(http.MethodGet) - router.HandleFunc("/machine/:id/map", h.PollNetMapHandler).Methods(http.MethodPost) - router.HandleFunc("/machine/:id", h.RegistrationHandler).Methods(http.MethodPost) - router.HandleFunc("/oidc/register/:mkey", h.RegisterOIDC).Methods(http.MethodGet) + router.HandleFunc("/machine/{mkey}/map", h.PollNetMapHandler).Methods(http.MethodPost) + router.HandleFunc("/machine/{mkey}", h.RegistrationHandler).Methods(http.MethodPost) + router.HandleFunc("/oidc/register/{mkey}", h.RegisterOIDC).Methods(http.MethodGet) router.HandleFunc("/oidc/callback", h.OIDCCallback).Methods(http.MethodGet) router.HandleFunc("/apple", h.AppleConfigMessage).Methods(http.MethodGet) - router.HandleFunc("/apple/:platform", h.ApplePlatformConfig).Methods(http.MethodGet) + router.HandleFunc("/apple/{platform}", h.ApplePlatformConfig).Methods(http.MethodGet) router.HandleFunc("/windows", h.WindowsConfigMessage).Methods(http.MethodGet) router.HandleFunc("/windows/tailscale.reg", h.WindowsRegConfig).Methods(http.MethodGet) router.HandleFunc("/swagger", SwaggerUI).Methods(http.MethodGet) diff --git a/oidc.go b/oidc.go index 477fe78c..67e3b467 100644 --- a/oidc.go +++ b/oidc.go @@ -13,7 +13,7 @@ import ( "time" "github.com/coreos/go-oidc/v3/oidc" - "github.com/gin-gonic/gin" + "github.com/gorilla/mux" "github.com/rs/zerolog/log" "golang.org/x/oauth2" "tailscale.com/types/key" @@ -63,10 +63,17 @@ func (h *Headscale) initOIDC() error { // RegisterOIDC redirects to the OIDC provider for authentication // Puts machine key in cache so the callback can retrieve it using the oidc state param // Listens in /oidc/register/:mKey. -func (h *Headscale) RegisterOIDC(ctx *gin.Context) { - machineKeyStr := ctx.Param("mkey") - if machineKeyStr == "" { - ctx.String(http.StatusBadRequest, "Wrong params") +func (h *Headscale) RegisterOIDC( + w http.ResponseWriter, + r *http.Request, +) { + vars := mux.Vars(r) + machineKeyStr, ok := vars["mkey"] + if !ok || machineKeyStr == "" { + log.Error(). + Caller(). + Msg("Missing machine key in URL") + http.Error(w, "Missing machine key in URL", http.StatusBadRequest) return } @@ -81,7 +88,7 @@ func (h *Headscale) RegisterOIDC(ctx *gin.Context) { log.Error(). Caller(). Msg("could not read 16 bytes from rand") - ctx.String(http.StatusInternalServerError, "could not read 16 bytes from rand") + http.Error(w, "Internal server error", http.StatusInternalServerError) return } @@ -101,7 +108,7 @@ func (h *Headscale) RegisterOIDC(ctx *gin.Context) { authURL := h.oauth2Config.AuthCodeURL(stateStr, extras...) log.Debug().Msgf("Redirecting to %s for authentication", authURL) - ctx.Redirect(http.StatusFound, authURL) + http.Redirect(w, r, authURL, http.StatusFound) } type oidcCallbackTemplateConfig struct {