Skip to content

Commit

Permalink
Configure the default OAuth2 ClientID (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshstrohminger authored May 4, 2022
1 parent 64d8dea commit bdcc4ec
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,6 @@ func main() {
| 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_).
| PersistAuthotization | bool | false | If set to true, it persists authorization data and it would not be lost on browser close/refresh. |
| 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. |
22 changes: 20 additions & 2 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type swaggerConfig struct {
DefaultModelsExpandDepth int
DeepLinking bool
PersistAuthorization bool
Oauth2DefaultClientID string
}

// Config stores ginSwagger configuration variables.
Expand All @@ -34,6 +35,7 @@ type Config struct {
DefaultModelsExpandDepth int
DeepLinking bool
PersistAuthorization bool
Oauth2DefaultClientID string
}

func (config Config) toSwaggerConfig() swaggerConfig {
Expand All @@ -45,8 +47,9 @@ 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,
Title: config.Title,
PersistAuthorization: config.PersistAuthorization,
Oauth2DefaultClientID: config.Oauth2DefaultClientID,
}
}

Expand Down Expand Up @@ -95,6 +98,13 @@ func PersistAuthorization(persistAuthorization bool) func(*Config) {
}
}

// Oauth2DefaultClientID set the default client ID used for OAuth2
func Oauth2DefaultClientID(oauth2DefaultClientID string) func(*Config) {
return func(c *Config) {
c.Oauth2DefaultClientID = oauth2DefaultClientID
}
}

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

for _, c := range options {
Expand Down Expand Up @@ -303,6 +314,13 @@ window.onload = function() {
defaultModelsExpandDepth: {{.DefaultModelsExpandDepth}}
})
const defaultClientId = "{{.Oauth2DefaultClientID}}";
if (defaultClientId) {
ui.initOAuth({
clientId: defaultClientId
})
}
window.ui = ui
}
</script>
Expand Down
13 changes: 13 additions & 0 deletions swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,16 @@ func TestPersistAuthorization(t *testing.T) {
configFunc(&cfg)
assert.Equal(t, false, cfg.PersistAuthorization)
}

func TestOauth2DefaultClientID(t *testing.T) {
var cfg Config
assert.Equal(t, "", cfg.Oauth2DefaultClientID)

configFunc := Oauth2DefaultClientID("default_client_id")
configFunc(&cfg)
assert.Equal(t, "default_client_id", cfg.Oauth2DefaultClientID)

configFunc = Oauth2DefaultClientID("")
configFunc(&cfg)
assert.Equal(t, "", cfg.Oauth2DefaultClientID)
}

0 comments on commit bdcc4ec

Please sign in to comment.