Skip to content

Commit 161c6e3

Browse files
committed
Swagger UI endpoint authorization.
1 parent 053713c commit 161c6e3

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

lib/grape-swagger.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ def add_swagger_documentation(options = {})
3232
options = { target_class: self }.merge(options)
3333
@target_class = options[:target_class]
3434

35+
use options[:endpoint_auth_wrapper] if !options[:endpoint_auth_wrapper].nil? && options[:endpoint_auth_wrapper].method_defined?(:before)
36+
3537
documentation_class.setup(options)
3638
mount(documentation_class)
3739

lib/grape-swagger/doc_methods.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ def setup(options)
4141
end if options[:format]
4242
# getting of the whole swagger2.0 spec file
4343
desc api_doc.delete(:desc), api_doc
44+
45+
unless options[:swagger_endpoint_guard].nil?
46+
send(options[:swagger_endpoint_guard].split.first.to_sym, *options[:swagger_endpoint_guard].split(/[\s,]+/).drop(1))
47+
end
48+
4449
get mount_path do
4550
header['Access-Control-Allow-Origin'] = '*'
4651
header['Access-Control-Request-Method'] = '*'
@@ -104,7 +109,10 @@ def defaults
104109
authorizations: nil,
105110
security_definitions: nil,
106111
api_documentation: { desc: 'Swagger compatible API description' },
107-
specific_api_documentation: { desc: 'Swagger compatible API description for specific API' }
112+
specific_api_documentation: { desc: 'Swagger compatible API description for specific API' },
113+
endpoint_auth_wrapper: nil,
114+
swagger_endpoint_guard: nil,
115+
oauth_token: nil
108116
}
109117
end
110118

lib/grape-swagger/endpoint.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def add_definitions_from(models)
8484
# path object
8585
def path_item(routes, options)
8686
routes.each do |route|
87-
next if hidden?(route)
87+
next if hidden?(route, options)
8888

8989
@item, path = GrapeSwagger::DocMethods::PathString.build(route, options)
9090
@entity = route.entity || route.options[:success]
@@ -282,10 +282,14 @@ def model_name(name)
282282
name.respond_to?(:name) ? name.name.demodulize.camelize : name.split('::').last
283283
end
284284

285-
def hidden?(route)
285+
def hidden?(route, options)
286286
route_hidden = route.options[:hidden]
287-
route_hidden = route_hidden.call if route_hidden.is_a?(Proc)
288-
route_hidden
287+
return route_hidden unless route_hidden.is_a?(Proc)
288+
if !options[:oauth_token].nil?
289+
route_hidden.call(send(options[:oauth_token].to_sym))
290+
else
291+
route_hidden.call
292+
end
289293
end
290294

291295
def public_parameter?(param)

0 commit comments

Comments
 (0)