-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Make auth middleware extendable #703
Merged
dblock
merged 1 commit into
ruby-grape:master
from
dspaeth-faber:probosal_for_auth_middelware_refactoring
Aug 6, 2014
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'rack/auth/basic' | ||
|
||
module Grape | ||
module Middleware | ||
module Auth | ||
module DSL | ||
# Add an authentication type to the API. Currently | ||
# only `:http_basic`, `:http_digest` are supported. | ||
def auth(type = nil, options = {}, &block) | ||
if type | ||
set(:auth, { type: type.to_sym, proc: block }.merge(options)) | ||
use Grape::Middleware::Auth::Base, settings[:auth] | ||
else | ||
settings[:auth] | ||
end | ||
end | ||
|
||
# Add HTTP Basic authorization to the API. | ||
# | ||
# @param [Hash] options A hash of options. | ||
# @option options [String] :realm "API Authorization" The HTTP Basic realm. | ||
def http_basic(options = {}, &block) | ||
options[:realm] ||= "API Authorization" | ||
auth :http_basic, options, &block | ||
end | ||
|
||
def http_digest(options = {}, &block) | ||
options[:realm] ||= "API Authorization" | ||
options[:opaque] ||= "secret" | ||
auth :http_digest, options, &block | ||
end | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Grape | ||
module Middleware | ||
module Auth | ||
module Strategies | ||
module_function | ||
|
||
def add(label, strategy, option_fetcher = ->(_) { [] }) | ||
auth_strategies[label] = StrategyInfo.new(strategy, option_fetcher) | ||
end | ||
|
||
def auth_strategies | ||
@auth_strategies ||= { | ||
http_basic: StrategyInfo.new(Rack::Auth::Basic, ->(settings) {[settings[:realm]] }), | ||
http_digest: StrategyInfo.new(Rack::Auth::Digest::MD5, ->(settings) { [settings[:realm], settings[:opaque]] }) | ||
} | ||
end | ||
|
||
def [](label) | ||
auth_strategies[label] | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Grape | ||
module Middleware | ||
module Auth | ||
StrategyInfo = Struct.new(:auth_class, :settings_fetcher) do | ||
|
||
def create(app, options, &block) | ||
strategy_args = settings_fetcher.call(options) | ||
|
||
auth_class.new(app, *strategy_args, &block) | ||
end | ||
|
||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the German
mittelware
:)More seriously, lets think about whether this auth-specific stuff can be taken out altogether?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One can't hide his heritage ;)
Warden uses it's strategy module to register new strategies.
Grape has no global config right?