Skip to content
This repository has been archived by the owner on Jan 3, 2021. It is now read-only.

Update FK api #85

Merged
merged 4 commits into from
Jul 27, 2017
Merged
Changes from 3 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
22 changes: 15 additions & 7 deletions app/controllers/backend/backend_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,29 @@ def request_clubnames
return Club.pluck(:internal_name) if Rails.env.development?

ugent_login = session[:cas]['user']

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

random line

def digest(*args)
Digest::SHA256.hexdigest args.join('-')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

256 is a magic number, please abstract!

Also digest function should be set in config, because it can change

end

# using httparty because it is much easier to read than net/http code
resp = HTTParty.get(Rails.application.secrets.fk_auth_url, :query => {
:k => digest(ugent_login, Rails.application.secrets.fk_auth_key),
:u => ugent_login
})
resp = HTTParty.get("#{ Rails.application.secrets.fk_auth_url }/#{ ugent_login }/FKEnrolment",
headers: {
'X-Authorization': Rails.application.secrets.fk_auth_key,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

X-Authorization is a magic string.

Accept: 'application/json'
})

# this will only return the club names if control-hash matches
if resp.body != 'FAIL'
if resp.success?
hash = JSON[resp.body]
dig = digest(Rails.application.secrets.fk_auth_salt, ugent_login, hash['clubnames'])
return hash['clubnames'] if hash['control'] == dig
clubs = hash['clubs'].map do |club| club['internal_name'] end
timestamp = hash['timestamp']

max_time_difference = 5*60 # Timestamp can't differ by more than 5 minutes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 magic numbers for the price of one 5.minutes

return [] unless (Time.now - DateTime.parse(timestamp)).abs < max_time_difference
dig = digest(Rails.application.secrets.fk_auth_salt, ugent_login, timestamp, clubs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where you gonna start digging?


return clubs if hash['sign'] == dig
end
[]
end
Expand Down