Skip to content

Commit 19f4300

Browse files
thalionathmgsbbern
andauthored
Allow to enable Proof Key for Code Exachange (PKCE) (#271)
* Allow to enable Proof Key for Code Exachange (PKCE) Wires usePkceWithAuthorizationCodeGrant OAuth2 option of the Swagger UI to the options interface * Changes according to maintainers review --------- Co-authored-by: Mario Gruber <[email protected]>
1 parent aa92a0a commit 19f4300

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,4 @@ func main() {
204204
| 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_). |
205205
| PersistAuthorization | bool | false | If set to true, it persists authorization data and it would not be lost on browser close/refresh. |
206206
| 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

+15-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type swaggerConfig struct {
2424
DeepLinking bool
2525
PersistAuthorization bool
2626
Oauth2DefaultClientID string
27+
Oauth2UsePkce bool
2728
}
2829

2930
// Config stores ginSwagger configuration variables.
@@ -37,6 +38,7 @@ type Config struct {
3738
DeepLinking bool
3839
PersistAuthorization bool
3940
Oauth2DefaultClientID string
41+
Oauth2UsePkce bool
4042
}
4143

4244
func (config Config) toSwaggerConfig() swaggerConfig {
@@ -51,6 +53,7 @@ func (config Config) toSwaggerConfig() swaggerConfig {
5153
Title: config.Title,
5254
PersistAuthorization: config.PersistAuthorization,
5355
Oauth2DefaultClientID: config.Oauth2DefaultClientID,
56+
Oauth2UsePkce: config.Oauth2UsePkce,
5457
}
5558
}
5659

@@ -106,6 +109,15 @@ func Oauth2DefaultClientID(oauth2DefaultClientID string) func(*Config) {
106109
}
107110
}
108111

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) {
116+
return func(c *Config) {
117+
c.Oauth2UsePkce = usePkce
118+
}
119+
}
120+
109121
// WrapHandler wraps `http.Handler` into `gin.HandlerFunc`.
110122
func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerFunc {
111123
var config = Config{
@@ -117,6 +129,7 @@ func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerF
117129
DeepLinking: true,
118130
PersistAuthorization: false,
119131
Oauth2DefaultClientID: "",
132+
Oauth2UsePkce: false,
120133
}
121134

122135
for _, c := range options {
@@ -273,7 +286,8 @@ window.onload = function() {
273286
const defaultClientId = "{{.Oauth2DefaultClientID}}";
274287
if (defaultClientId) {
275288
ui.initOAuth({
276-
clientId: defaultClientId
289+
clientId: defaultClientId,
290+
usePkceWithAuthorizationCodeGrant: {{.Oauth2UsePkce}}
277291
})
278292
}
279293

swagger_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,16 @@ func TestOauth2DefaultClientID(t *testing.T) {
254254
configFunc(&cfg)
255255
assert.Equal(t, "", cfg.Oauth2DefaultClientID)
256256
}
257+
258+
func TestOauth2UsePkce(t *testing.T) {
259+
var cfg Config
260+
assert.Equal(t, false, cfg.Oauth2UsePkce)
261+
262+
configFunc := Oauth2UsePkce(true)
263+
configFunc(&cfg)
264+
assert.Equal(t, true, cfg.Oauth2UsePkce)
265+
266+
configFunc = Oauth2UsePkce(false)
267+
configFunc(&cfg)
268+
assert.Equal(t, false, cfg.Oauth2UsePkce)
269+
}

0 commit comments

Comments
 (0)