From 1377013d3e3026d85cb171a78b3266b26d24b51d Mon Sep 17 00:00:00 2001
From: Yordan Kinkov <yordan.kinkov@vereign.com>
Date: Thu, 10 Nov 2022 11:41:48 +0200
Subject: [PATCH] Apply Authentication middleware to infohub service

---
 cmd/infohub/main.go       | 10 ++++++++++
 internal/config/config.go |  7 +++++++
 2 files changed, 17 insertions(+)

diff --git a/cmd/infohub/main.go b/cmd/infohub/main.go
index 138bf4b..2166616 100644
--- a/cmd/infohub/main.go
+++ b/cmd/infohub/main.go
@@ -21,6 +21,7 @@ import (
 	"golang.org/x/oauth2/clientcredentials"
 	"golang.org/x/sync/errgroup"
 
+	"gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/golib/auth"
 	"gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/golib/cache"
 	"gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/golib/goadec"
 	"gitlab.com/gaia-x/data-infrastructure-federation-services/tsa/golib/graceful"
@@ -156,6 +157,15 @@ func main() {
 		errFormatter,
 	)
 
+	// Apply Authentication middleware if enabled
+	if cfg.Auth.Enabled {
+		m, err := auth.NewMiddleware(cfg.Auth.JwkURL, cfg.Auth.RefreshInterval, httpClient)
+		if err != nil {
+			log.Fatalf("failed to create authentication middleware: %v", err)
+		}
+		infohubServer.Use(m.Handler())
+	}
+
 	// Configure the mux.
 	goainfohubsrv.Mount(mux, infohubServer)
 	goahealthsrv.Mount(mux, healthServer)
diff --git a/internal/config/config.go b/internal/config/config.go
index cd46395..b4a0439 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -11,6 +11,7 @@ type Config struct {
 	Signer     signerConfig
 	Metrics    metricsConfig
 	OAuth      oauthConfig
+	Auth       authConfig
 
 	LogLevel string `envconfig:"LOG_LEVEL" default:"INFO"`
 }
@@ -56,3 +57,9 @@ type oauthConfig struct {
 	ClientSecret string `envconfig:"OAUTH_CLIENT_SECRET" required:"true"`
 	TokenURL     string `envconfig:"OAUTH_TOKEN_URL" required:"true"`
 }
+
+type authConfig struct {
+	Enabled         bool          `envconfig:"AUTH_ENABLED" default:"true"`
+	JwkURL          string        `envconfig:"AUTH_JWK_URL"`
+	RefreshInterval time.Duration `envconfig:"AUTH_REFRESH_INTERVAL" default:"1h"`
+}
-- 
GitLab