Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to enable Proof Key for Code Exachange (PKCE) #271

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading