Skip to content

Added the token api_auth type #86

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

Merged
merged 1 commit into from
Feb 4, 2019
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Your contribution here.
* [#82](https://github.com/ruby-grape/grape-swagger-rails/pull/82): Fixed api_key_default_value - [@konto-andrzeja](https://github.com/konto-andrzeja).
* [#84](https://github.com/ruby-grape/grape-swagger-rails/pull/84): Added the token api_auth type - [@Jack12816](https://github.com/Jack12816).

### 0.3.0 (September 22, 2016)

Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ GrapeSwaggerRails.options.api_key_name = 'api_token'
GrapeSwaggerRails.options.api_key_type = 'query'
```

If your application used token authentication passed as a header, like Rails does (`authenticate_or_request_with_http_token`), you can configure Swagger to send the token in this form:

```
Authorization: Token token="WCZZYjnOQFUYfJIN2ShH1iD24UHo58A6TI"
```

by specify:

```ruby
GrapeSwaggerRails.options.api_auth = 'token'
GrapeSwaggerRails.options.api_key_name = 'Authorization'
GrapeSwaggerRails.options.api_key_type = 'header'
```

You can use the ```api_key``` input box to fill in your API token.
### Swagger UI Authorization

Expand Down
4 changes: 3 additions & 1 deletion app/views/grape_swagger_rails/application/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@
if (options.api_auth == 'basic') {
key = "Basic " + Base64.encode(key);
} else if (options.api_auth == 'bearer') {
key = "Bearer " + key
key = "Bearer " + key;
} else if (options.api_auth == 'token') {
key = 'Token token="' + key + '"';
}
return new SwaggerClient.ApiKeyAuthorization(options.api_key_name, key, options.api_key_type);
}
Expand Down
17 changes: 17 additions & 0 deletions spec/features/swagger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@
expect(page).to have_css 'span.hljs-string', text: 'Bearer token'
end
end
context '#api_auth:token and #api_key_type:header' do
before do
GrapeSwaggerRails.options.api_auth = 'token'
GrapeSwaggerRails.options.api_key_name = 'Authorization'
GrapeSwaggerRails.options.api_key_type = 'header'
visit '/swagger'
end
it 'adds an Authorization header' do
page.execute_script("$('#input_apiKey').val('token')")
page.execute_script("$('#input_apiKey').trigger('change')")
find('#endpointListTogger_headers', visible: true).click
first('span[class="http_method"] a', visible: true).click
click_button 'Try it out!'
expect(page).to have_css 'span.hljs-attr', text: 'Authorization'
expect(page).to have_css 'span.hljs-string', text: 'Token token'
end
end
context '#api_auth:token' do
before do
GrapeSwaggerRails.options.api_key_name = 'api_token'
Expand Down