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
11 changes: 5 additions & 6 deletions app/models/concerns/calnet_authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def auth_params_from(auth)
# Raise [Error::CalnetError] if any required attributes are missing
def verify_calnet_attributes!(auth_extra)
affiliations = affiliations_from(auth_extra)
raise_missing_calnet_attribute_error(auth_extra, ['berkeleyEduAffiliations']) if affiliations.blank?
return log_missing_calnet_attribute(auth_extra, ['berkeleyEduAffiliations']) if affiliations.blank?

required_attributes = required_attributes_for(affiliations)

Expand All @@ -67,15 +67,14 @@ def verify_calnet_attributes!(auth_extra)

return if missing.empty?

raise_missing_calnet_attribute_error(auth_extra, missing)
log_missing_calnet_attribute(auth_extra, missing)
end

def raise_missing_calnet_attribute_error(auth_extra, missing)
def log_missing_calnet_attribute(auth_extra, missing)
missing_attrs = "Expected CalNet attribute(s) not found (case-sensitive): #{missing.join(', ')}."
actual_calnet_keys = auth_extra.keys.reject { |k| k.start_with?('duo') }.sort
msg = "#{missing_attrs} The actual CalNet attributes: #{actual_calnet_keys.join(', ')}. The user is #{auth_extra['displayName']}"
Rails.logger.error(msg)
raise Error::CalnetError, msg
msg = "#{missing_attrs} The actual CalNet attributes: #{actual_calnet_keys.join(', ')}. The user is #{auth_extra['uid']}"
Rails.logger.info(msg)
end

def affiliations_from(auth_extra)
Expand Down
17 changes: 10 additions & 7 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
expect { User.from_omniauth(auth) }.to raise_error(Error::InvalidAuthProviderError)
end

it 'rejects calnet when a required schema attribute is missing or renamed' do
it 'logs a warning when a required schema attribute is missing or renamed' do
auth = {
'provider' => 'calnet',
'extra' => {
Expand All @@ -43,9 +43,10 @@
actual = %w[berkeleyEduAffiliations berkeleyEduAlternatid berkeleyEduCSID berkeleyEduIsMemberOf berkeleyEduUCPathID departmentNumber
displayName employeeNumber givenName surname uid]
# rubocop:disable Layout/LineLength
msg = "Expected CalNet attribute(s) not found (case-sensitive): #{missing.join(', ')}. The actual CalNet attributes: #{actual.join(', ')}. The user is expected display name"
msg = "Expected CalNet attribute(s) not found (case-sensitive): #{missing.join(', ')}. The actual CalNet attributes: #{actual.join(', ')}. The user is expected UID"
# rubocop:enable Layout/LineLength
expect { User.from_omniauth(auth) }.to raise_error(Error::CalnetError, msg)
expect(Rails.logger).to receive(:info).with(msg)
User.from_omniauth(auth)
end

it 'populates a User object' do
Expand Down Expand Up @@ -198,7 +199,7 @@
expect { User.from_omniauth({ 'provider' => 'calnet', 'extra' => auth_extra }) }.not_to raise_error
end

it 'rejects student-affiliated users if berkeleyEduStuID is missing' do
it 'logs missing berkeleyEduStuID for student-affiliated users' do
auth_extra = {
'berkeleyEduAffiliations' => ['STUDENT-TYPE-REGISTERED'],
'berkeleyEduCSID' => 'cs123',
Expand All @@ -211,10 +212,11 @@
'uid' => 'student1'
}

expect { User.from_omniauth({ 'provider' => 'calnet', 'extra' => auth_extra }) }.to raise_error(Error::CalnetError)
expect(Rails.logger).to receive(:info).with(/berkeleyEduStuID/)
User.from_omniauth({ 'provider' => 'calnet', 'extra' => auth_extra })
end

it 'rejects employee-affiliated users if employeeNumber is missing' do
it 'logs missing employeeNumber for employee-affiliated users' do
auth_extra = {
'berkeleyEduAffiliations' => ['EMPLOYEE-TYPE-STAFF'],
'berkeleyEduCSID' => 'cs123',
Expand All @@ -228,7 +230,8 @@
'uid' => 'staff1'
}

expect { User.from_omniauth({ 'provider' => 'calnet', 'extra' => auth_extra }) }.to raise_error(Error::CalnetError)
expect(Rails.logger).to receive(:info).with(/employeeNumber/)
User.from_omniauth({ 'provider' => 'calnet', 'extra' => auth_extra })
end
end

Expand Down