Skip to content

Commit b160428

Browse files
committed
Merge branch 'master' of https://github.com/ruby-grape/grape-swagger into bugfix/param-types-definition
2 parents 885a9ef + e0037fc commit b160428

File tree

5 files changed

+24
-18
lines changed

5 files changed

+24
-18
lines changed

CHANGELOG.md

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

33
#### Features
44

5+
* [#510](https://github.com/ruby-grape/grape-swagger/pull/510): Use 'token_owner' instead of 'oauth_token' on Swagger UI endpoint authorization. - [@texpert](https://github.com/texpert).
56
* Your contribution here.
67

78
#### Fixes

README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ end
196196
* [markdown](#markdown)
197197
* [endpoint_auth_wrapper](#endpoint_auth_wrapper)
198198
* [swagger_endpoint_guard](#swagger_endpoint_guard)
199-
* [oauth_token](#oauth_token)
199+
* [token_owner](#token_owner)
200200
* [security_definitions](#security_definitions)
201201
* [models](#models)
202202
* [hide_documentation_path](#hide_documentation_path)
@@ -296,13 +296,13 @@ add_swagger_documentation \
296296
swagger_endpoint_guard: 'oauth2 false'
297297
```
298298
299-
<a name="oauth_token" />
300-
#### oauth_token:
301-
Specify the method to get the oauth_token, provided by the middleware.
299+
<a name="token_owner" />
300+
#### token_owner:
301+
Specify the token_owner method, provided by the middleware, which is typically named 'resource_owner'.
302302
303303
```ruby
304304
add_swagger_documentation \
305-
oauth_token: 'doorkeeper_access_token'
305+
token_owner: 'resource_owner'
306306
```
307307
308308
<a name="security_definitions" />
@@ -836,9 +836,10 @@ end
836836
837837
The Swagger UI on Grape could be secured from unauthorized access using any middleware, which provides certain methods:
838838
839-
- a *before* method to be run in the Grape controller for authorization purpose;
840839
- some guard method, which could receive as argument a string or an array of authorization scopes;
841-
- a method which processes and returns the access token received in the HTTP request headers (usually in the 'HTTP_AUTHORIZATION' header).
840+
- a *before* method to be run in the Grape controller for authorization purpose;
841+
- a set of methods which will process the access token received in the HTTP request headers (usually in the
842+
'HTTP_AUTHORIZATION' header) and try to return the owner of the token.
842843
843844
Below are some examples of securing the Swagger UI on Grape installed along with Ruby on Rails:
844845
@@ -856,14 +857,14 @@ This is how to configure the grape_swagger documentation:
856857
hide_format: true,
857858
endpoint_auth_wrapper: WineBouncer::OAuth2, # This is the middleware for securing the Swagger UI
858859
swagger_endpoint_guard: 'oauth2 false', # this is the guard method and scope
859-
oauth_token: 'doorkeeper_access_token' # This is the method returning the access_token
860+
token_owner: 'resource_owner' # This is the method returning the owner of the token
860861
```
861862
862863
The guard method should inject the Security Requirement Object into the endpoint's route settings (see Grape::DSL::Settings.route_setting method).
863864

864-
The 'oauth2 false' added to swagger_documentation is making the main Swagger endpoint protected with OAuth, i.e. it
865-
is retreiving the access_token from the HTTP request, but the 'false' scope is for skipping authorization and showing
866-
the UI for everyone. If the scope would be set to something else, like 'oauth2 admin', for example, than the UI
865+
The 'oauth2 false' added to swagger_documentation is making the main Swagger endpoint protected with OAuth, i.e. the
866+
access_token is being retreiving from the HTTP request, but the 'false' scope is for skipping authorization and
867+
showing the UI for everyone. If the scope would be set to something else, like 'oauth2 admin', for example, than the UI
867868
wouldn't be displayed at all to unauthorized users.
868869
869870
Further on, the guard could be used, where necessary, for endpoint access protection. Put it prior to the endpoint's method:
@@ -886,7 +887,7 @@ And, finally, if you want to not only restrict the access, but to completely hid
886887
users, you could pass a lambda to the :hidden key of a endpoint's description:
887888
888889
```ruby
889-
not_admins = lambda { |token=nil| token.nil? || !User.find(token.resource_owner_id).admin? }
890+
not_admins = lambda { |token_owner = nil| token_owner.nil? || !token_owner.admin? }
890891
891892
resource :users do
892893
desc 'Create user', hidden: not_admins
@@ -897,7 +898,7 @@ users, you could pass a lambda to the :hidden key of a endpoint's description:
897898
end
898899
```
899900
900-
The lambda is checking whether the user is authenticated (if not, the token is nil by default), and has the admin
901+
The lambda is checking whether the user is authenticated (if not, the token_owner is nil by default), and has the admin
901902
role - only admins can see this endpoint.
902903
903904
<a name="md_usage" />

lib/grape-swagger/doc_methods.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def defaults
9797
specific_api_documentation: { desc: 'Swagger compatible API description for specific API' },
9898
endpoint_auth_wrapper: nil,
9999
swagger_endpoint_guard: nil,
100-
oauth_token: nil
100+
token_owner: nil
101101
}
102102
end
103103

lib/grape-swagger/endpoint.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def model_name(name)
303303
def hidden?(route, options)
304304
route_hidden = route.options[:hidden]
305305
return route_hidden unless route_hidden.is_a?(Proc)
306-
options[:oauth_token] ? route_hidden.call(send(options[:oauth_token].to_sym)) : route_hidden.call
306+
options[:token_owner] ? route_hidden.call(send(options[:token_owner].to_sym)) : route_hidden.call
307307
end
308308

309309
def public_parameter?(param)

spec/swagger_v2/guarded_endpoint_spec.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ def access_token
2121
def access_token=(token)
2222
@_access_token = token
2323
end
24+
25+
def resource_owner
26+
@resource_owner = true if access_token == '12345'
27+
end
2428
end
2529

2630
def context
@@ -50,9 +54,9 @@ def sample_auth(*scopes)
5054
describe 'a guarded api endpoint' do
5155
before :all do
5256
class GuardedMountedApi < Grape::API
53-
access_token_valid = proc { |token = nil| token.nil? || token != '12345' }
57+
resource_owner_valid = proc { |token_owner = nil| token_owner.nil? }
5458

55-
desc 'Show endpoint if authenticated', hidden: access_token_valid
59+
desc 'Show endpoint if authenticated', hidden: resource_owner_valid
5660
get '/auth' do
5761
{ foo: 'bar' }
5862
end
@@ -62,7 +66,7 @@ class GuardedApi < Grape::API
6266
mount GuardedMountedApi
6367
add_swagger_documentation endpoint_auth_wrapper: SampleAuth,
6468
swagger_endpoint_guard: 'sample_auth false',
65-
oauth_token: 'access_token'
69+
token_owner: 'resource_owner'
6670
end
6771
end
6872

0 commit comments

Comments
 (0)