feat: oauth routes
This commit is contained in:
49
internal/oauth/routes.go
Normal file
49
internal/oauth/routes.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user