2024-03-12 15:49:08 +01:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2024-09-10 19:46:16 +02:00
|
|
|
"context"
|
2024-03-12 15:49:08 +01:00
|
|
|
"net/http"
|
|
|
|
|
2024-09-10 19:46:16 +02:00
|
|
|
"github.com/google/uuid"
|
2024-03-12 15:49:08 +01:00
|
|
|
"github.com/r3labs/sse/v2"
|
2024-09-10 19:46:16 +02:00
|
|
|
"github.com/zitadel/oidc/v3/pkg/client/rp"
|
2024-03-12 15:49:08 +01:00
|
|
|
)
|
|
|
|
|
2024-09-10 19:46:16 +02:00
|
|
|
func SetupRoutes(router *http.ServeMux, sse *sse.Server, appHandler *AppHandler, authHandler *AuthHandler) {
|
2024-05-21 17:33:13 +02:00
|
|
|
router.HandleFunc("GET /sse", sse.ServeHTTP)
|
2024-04-12 16:19:11 +02:00
|
|
|
|
2024-05-21 17:33:13 +02:00
|
|
|
fsAssets := http.FileServer(http.Dir("assets"))
|
|
|
|
router.Handle("GET /assets/", http.StripPrefix("/assets/", fsAssets))
|
2024-04-12 16:19:11 +02:00
|
|
|
|
2024-06-11 19:08:02 +02:00
|
|
|
icons := http.FileServer(http.Dir("storage/icons"))
|
|
|
|
router.Handle("GET /icons/", http.StripPrefix("/icons/", icons))
|
|
|
|
|
2024-09-10 19:46:16 +02:00
|
|
|
state := func() string {
|
|
|
|
return uuid.New().String()
|
|
|
|
}
|
|
|
|
router.Handle("GET /login", rp.AuthURLHandler(state, authHandler.provider, authHandler.urlOptions...))
|
|
|
|
router.Handle("GET /auch/callback", rp.CodeExchangeHandler(authHandler.marshalToken, authHandler.provider))
|
|
|
|
|
|
|
|
router.HandleFunc("GET /", authMiddleware(http.HandlerFunc(appHandler.appHandler), authHandler))
|
|
|
|
}
|
|
|
|
|
|
|
|
func authMiddleware(next http.Handler, authHandler *AuthHandler) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := context.Background()
|
|
|
|
clains, err := rp.VerifyTokens(ctx, authHandler.provider)
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
2024-03-12 15:49:08 +01:00
|
|
|
}
|