Skip to content

Commit a764d6d

Browse files
authored
Merge pull request #86 from Jack12816/token-auth
Added the token api_auth type
2 parents ce96d4f + 9690010 commit a764d6d

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

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

67
### 0.3.0 (September 22, 2016)
78

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ GrapeSwaggerRails.options.api_key_name = 'api_token'
128128
GrapeSwaggerRails.options.api_key_type = 'query'
129129
```
130130

131+
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:
132+
133+
```
134+
Authorization: Token token="WCZZYjnOQFUYfJIN2ShH1iD24UHo58A6TI"
135+
```
136+
137+
by specify:
138+
139+
```ruby
140+
GrapeSwaggerRails.options.api_auth = 'token'
141+
GrapeSwaggerRails.options.api_key_name = 'Authorization'
142+
GrapeSwaggerRails.options.api_key_type = 'header'
143+
```
144+
131145
You can use the ```api_key``` input box to fill in your API token.
132146
### Swagger UI Authorization
133147

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@
5353
if (options.api_auth == 'basic') {
5454
key = "Basic " + Base64.encode(key);
5555
} else if (options.api_auth == 'bearer') {
56-
key = "Bearer " + key
56+
key = "Bearer " + key;
57+
} else if (options.api_auth == 'token') {
58+
key = 'Token token="' + key + '"';
5759
}
5860
return new SwaggerClient.ApiKeyAuthorization(options.api_key_name, key, options.api_key_type);
5961
}

spec/features/swagger_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,23 @@
9898
expect(page).to have_css 'span.hljs-string', text: 'Bearer token'
9999
end
100100
end
101+
context '#api_auth:token and #api_key_type:header' do
102+
before do
103+
GrapeSwaggerRails.options.api_auth = 'token'
104+
GrapeSwaggerRails.options.api_key_name = 'Authorization'
105+
GrapeSwaggerRails.options.api_key_type = 'header'
106+
visit '/swagger'
107+
end
108+
it 'adds an Authorization header' do
109+
page.execute_script("$('#input_apiKey').val('token')")
110+
page.execute_script("$('#input_apiKey').trigger('change')")
111+
find('#endpointListTogger_headers', visible: true).click
112+
first('span[class="http_method"] a', visible: true).click
113+
click_button 'Try it out!'
114+
expect(page).to have_css 'span.hljs-attr', text: 'Authorization'
115+
expect(page).to have_css 'span.hljs-string', text: 'Token token'
116+
end
117+
end
101118
context '#api_auth:token' do
102119
before do
103120
GrapeSwaggerRails.options.api_key_name = 'api_token'

0 commit comments

Comments
 (0)