You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

db_store.rb 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # frozen_string_literal: false
  2. require 'openid/store/interface'
  3. module OpenIdAuthentication
  4. class DbStore < OpenID::Store::Interface
  5. def self.cleanup_nonces
  6. now = Time.now.to_i
  7. Nonce.delete_all(["timestamp > ? OR timestamp < ?", now + OpenID::Nonce.skew, now - OpenID::Nonce.skew])
  8. end
  9. def self.cleanup_associations
  10. now = Time.now.to_i
  11. Association.delete_all(['issued + lifetime > ?',now])
  12. end
  13. def store_association(server_url, assoc)
  14. remove_association(server_url, assoc.handle)
  15. Association.create(:server_url => server_url,
  16. :handle => assoc.handle,
  17. :secret => assoc.secret,
  18. :issued => assoc.issued,
  19. :lifetime => assoc.lifetime,
  20. :assoc_type => assoc.assoc_type)
  21. end
  22. def get_association(server_url, handle = nil)
  23. assocs = if handle.blank?
  24. Association.find_all_by_server_url(server_url)
  25. else
  26. Association.find_all_by_server_url_and_handle(server_url, handle)
  27. end
  28. assocs.reverse_each do |assoc|
  29. a = assoc.from_record
  30. if a.expires_in == 0
  31. assoc.destroy
  32. else
  33. return a
  34. end
  35. end if assocs.any?
  36. return nil
  37. end
  38. def remove_association(server_url, handle)
  39. Association.delete_all(['server_url = ? AND handle = ?', server_url, handle]) > 0
  40. end
  41. def use_nonce(server_url, timestamp, salt)
  42. return false if Nonce.find_by_server_url_and_timestamp_and_salt(server_url, timestamp, salt)
  43. return false if (timestamp - Time.now.to_i).abs > OpenID::Nonce.skew
  44. Nonce.create(:server_url => server_url, :timestamp => timestamp, :salt => salt)
  45. return true
  46. end
  47. end
  48. end