Class: Login::UserVerifier

Inherits:
Object
  • Object
show all
Defined in:
app/services/login/user_verifier.rb

Constant Summary collapse

MHV_TYPE =
:mhv_uuid
IDME_TYPE =
:idme_uuid
DSLOGON_TYPE =
:dslogon_uuid
LOGINGOV_TYPE =
:logingov_uuid

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ UserVerifier

Returns a new instance of UserVerifier.



5
6
7
8
9
10
11
12
13
14
15
# File 'app/services/login/user_verifier.rb', line 5

def initialize(user)
  @login_type = user.&.dig(:service_name)
  @auth_broker = user.&.dig(:auth_broker)
  @mhv_uuid = user.mhv_correlation_id
  @idme_uuid = user.idme_uuid
  @dslogon_uuid = user.edipi
  @logingov_uuid = user.logingov_uuid
  @icn = user.icn.presence
  @deprecated_log = nil
  @user_account_mismatch_log = nil
end

Instance Attribute Details

#auth_brokerObject (readonly, private)

Returns the value of attribute auth_broker.



23
24
25
# File 'app/services/login/user_verifier.rb', line 23

def auth_broker
  @auth_broker
end

#deprecated_logObject (readonly, private)

Returns the value of attribute deprecated_log.



23
24
25
# File 'app/services/login/user_verifier.rb', line 23

def deprecated_log
  @deprecated_log
end

#dslogon_uuidObject (readonly, private)

Returns the value of attribute dslogon_uuid.



23
24
25
# File 'app/services/login/user_verifier.rb', line 23

def dslogon_uuid
  @dslogon_uuid
end

#icnObject (readonly, private)

Returns the value of attribute icn.



23
24
25
# File 'app/services/login/user_verifier.rb', line 23

def icn
  @icn
end

#idme_uuidObject (readonly, private)

Returns the value of attribute idme_uuid.



23
24
25
# File 'app/services/login/user_verifier.rb', line 23

def idme_uuid
  @idme_uuid
end

#login_typeObject (readonly, private)

Returns the value of attribute login_type.



23
24
25
# File 'app/services/login/user_verifier.rb', line 23

def 
  @login_type
end

#logingov_uuidObject (readonly, private)

Returns the value of attribute logingov_uuid.



23
24
25
# File 'app/services/login/user_verifier.rb', line 23

def logingov_uuid
  @logingov_uuid
end

#mhv_uuidObject (readonly, private)

Returns the value of attribute mhv_uuid.



23
24
25
# File 'app/services/login/user_verifier.rb', line 23

def mhv_uuid
  @mhv_uuid
end

#new_user_logObject (readonly, private)

Returns the value of attribute new_user_log.



23
24
25
# File 'app/services/login/user_verifier.rb', line 23

def new_user_log
  @new_user_log
end

#user_account_mismatch_logObject (readonly, private)

Returns the value of attribute user_account_mismatch_log.



23
24
25
# File 'app/services/login/user_verifier.rb', line 23

def 
  @user_account_mismatch_log
end

Instance Method Details

#attempt_secondary_idme_identifierObject (private)

ID.me uuid has historically been a primary identifier, even for non-ID.me credentials. For now it is still worth attempting to use it as a backup identifier



134
135
136
137
138
139
140
# File 'app/services/login/user_verifier.rb', line 134

def attempt_secondary_idme_identifier
  @type = :idme_uuid
  @identifier = idme_uuid
  raise Errors::UserVerificationNotCreatedError if identifier.nil?

  Rails.logger.info("[Login::UserVerifier] Attempting alternate type=#{type} identifier=#{identifier}")
end

#backing_idme_uuidObject (private)



156
157
158
# File 'app/services/login/user_verifier.rb', line 156

def backing_idme_uuid
  @backing_idme_uuid ||= type_with_backing_idme_uuid ? idme_uuid : nil
end

#backing_idme_uuid_has_changed?Boolean (private)

Returns:

  • (Boolean)


112
113
114
# File 'app/services/login/user_verifier.rb', line 112

def backing_idme_uuid_has_changed?
  backing_idme_uuid != user_verification.backing_idme_uuid
end

#create_user_verificationObject (private)



98
99
100
101
102
103
104
105
106
# File 'app/services/login/user_verifier.rb', line 98

def create_user_verification
  set_new_user_log
  verified_at = icn ? Time.zone.now : nil
  UserVerification.create!(type => identifier,
                           user_account:  || UserAccount.new(icn:),
                           backing_idme_uuid:,
                           verified_at:,
                           locked:)
end

#deprecate_unverified_user_accountObject (private)



84
85
86
87
88
89
90
# File 'app/services/login/user_verifier.rb', line 84

def 
   = user_verification.
  DeprecatedUserAccount.create!(user_account: ,
                                user_verification:)
  user_verification.update(user_account: , verified_at: Time.zone.now)
  set_deprecated_log(.id, user_verification.id, .id)
end

#existing_user_accountObject (private)



148
149
150
# File 'app/services/login/user_verifier.rb', line 148

def 
  @existing_user_account ||= icn ? UserAccount.find_by(icn:) : nil
end

#find_or_create_user_verificationObject (private)

Queries for a UserVerification on the user, based off the credential identifier If a UserVerification doesn’t exist, create one and a UserAccount record associated with that UserVerification



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/services/login/user_verifier.rb', line 42

def find_or_create_user_verification
  if identifier.nil?
    Rails.logger.info("[Login::UserVerifier] Nil identifier for type=#{type}")
    attempt_secondary_idme_identifier
  end

  ActiveRecord::Base.transaction do
    if user_verification
      update_existing_user_verification if user_verification_needs_to_be_updated?
      update_backing_idme_uuid if backing_idme_uuid_has_changed?
    else
      create_user_verification
    end
  end
  post_transaction_message_logs
  user_verification
end

#identifierObject (private)



177
178
179
180
181
182
183
184
185
186
187
188
# File 'app/services/login/user_verifier.rb', line 177

def identifier
  @identifier ||= case type
                  when MHV_TYPE
                    mhv_uuid
                  when IDME_TYPE
                    idme_uuid
                  when DSLOGON_TYPE
                    dslogon_uuid
                  when LOGINGOV_TYPE
                    logingov_uuid
                  end
end

#lockedObject (private)



142
143
144
145
146
# File 'app/services/login/user_verifier.rb', line 142

def locked
  return false unless 

  @locked ||= .user_verifications.send().where(locked: true).present?
end

#performObject



17
18
19
# File 'app/services/login/user_verifier.rb', line 17

def perform
  find_or_create_user_verification
end

#post_transaction_message_logsObject (private)



121
122
123
124
125
# File 'app/services/login/user_verifier.rb', line 121

def post_transaction_message_logs
  Rails.logger.info(deprecated_log) if deprecated_log
  Rails.logger.info() if 
  Rails.logger.info(new_user_log, { icn: }) if new_user_log
end

#set_deprecated_log(deprecated_user_account_id, user_verification_id, user_account_id) ⇒ Object (private)



127
128
129
130
# File 'app/services/login/user_verifier.rb', line 127

def set_deprecated_log(, user_verification_id, )
  @deprecated_log = "[Login::UserVerifier] Deprecating UserAccount id=#{}, " \
                    "Updating UserVerification id=#{user_verification_id} with UserAccount id=#{}"
end

#set_new_user_logObject (private)



116
117
118
119
# File 'app/services/login/user_verifier.rb', line 116

def set_new_user_log
  @new_user_log = '[Login::UserVerifier] New VA.gov user, ' \
                  "type=#{}, broker=#{auth_broker}, identifier=#{identifier}, locked=#{locked}"
end

#typeObject (private)



164
165
166
167
168
169
170
171
172
173
174
175
# File 'app/services/login/user_verifier.rb', line 164

def type
  @type ||= case 
            when SAML::User::MHV_ORIGINAL_CSID
              MHV_TYPE
            when SAML::User::IDME_CSID
              IDME_TYPE
            when SAML::User::DSLOGON_CSID
              DSLOGON_TYPE
            when SAML::User::LOGINGOV_CSID
              LOGINGOV_TYPE
            end
end

#type_with_backing_idme_uuidObject (private)



160
161
162
# File 'app/services/login/user_verifier.rb', line 160

def type_with_backing_idme_uuid
  type == MHV_TYPE || type == DSLOGON_TYPE
end

#update_backing_idme_uuidObject (private)



80
81
82
# File 'app/services/login/user_verifier.rb', line 80

def update_backing_idme_uuid
  user_verification.update(backing_idme_uuid:)
end

#update_existing_user_verificationObject (private)



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/services/login/user_verifier.rb', line 60

def update_existing_user_verification
  if 
    if user_verification.verified?
      @user_account_mismatch_log = '[Login::UserVerifier] User Account Mismatch for ' \
                                   "UserVerification id=#{user_verification.id}, " \
                                   "UserAccount id=#{user_verification..id}, " \
                                   "icn=#{user_verification..icn}, conflicts with " \
                                   "UserAccount id=#{.id} " \
                                   "icn=#{.icn} " \
                                   "Setting UserVerification id=#{user_verification.id} " \
                                   "association to UserAccount id=#{.id}"
      user_verification.update(user_account: )
    else
      
    end
  else
    update_newly_verified_user
  end
end

#update_newly_verified_userObject (private)



92
93
94
95
96
# File 'app/services/login/user_verifier.rb', line 92

def update_newly_verified_user
   = user_verification.
  user_verification.update(verified_at: Time.zone.now)
  .update(icn:)
end

#user_verificationObject (private)



152
153
154
# File 'app/services/login/user_verifier.rb', line 152

def user_verification
  @user_verification ||= identifier ? UserVerification.find_by(type => identifier) : nil
end

#user_verification_needs_to_be_updated?Boolean (private)

Returns:

  • (Boolean)


108
109
110
# File 'app/services/login/user_verifier.rb', line 108

def user_verification_needs_to_be_updated?
  icn.present? && user_verification. != 
end