Skip to content
Merged
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
70 changes: 58 additions & 12 deletions app/services/analytics_events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3766,13 +3766,22 @@ def multi_factor_auth(
# Tracks when the the user has added the MFA method phone to their account
# @param [Integer] enabled_mfa_methods_count number of registered mfa methods for the user
# @param [Hash] recaptcha_annotation Details of reCAPTCHA annotation, if submitted
def multi_factor_auth_added_phone(enabled_mfa_methods_count:, recaptcha_annotation:, **extra)
# @param [Boolean] in_account_creation_flow whether user is going through creation flow
# @param ['phone'] method_name Authentication method added
def multi_factor_auth_added_phone(
enabled_mfa_methods_count:,
recaptcha_annotation:,
in_account_creation_flow:,
method_name: :phone,
**extra
)
track_event(
'Multi-Factor Authentication: Added phone',
{
method_name: :phone,
method_name:,
enabled_mfa_methods_count: enabled_mfa_methods_count,
recaptcha_annotation:,
in_account_creation_flow:,
**extra,
}.compact,
)
Expand All @@ -3798,12 +3807,17 @@ def multi_factor_auth_added_piv_cac(enabled_mfa_methods_count:, in_account_creat
# Tracks when the user has added the MFA method TOTP to their account
# @param [Integer] enabled_mfa_methods_count number of registered mfa methods for the user
# @param [Boolean] in_account_creation_flow whether user is going through creation flow
def multi_factor_auth_added_totp(enabled_mfa_methods_count:, in_account_creation_flow:,
**extra)
# @param ['totp'] method_name Authentication method added
def multi_factor_auth_added_totp(
enabled_mfa_methods_count:,
in_account_creation_flow:,
method_name: :totp,
**extra
)
track_event(
'Multi-Factor Authentication: Added TOTP',
{
method_name: :totp,
method_name:,
in_account_creation_flow:,
enabled_mfa_methods_count:,
**extra,
Expand Down Expand Up @@ -3995,24 +4009,56 @@ def multi_factor_auth_phone_setup(
# @param [String] multi_factor_auth_method
# @param [Boolean] in_account_creation_flow whether user is going through account creation flow
# @param [integer] enabled_mfa_methods_count
# @param [DateTime] multi_factor_auth_method_created_at time auth method was created
# @param ['authentication','reauthentication','confirmation'] context User session context
# @param [Boolean] confirmation_for_add_phone Whether authenticating while adding phone
# @param [String] area_code Area code of phone number
# @param [String] country_code Country code associated with phone number
# @param [String] phone_fingerprint The hmac fingerprint of the phone number formatted as e164
# @param [Integer] phone_configuration_id Database ID of phone configuration
# @param [Integer] auth_app_configuration_id Database ID of authentication app configuration
# @param [Boolean] totp_secret_present Whether TOTP secret was present in form validation
# @param [Boolean] new_device Whether the user is authenticating from a new device
def multi_factor_auth_setup(
success:,
multi_factor_auth_method:,
enabled_mfa_methods_count:,
in_account_creation_flow:,
errors: nil,
error_details: nil,
multi_factor_auth_method_created_at: nil,
context: nil,
confirmation_for_add_phone: nil,
area_code: nil,
country_code: nil,
phone_fingerprint: nil,
phone_configuration_id: nil,
totp_secret_present: nil,
auth_app_configuration_id: nil,
new_device: nil,
**extra
)
track_event(
'Multi-Factor Authentication Setup',
success:,
errors:,
error_details:,
multi_factor_auth_method:,
in_account_creation_flow:,
enabled_mfa_methods_count:,
**extra,
{
success:,
errors:,
error_details:,
multi_factor_auth_method:,
in_account_creation_flow:,
enabled_mfa_methods_count:,
multi_factor_auth_method_created_at:,
context:,
confirmation_for_add_phone:,
area_code:,
country_code:,
phone_fingerprint:,
phone_configuration_id:,
totp_secret_present:,
auth_app_configuration_id:,
new_device:,
**extra,
}.compact,
)
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'rails_helper'

RSpec.describe TwoFactorAuthentication::OtpVerificationController, allowed_extra_analytics: [:*] do
RSpec.describe TwoFactorAuthentication::OtpVerificationController do
describe '#show' do
context 'when resource is not fully authenticated yet' do
before do
Expand Down Expand Up @@ -466,10 +466,19 @@
phone_configuration_created_at = controller.current_user.
default_phone_configuration.created_at

properties = {
controller.user_session[:phone_id] = phone_id

post(
:create,
params: {
code: subject.current_user.direct_otp,
otp_delivery_preference: 'sms',
},
)

expect(@analytics).to have_logged_event(
'Multi-Factor Authentication Setup',
success: true,
errors: nil,
error_details: nil,
confirmation_for_add_phone: true,
context: 'confirmation',
multi_factor_auth_method: 'sms',
Expand All @@ -481,19 +490,7 @@
phone_fingerprint: Pii::Fingerprinter.fingerprint(parsed_phone.e164),
enabled_mfa_methods_count: 1,
in_account_creation_flow: true,
}

controller.user_session[:phone_id] = phone_id

post(
:create,
params: {
code: subject.current_user.direct_otp,
otp_delivery_preference: 'sms',
},
)

expect(@analytics).to have_logged_event('Multi-Factor Authentication Setup', properties)
end

it 'resets otp session data' do
Expand Down Expand Up @@ -547,27 +544,23 @@
end

it 'tracks an event' do
phone_configuration_created_at = controller.current_user.
default_phone_configuration.created_at

properties = {
expect(@analytics).to have_logged_event(
'Multi-Factor Authentication Setup',
success: false,
errors: nil,
error_details: { code: { wrong_length: true, incorrect: true } },
confirmation_for_add_phone: true,
context: 'confirmation',
multi_factor_auth_method: 'sms',
phone_configuration_id: controller.current_user.default_phone_configuration.id,
multi_factor_auth_method_created_at: phone_configuration_created_at.strftime('%s%L'),
multi_factor_auth_method_created_at: controller.current_user.
default_phone_configuration.created_at.strftime('%s%L'),
new_device: true,
area_code: parsed_phone.area_code,
country_code: parsed_phone.country,
phone_fingerprint: Pii::Fingerprinter.fingerprint(parsed_phone.e164),
enabled_mfa_methods_count: 1,
in_account_creation_flow: false,
}

expect(@analytics).to have_logged_event('Multi-Factor Authentication Setup', properties)
)
end

context 'user enters in valid code after invalid entry' do
Expand Down Expand Up @@ -618,26 +611,22 @@

it 'tracks the confirmation event' do
parsed_phone = Phonelib.parse('+1 (703) 555-5555')
properties = {

response

expect(@analytics).to have_logged_event(
'Multi-Factor Authentication Setup',
success: true,
errors: nil,
error_details: nil,
context: 'confirmation',
multi_factor_auth_method: 'sms',
multi_factor_auth_method_created_at: nil,
new_device: true,
confirmation_for_add_phone: false,
phone_configuration_id: nil,
area_code: parsed_phone.area_code,
country_code: parsed_phone.country,
phone_fingerprint: Pii::Fingerprinter.fingerprint(parsed_phone.e164),
enabled_mfa_methods_count: 0,
in_account_creation_flow: false,
}

response

expect(@analytics).to have_logged_event('Multi-Factor Authentication Setup', properties)
)

expect(controller).to have_received(:create_user_event).with(:phone_confirmed)
expect(controller).to have_received(:create_user_event).exactly(:once)
Expand Down
Loading