From cb6be4cbcf70c8f278698a688ebb01fe2a6bd943 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Mik=C5=82asz?= Date: Sun, 6 Feb 2022 12:05:35 +0100 Subject: [PATCH] feat: add support for swagger-ui persist-authorization (#195) --- README.md | 3 ++- swagger.go | 14 +++++++++++++- swagger_test.go | 13 +++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d04cb01..d7472a5 100644 --- a/README.md +++ b/README.md @@ -165,4 +165,5 @@ func main() { | DocExpantion | string | "list" | Controls the default expansion setting for the operations and tags. It can be 'list' (expands only the tags), 'full' (expands the tags and operations) or 'none' (expands nothing). | | DeepLinking | bool | true | If set to true, enables deep linking for tags and operations. See the Deep Linking documentation for more information. | | DefaultModelsExpandDepth | int | 1 | Default expansion depth for models (set to -1 completely hide the models). | -| InstanceName | string | "swagger" | The instance name of the swagger document. If multiple different swagger instances should be deployed on one gin router, ensure that each instance has a unique name (use the _--instanceName_ parameter to generate swagger documents with _swag init_). | +| InstanceName | string | "swagger" | The instance name of the swagger document. If multiple different swagger instances should be deployed on one gin router, ensure that each instance has a unique name (use the _--instanceName_ parameter to generate swagger documents with _swag init_). +| PersistAuthotization | bool | false | If set to true, it persists authorization data and it would not be lost on browser close/refresh. | diff --git a/swagger.go b/swagger.go index 8403f6b..b3aceea 100644 --- a/swagger.go +++ b/swagger.go @@ -21,6 +21,7 @@ type swaggerConfig struct { DefaultModelsExpandDepth int Oauth2RedirectURL template.JS Title string + PersistAuthorization bool } // Config stores ginSwagger configuration variables. @@ -32,6 +33,7 @@ type Config struct { DefaultModelsExpandDepth int InstanceName string Title string + PersistAuthorization bool } // Convert the config to a swagger one in order to fill unexposed template values. @@ -46,7 +48,8 @@ func (c Config) ToSwaggerConfig() swaggerConfig { "{window.location.pathname.split('/').slice(0, window.location.pathname.split('/').length - 1).join('/')}" + "/oauth2-redirect.html`", ), - Title: c.Title, + Title: c.Title, + PersistAuthorization: c.PersistAuthorization, } } @@ -87,6 +90,14 @@ func InstanceName(name 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 + } +} + // WrapHandler wraps `http.Handler` into `gin.HandlerFunc`. func WrapHandler(h *webdav.Handler, confs ...func(c *Config)) gin.HandlerFunc { defaultConfig := &Config{ @@ -275,6 +286,7 @@ window.onload = function() { dom_id: '#swagger-ui', validatorUrl: null, oauth2RedirectUrl: {{.Oauth2RedirectURL}}, + persistAuthorization: {{.PersistAuthorization}}, presets: [ SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset diff --git a/swagger_test.go b/swagger_test.go index a68ad1a..85a51da 100644 --- a/swagger_test.go +++ b/swagger_test.go @@ -226,3 +226,16 @@ func TestInstanceName(t *testing.T) { configFunc(&cfg) assert.Equal(t, expected, cfg.InstanceName) } + +func TestPersistAuthorization(t *testing.T) { + var cfg Config + assert.Equal(t, false, cfg.PersistAuthorization) + + configFunc := PersistAuthorization(true) + configFunc(&cfg) + assert.Equal(t, true, cfg.PersistAuthorization) + + configFunc = PersistAuthorization(false) + configFunc(&cfg) + assert.Equal(t, false, cfg.PersistAuthorization) +}