-
Notifications
You must be signed in to change notification settings - Fork 120
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
Create classes for tracking Attempts API events #11716
Open
mitchellhenke
wants to merge
3
commits into
main
Choose a base branch
from
mitchellhenke/attempts-api-tracker
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# frozen_string_literal: true | ||
|
||
module AttemptsApi | ||
class Tracker | ||
attr_reader :session_id, :enabled_for_session, :request, :user, :sp, :cookie_device_uuid, | ||
:sp_request_uri, :analytics | ||
|
||
def initialize(session_id:, request:, user:, sp:, cookie_device_uuid:, | ||
sp_request_uri:, enabled_for_session:, analytics:) | ||
@session_id = session_id | ||
@request = request | ||
@user = user | ||
@sp = sp | ||
@cookie_device_uuid = cookie_device_uuid | ||
@sp_request_uri = sp_request_uri | ||
@enabled_for_session = enabled_for_session | ||
@analytics = analytics | ||
end | ||
include TrackerEvents | ||
|
||
def track_event(event_type, metadata = {}) | ||
return unless enabled? | ||
|
||
if metadata.has_key?(:failure_reason) && | ||
(metadata[:failure_reason].blank? || | ||
metadata[:success].present?) | ||
metadata.delete(:failure_reason) | ||
end | ||
|
||
event_metadata = { | ||
user_agent: request&.user_agent, | ||
unique_session_id: hashed_session_id, | ||
user_uuid: sp && AgencyIdentityLinker.for(user: user, service_provider: sp)&.uuid, | ||
device_fingerprint: hashed_cookie_device_uuid, | ||
user_ip_address: request&.remote_ip, | ||
application_url: sp_request_uri, | ||
client_port: CloudFrontHeaderParser.new(request).client_port, | ||
} | ||
|
||
event_metadata.merge!(metadata) | ||
|
||
event = AttemptEvent.new( | ||
event_type: event_type, | ||
session_id: session_id, | ||
occurred_at: Time.zone.now, | ||
event_metadata: event_metadata, | ||
) | ||
|
||
jwe = event.to_jwe( | ||
issuer: sp.issuer, | ||
public_key: sp.ssl_certs.first.public_key, | ||
) | ||
|
||
redis_client.write_event( | ||
event_key: event.jti, | ||
jwe: jwe, | ||
timestamp: event.occurred_at, | ||
issuer: sp.issuer, | ||
) | ||
|
||
event | ||
end | ||
|
||
def parse_failure_reason(result) | ||
return result.to_h[:error_details] || result.errors.presence | ||
end | ||
|
||
private | ||
|
||
def hashed_session_id | ||
return nil unless user&.unique_session_id | ||
Digest::SHA1.hexdigest(user&.unique_session_id) | ||
mitchellhenke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
def hashed_cookie_device_uuid | ||
return nil unless cookie_device_uuid | ||
Digest::SHA1.hexdigest(cookie_device_uuid) | ||
end | ||
|
||
def enabled? | ||
IdentityConfig.store.attempts_api_enabled && @enabled_for_session | ||
end | ||
|
||
def redis_client | ||
@redis_client ||= AttemptsApi::RedisClient.new | ||
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,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module AttemptsApi | ||
module TrackerEvents | ||
# @param [String] email The submitted email address | ||
# @param [Boolean] success True if the email and password matched | ||
# A user has submitted an email address and password for authentication | ||
def email_and_password_auth(email:, success:) | ||
track_event( | ||
'login-email-and-password-auth', | ||
email: email, | ||
success: success, | ||
) | ||
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,150 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe AttemptsApi::Tracker do | ||
before do | ||
allow(IdentityConfig.store).to receive(:attempts_api_enabled) | ||
.and_return(attempts_api_enabled) | ||
allow(request).to receive(:user_agent).and_return('example/1.0') | ||
allow(request).to receive(:remote_ip).and_return('192.0.2.1') | ||
allow(request).to receive(:headers).and_return( | ||
{ 'CloudFront-Viewer-Address' => '192.0.2.1:1234' }, | ||
) | ||
end | ||
|
||
let(:attempts_api_enabled) { true } | ||
let(:session_id) { 'test-session-id' } | ||
let(:enabled_for_session) { true } | ||
let(:request) { instance_double(ActionDispatch::Request) } | ||
let(:service_provider) { create(:service_provider) } | ||
let(:cookie_device_uuid) { 'device_id' } | ||
let(:sp_request_uri) { 'https://example.com/auth_page' } | ||
let(:user) { create(:user) } | ||
let(:analytics) { FakeAnalytics.new } | ||
|
||
subject do | ||
described_class.new( | ||
session_id: session_id, | ||
request: request, | ||
user: user, | ||
sp: service_provider, | ||
cookie_device_uuid: cookie_device_uuid, | ||
sp_request_uri: sp_request_uri, | ||
enabled_for_session: enabled_for_session, | ||
analytics: analytics, | ||
) | ||
end | ||
|
||
describe '#track_event' do | ||
it 'omit failure reason when success is true' do | ||
freeze_time do | ||
event = subject.track_event(:test_event, foo: :bar, success: true, failure_reason: nil) | ||
expect(event.event_metadata).to_not have_key(:failure_reason) | ||
end | ||
end | ||
|
||
it 'omit failure reason when failure_reason is blank' do | ||
freeze_time do | ||
event = subject.track_event(:test_event, foo: :bar, failure_reason: nil) | ||
expect(event.event_metadata).to_not have_key(:failure_reason) | ||
end | ||
end | ||
|
||
it 'should not omit failure reason when success is false and failure_reason is not blank' do | ||
freeze_time do | ||
event = subject.track_event( | ||
:test_event, foo: :bar, success: false, | ||
failure_reason: { foo: [:bar] } | ||
) | ||
expect(event.event_metadata).to have_key(:failure_reason) | ||
expect(event.event_metadata).to have_key(:success) | ||
end | ||
end | ||
|
||
it 'records the event in redis' do | ||
freeze_time do | ||
subject.track_event(:test_event, foo: :bar) | ||
|
||
events = AttemptsApi::RedisClient.new.read_events( | ||
timestamp: Time.zone.now, | ||
issuer: service_provider.issuer, | ||
) | ||
|
||
expect(events.values.length).to eq(1) | ||
end | ||
end | ||
|
||
it 'does not store events in plaintext in redis' do | ||
freeze_time do | ||
subject.track_event(:event, first_name: Idp::Constants::MOCK_IDV_APPLICANT[:first_name]) | ||
|
||
events = AttemptsApi::RedisClient.new.read_events( | ||
timestamp: Time.zone.now, | ||
issuer: service_provider.issuer, | ||
) | ||
|
||
expect(events.keys.first).to_not include('first_name') | ||
expect(events.values.first).to_not include(Idp::Constants::MOCK_IDV_APPLICANT[:first_name]) | ||
end | ||
end | ||
|
||
context 'the current session is not an attempts API session' do | ||
let(:enabled_for_session) { false } | ||
|
||
it 'does not record any events in redis' do | ||
freeze_time do | ||
subject.track_event(:test_event, foo: :bar) | ||
|
||
events = AttemptsApi::RedisClient.new.read_events( | ||
timestamp: Time.zone.now, | ||
issuer: service_provider.issuer, | ||
) | ||
|
||
expect(events.values.length).to eq(0) | ||
end | ||
end | ||
end | ||
|
||
context 'the attempts API is not enabled' do | ||
let(:attempts_api_enabled) { false } | ||
|
||
it 'does not record any events in redis' do | ||
freeze_time do | ||
subject.track_event(:test_event, foo: :bar) | ||
|
||
events = AttemptsApi::RedisClient.new.read_events( | ||
timestamp: Time.zone.now, | ||
issuer: service_provider.issuer, | ||
) | ||
|
||
expect(events.values.length).to eq(0) | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe '#parse_failure_reason' do | ||
let(:mock_error_message) { 'failure_reason_from_error' } | ||
let(:mock_error_details) { [{ mock_error: 'failure_reason_from_error_details' }] } | ||
|
||
it 'parses failure_reason from error_details' do | ||
test_failure_reason = subject.parse_failure_reason( | ||
{ errors: mock_error_message, | ||
error_details: mock_error_details }, | ||
) | ||
|
||
expect(test_failure_reason).to eq(mock_error_details) | ||
end | ||
|
||
it 'parses failure_reason from errors when no error_details present' do | ||
mock_failure_reason = double( | ||
'MockFailureReason', | ||
errors: mock_error_message, | ||
to_h: {}, | ||
) | ||
|
||
test_failure_reason = subject.parse_failure_reason(mock_failure_reason) | ||
|
||
expect(test_failure_reason).to eq(mock_error_message) | ||
end | ||
end | ||
end |
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.
should we do
metadata.dup
so we're not mutating an input?