Add the ability to configure the default OAuth2 client_id

This commit is contained in:
Josh Strohminger 2022-05-03 15:33:23 -07:00
parent 64d8dea07f
commit 948a7d40e3
2 changed files with 33 additions and 2 deletions

View File

@ -22,6 +22,7 @@ type swaggerConfig struct {
DefaultModelsExpandDepth int
DeepLinking bool
PersistAuthorization bool
Oauth2DefaultClientID string
}
// Config stores ginSwagger configuration variables.
@ -34,6 +35,7 @@ type Config struct {
DefaultModelsExpandDepth int
DeepLinking bool
PersistAuthorization bool
Oauth2DefaultClientID string
}
func (config Config) toSwaggerConfig() swaggerConfig {
@ -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,
}
}
@ -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{
@ -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 {
@ -303,6 +314,13 @@ window.onload = function() {
defaultModelsExpandDepth: {{.DefaultModelsExpandDepth}}
})
const defaultClientId = "{{.Oauth2DefaultClientID}}";
if (defaultClientId) {
ui.initOAuth({
clientId: defaultClientId
})
}
window.ui = ui
}
</script>

View File

@ -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)
}