Skip to content

Commit cf690ed

Browse files
committed
Changes according to maintainers review
1 parent da6a206 commit cf690ed

File tree

3 files changed

+60
-55
lines changed

3 files changed

+60
-55
lines changed

README.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,13 @@ func main() {
195195
}
196196
```
197197

198-
| Option | Type | Default | Description |
199-
| --------------------------------- | ------ | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
200-
| URL | string | "doc.json" | URL pointing to API definition |
201-
| 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). |
202-
| DeepLinking | bool | true | If set to true, enables deep linking for tags and operations. See the Deep Linking documentation for more information. |
203-
| DefaultModelsExpandDepth | int | 1 | Default expansion depth for models (set to -1 completely hide the models). |
204-
| 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_). |
205-
| PersistAuthorization | bool | false | If set to true, it persists authorization data and it would not be lost on browser close/refresh. |
206-
| Oauth2DefaultClientID | string | "" | If set, it's used to prepopulate the _client_id_ field of the OAuth2 Authorization dialog. |
207-
| UsePkceWithAuthorizationCodeGrant | bool | false | If set to true, it enables Proof Key for Code Exchange to enhance security for OAuth public clients. |
198+
| Option | Type | Default | Description |
199+
| -------------------------| ------ | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
200+
| URL | string | "doc.json" | URL pointing to API definition |
201+
| 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). |
202+
| DeepLinking | bool | true | If set to true, enables deep linking for tags and operations. See the Deep Linking documentation for more information. |
203+
| DefaultModelsExpandDepth | int | 1 | Default expansion depth for models (set to -1 completely hide the models). |
204+
| 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_). |
205+
| PersistAuthorization | bool | false | If set to true, it persists authorization data and it would not be lost on browser close/refresh. |
206+
| Oauth2DefaultClientID | string | "" | If set, it's used to prepopulate the _client_id_ field of the OAuth2 Authorization dialog. |
207+
| Oauth2UsePkce | bool | false | If set to true, it enables Proof Key for Code Exchange to enhance security for OAuth public clients. |

swagger.go

+44-39
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,29 @@ import (
1616
)
1717

1818
type swaggerConfig struct {
19-
URL string
20-
DocExpansion string
21-
Title string
22-
Oauth2RedirectURL htmlTemplate.JS
23-
DefaultModelsExpandDepth int
24-
DeepLinking bool
25-
PersistAuthorization bool
26-
Oauth2DefaultClientID string
27-
UsePkceWithAuthorizationCodeGrant bool
19+
URL string
20+
DocExpansion string
21+
Title string
22+
Oauth2RedirectURL htmlTemplate.JS
23+
DefaultModelsExpandDepth int
24+
DeepLinking bool
25+
PersistAuthorization bool
26+
Oauth2DefaultClientID string
27+
Oauth2UsePkce bool
2828
}
2929

3030
// Config stores ginSwagger configuration variables.
3131
type Config struct {
3232
// The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `doc.json`.
33-
URL string
34-
DocExpansion string
35-
InstanceName string
36-
Title string
37-
DefaultModelsExpandDepth int
38-
DeepLinking bool
39-
PersistAuthorization bool
40-
Oauth2DefaultClientID string
41-
UsePkceWithAuthorizationCodeGrant bool
33+
URL string
34+
DocExpansion string
35+
InstanceName string
36+
Title string
37+
DefaultModelsExpandDepth int
38+
DeepLinking bool
39+
PersistAuthorization bool
40+
Oauth2DefaultClientID string
41+
Oauth2UsePkce bool
4242
}
4343

4444
func (config Config) toSwaggerConfig() swaggerConfig {
@@ -50,10 +50,10 @@ func (config Config) toSwaggerConfig() swaggerConfig {
5050
Oauth2RedirectURL: "`${window.location.protocol}//${window.location.host}$" +
5151
"{window.location.pathname.split('/').slice(0, window.location.pathname.split('/').length - 1).join('/')}" +
5252
"/oauth2-redirect.html`",
53-
Title: config.Title,
54-
PersistAuthorization: config.PersistAuthorization,
55-
Oauth2DefaultClientID: config.Oauth2DefaultClientID,
56-
UsePkceWithAuthorizationCodeGrant: config.UsePkceWithAuthorizationCodeGrant,
53+
Title: config.Title,
54+
PersistAuthorization: config.PersistAuthorization,
55+
Oauth2DefaultClientID: config.Oauth2DefaultClientID,
56+
Oauth2UsePkce: config.Oauth2UsePkce,
5757
}
5858
}
5959

@@ -109,26 +109,27 @@ func Oauth2DefaultClientID(oauth2DefaultClientID string) func(*Config) {
109109
}
110110
}
111111

112-
// UsePkceWithAuthorizationCodeGrant enables Proof Key for Code Exchange.
113-
// Only applies to accessCode (Authorization Code) flows.
114-
func UsePkceWithAuthorizationCodeGrant(usePkce bool) func(*Config) {
112+
// Oauth2UsePkce enables Proof Key for Code Exchange.
113+
// Corresponds to the usePkceWithAuthorizationCodeGrant property of the Swagger UI
114+
// and applies only to accessCode (Authorization Code) flows.
115+
func Oauth2UsePkce(usePkce bool) func(*Config) {
115116
return func(c *Config) {
116-
c.UsePkceWithAuthorizationCodeGrant = usePkce
117+
c.Oauth2UsePkce = usePkce
117118
}
118119
}
119120

120121
// WrapHandler wraps `http.Handler` into `gin.HandlerFunc`.
121122
func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerFunc {
122123
var config = Config{
123-
URL: "doc.json",
124-
DocExpansion: "list",
125-
InstanceName: swag.Name,
126-
Title: "Swagger UI",
127-
DefaultModelsExpandDepth: 1,
128-
DeepLinking: true,
129-
PersistAuthorization: false,
130-
Oauth2DefaultClientID: "",
131-
UsePkceWithAuthorizationCodeGrant: false,
124+
URL: "doc.json",
125+
DocExpansion: "list",
126+
InstanceName: swag.Name,
127+
Title: "Swagger UI",
128+
DefaultModelsExpandDepth: 1,
129+
DeepLinking: true,
130+
PersistAuthorization: false,
131+
Oauth2DefaultClientID: "",
132+
Oauth2UsePkce: false,
132133
}
133134

134135
for _, c := range options {
@@ -282,10 +283,14 @@ window.onload = function() {
282283
defaultModelsExpandDepth: {{.DefaultModelsExpandDepth}}
283284
})
284285
285-
ui.initOAuth({
286-
clientId: {{.Oauth2DefaultClientID}},
287-
usePkceWithAuthorizationCodeGrant: {{.UsePkceWithAuthorizationCodeGrant}}
288-
})
286+
const defaultClientId = "{{.Oauth2DefaultClientID}}";
287+
288+
if (defaultClientId) {
289+
ui.initOAuth({
290+
clientId: defaultClientId,
291+
usePkceWithAuthorizationCodeGrant: {{.Oauth2UsePkce}}
292+
})
293+
}
289294
290295
window.ui = ui
291296
}

swagger_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,15 @@ func TestOauth2DefaultClientID(t *testing.T) {
255255
assert.Equal(t, "", cfg.Oauth2DefaultClientID)
256256
}
257257

258-
func TestUsePkceWithAuthorizationCodeGrant(t *testing.T) {
258+
func TestOauth2UsePkce(t *testing.T) {
259259
var cfg Config
260-
assert.Equal(t, false, cfg.UsePkceWithAuthorizationCodeGrant)
260+
assert.Equal(t, false, cfg.Oauth2UsePkce)
261261

262-
configFunc := UsePkceWithAuthorizationCodeGrant(true)
262+
configFunc := Oauth2UsePkce(true)
263263
configFunc(&cfg)
264-
assert.Equal(t, true, cfg.UsePkceWithAuthorizationCodeGrant)
264+
assert.Equal(t, true, cfg.Oauth2UsePkce)
265265

266-
configFunc = UsePkceWithAuthorizationCodeGrant(false)
266+
configFunc = Oauth2UsePkce(false)
267267
configFunc(&cfg)
268-
assert.Equal(t, false, cfg.UsePkceWithAuthorizationCodeGrant)
268+
assert.Equal(t, false, cfg.Oauth2UsePkce)
269269
}

0 commit comments

Comments
 (0)