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

OAuth2 support #43

Merged
merged 3 commits into from
Nov 23, 2016
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
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ end
```

## Authorization ##
### API Key auth ###

Currently, SwaggerYard only supports API Key auth descriptions. Start by adding `@authorization` to your `ApplicationController`.
SwaggerYard supports API Key auth descriptions. Start by adding `@authorization` to your `ApplicationController`.

```ruby
#
Expand All @@ -177,6 +178,28 @@ class PetController < ApplicationController
end
```

### Custom security definitions (OAuth2) ###

Additionally SwaggerYard also supports custom [security definitions](http://swagger.io/specification/#securityDefinitionsObject). You can define these in your configuration like:

```ruby
SwaggerYard.configure do |config|
config.security_definitions['petstore_oauth'] = {
type: "oauth2",
authorizationUrl: "http://swagger.io/api/oauth/dialog",
flow: :implicit
}
end
```

Then you can also use these authorizations from your controller or actions in a controller.

```ruby
# @authorize_with petstore_oauth
class PetController < ApplicationController
end
```

## UI ##

We suggest using something like [swagger-ui_rails](https://github.com/3scale/swagger-ui_rails/tree/dev-2.1.3) for your UI needs inside of Rails.
Expand Down
2 changes: 2 additions & 0 deletions lib/swagger_yard/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Configuration
attr_accessor :enable, :reload
attr_accessor :controller_path, :model_path
attr_accessor :path_discovery_function
attr_accessor :security_definitions

def initialize
self.swagger_version = "2.0"
Expand All @@ -14,6 +15,7 @@ def initialize
self.reload = true
self.title = "Configure title with SwaggerYard.config.title"
self.description = "Configure description with SwaggerYard.config.description"
self.security_definitions = {}
end

def swagger_spec_base_path=(ignored)
Expand Down
5 changes: 4 additions & 1 deletion lib/swagger_yard/resource_listing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ def model_objects
end

def security_objects
Hash[authorizations.map {|auth| [auth.name, auth.to_h]}]
controllers # triggers controller parsing in case it did not happen before
SwaggerYard.config.security_definitions.merge(
Hash[authorizations.map {|auth| [auth.name, auth.to_h]}]
)
end

private
Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/resource_listing/goodbye_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# @resource Farewell
#
# @authorization [api_key] header X-APPLICATION-API-KEY
class FarewellController
# @path [GET] /goodbye
def index
Expand Down
15 changes: 15 additions & 0 deletions spec/lib/swagger_yard/resource_listing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,19 @@ def show

expect(hash['paths'].keys).to contain_exactly('/bonjour', '/goodbye')
end

context '#security_objects' do
before { SwaggerYard.config.security_definitions = security_definitions }

let (:security_definitions) { {key: "value"} }

it 'contains constructors authorizations' do
actual_security_object = multi_resource_listing.security_objects
expected_security_object = {"type" => "apiKey", "name" => "X-APPLICATION-API-KEY", "in" => "header"}
expect(actual_security_object).to include("header_x_application_api_key" => expected_security_object)
end
it 'merges config authorizations' do
expect(multi_resource_listing.security_objects).to include(security_definitions)
end
end
end