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
168| 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). |168| 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). |
169| DeepLinking | bool | true | If set to true, enables deep linking for tags and operations. See the Deep Linking documentation for more information. |169| DeepLinking | bool | true | If set to true, enables deep linking for tags and operations. See the Deep Linking documentation for more information. |
170| DefaultModelsExpandDepth | int | 1 | Default expansion depth for models (set to -1 completely hide the models). |170| 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_). 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. |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
22 DefaultModelsExpandDepth int22 DefaultModelsExpandDepth int
23 DeepLinking bool23 DeepLinking bool
24 PersistAuthorization bool24 PersistAuthorization bool
25 Oauth2DefaultClientID string
25}26}
26 27
27// Config stores ginSwagger configuration variables.28// Config stores ginSwagger configuration variables.
@@ -34,6 +35,7 @@ type Config struct { · 6 unmodified lines
34 DefaultModelsExpandDepth int35 DefaultModelsExpandDepth int
35 DeepLinking bool36 DeepLinking bool
36 PersistAuthorization bool37 PersistAuthorization bool
38 Oauth2DefaultClientID string
37}39}
38 40
39func (config Config) toSwaggerConfig() swaggerConfig {41func (config Config) toSwaggerConfig() swaggerConfig {
@@ -45,8 +47,9 @@ func (config Config) toSwaggerConfig() swaggerConfig { · 5 unmodified lines
45 Oauth2RedirectURL: "`${window.location.protocol}//${window.location.host}$" +47 Oauth2RedirectURL: "`${window.location.protocol}//${window.location.host}$" +
46 "{window.location.pathname.split('/').slice(0, window.location.pathname.split('/').length - 1).join('/')}" +48 "{window.location.pathname.split('/').slice(0, window.location.pathname.split('/').length - 1).join('/')}" +
47 "/oauth2-redirect.html`",49 "/oauth2-redirect.html`",
48 Title: config.Title,50 Title: config.Title,
49 PersistAuthorization: config.PersistAuthorization,51 PersistAuthorization: config.PersistAuthorization,
52 Oauth2DefaultClientID: config.Oauth2DefaultClientID,
50 }53 }
51}54}
52 55
@@ -95,6 +98,13 @@ func PersistAuthorization(persistAuthorization bool) func(*Config) { · 42 unmodified lines
95 }98 }
96}99}
97 100
101// Oauth2DefaultClientID set the default client ID used for OAuth2
102func Oauth2DefaultClientID(oauth2DefaultClientID string) func(*Config) {
103 return func(c *Config) {
104 c.Oauth2DefaultClientID = oauth2DefaultClientID
105 }
106}
107
98// WrapHandler wraps `http.Handler` into `gin.HandlerFunc`.108// WrapHandler wraps `http.Handler` into `gin.HandlerFunc`.
99func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerFunc {109func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerFunc {
100 var config = Config{110 var config = Config{
@@ -105,6 +115,7 @@ func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerF · 4 unmodified lines
105 DefaultModelsExpandDepth: 1,115 DefaultModelsExpandDepth: 1,
106 DeepLinking: true,116 DeepLinking: true,
107 PersistAuthorization: false,117 PersistAuthorization: false,
118 Oauth2DefaultClientID: "",
108 }119 }
109 120
110 for _, c := range options {121 for _, c := range options {
@@ -303,6 +314,13 @@ window.onload = function() { · 192 unmodified lines
303 defaultModelsExpandDepth: {{.DefaultModelsExpandDepth}}314 defaultModelsExpandDepth: {{.DefaultModelsExpandDepth}}
304 })315 })
305 316
317 const defaultClientId = "{{.Oauth2DefaultClientID}}";
318 if (defaultClientId) {
319 ui.initOAuth({
320 clientId: defaultClientId
321 })
322 }
323
306 window.ui = ui324 window.ui = ui
307}325}
308</script>326</script>
swagger_test.go+13−0
@@ -233,3 +233,16 @@ func TestPersistAuthorization(t *testing.T) { · 232 unmodified lines
233 configFunc(&cfg)233 configFunc(&cfg)
234 assert.Equal(t, false, cfg.PersistAuthorization)234 assert.Equal(t, false, cfg.PersistAuthorization)
235}235}
236
237func 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