Skip to content

Commit

Permalink
Merge c946bd0 into 80f04ba
Browse files Browse the repository at this point in the history
  • Loading branch information
drewsilcock authored May 30, 2022
2 parents 80f04ba + c946bd0 commit ad70f0b
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
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)
}

0 comments on commit ad70f0b

Please sign in to comment.