diff --git a/swagger.go b/swagger.go index 53ebe29..cd2a842 100644 --- a/swagger.go +++ b/swagger.go @@ -22,6 +22,22 @@ type Config struct { InstanceName string DeepLinking bool PersistAuthorization bool + + // The information for OAuth2 integration, if any. + OAuth *OAuthConfig +} + +// OAuthConfig stores configuration for Swagger UI OAuth2 integration. See +// https://swagger.io/docs/open-source-tools/swagger-ui/usage/oauth2/ for further details. +type OAuthConfig struct { + // The ID of the client sent to the OAuth2 IAM provider. + ClientId string + + // The OAuth2 realm that the client should operate in. If not applicable, use empty string. + Realm string + + // The name to display for the application in the authentication popup. + AppName string } // URL presents the url pointing to API definition (normally swagger.json or swagger.yaml). @@ -67,6 +83,12 @@ func PersistAuthorization(persistAuthorization bool) func(*Config) { } } +func OAuth(config *OAuthConfig) func(*Config) { + return func(c *Config) { + c.OAuth = config + } +} + func newConfig(configFns ...func(*Config)) *Config { config := Config{ URL: "doc.json", @@ -250,6 +272,15 @@ window.onload = function() { ], layout: "StandaloneLayout" }) + + {{if .OAuth}} + ui.initOAuth({ + clientId: "{{.OAuth.ClientId}}", + realm: "{{.OAuth.Realm}}", + appName: "{{.OAuth.AppName}}" + }) + {{end}} + window.ui = ui } diff --git a/swagger_test.go b/swagger_test.go index 7f73a7b..5e42c94 100644 --- a/swagger_test.go +++ b/swagger_test.go @@ -236,7 +236,7 @@ func (s *mockedSwag) ReadDoc() string { func TestWrapHandler(t *testing.T) { router := echo.New() - router.Any("/*", EchoWrapHandler(DocExpansion("none"), DomID("#swagger-ui"))) + router.Any("/*", EchoWrapHandler(DocExpansion("none"), DomID("swagger-ui"))) w1 := performRequest(http.MethodGet, "/index.html", router) assert.Equal(t, http.StatusOK, w1.Code) @@ -277,6 +277,37 @@ func TestWrapHandler(t *testing.T) { } +func TestConfig(t *testing.T) { + router := echo.New() + + swaggerHandler := URL("swagger.json") + router.Any("/*", EchoWrapHandler(swaggerHandler)) + + w := performRequest("GET", "/index.html", router) + assert.Equal(t, 200, w.Code) + assert.Contains(t, w.Body.String(), `url: "swagger.json"`) +} + +func TestConfigWithOAuth(t *testing.T) { + router := echo.New() + + swaggerHandler := EchoWrapHandler(OAuth(&OAuthConfig{ + ClientId: "my-client-id", + Realm: "my-realm", + AppName: "My App Name", + })) + router.GET("/*", swaggerHandler) + + w := performRequest("GET", "/index.html", router) + assert.Equal(t, 200, w.Code) + body := w.Body.String() + assert.Contains(t, body, `ui.initOAuth({ + clientId: "my-client-id", + realm: "my-realm", + appName: "My App Name" + })`) +} + func TestHandlerReuse(t *testing.T) { router := echo.New() @@ -352,7 +383,7 @@ func TestDocExpansion(t *testing.T) { func TestDomID(t *testing.T) { var cfg Config - expected := "#swagger-ui" + expected := "swagger-ui" DomID(expected)(&cfg) assert.Equal(t, expected, cfg.DomID) } @@ -374,3 +405,23 @@ func TestPersistAuthorization(t *testing.T) { PersistAuthorization(expected)(&cfg) assert.Equal(t, expected, cfg.PersistAuthorization) } + +func TestOAuth(t *testing.T) { + var cfg Config + expected := OAuthConfig{ + ClientId: "my-client-id", + Realm: "my-realm", + AppName: "My App Name", + } + OAuth(&expected)(&cfg) + assert.Equal(t, expected.ClientId, cfg.OAuth.ClientId) + assert.Equal(t, expected.Realm, cfg.OAuth.Realm) + assert.Equal(t, expected.AppName, cfg.OAuth.AppName) +} + +func TestOAuthNil(t *testing.T) { + var cfg Config + var expected *OAuthConfig + OAuth(expected)(&cfg) + assert.Equal(t, expected, cfg.OAuth) +}