History

Configure the default OAuth2 ClientID (#209)

Josh Strohminger committed 4 years ago

Commit bdcc4ec1 parent 64d8dea3 files · +36 −4Browse files

Unified · Split

3 files
  1. README.md +3 −2
  2. swagger.go +20 −2
  3. swagger_test.go +13 −0
README.md+3−2
@@ -168,5 +168,6 @@ func main() { · 167 unmodified lines
168168 | 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). |
169169 | DeepLinking | bool | true | If set to true, enables deep linking for tags and operations. See the Deep Linking documentation for more information. |
170170 | DefaultModelsExpandDepth | int | 1 | Default expansion depth for models (set to -1 completely hide the models). |
171−| 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_).
172−| PersistAuthotization | bool | false | If set to true, it persists authorization data and it would not be lost on browser close/refresh. |
171+| 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_). |
172+| PersistAuthorization | bool | false | If set to true, it persists authorization data and it would not be lost on browser close/refresh. |
173+| Oauth2DefaultClientID | string | "" | If set, it's used to prepopulate the *client_id* field of the OAuth2 Authorization dialog. |
swagger.go+20−2
@@ -22,6 +22,7 @@ type swaggerConfig struct { · 21 unmodified lines
2222 DefaultModelsExpandDepth int
2323 DeepLinking bool
2424 PersistAuthorization bool
25+ Oauth2DefaultClientID string
2526 }
2627
2728 // Config stores ginSwagger configuration variables.
@@ -34,6 +35,7 @@ type Config struct { · 6 unmodified lines
3435 DefaultModelsExpandDepth int
3536 DeepLinking bool
3637 PersistAuthorization bool
38+ Oauth2DefaultClientID string
3739 }
3840
3941 func (config Config) toSwaggerConfig() swaggerConfig {
@@ -45,8 +47,9 @@ func (config Config) toSwaggerConfig() swaggerConfig { · 5 unmodified lines
4547 Oauth2RedirectURL: "`${window.location.protocol}//${window.location.host}$" +
4648 "{window.location.pathname.split('/').slice(0, window.location.pathname.split('/').length - 1).join('/')}" +
4749 "/oauth2-redirect.html`",
48− Title: config.Title,
49− PersistAuthorization: config.PersistAuthorization,
50+ Title: config.Title,
51+ PersistAuthorization: config.PersistAuthorization,
52+ Oauth2DefaultClientID: config.Oauth2DefaultClientID,
5053 }
5154 }
5255
@@ -95,6 +98,13 @@ func PersistAuthorization(persistAuthorization bool) func(*Config) { · 42 unmodified lines
9598 }
9699 }
97100
101+// Oauth2DefaultClientID set the default client ID used for OAuth2
102+func Oauth2DefaultClientID(oauth2DefaultClientID string) func(*Config) {
103+ return func(c *Config) {
104+ c.Oauth2DefaultClientID = oauth2DefaultClientID
105+ }
106+}
107+
98108 // WrapHandler wraps `http.Handler` into `gin.HandlerFunc`.
99109 func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerFunc {
100110 var config = Config{
@@ -105,6 +115,7 @@ func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerF · 4 unmodified lines
105115 DefaultModelsExpandDepth: 1,
106116 DeepLinking: true,
107117 PersistAuthorization: false,
118+ Oauth2DefaultClientID: "",
108119 }
109120
110121 for _, c := range options {
@@ -303,6 +314,13 @@ window.onload = function() { · 192 unmodified lines
303314 defaultModelsExpandDepth: {{.DefaultModelsExpandDepth}}
304315 })
305316
317+ const defaultClientId = "{{.Oauth2DefaultClientID}}";
318+ if (defaultClientId) {
319+ ui.initOAuth({
320+ clientId: defaultClientId
321+ })
322+ }
323+
306324 window.ui = ui
307325 }
308326 </script>
swagger_test.go+13−0
@@ -233,3 +233,16 @@ func TestPersistAuthorization(t *testing.T) { · 232 unmodified lines
233233 configFunc(&cfg)
234234 assert.Equal(t, false, cfg.PersistAuthorization)
235235 }
236+
237+func TestOauth2DefaultClientID(t *testing.T) {
238+ var cfg Config
239+ assert.Equal(t, "", cfg.Oauth2DefaultClientID)
240+
241+ configFunc := Oauth2DefaultClientID("default_client_id")
242+ configFunc(&cfg)
243+ assert.Equal(t, "default_client_id", cfg.Oauth2DefaultClientID)
244+
245+ configFunc = Oauth2DefaultClientID("")
246+ configFunc(&cfg)
247+ assert.Equal(t, "", cfg.Oauth2DefaultClientID)
248+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t