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 1 commit
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
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,13 @@ func main() {
}
```

| Option | Type | Default | Description |
| ------------------------ | ------ | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| URL | string | "doc.json" | URL pointing to API definition |
| 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). |
| DeepLinking | bool | true | If set to true, enables deep linking for tags and operations. See the Deep Linking documentation for more information. |
| DefaultModelsExpandDepth | int | 1 | Default expansion depth for models (set to -1 completely hide the models). |
| 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. |
| Option | Type | Default | Description |
| --------------------------------- | ------ | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| URL | string | "doc.json" | URL pointing to API definition |
| 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). |
| DeepLinking | bool | true | If set to true, enables deep linking for tags and operations. See the Deep Linking documentation for more information. |
| DefaultModelsExpandDepth | int | 1 | Default expansion depth for models (set to -1 completely hide the models). |
| 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. |
| UsePkceWithAuthorizationCodeGrant | bool | false | If set to true, it enables Proof Key for Code Exchange to enhance security for OAuth public clients. |
76 changes: 43 additions & 33 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,29 @@ import (
)

type swaggerConfig struct {
URL string
DocExpansion string
Title string
Oauth2RedirectURL htmlTemplate.JS
DefaultModelsExpandDepth int
DeepLinking bool
PersistAuthorization bool
Oauth2DefaultClientID string
URL string
DocExpansion string
Title string
Oauth2RedirectURL htmlTemplate.JS
DefaultModelsExpandDepth int
DeepLinking bool
PersistAuthorization bool
Oauth2DefaultClientID string
UsePkceWithAuthorizationCodeGrant bool
}

// Config stores ginSwagger configuration variables.
type Config struct {
// The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `doc.json`.
URL string
DocExpansion string
InstanceName string
Title string
DefaultModelsExpandDepth int
DeepLinking bool
PersistAuthorization bool
Oauth2DefaultClientID string
URL string
DocExpansion string
InstanceName string
Title string
DefaultModelsExpandDepth int
DeepLinking bool
PersistAuthorization bool
Oauth2DefaultClientID string
UsePkceWithAuthorizationCodeGrant bool
}

func (config Config) toSwaggerConfig() swaggerConfig {
Expand All @@ -48,9 +50,10 @@ func (config Config) toSwaggerConfig() swaggerConfig {
Oauth2RedirectURL: "`${window.location.protocol}//${window.location.host}$" +
"{window.location.pathname.split('/').slice(0, window.location.pathname.split('/').length - 1).join('/')}" +
"/oauth2-redirect.html`",
Title: config.Title,
PersistAuthorization: config.PersistAuthorization,
Oauth2DefaultClientID: config.Oauth2DefaultClientID,
Title: config.Title,
PersistAuthorization: config.PersistAuthorization,
Oauth2DefaultClientID: config.Oauth2DefaultClientID,
UsePkceWithAuthorizationCodeGrant: config.UsePkceWithAuthorizationCodeGrant,
}
}

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

// UsePkceWithAuthorizationCodeGrant enables Proof Key for Code Exchange.
// Only applies to accessCode (Authorization Code) flows.
func UsePkceWithAuthorizationCodeGrant(usePkce bool) func(*Config) {
thalionath marked this conversation as resolved.
Show resolved Hide resolved
return func(c *Config) {
c.UsePkceWithAuthorizationCodeGrant = usePkce
}
}

// WrapHandler wraps `http.Handler` into `gin.HandlerFunc`.
func WrapHandler(handler *webdav.Handler, options ...func(*Config)) gin.HandlerFunc {
var config = Config{
URL: "doc.json",
DocExpansion: "list",
InstanceName: swag.Name,
Title: "Swagger UI",
DefaultModelsExpandDepth: 1,
DeepLinking: true,
PersistAuthorization: false,
Oauth2DefaultClientID: "",
URL: "doc.json",
DocExpansion: "list",
InstanceName: swag.Name,
Title: "Swagger UI",
DefaultModelsExpandDepth: 1,
DeepLinking: true,
PersistAuthorization: false,
Oauth2DefaultClientID: "",
UsePkceWithAuthorizationCodeGrant: false,
}

for _, c := range options {
Expand Down Expand Up @@ -270,12 +282,10 @@ window.onload = function() {
defaultModelsExpandDepth: {{.DefaultModelsExpandDepth}}
})

const defaultClientId = "{{.Oauth2DefaultClientID}}";
if (defaultClientId) {
ui.initOAuth({
clientId: defaultClientId
})
}
ui.initOAuth({
clientId: {{.Oauth2DefaultClientID}},
usePkceWithAuthorizationCodeGrant: {{.UsePkceWithAuthorizationCodeGrant}}
thalionath marked this conversation as resolved.
Show resolved Hide resolved
})

window.ui = ui
}
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 TestUsePkceWithAuthorizationCodeGrant(t *testing.T) {
var cfg Config
assert.Equal(t, false, cfg.UsePkceWithAuthorizationCodeGrant)

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

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