Skip to content

Commit db16fdb

Browse files
committed
AP-610 calnet attribute validate is too strict
1 parent 12fdb9b commit db16fdb

2 files changed

Lines changed: 16 additions & 60 deletions

File tree

app/models/concerns/calnet_authentication.rb

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -51,63 +51,24 @@ def auth_params_from(auth)
5151
end
5252
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
5353

54-
# Verifies that auth_extra contains all required CalNet attributes with exact case-sensitive names
54+
# Verifies that auth_extra contains the required email CalNet attribute
5555
# For array attributes, at least one value in the array must be present in auth_extra
56-
# Not all users have the same attributes, so the required attributes are determined based on the user's affiliations (e.g. employee vs student)
57-
# Raise [Error::CalnetError] if any required attributes are missing
56+
# Raise [Error::CalnetError] if the email attribute is missing
5857
def verify_calnet_attributes!(auth_extra)
59-
affiliations = affiliations_from(auth_extra)
60-
raise_missing_calnet_attribute_error(auth_extra, ['berkeleyEduAffiliations']) if affiliations.blank?
58+
email_attrs = CALNET_ATTRS[:email]
59+
return if present_in_auth_extra?(auth_extra, email_attrs)
6160

62-
required_attributes = required_attributes_for(affiliations)
63-
64-
missing = required_attributes.reject do |attr|
65-
present_in_auth_extra?(auth_extra, attr)
66-
end
67-
68-
return if missing.empty?
69-
70-
raise_missing_calnet_attribute_error(auth_extra, missing)
61+
raise_missing_calnet_attribute_error(auth_extra, Array(email_attrs))
7162
end
7263

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

81-
def affiliations_from(auth_extra)
82-
Array(auth_extra['berkeleyEduAffiliations'])
83-
end
84-
85-
def employee_affiliated?(affiliations)
86-
affiliations.include?('EMPLOYEE-TYPE-STAFF') ||
87-
affiliations.include?('EMPLOYEE-TYPE-ACADEMIC')
88-
end
89-
90-
def student_affiliated?(affiliations)
91-
affiliations.include?('STUDENT-TYPE-NOT-REGISTERED') ||
92-
affiliations.include?('STUDENT-TYPE-REGISTERED')
93-
end
94-
95-
def required_attributes_for(affiliations)
96-
required_cal_attrs = CALNET_ATTRS.dup
97-
required_cal_attrs.delete(:affiliations)
98-
99-
# only employee afflication will validate employee_id and ucpath_id attributes.
100-
unless employee_affiliated?(affiliations)
101-
required_cal_attrs.delete(:employee_id)
102-
required_cal_attrs.delete(:ucpath_id)
103-
end
104-
105-
# only student registered and not-registered affiliation will validate student_id attribute.
106-
required_cal_attrs.delete(:student_id) unless student_affiliated?(affiliations)
107-
108-
required_cal_attrs.values
109-
end
110-
11172
def present_in_auth_extra?(auth_extra, attr)
11273
if attr.is_a?(Array)
11374
attr.any? { |a| auth_extra.key?(a) }

spec/models/user_spec.rb

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
actual = %w[berkeleyEduAffiliations berkeleyEduAlternatid berkeleyEduCSID berkeleyEduIsMemberOf berkeleyEduUCPathID departmentNumber
4444
displayName employeeNumber givenName surname uid]
4545
# rubocop:disable Layout/LineLength
46-
msg = "Expected CalNet attribute(s) not found (case-sensitive): #{missing.join(', ')}. The actual CalNet attributes: #{actual.join(', ')}. The user is expected display name"
46+
msg = "Expected CalNet attribute(s) not found (case-sensitive): #{missing.join(', ')}. The actual CalNet attributes: #{actual.join(', ')}. The user is expected UID"
4747
# rubocop:enable Layout/LineLength
4848
expect { User.from_omniauth(auth) }.to raise_error(Error::CalnetError, msg)
4949
end
@@ -163,14 +163,13 @@
163163
end
164164

165165
describe :verify_calnet_attributes! do
166-
it 'allows employee-affiliated users without berkeleyEduStuID' do
166+
it 'allows users without departmentNumber' do
167167
auth_extra = {
168168
'berkeleyEduAffiliations' => ['EMPLOYEE-TYPE-ACADEMIC'],
169169
'berkeleyEduCSID' => 'cs123',
170170
'berkeleyEduIsMemberOf' => [],
171171
'berkeleyEduUCPathID' => 'ucpath456',
172172
'berkeleyEduAlternateID' => 'email@berkeley.edu',
173-
'departmentNumber' => 'dept1',
174173
'displayName' => 'Test Faculty',
175174
'employeeNumber' => 'emp789',
176175
'givenName' => 'Test',
@@ -181,14 +180,12 @@
181180
expect { User.from_omniauth({ 'provider' => 'calnet', 'extra' => auth_extra }) }.not_to raise_error
182181
end
183182

184-
it 'allows student-affiliated users without employeeNumber and berkeleyEduUCPathID' do
183+
it 'allows users without berkeleyEduStuID, employeeNumber, or berkeleyEduUCPathID' do
185184
auth_extra = {
186185
'berkeleyEduAffiliations' => ['STUDENT-TYPE-REGISTERED'],
187186
'berkeleyEduCSID' => 'cs123',
188187
'berkeleyEduIsMemberOf' => [],
189-
'berkeleyEduStuID' => 'stu456',
190188
'berkeleyEduAlternateID' => 'email@berkeley.edu',
191-
'departmentNumber' => 'dept1',
192189
'displayName' => 'Test Student',
193190
'givenName' => 'Test',
194191
'surname' => 'Student',
@@ -198,31 +195,29 @@
198195
expect { User.from_omniauth({ 'provider' => 'calnet', 'extra' => auth_extra }) }.not_to raise_error
199196
end
200197

201-
it 'rejects student-affiliated users if berkeleyEduStuID is missing' do
198+
it 'allows users without berkeleyEduAffiliations' do
202199
auth_extra = {
203-
'berkeleyEduAffiliations' => ['STUDENT-TYPE-REGISTERED'],
204200
'berkeleyEduCSID' => 'cs123',
205201
'berkeleyEduIsMemberOf' => [],
206202
'berkeleyEduAlternateID' => 'email@berkeley.edu',
207-
'departmentNumber' => 'dept1',
208-
'displayName' => 'Test Student',
203+
'displayName' => 'Test User',
209204
'givenName' => 'Test',
210-
'surname' => 'Student',
211-
'uid' => 'student1'
205+
'surname' => 'User',
206+
'uid' => 'user1'
212207
}
213208

214-
expect { User.from_omniauth({ 'provider' => 'calnet', 'extra' => auth_extra }) }.to raise_error(Error::CalnetError)
209+
expect { User.from_omniauth({ 'provider' => 'calnet', 'extra' => auth_extra }) }.not_to raise_error
215210
end
216211

217-
it 'rejects employee-affiliated users if employeeNumber is missing' do
212+
it 'rejects users if email attribute is missing' do
218213
auth_extra = {
219214
'berkeleyEduAffiliations' => ['EMPLOYEE-TYPE-STAFF'],
220215
'berkeleyEduCSID' => 'cs123',
221216
'berkeleyEduIsMemberOf' => [],
222217
'berkeleyEduUCPathID' => 'ucpath456',
223-
'berkeleyEduAlternateID' => 'email@berkeley.edu',
224218
'departmentNumber' => 'dept1',
225219
'displayName' => 'Test Staff',
220+
'employeeNumber' => 'emp789',
226221
'givenName' => 'Test',
227222
'surname' => 'Staff',
228223
'uid' => 'staff1'

0 commit comments

Comments
 (0)