From d423d9ba62746f0bdbc41e21969e195eb926d21f Mon Sep 17 00:00:00 2001 From: LandaMm Date: Sat, 24 May 2025 20:58:04 +0200 Subject: [PATCH] feat: oauth routes --- internal/oauth/routes.go | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 internal/oauth/routes.go diff --git a/internal/oauth/routes.go b/internal/oauth/routes.go new file mode 100644 index 0000000..0633abc --- /dev/null +++ b/internal/oauth/routes.go @@ -0,0 +1,49 @@ +package oauth + +import ( + "encoding/json" + "log" + "net/http" + + "gitea.local/admin/hspguard/internal/web" + "github.com/go-chi/chi/v5" +) + +type OAuthHandler struct{} + +func NewOAuthHandler() *OAuthHandler { + return &OAuthHandler{} +} + +func (h *OAuthHandler) RegisterRoutes(r chi.Router) { + r.Get("/oauth/authorize", h.authorizeEndpoint) + r.Get("/oauth/token", h.tokenEndpoint) +} + +func (h *OAuthHandler) tokenEndpoint(w http.ResponseWriter, r *http.Request) { + log.Println("[OAUTH] New request to token endpoint") + w.WriteHeader(http.StatusOK) + w.Write([]byte("OK")) +} + +func (h *OAuthHandler) authorizeEndpoint(w http.ResponseWriter, r *http.Request) { + log.Println("[OAUTH] New request to authorize endpoint") + w.WriteHeader(http.StatusOK) + w.Write([]byte("OK")) +} + +func (h *OAuthHandler) Metadata(w http.ResponseWriter, r *http.Request) { + type Response struct { + TokenEndpoint string `json:"token_endpoint"` + AuthEndpoint string `json:"authorization_endpoint"` + } + + encoder := json.NewEncoder(w) + + if err := encoder.Encode(Response{ + TokenEndpoint: "http://192.168.178.21:3001/api/v1/oauth/token", + AuthEndpoint: "http://192.168.178.21:5173/authorize", + }); err != nil { + web.Error(w, "failed to encode response", http.StatusInternalServerError) + } +}