From 5c28f1b59e8c9b75848692bca16140794e1bc13d Mon Sep 17 00:00:00 2001
From: wanna <wanna.w@binarywalk.com>
Date: Tue, 3 Dec 2024 14:16:53 +0800
Subject: [PATCH 1/2] =?UTF-8?q?api.json=E9=89=B4=E6=9D=83=E6=A1=88?=
 =?UTF-8?q?=E5=88=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 example/httpserver/swagger/main.go            |  9 +++++++
 net/ghttp/ghttp_server_config_api.go          |  5 ++++
 ...ttp_z_unit_feature_openapi_swagger_test.go | 25 +++++++++++++++++++
 3 files changed, 39 insertions(+)

diff --git a/example/httpserver/swagger/main.go b/example/httpserver/swagger/main.go
index d453588d3e2..3d934352c86 100644
--- a/example/httpserver/swagger/main.go
+++ b/example/httpserver/swagger/main.go
@@ -45,5 +45,14 @@ func main() {
 			new(Hello),
 		)
 	})
+	// if api.json requires authentication, add openApiBasicAuth handler
+	s.BindHookHandler(s.GetOpenApiPath(), ghttp.HookBeforeServe, openApiBasicAuth)
 	s.Run()
 }
+
+func openApiBasicAuth(r *ghttp.Request) {
+	if !r.BasicAuth("OpenApiAuthUserName", "OpenApiAuthPass", "Restricted") {
+		r.ExitAll()
+		return
+	}
+}
diff --git a/net/ghttp/ghttp_server_config_api.go b/net/ghttp/ghttp_server_config_api.go
index d9a66d3cb09..06878767475 100644
--- a/net/ghttp/ghttp_server_config_api.go
+++ b/net/ghttp/ghttp_server_config_api.go
@@ -21,3 +21,8 @@ func (s *Server) SetSwaggerUITemplate(swaggerUITemplate string) {
 func (s *Server) SetOpenApiPath(path string) {
 	s.config.OpenApiPath = path
 }
+
+// GetOpenApiPath get the OpenApiPath for server.
+func (s *Server) GetOpenApiPath() string {
+	return s.config.OpenApiPath
+}
diff --git a/net/ghttp/ghttp_z_unit_feature_openapi_swagger_test.go b/net/ghttp/ghttp_z_unit_feature_openapi_swagger_test.go
index e46db1ddf1e..2ce097ac9e1 100644
--- a/net/ghttp/ghttp_z_unit_feature_openapi_swagger_test.go
+++ b/net/ghttp/ghttp_z_unit_feature_openapi_swagger_test.go
@@ -185,3 +185,28 @@ func Test_OpenApi_Method_All_Swagger(t *testing.T) {
 		t.Assert(gstr.Contains(c.GetContent(ctx, "/api.json"), `/test/error`), true)
 	})
 }
+
+func Test_OpenApi_Auth(t *testing.T) {
+	s := g.Server(guid.S())
+	apiPath := "/api.json"
+	s.SetOpenApiPath(apiPath)
+	s.BindHookHandler(s.GetOpenApiPath(), ghttp.HookBeforeServe, openApiBasicAuth)
+	s.Start()
+	defer s.Shutdown()
+	gtest.C(t, func(t *gtest.T) {
+		t.Assert(s.GetOpenApiPath(), apiPath)
+		c := g.Client()
+		c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
+		t.Assert(c.GetContent(ctx, apiPath), "Unauthorized")
+		c.SetBasicAuth("OpenApiAuthUserName", "OpenApiAuthPass")
+		cc := c.GetContent(ctx, apiPath)
+		t.AssertNE(cc, "Unauthorized")
+	})
+}
+
+func openApiBasicAuth(r *ghttp.Request) {
+	if !r.BasicAuth("OpenApiAuthUserName", "OpenApiAuthPass", "Restricted") {
+		r.ExitAll()
+		return
+	}
+}

From 9342802cd696b5167b1842380c06a332d0f2afe4 Mon Sep 17 00:00:00 2001
From: wanna <wanna.w@binarywalk.com>
Date: Thu, 5 Dec 2024 10:54:02 +0800
Subject: [PATCH 2/2] fix annotation

---
 net/ghttp/ghttp_server_config_api.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/ghttp/ghttp_server_config_api.go b/net/ghttp/ghttp_server_config_api.go
index 06878767475..a89888b1fba 100644
--- a/net/ghttp/ghttp_server_config_api.go
+++ b/net/ghttp/ghttp_server_config_api.go
@@ -22,7 +22,7 @@ func (s *Server) SetOpenApiPath(path string) {
 	s.config.OpenApiPath = path
 }
 
-// GetOpenApiPath get the OpenApiPath for server.
+// GetOpenApiPath returns the configuration of `OpenApiPath` of server.
 func (s *Server) GetOpenApiPath() string {
 	return s.config.OpenApiPath
 }