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

Add OAuth config and rework URL config #25

Merged
merged 10 commits into from
May 30, 2022
31 changes: 31 additions & 0 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
}
</script>
Expand Down
55 changes: 53 additions & 2 deletions swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}