Skip to content
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

Add HTTP Basic Authentication #95

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ GrapeSwaggerRails.options.validator_url = nil
Using the `headers` option above, you could hard-code Basic Authentication credentials.
Alternatively, you can configure Basic Authentication through the UI, as described below.

### HTTP Basic Authentication
If you want to use HTTP Basic Authentication add:

```ruby
GrapeSwaggerRails.options.http_base_auth_name = 'admin'
GrapeSwaggerRails.options.http_base_auth_password = 'admin'
```

### Basic Authentication

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:
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/grape_swagger_rails/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
module GrapeSwaggerRails
class ApplicationController < ActionController::Base

# HTTP Base Auth
if GrapeSwaggerRails.options.http_base_auth_name.present? && GrapeSwaggerRails.options.http_base_auth_password.present?
http_basic_authenticate_with name: GrapeSwaggerRails.options.http_base_auth_name,
password: GrapeSwaggerRails.options.http_base_auth_password
end

if Rails::VERSION::MAJOR >= 4
before_action { run_before_action }
else
Expand Down
6 changes: 5 additions & 1 deletion lib/grape-swagger-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def before_action(&block)
before_action_proc: nil, # Proc used as a controller before action

hide_url_input: false,
hide_api_key_input: false
hide_api_key_input: false,

# HTTP Base Auth
http_base_auth_name: '',
http_base_auth_password: ''
)
end