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 #124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ type Config struct {
Layout SwaggerLayout
DefaultModelsExpandDepth ModelsExpandDepthType
ShowExtensions 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 @@ -113,6 +129,12 @@ func AfterScript(js string) func(*Config) {
}
}

func OAuth(config *OAuthConfig) func(*Config) {
return func(c *Config) {
c.OAuth = config
}
}

type SwaggerLayout string

const (
Expand Down Expand Up @@ -176,7 +198,6 @@ func newConfig(configFns ...func(*Config)) *Config {

// Handler wraps `http.Handler` into `http.HandlerFunc`.
func Handler(configFns ...func(*Config)) http.HandlerFunc {

config := newConfig(configFns...)

// create a template with name
Expand Down Expand Up @@ -334,6 +355,13 @@ window.onload = function() {
defaultModelsExpandDepth: {{.DefaultModelsExpandDepth}},
showExtensions: {{.ShowExtensions}}
})
{{if .OAuth}}
ui.initOAuth({
clientId: "{{.OAuth.ClientId}}",
realm: "{{.OAuth.Realm}}",
appName: "{{.OAuth.AppName}}"
})
{{end}}

window.ui = ui
{{- if .AfterScript}}
Expand Down
53 changes: 46 additions & 7 deletions swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func (s *mockedSwag) ReadDoc() string {
}

func TestWrapHandler(t *testing.T) {

tests := []struct {
RootFolder string
InstanceName string
Expand Down Expand Up @@ -110,6 +109,24 @@ func TestWrapHandler(t *testing.T) {
}
}

func TestConfigWithOAuth(t *testing.T) {
router := http.NewServeMux()
router.Handle("/", Handler(OAuth(&OAuthConfig{
ClientId: "my-client-id",
Realm: "my-realm",
AppName: "My App Name",
})))

w := performRequest(http.MethodGet, "/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 performRequest(method, target string, h http.Handler) *httptest.ResponseRecorder {
r := httptest.NewRequest(method, target, nil)
w := httptest.NewRecorder()
Expand Down Expand Up @@ -199,7 +216,6 @@ func TestPersistAuthorization(t *testing.T) {
}

func TestConfigURL(t *testing.T) {

type fixture struct {
desc string
cfgfn func(c *Config)
Expand Down Expand Up @@ -293,7 +309,6 @@ func TestConfigURL(t *testing.T) {
}

func TestUIConfigOptions(t *testing.T) {

type fixture struct {
desc string
cfg *Config
Expand Down Expand Up @@ -390,7 +405,7 @@ func TestUIConfigOptions(t *testing.T) {
DefaultModelsExpandDepth: ShowModel,
},
exp: `window.onload = function() {

const ui = SwaggerUIBundle({
url: "doc.json",
deepLinking: true ,
Expand All @@ -406,9 +421,11 @@ func TestUIConfigOptions(t *testing.T) {
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout",
defaultModelsExpandDepth: 1
defaultModelsExpandDepth: 1 ,
showExtensions: false
})


window.ui = ui
}`,
},
Expand Down Expand Up @@ -445,7 +462,7 @@ func TestUIConfigOptions(t *testing.T) {
// Some plugin
});


const ui = SwaggerUIBundle({
url: "swagger.json",
deepLinking: false ,
Expand All @@ -466,9 +483,11 @@ func TestUIConfigOptions(t *testing.T) {
onComplete: () => { window.ui.setBasePath('v3'); },
showExtensions: true,
layout: "StandaloneLayout",
defaultModelsExpandDepth: -1
defaultModelsExpandDepth: -1 ,
showExtensions: true
})


window.ui = ui
const someOtherCode = function(){
// Do something
Expand Down Expand Up @@ -572,3 +591,23 @@ func TestShowExtensions(t *testing.T) {
cfg = newConfig(ShowExtensions(false))
assert.False(t, cfg.ShowExtensions)
}

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