From b045c47a3a976d25f810d07c09601b7ed6862160 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Mik=C5=82asz?= Date: Sun, 6 Feb 2022 11:29:57 +0100 Subject: [PATCH] Add support for swagger-ui persist-authorization --- README.md | 1 + swagger.go | 35 +++++++++++++++++++++++------------ swagger_test.go | 8 ++++++++ 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 4d0aec8..f36809f 100644 --- a/README.md +++ b/README.md @@ -153,6 +153,7 @@ window.onload = function() { docExpansion: "none", dom_id: "#swagger-ui-id", validatorUrl: null, + persistAuthorization: false, presets: [ SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset diff --git a/swagger.go b/swagger.go index bf07690..117e601 100644 --- a/swagger.go +++ b/swagger.go @@ -17,14 +17,15 @@ var WrapHandler = Handler() // Config stores httpSwagger configuration variables. type Config struct { // The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `doc.json`. - URL string - DeepLinking bool - DocExpansion string - DomID string - Plugins []template.JS - UIConfig map[template.JS]template.JS - BeforeScript template.JS - AfterScript template.JS + URL string + DeepLinking bool + DocExpansion string + DomID string + PersistAuthorization bool + Plugins []template.JS + UIConfig map[template.JS]template.JS + BeforeScript template.JS + AfterScript template.JS } // URL presents the url pointing to API definition (normally swagger.json or swagger.yaml). @@ -55,6 +56,14 @@ func DomID(domID string) func(c *Config) { } } +// If set to true, it persists authorization data and it would not be lost on browser close/refresh +// Defaults to false +func PersistAuthorization(persistAuthorization bool) func(c *Config) { + return func(c *Config) { + c.PersistAuthorization = persistAuthorization + } +} + // Plugins specifies additional plugins to load into Swagger UI. func Plugins(plugins []string) func(c *Config) { return func(c *Config) { @@ -97,10 +106,11 @@ func Handler(configFns ...func(*Config)) http.HandlerFunc { var once sync.Once config := &Config{ - URL: "doc.json", - DeepLinking: true, - DocExpansion: "list", - DomID: "#swagger-ui", + URL: "doc.json", + DeepLinking: true, + DocExpansion: "list", + DomID: "#swagger-ui", + PersistAuthorization: false, } for _, configFn := range configFns { configFn(config) @@ -233,6 +243,7 @@ window.onload = function() { deepLinking: {{.DeepLinking}}, docExpansion: "{{.DocExpansion}}", dom_id: "{{.DomID}}", + persistAuthorization: {{.PersistAuthorization}}, validatorUrl: null, presets: [ SwaggerUIBundle.presets.apis, diff --git a/swagger_test.go b/swagger_test.go index 57aa259..7d3a752 100644 --- a/swagger_test.go +++ b/swagger_test.go @@ -115,6 +115,14 @@ func TestDomID(t *testing.T) { assert.Equal(t, expected, cfg.DomID) } +func TestPersistAuthorization(t *testing.T) { + expected := true + cfg := Config{} + configFunc := PersistAuthorization(expected) + configFunc(&cfg) + assert.Equal(t, expected, cfg.PersistAuthorization) +} + func TestConfigURL(t *testing.T) { type fixture struct {