Skip to content

Commit

Permalink
Allow to enable Proof Key for Code Exachange (PKCE) (#271)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
thalionath and mgsbbern authored Dec 2, 2024
1 parent aa92a0a commit 19f4300
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,4 @@ func main() {
| 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. |
| Oauth2UsePkce | bool | false | If set to true, it enables Proof Key for Code Exchange to enhance security for OAuth public clients. |
16 changes: 15 additions & 1 deletion swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type swaggerConfig struct {
DeepLinking bool
PersistAuthorization bool
Oauth2DefaultClientID string
Oauth2UsePkce bool
}

// Config stores ginSwagger configuration variables.
Expand All @@ -37,6 +38,7 @@ type Config struct {
DeepLinking bool
PersistAuthorization bool
Oauth2DefaultClientID string
Oauth2UsePkce bool
}

func (config Config) toSwaggerConfig() swaggerConfig {
Expand All @@ -51,6 +53,7 @@ func (config Config) toSwaggerConfig() swaggerConfig {
Title: config.Title,
PersistAuthorization: config.PersistAuthorization,
Oauth2DefaultClientID: config.Oauth2DefaultClientID,
Oauth2UsePkce: config.Oauth2UsePkce,
}
}

Expand Down Expand Up @@ -106,6 +109,15 @@ func Oauth2DefaultClientID(oauth2DefaultClientID string) func(*Config) {
}
}

// Oauth2UsePkce enables Proof Key for Code Exchange.
// Corresponds to the usePkceWithAuthorizationCodeGrant property of the Swagger UI
// and applies only to accessCode (Authorization Code) flows.
func Oauth2UsePkce(usePkce bool) func(*Config) {
return func(c *Config) {
c.Oauth2UsePkce = usePkce
}
}

// WrapHandler wraps `http.Handler` into `gin.HandlerFunc`.
func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerFunc {
var config = Config{
Expand All @@ -117,6 +129,7 @@ func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerF
DeepLinking: true,
PersistAuthorization: false,
Oauth2DefaultClientID: "",
Oauth2UsePkce: false,
}

for _, c := range options {
Expand Down Expand Up @@ -273,7 +286,8 @@ window.onload = function() {
const defaultClientId = "{{.Oauth2DefaultClientID}}";
if (defaultClientId) {
ui.initOAuth({
clientId: defaultClientId
clientId: defaultClientId,
usePkceWithAuthorizationCodeGrant: {{.Oauth2UsePkce}}
})
}
Expand Down
13 changes: 13 additions & 0 deletions swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,16 @@ func TestOauth2DefaultClientID(t *testing.T) {
configFunc(&cfg)
assert.Equal(t, "", cfg.Oauth2DefaultClientID)
}

func TestOauth2UsePkce(t *testing.T) {
var cfg Config
assert.Equal(t, false, cfg.Oauth2UsePkce)

configFunc := Oauth2UsePkce(true)
configFunc(&cfg)
assert.Equal(t, true, cfg.Oauth2UsePkce)

configFunc = Oauth2UsePkce(false)
configFunc(&cfg)
assert.Equal(t, false, cfg.Oauth2UsePkce)
}

0 comments on commit 19f4300

Please sign in to comment.