Skip to content

Commit

Permalink
feat: add support for swagger-ui persist-authorization (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuklyy authored Feb 6, 2022
1 parent 0bb2c39 commit cb6be4c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
14 changes: 13 additions & 1 deletion swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type swaggerConfig struct {
DefaultModelsExpandDepth int
Oauth2RedirectURL template.JS
Title string
PersistAuthorization bool
}

// Config stores ginSwagger configuration variables.
Expand All @@ -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.
Expand All @@ -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,
}
}

Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -275,6 +286,7 @@ window.onload = function() {
dom_id: '#swagger-ui',
validatorUrl: null,
oauth2RedirectUrl: {{.Oauth2RedirectURL}},
persistAuthorization: {{.PersistAuthorization}},
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
Expand Down
13 changes: 13 additions & 0 deletions swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit cb6be4c

Please sign in to comment.