Skip to content

Commit 1645af6

Browse files
committed
oauth and http authentication support.
1 parent f4503fa commit 1645af6

File tree

8 files changed

+139
-58
lines changed

8 files changed

+139
-58
lines changed

Controller/SwaggerUIController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public function indexAction(Request $request)
1414
{
1515
$docUrl = $this->get('service_container')->getParameter('al_swagger_ui.resource_list');
1616
$jsConfig = $this->get('service_container')->getParameter('al_swagger_ui.js_config');
17+
$authConfig = $this->get('service_container')->getParameter('al_swagger_ui.authentication_config');
1718

1819
if (preg_match('/^(https?:)?\/\//', $docUrl)) {
1920
// If https://..., http://..., or //...
@@ -31,6 +32,7 @@ public function indexAction(Request $request)
3132
return $this->render('ALSwaggerUIBundle:SwaggerUI:index.html.twig', array(
3233
'resource_list_url' => $url,
3334
'js_config' => $jsConfig,
35+
'auth_config' => $authConfig,
3436
));
3537
}
3638
}

DependencyInjection/ALSwaggerUIExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function load(array $configs, ContainerBuilder $container)
3131

3232
$container->setParameter('al_swagger_ui.static_resources_dir', $config['static_resources']['resource_dir']);
3333
$container->setParameter('al_swagger_ui.static_resource_list_filename', $config['static_resources']['resource_list_filename']);
34+
$container->setParameter('al_swagger_ui.authentication_config', $config['authentication']);
3435

3536
}
3637
}

DependencyInjection/Configuration.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,28 @@ public function getConfigTreeBuilder()
5050
->end()
5151
->end()
5252
->end()
53+
->arrayNode('authentication')
54+
->addDefaultsIfNotSet()
55+
->children()
56+
->arrayNode('oauth')
57+
->addDefaultsIfNotSet()
58+
->children()
59+
->booleanNode('enable')->defaultValue(false)->end()
60+
->scalarNode('client_id')->defaultValue(null)->end()
61+
->scalarNode('realm')->defaultValue(null)->end()
62+
->scalarNode('app_name')->defaultValue(null)->end()
63+
->end()
64+
->end()
65+
->arrayNode('http')
66+
->addDefaultsIfNotSet()
67+
->children()
68+
->booleanNode('enable')->defaultValue(false)->end()
69+
->scalarNode('key_name')->defaultValue(null)->end()
70+
->scalarNode('delivery')->defaultValue(null)->end()
71+
->end()
72+
->end()
73+
->end()
74+
->end()
5375
->end();
5476

5577
return $treeBuilder;

Resources/views/SwaggerUI/index.html.twig

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
{% extends "ALSwaggerUIBundle::layout.html.twig" %}
2-
{% block stylesheets %}
3-
{{ parent() }}
4-
<link href='https://fonts.googleapis.com/css?family=Droid+Sans:400,700' rel='stylesheet' type='text/css'/>
2+
{% block al_swagger_ui_stylesheets %}
3+
<link href='https://fonts.googleapis.com/css?family=Droid+Sans:400,700' rel='stylesheet' type='text/css'/>
4+
<link href="{{ asset('bundles/alswaggerui/css/reset.css') }}"
5+
type="text/css"
6+
rel="stylesheet"
7+
media="print" />
8+
<link href="{{ asset('bundles/alswaggerui/css/screen.css') }}"
9+
type="text/css"
10+
rel="stylesheet"
11+
media="screen" />
12+
<link href="{{ asset('bundles/alswaggerui/css/screen.css') }}"
13+
type="text/css"
14+
rel="stylesheet"
15+
media="print" />
516
{% endblock %}
6-
{% block javascripts %}
7-
{{ parent() }}
17+
{% block al_swagger_ui_javascripts %}
818
<script src="{{ asset('bundles/alswaggerui/lib/shred.bundle.js') }}"></script>
919
<script src="{{ asset('bundles/alswaggerui/lib/jquery-1.8.0.min.js') }}"></script>
1020
<script src="{{ asset('bundles/alswaggerui/lib/jquery.slideto.min.js') }}"></script>
@@ -16,49 +26,52 @@
1626
<script src="{{ asset('bundles/alswaggerui/lib/swagger.js') }}"></script>
1727
<script src="{{ asset('bundles/alswaggerui/js/swagger-ui.js') }}"></script>
1828
<script src="{{ asset('bundles/alswaggerui/lib/highlight.7.3.pack.js') }}"></script>
29+
{% if auth_config.oauth.enable %}
1930
<script src="{{ asset('bundles/alswaggerui/lib/swagger-oauth.js') }}"></script>
20-
<script type="text/javascript">
21-
$(function () {
22-
window.swaggerUi = new SwaggerUi({
23-
url: {{ resource_list_url|json_encode|raw }},
24-
dom_id: "swagger-ui-container",
25-
supportedSubmitMethods: {{ js_config.supported_submit_methods|json_encode|raw }},
26-
onComplete: function(swaggerApi, swaggerUi){
27-
log("Loaded SwaggerUI");
31+
{% endif %}
32+
<script type="text/javascript">
33+
$(function () {
34+
window.swaggerUi = new SwaggerUi({
35+
url: {{ resource_list_url|json_encode|raw }},
36+
dom_id: "swagger-ui-container",
37+
supportedSubmitMethods: {{ js_config.supported_submit_methods|json_encode|raw }},
38+
onComplete: function(swaggerApi, swaggerUi){
39+
log("Loaded SwaggerUI");
2840
29-
if(typeof initOAuth == "function") {
30-
/*
31-
initOAuth({
32-
clientId: "your-client-id",
33-
realm: "your-realms",
34-
appName: "your-app-name"
35-
});
36-
*/
37-
}
38-
$('pre code').each(function(i, e) {
39-
hljs.highlightBlock(e)
40-
});
41-
},
42-
onFailure: function(data) {
43-
log("Unable to Load SwaggerUI");
44-
},
45-
docExpansion: {{ js_config.expansion|json_encode|raw }},
46-
sorter: {{ js_config.sorter|json_encode|raw }},
47-
booleanValues: {{ js_config.boolean_values|json_encode|raw }},
48-
highlightSizeThreshold: {{ js_config.highlight_size_threshold|json_encode|raw }}
49-
});
41+
if(typeof initOAuth == "function") {
5042
51-
/* $('#input_apiKey').change(function() {
52-
var key = $('#input_apiKey')[0].value;
53-
log("key: " + key);
54-
if(key && key.trim() != "") {
55-
log("added key " + key);
56-
window.authorizations.add("key", new ApiKeyAuthorization("api_key", key, "query"));
43+
initOAuth({
44+
clientId: {{ auth_config.oauth.client_id|json_encode|raw }},
45+
realm: {{ auth_config.oauth.realm|json_encode|raw }},
46+
appName: {{ auth_config.oauth.app_name|json_encode|raw }}
47+
});
5748
}
58-
}) */
59-
window.swaggerUi.load();
49+
$('pre code').each(function(i, e) {
50+
hljs.highlightBlock(e)
51+
});
52+
},
53+
onFailure: function(data) {
54+
log("Unable to Load SwaggerUI");
55+
},
56+
docExpansion: {{ js_config.expansion|json_encode|raw }},
57+
sorter: {{ js_config.sorter|json_encode|raw }},
58+
booleanValues: {{ js_config.boolean_values|json_encode|raw }},
59+
highlightSizeThreshold: {{ js_config.highlight_size_threshold|json_encode|raw }}
6060
});
61-
</script>
61+
62+
{% if auth_config.http.enable %}
63+
$('#input_apiKey').change(function() {
64+
var key = $('#input_apiKey')[0].value;
65+
log("key: " + key);
66+
if(key && key.trim() != "") {
67+
log("added key " + key);
68+
window.authorizations.add("key", new ApiKeyAuthorization({{ auth_config.http.key_name|json_encode|raw }}, key, {{ auth_config.http.delivery|json_encode|raw }}));
69+
}
70+
});
71+
{% endif %}
72+
window.swaggerUi.load();
73+
});
74+
</script>
6275
{% endblock %}
6376
{% block al_swagger_ui_auth_form %}
6477
{% endblock %}

Resources/views/layout.html.twig

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,8 @@
77
type="text/css"
88
rel="stylesheet"
99
media="screen" />
10-
<link href="{{ asset('bundles/alswaggerui/css/reset.css') }}"
11-
type="text/css"
12-
rel="stylesheet"
13-
media="print" />
14-
<link href="{{ asset('bundles/alswaggerui/css/screen.css') }}"
15-
type="text/css"
16-
rel="stylesheet"
17-
media="screen" />
18-
<link href="{{ asset('bundles/alswaggerui/css/screen.css') }}"
19-
type="text/css"
20-
rel="stylesheet"
21-
media="print" />
10+
{% block al_swagger_ui_stylesheets %}
11+
{% endblock %}
2212
{% endblock %}
2313
</head>
2414
<body class="swagger-section">
@@ -32,6 +22,8 @@
3222
{% block al_swagger_ui_content %}
3323
{% endblock %}
3424
{% block javascripts %}
25+
{% block al_swagger_ui_javascripts %}
26+
{% endblock %}
3527
{% endblock %}
3628
</body>
3729
</html>

Tests/Application/config/config_external.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,11 @@ imports:
22
- { resource: config_test.yml }
33

44
al_swagger_ui:
5-
resource_list: http://petstore.swagger.wordnik.com/api/api-docs
5+
resource_list: http://petstore.swagger.wordnik.com/api/api-docs
6+
authentication:
7+
oauth:
8+
enable: false
9+
http:
10+
enable: true
11+
key_name: api_key
12+
delivery: header

Tests/Application/config/config_test.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@ al_swagger_ui:
2121
sorter: alpha
2222
highlight_size_threshold: 100
2323
boolean_values: [0, 1]
24-
24+
authentication:
25+
oauth:
26+
enable: true
27+
realm: foobar
28+
app_name: "ActiveLAMP Swagger UI"
29+
client_id: 8324737

Tests/Controller/SwaggerUIControllerTest.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,54 @@ public function testSwaggerUIPage()
2727
$this->assertRegExp('/highlightSizeThreshold:\s?100/', $content);
2828
}
2929

30+
public function testOauth()
31+
{
32+
$client = static::createClient();
33+
$crawler = $client->request('GET', '/documentation/');
34+
35+
$response = $client->getResponse();
36+
$this->assertEquals(200, $response->getStatusCode());
37+
38+
$this->assertCount(1, $crawler->filter('#message-bar'));
39+
$this->assertCount(1, $crawler->filter('#swagger-ui-container'));
40+
41+
$content = $response->getContent();
42+
$this->assertRegExp('/realm:\s?"foobar"/', $content);
43+
$this->assertRegExp('/initOAuth\({/', $content);
44+
$this->assertRegExp('/clientId:\s?8324737/', $content);
45+
$this->assertRegExp('/appName:\s?"ActiveLAMP Swagger UI"/', $content);
46+
$this->assertRegExp('/src="(.*)swagger-oauth.js"/', $content);
47+
$this->assertNotRegExp(
48+
'/window\.authorizations\.add\("key"/',
49+
$content
50+
);
51+
}
52+
3053
public function testExternalUrl()
3154
{
3255
$client = static::createClient(array('environment' => 'external'));
33-
$crawler = $client->request('GET', '/documentation/');
56+
$client->request('GET', '/documentation/');
3457

3558
$response = $client->getResponse();
3659
$this->assertEquals(200, $response->getStatusCode());
3760

3861
$content = $response->getContent();
3962
$this->assertRegExp('#url:\s?"http:\\\/\\\/petstore.swagger.wordnik.com\\\/api\\\/api-docs"#', $content);
4063
}
64+
65+
public function testHttpAuth()
66+
{
67+
$client = static::createClient(array('environment' => 'external'));
68+
$client->request('GET', '/documentation/');
69+
70+
$response = $client->getResponse();
71+
$this->assertEquals(200, $response->getStatusCode());
72+
73+
$content = $response->getContent();
74+
$this->assertRegExp(
75+
'/window\.authorizations\.add\("key", new ApiKeyAuthorization\("api_key", key, "header"\)\);/',
76+
$content
77+
);
78+
$this->assertNotRegExp('/src="(.*)swagger-oauth.js"/', $content);
79+
}
4180
}

0 commit comments

Comments
 (0)