This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
264 additions
and
210 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'castle/middleware/request_config' | ||
|
||
module Castle | ||
class Middleware | ||
class Authenticating | ||
extend Forwardable | ||
def_delegators :@middleware, :log, :configuration, :event_mapping, :authenticate | ||
|
||
attr_reader :app | ||
|
||
def initialize(app) | ||
@app = app | ||
@middleware = Middleware.instance | ||
end | ||
|
||
def call(env) | ||
env['castle'] = RequestConfig.new | ||
req = Rack::Request.new(env) | ||
|
||
if login?(req) | ||
byebug | ||
redirect_result = authentication_verdict(req, env) | ||
return [301, {'Location' => redirect_result}, []] if redirect_result | ||
end | ||
|
||
env['castle'].identify(req.session['castle_user_id'], {}) if req.session['castle_user_id'] | ||
|
||
# [status, headers, body] | ||
app.call(env) | ||
end | ||
|
||
private | ||
|
||
def authentication_verdict(req, env) | ||
key = req.params.dig(*configuration.login.dig('authentication', 'key').split('.')) | ||
pass = req.params.dig(*configuration.login.dig('authentication', 'password').split('.')) | ||
|
||
resource = configuration.services.provide_by_login_key.call(key) | ||
return if resource.nil? | ||
|
||
env['castle'].identify(resource.public_send(configuration.login['user_id']), {}) | ||
|
||
return unless configuration.services.validate_password.call(resource, pass) | ||
|
||
verdict = castle_authenticate(req, env) | ||
|
||
case verdict[:action] | ||
when 'allow' | ||
req.session['castle_user_id'] = env['castle'].user_id | ||
when 'challenge' | ||
redirect_result = configuration.challenge.call(req, resource) | ||
configuration.logout.call(req, env) | ||
redirect_result | ||
when 'deny' | ||
redirect_result = configuration.services.deny.call(req, resource) | ||
configuration.services.logout.call(req, env) | ||
redirect_result | ||
end | ||
end | ||
|
||
def castle_authenticate(req, env) | ||
authenticate( | ||
Castle::Client.to_context(req), | ||
Castle::Client.to_options( | ||
user_id: env['castle'].user_id, | ||
event: '$login.succeeded' | ||
) | ||
) | ||
end | ||
|
||
def login?(req) | ||
req.path == configuration.login['path'] && req.request_method == configuration.login['method'] && req.form_data? | ||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module Castle | ||
class Middleware | ||
# Configuration services (procs, lambdas) available to setup in configure block | ||
class ConfigurationServices | ||
%i[ | ||
error_handler | ||
transport | ||
deny | ||
challenge | ||
logout | ||
provide_by_id | ||
provide_by_login_key | ||
validate_password | ||
].each do |opt| | ||
attr_accessor opt | ||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module Castle | ||
class Middleware | ||
# Configuration services (procs, lambdas) available to setup in configure block | ||
class SecureHeaders | ||
def initialize | ||
@can_append_nonce = ::SecureHeaders.respond_to?(:content_security_policy_script_nonce) && | ||
defined?(::SecureHeaders::Configuration) && | ||
!::SecureHeaders::Configuration.get.csp.opt_out? && | ||
!::SecureHeaders::Configuration.get.current_csp[:script_src].to_a.include?("'unsafe-inline'") | ||
end | ||
|
||
def call(env) | ||
return unless @can_append_nonce | ||
nonce = ::SecureHeaders.content_security_policy_script_nonce(::Rack::Request.new(env)) | ||
" nonce=\"#{nonce}\"" | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.