Skip to content

Instantly share code, notes, and snippets.

@aiwilliams
Last active December 15, 2015 00:39
Show Gist options
  • Save aiwilliams/5174567 to your computer and use it in GitHub Desktop.
Save aiwilliams/5174567 to your computer and use it in GitHub Desktop.
Using Warden and Grape? You'll need some code to configure the Warden::Manager and install the Warden::Proxy in the Rack env. For Rails, this is typically done using Devise or rails_warden, and then you'll need some helper methods in your Grape::API, similar to those found in http://github.com/hassox/rails_warden/blob/master/lib/rails_warden/con…
# Provide access to the Warden::Proxy in the Rack env by including this module in your Grape::API:
#
# helpers Api::Helpers::Warden
#
# These methods require that something has configured the Warden::Manager, and
# the upstream middleware is in place to make the Warden::Proxy exist in the
# env! In a Rails app, this is typically done by Devise or rails_warden.
#
module Api::Helpers::Warden
# Answers the Warden::Proxy from the Rack env, installing the params from the
# Grape route into the Warden request params when we request the
# Warden::Proxy.
#
# routes.rb
# mount Twitter::API => '/'
#
# twitter/api.rb
# class Twitter::API < Grape::API
# resource 'cars/:car_id/parts' do
# end
# end
#
# When Grape is used in Rails, the
# env['action_dispatch.request.path_parameters'] does not include the
# :car_id, nor does the env['warden'].request.params. Grape::Endpoint will
# end up with it's own params hash, having the :app_id in it. This isn't a
# problem unless your Warden::Strategies use the route segment :car_id.
#
def warden
unless @warden
@warden = env['warden']
@warden.request.params.merge! params
end
@warden
end
def authenticate!
warden.authenticate! scope: :api
end
def authenticate
warden.authenticate scope: :api
end
def current_user
@current_user ||= authenticate
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment