Skip to content
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

Send $login event fo filter #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/castle_devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def castle
require_relative "castle_devise/hooks/castle_protectable"
require_relative "castle_devise/models/castle_protectable"
require_relative "castle_devise/patches/registrations_controller"
require_relative "castle_devise/patches/sessions_controller"

require_relative "castle_devise/rails"

Expand Down
1 change: 1 addition & 0 deletions lib/castle_devise/patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Patches
class << self
def apply
Devise::RegistrationsController.send(:include, Patches::RegistrationsController)
Devise::SessionsController.send(:include, Patches::SessionsController)
end
end
end
Expand Down
30 changes: 30 additions & 0 deletions lib/castle_devise/patches/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module CastleDevise
module Patches
module SessionsController
extend ActiveSupport::Concern

included do
before_action :castle_filter, only: :create
madejejej marked this conversation as resolved.
Show resolved Hide resolved
end

def castle_filter
response = CastleDevise.sdk_facade.filter(
event: "$login",
context: CastleDevise::Context.from_rack_env(request.env)
)

case response.dig(:policy, :action)
when "deny"
throw(:warden, scope: resource_name, message: :not_found_in_database)
else
# everything fine, continue
end
rescue Castle::Error => e

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of rescuing the error, should we default the failover in the gem? This would allow implementer of the gem to catch and handle the error themselves.

https://github.com/castle/castle-ruby/blob/108e9174af109d18d667979a9fa7b16017b779c9/lib/castle/api/filter.rb#L21-L29

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense! My concern is that the SDK doesn't really handle all the errors. WDYT we should do in case we get one ofCastle::APIErrors that are not handled? https://github.com/castle/castle-ruby/blob/108e9174af109d18d667979a9fa7b16017b779c9/lib/castle/errors.rb#L29

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let the implementer decide! At least that's what makes the most sense to me at least. And that seems more like a bug in the underlying logic of castle-ruby but that's my interpretation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @bartes could you chime in here as well? Why do we handle only some errors in the Ruby SDK?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What errors does the SDK do not cover ? I think it covers those which are related to performing the request to the api (at least it should) ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One case where the Exception doesn't get caught is InvalidParams. I'm not sure that I'd want to swallow that error though because in that case the implementer would never really know their request was invalid. I'm not sure about other cases.

# log API errors and allow
logger.info "#{e}: #{e.message}"
end
end
end
end
71 changes: 68 additions & 3 deletions spec/castle_devise/integration/login_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,72 @@

before do
allow(CastleDevise).to receive(:sdk_facade).and_return(facade)
allow(facade).to receive(:risk).and_return(castle_response)
allow(facade).to receive(:risk).and_return(castle_risk_response)
allow(facade).to receive(:filter).and_return(castle_filter_response)
end

describe "with correct password" do
let(:castle_response) do
context "when filter denies" do
let(:castle_filter_response) do
{
risk: 0.92,
signals: {},
policy: {
action: "deny",
name: "My Policy",
id: "e14c5a8d-c682-4a22-bbca-04fa6b98ad0c",
revision_id: "b5cf794e-88c0-426e-8276-037ba1e7ceca"
},
}
end
let(:castle_risk_response) { double }

before do
post "/users/sign_in",
params: {
user: {email: user.email, password: "123456"},
castle_request_token: "token123"
}
end

it "calls the facade with valid arguments" do
expect(facade).to have_received(:filter) do |event:, context:|
expect(event).to eq("$login")
expect(context).to be_a(CastleDevise::Context)
end
end

it "does not call risk" do
expect(facade).not_to have_received(:risk)
end

it "does not authenticate the user" do
expect(request.env["warden"].user(:user)).to be_nil
end

it "sets a flash message" do
expect(flash.alert).to match(/invalid email or password/i)
end

it "redirects to sign_in path" do
expect(response).to redirect_to('/users/sign_in')
end
end

context "with correct password" do
let(:castle_filter_response) do
{
risk: 0.32,
signals: {},
policy: {
action: "allow",
name: "My Policy",
id: "e14c5a8d-c682-4a22-bbca-04fa6b98ad0c",
revision_id: "b5cf794e-88c0-426e-8276-037ba1e7ceca"
},
madejejej marked this conversation as resolved.
Show resolved Hide resolved
}
end

let(:castle_risk_response) do
{
risk: 0.4,
signals: {},
Expand Down Expand Up @@ -90,6 +151,10 @@
it "sets a flash message" do
expect(flash.alert).to match(/invalid email or password/i)
end

it "redirects to sign_in path" do
expect(response).to redirect_to('/users/sign_in')
end
end
end
end