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

Configure the default OAuth2 ClientID #209

Merged
merged 2 commits into from
May 4, 2022
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
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)
}