Skip to content

Commit b99c764

Browse files
authored
feat: add OAuth config and rework URL config (#25)
1 parent 80f04ba commit b99c764

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

swagger.go

+31
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ type Config struct {
2222
InstanceName string
2323
DeepLinking bool
2424
PersistAuthorization bool
25+
26+
// The information for OAuth2 integration, if any.
27+
OAuth *OAuthConfig
28+
}
29+
30+
// OAuthConfig stores configuration for Swagger UI OAuth2 integration. See
31+
// https://swagger.io/docs/open-source-tools/swagger-ui/usage/oauth2/ for further details.
32+
type OAuthConfig struct {
33+
// The ID of the client sent to the OAuth2 IAM provider.
34+
ClientId string
35+
36+
// The OAuth2 realm that the client should operate in. If not applicable, use empty string.
37+
Realm string
38+
39+
// The name to display for the application in the authentication popup.
40+
AppName string
2541
}
2642

2743
// URL presents the url pointing to API definition (normally swagger.json or swagger.yaml).
@@ -67,6 +83,12 @@ func PersistAuthorization(persistAuthorization bool) func(*Config) {
6783
}
6884
}
6985

86+
func OAuth(config *OAuthConfig) func(*Config) {
87+
return func(c *Config) {
88+
c.OAuth = config
89+
}
90+
}
91+
7092
func newConfig(configFns ...func(*Config)) *Config {
7193
config := Config{
7294
URL: "doc.json",
@@ -250,6 +272,15 @@ window.onload = function() {
250272
],
251273
layout: "StandaloneLayout"
252274
})
275+
276+
{{if .OAuth}}
277+
ui.initOAuth({
278+
clientId: "{{.OAuth.ClientId}}",
279+
realm: "{{.OAuth.Realm}}",
280+
appName: "{{.OAuth.AppName}}"
281+
})
282+
{{end}}
283+
253284
window.ui = ui
254285
}
255286
</script>

swagger_test.go

+53-2
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func (s *mockedSwag) ReadDoc() string {
236236
func TestWrapHandler(t *testing.T) {
237237
router := echo.New()
238238

239-
router.Any("/*", EchoWrapHandler(DocExpansion("none"), DomID("#swagger-ui")))
239+
router.Any("/*", EchoWrapHandler(DocExpansion("none"), DomID("swagger-ui")))
240240

241241
w1 := performRequest(http.MethodGet, "/index.html", router)
242242
assert.Equal(t, http.StatusOK, w1.Code)
@@ -277,6 +277,37 @@ func TestWrapHandler(t *testing.T) {
277277

278278
}
279279

280+
func TestConfig(t *testing.T) {
281+
router := echo.New()
282+
283+
swaggerHandler := URL("swagger.json")
284+
router.Any("/*", EchoWrapHandler(swaggerHandler))
285+
286+
w := performRequest("GET", "/index.html", router)
287+
assert.Equal(t, 200, w.Code)
288+
assert.Contains(t, w.Body.String(), `url: "swagger.json"`)
289+
}
290+
291+
func TestConfigWithOAuth(t *testing.T) {
292+
router := echo.New()
293+
294+
swaggerHandler := EchoWrapHandler(OAuth(&OAuthConfig{
295+
ClientId: "my-client-id",
296+
Realm: "my-realm",
297+
AppName: "My App Name",
298+
}))
299+
router.GET("/*", swaggerHandler)
300+
301+
w := performRequest("GET", "/index.html", router)
302+
assert.Equal(t, 200, w.Code)
303+
body := w.Body.String()
304+
assert.Contains(t, body, `ui.initOAuth({
305+
clientId: "my-client-id",
306+
realm: "my-realm",
307+
appName: "My App Name"
308+
})`)
309+
}
310+
280311
func TestHandlerReuse(t *testing.T) {
281312
router := echo.New()
282313

@@ -352,7 +383,7 @@ func TestDocExpansion(t *testing.T) {
352383

353384
func TestDomID(t *testing.T) {
354385
var cfg Config
355-
expected := "#swagger-ui"
386+
expected := "swagger-ui"
356387
DomID(expected)(&cfg)
357388
assert.Equal(t, expected, cfg.DomID)
358389
}
@@ -374,3 +405,23 @@ func TestPersistAuthorization(t *testing.T) {
374405
PersistAuthorization(expected)(&cfg)
375406
assert.Equal(t, expected, cfg.PersistAuthorization)
376407
}
408+
409+
func TestOAuth(t *testing.T) {
410+
var cfg Config
411+
expected := OAuthConfig{
412+
ClientId: "my-client-id",
413+
Realm: "my-realm",
414+
AppName: "My App Name",
415+
}
416+
OAuth(&expected)(&cfg)
417+
assert.Equal(t, expected.ClientId, cfg.OAuth.ClientId)
418+
assert.Equal(t, expected.Realm, cfg.OAuth.Realm)
419+
assert.Equal(t, expected.AppName, cfg.OAuth.AppName)
420+
}
421+
422+
func TestOAuthNil(t *testing.T) {
423+
var cfg Config
424+
var expected *OAuthConfig
425+
OAuth(expected)(&cfg)
426+
assert.Equal(t, expected, cfg.OAuth)
427+
}

0 commit comments

Comments
 (0)