diff --git a/README.md b/README.md index d610e08..097cbee 100644 --- a/README.md +++ b/README.md @@ -168,5 +168,6 @@ func main() { | DocExpansion | 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_). -| PersistAuthotization | bool | false | If set to true, it persists authorization data and it would not be lost on browser close/refresh. | +| 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_). | +| PersistAuthorization | bool | false | If set to true, it persists authorization data and it would not be lost on browser close/refresh. | +| Oauth2DefaultClientID | string | "" | If set, it's used to prepopulate the *client_id* field of the OAuth2 Authorization dialog. | diff --git a/swagger.go b/swagger.go index e918405..a555fc3 100644 --- a/swagger.go +++ b/swagger.go @@ -22,6 +22,7 @@ type swaggerConfig struct { DefaultModelsExpandDepth int DeepLinking bool PersistAuthorization bool + Oauth2DefaultClientID string } // Config stores ginSwagger configuration variables. @@ -34,6 +35,7 @@ type Config struct { DefaultModelsExpandDepth int DeepLinking bool PersistAuthorization bool + Oauth2DefaultClientID string } func (config Config) toSwaggerConfig() swaggerConfig { @@ -45,8 +47,9 @@ func (config Config) toSwaggerConfig() swaggerConfig { Oauth2RedirectURL: "`${window.location.protocol}//${window.location.host}$" + "{window.location.pathname.split('/').slice(0, window.location.pathname.split('/').length - 1).join('/')}" + "/oauth2-redirect.html`", - Title: config.Title, - PersistAuthorization: config.PersistAuthorization, + Title: config.Title, + PersistAuthorization: config.PersistAuthorization, + Oauth2DefaultClientID: config.Oauth2DefaultClientID, } } @@ -95,6 +98,13 @@ func PersistAuthorization(persistAuthorization bool) func(*Config) { } } +// Oauth2DefaultClientID set the default client ID used for OAuth2 +func Oauth2DefaultClientID(oauth2DefaultClientID string) func(*Config) { + return func(c *Config) { + c.Oauth2DefaultClientID = oauth2DefaultClientID + } +} + // WrapHandler wraps `http.Handler` into `gin.HandlerFunc`. func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerFunc { var config = Config{ @@ -105,6 +115,7 @@ func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerF DefaultModelsExpandDepth: 1, DeepLinking: true, PersistAuthorization: false, + Oauth2DefaultClientID: "", } for _, c := range options { @@ -303,6 +314,13 @@ window.onload = function() { defaultModelsExpandDepth: {{.DefaultModelsExpandDepth}} }) + const defaultClientId = "{{.Oauth2DefaultClientID}}"; + if (defaultClientId) { + ui.initOAuth({ + clientId: defaultClientId + }) + } + window.ui = ui } diff --git a/swagger_test.go b/swagger_test.go index 1412d6a..fd7000d 100644 --- a/swagger_test.go +++ b/swagger_test.go @@ -233,3 +233,16 @@ func TestPersistAuthorization(t *testing.T) { configFunc(&cfg) assert.Equal(t, false, cfg.PersistAuthorization) } + +func TestOauth2DefaultClientID(t *testing.T) { + var cfg Config + assert.Equal(t, "", cfg.Oauth2DefaultClientID) + + configFunc := Oauth2DefaultClientID("default_client_id") + configFunc(&cfg) + assert.Equal(t, "default_client_id", cfg.Oauth2DefaultClientID) + + configFunc = Oauth2DefaultClientID("") + configFunc(&cfg) + assert.Equal(t, "", cfg.Oauth2DefaultClientID) +}