196
196
* [markdown](#markdown)
197
197
* [endpoint_auth_wrapper](#endpoint_auth_wrapper)
198
198
* [swagger_endpoint_guard](#swagger_endpoint_guard)
199
- * [oauth_token ](#oauth_token )
199
+ * [token_owner ](#token_owner )
200
200
* [security_definitions](#security_definitions)
201
201
* [models](#models)
202
202
* [hide_documentation_path](#hide_documentation_path)
@@ -296,13 +296,13 @@ add_swagger_documentation \
296
296
swagger_endpoint_guard: ' oauth2 false '
297
297
```
298
298
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 ' .
302
302
303
303
```ruby
304
304
add_swagger_documentation \
305
- oauth_token : ' doorkeeper_access_token '
305
+ token_owner : ' resource_owner '
306
306
```
307
307
308
308
<a name="security_definitions" />
836
836
837
837
The Swagger UI on Grape could be secured from unauthorized access using any middleware, which provides certain methods:
838
838
839
- - a *before* method to be run in the Grape controller for authorization purpose;
840
839
- 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.
842
843
843
844
Below are some examples of securing the Swagger UI on Grape installed along with Ruby on Rails:
844
845
@@ -856,14 +857,14 @@ This is how to configure the grape_swagger documentation:
856
857
hide_format: true,
857
858
endpoint_auth_wrapper: WineBouncer::OAuth2, # This is the middleware for securing the Swagger UI
858
859
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
860
861
```
861
862
862
863
The guard method should inject the Security Requirement Object into the endpoint' s route settings (see Grape ::DSL ::Settings .route_setting method).
863
864
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
867
868
wouldn' t be displayed at all to unauthorized users.
868
869
869
870
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
886
887
users, you could pass a lambda to the :hidden key of a endpoint' s description:
887
888
888
889
```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? }
890
891
891
892
resource :users do
892
893
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:
897
898
end
898
899
```
899
900
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
901
902
role - only admins can see this endpoint.
902
903
903
904
<a name="md_usage" />
0 commit comments