Class: J1WardenOmniAuth
- Inherits:
-
Object
- Object
- J1WardenOmniAuth
- Defined in:
- lib/j1_app/j1_auth_manager/warden_omniauth.rb
Overview
~/lib/j1_auth_manager/auth_manager/.rb
Provides Warden authentication strategy based on OmniAuth
Product/Info:
https://jekyll.one
Copyright (C) 2019 Juergen Adams
J1 Template is licensed under the MIT License.
See: https://github.com/jekyll-one-org/j1_template/blob/master/LICENSE
NOTES
Defined Under Namespace
Classes: Strategy
Constant Summary collapse
- DEFAULT_CALLBACK =
lambda do |user| u = {} u[:info] = user['info'] u[:uid] = user['uid'] u[:credentials] = user['credentials'] u[:provider] = user['provider'] u[:extra] = user['extra'] u end
- SCOPE_KEY =
'warden_omni_auth.scope'
- SESSION_KEY =
'rack.session'
Class Method Summary collapse
-
.on_callback(&blk) ⇒ Object
Setup a callback to transform the user from the OmniAuth user hash to what warden to store as the user object.
-
.setup_strategies(*names) ⇒ Object
Create a warden strategy to wrap OmniAuth strategies configured NOTE: Warden strategy is prefixed by ‘omni_’ for OmniAuth.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) {|_self| ... } ⇒ J1WardenOmniAuth
constructor
A new instance of J1WardenOmniAuth.
-
#redirect_after_callback=(path) ⇒ Object
redirect after a callback.
- #redirect_after_callback_path ⇒ Object
Constructor Details
#initialize(app) {|_self| ... } ⇒ J1WardenOmniAuth
Returns a new instance of J1WardenOmniAuth.
101 102 103 104 105 106 107 108 109 |
# File 'lib/j1_app/j1_auth_manager/warden_omniauth.rb', line 101 def initialize(app) # setup all warden strategies to wrap supported omniauth ones names = OmniAuth::Strategies.constants.map do |konstant| name = konstant.to_s.downcase end J1WardenOmniAuth.setup_strategies(*names) yield self if block_given? @app = app end |
Class Method Details
.on_callback(&blk) ⇒ Object
Setup a callback to transform the user from the OmniAuth user hash to what warden to store as the user object
42 43 44 45 |
# File 'lib/j1_app/j1_auth_manager/warden_omniauth.rb', line 42 def self.on_callback(&blk) @on_callback = blk if blk @on_callback || DEFAULT_CALLBACK end |
.setup_strategies(*names) ⇒ Object
Create a warden strategy to wrap OmniAuth strategies configured NOTE: Warden strategy is prefixed by ‘omni_’ for OmniAuth
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/j1_app/j1_auth_manager/warden_omniauth.rb', line 53 def self.setup_strategies(*names) names.map do |name| full_name = :"omni_#{name}" unless Warden::Strategies[full_name] klass = Class.new(J1WardenOmniAuth::Strategy) klass.omni_name = name Warden::Strategies.add(full_name, klass) end Warden::Strategies[full_name] end end |
Instance Method Details
#call(env) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/j1_app/j1_auth_manager/warden_omniauth.rb', line 120 def call(env) request = Rack::Request.new(env) prefix = OmniAuth::Configuration.instance.path_prefix if request.path =~ /^#{prefix}\/(.+?)\/callback$/i strategy_name = Regexp.last_match(1) strategy = Warden::Strategies._strategies.keys.detect { |k| k.to_s == "omni_#{strategy_name}" } if !strategy Rack::Response.new('Unknown Handler', 401).finish else # Warden needs to use a hash for looking up scope and strategy names session = env[SESSION_KEY] scope = session[SCOPE_KEY] if scope.nil? || scope.to_s.length < 100 # have to protect against symbols - need a hash args = [strategy] args << { scope: scope.to_sym } if scope response = Rack::Response.new if env['warden'].authenticate? *args response.redirect(redirect_after_callback_path) response.finish else auth_path = request.path.gsub(/\/callback$/, '') response.redirect(auth_path) response.finish end else Rack::Response.new('Bad Session', 400).finish end end else @app.call(env) end end |
#redirect_after_callback=(path) ⇒ Object
redirect after a callback
112 113 114 |
# File 'lib/j1_app/j1_auth_manager/warden_omniauth.rb', line 112 def redirect_after_callback=(path) @redirect_after_callback_path = path end |
#redirect_after_callback_path ⇒ Object
116 117 118 |
# File 'lib/j1_app/j1_auth_manager/warden_omniauth.rb', line 116 def redirect_after_callback_path @redirect_after_callback_path ||= '/' end |