Skip to content

Commit 51403e0

Browse files
author
Blair McKenzie
committed
customisation to filter swagger definition by authorization
1 parent 0029039 commit 51403e0

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

packages/api/base.cfc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,18 @@ component {
2626
var stSwagger = duplicate(application.fc.lib.api.swagger[request.req.handler.api]);
2727
var path = "";
2828
var method = "";
29+
var allowedTags = [];
30+
var i = 0;
2931

30-
/* CURRENT VERSION OF SWAGGER UI DOES NOT SUPPORT AUTHENTICATED SWAGGER LOADING
3132
for (path in stSwagger.paths) {
3233
for (method in stSwagger.paths[path]) {
3334
if (request.req.authentication eq "public") {
3435
structDelete(stSwagger.paths[path][method], "x-permission");
36+
arrayAppend(allowedTags, stSwagger.paths[path][method].tags);
3537
}
3638
else if (stSwagger.paths[path][method]["x-permission"] eq "public") {
3739
structDelete(stSwagger.paths[path][method], "x-permission");
40+
arrayAppend(allowedTags, stSwagger.paths[path][method].tags);
3841
}
3942
else if (not structKeyExists(request.req, "user")) {
4043
structDelete(stSwagger.paths[path], method);
@@ -44,9 +47,16 @@ component {
4447
}
4548
else {
4649
structDelete(stSwagger.paths[path][method], "x-permission");
50+
arrayAppend(allowedTags, stSwagger.paths[path][method].tags);
4751
}
4852
}
49-
}*/
53+
}
54+
55+
for (i=arrayLen(stSwagger.tags); i>0; i--) {
56+
if (not arrayFindNoCase(allowedTags, stSwagger.tags[i].name)) {
57+
arrayDeleteAt(stSwagger.tags, i);
58+
}
59+
}
5060

5161
setResponse(stSwagger);
5262
}

www/index.cfm

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,36 @@
7070
],
7171
plugins: [
7272
SwaggerUIBundle.plugins.DownloadUrl,
73-
SwaggerUIBundle.plugins.Topbar
73+
SwaggerUIBundle.plugins.Topbar,
74+
function (system) {
75+
window.configSystem = system;
76+
return {
77+
statePlugins: {
78+
auth: {
79+
wrapActions: {
80+
authorize: function(oriAction, system) {
81+
return function(a) {
82+
var r = oriAction(a);
83+
system.specActions.download();
84+
return r;
85+
}
86+
}
87+
}
88+
}
89+
}
90+
}
91+
}
7492
],
93+
requestInterceptor: function(e) {
94+
var token = window.configSystem.auth().getIn(["authorized"]);
95+
if (token != null && e.headers.authorization == null) {
96+
if (j.api_key)
97+
e.headers[j.api_key.schema.name] = j.api_key.value;
98+
if (j.basic)
99+
e.headers.Authorization = j.basic.value.header;
100+
}
101+
return e;
102+
},
75103
layout: "StandaloneLayout",
76104
})
77105

0 commit comments

Comments
 (0)