Skip to content

Commit 817ee39

Browse files
committed
Merge pull request #23 from shinnyx/oauth-bearer
Add bearer support
2 parents cddd3ba + 3dcf99e commit 817ee39

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ You can specify additional headers to add to each request:
4040
GrapeSwaggerRails.options.headers['Special-Header'] = 'Some Secret Value'
4141
```
4242

43-
Using the `headers` option above, you could hard-code Basic Authentication credentials.
43+
Using the `headers` option above, you could hard-code Basic Authentication credentials.
4444
Alternatively, you can configure Basic Authentication through the UI, as described below.
4545

4646
### Basic Authentication
4747

4848
If your application uses Basic Authentication, you can setup Swagger to send the username and password to the server with each request to your API:
4949

5050
```ruby
51-
GrapeSwaggerRails.options.api_auth = 'basic'
51+
GrapeSwaggerRails.options.api_auth = 'basic' # Or 'bearer' for OAuth
5252
GrapeSwaggerRails.options.api_key_name = 'Authorization'
5353
GrapeSwaggerRails.options.api_key_type = 'header'
5454
```
@@ -57,7 +57,7 @@ Now you can specify the username and password to your API in the Swagger "API ke
5757

5858
username:password
5959

60-
The javascript that loads on the Swagger page automatically encodes the username and password and adds the authorization header to your API request.
60+
The javascript that loads on the Swagger page automatically encodes the username and password and adds the authorization header to your API request.
6161
See the official Swagger documentation about [Custom Header Parameters](https://github.com/wordnik/swagger-ui#custom-header-parameters---for-basic-auth-etc)
6262

6363
### API Token Authentication
@@ -72,7 +72,7 @@ GrapeSwaggerRails.options.api_key_type = 'query'
7272
You can use the ```api_key``` input box to fill in your API token.
7373
### Swagger UI Authorization
7474

75-
You may want to authenticate users before displaying the Swagger UI, particularly when the API is protected by Basic Authentication.
75+
You may want to authenticate users before displaying the Swagger UI, particularly when the API is protected by Basic Authentication.
7676
Use the `authenticate_with` option to inspect the request to the Swagger UI:
7777

7878
```ruby

app/views/grape_swagger_rails/application/index.html.erb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<script type="text/javascript">
99
$(function () {
1010
var options = $("html").data('swagger-options');
11-
11+
1212
window.swaggerUi = new SwaggerUi({
1313
url: options.app_url + options.url,
1414
dom_id:"swagger-ui-container",
@@ -34,10 +34,12 @@
3434

3535
$('#input_apiKey').change(function() {
3636
var key = $('#input_apiKey')[0].value;
37-
37+
3838
if(key && key.trim() != "") {
3939
if (options.api_auth == 'basic') {
4040
key = "Basic " + Base64.encode(key);
41+
} else if (options.api_auth == 'bearer') {
42+
key = "Bearer " + key
4143
}
4244
window.authorizations.add("key", new ApiKeyAuthorization(options.api_key_name, key, options.api_key_type));
4345
} else {

lib/grape-swagger-rails.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,22 @@ def authenticate_with(&block)
66
self.authentication_proc = block
77
end
88
end
9-
9+
1010
mattr_accessor :options
11-
11+
1212
self.options = Options.new(
13-
13+
1414
url: '/swagger_doc.json',
1515
app_name: 'Swagger',
1616
app_url: 'http://swagger.wordnik.com',
17-
17+
1818
headers: {},
19-
20-
api_auth: '', # 'basic'
19+
20+
api_auth: '', # 'basic' or 'bearer'
2121
api_key_name: 'api_key', # 'Authorization'
2222
api_key_type: 'query', # 'header'
23-
23+
2424
authentication_proc: nil # Proc used as a controller before filter that returns a boolean
2525
)
26-
27-
end
2826

27+
end

0 commit comments

Comments
 (0)