-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #703 from dspaeth-faber/probosal_for_auth_middelwa…
…re_refactoring Make auth middleware extendable
- Loading branch information
Showing
16 changed files
with
221 additions
and
380 deletions.
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.