Sha256: c1e07114edbee9954200414d0c8f59f700d87890afef57b537eb207ae4608d24

Contents?: true

Size: 1.74 KB

Versions: 12

Compression:

Stored size: 1.74 KB

Contents

require 'association'
require 'nonce'
require 'openid/store/interface'

# not in OpenID module to avoid namespace conflict
class ActiveRecordStore < OpenID::Store::Interface
  def store_association(server_url, assoc)
    remove_association(server_url, assoc.handle)    
    Association.create!(:server_url => server_url,
                       :handle     => assoc.handle,
                       :secret     => assoc.secret,
                       :issued     => assoc.issued.to_i,
                       :lifetime   => assoc.lifetime,
                       :assoc_type => assoc.assoc_type)
  end

  def get_association(server_url, handle=nil)
    assocs = if handle.blank?
        Association.find_all_by_server_url(server_url)
      else
        Association.find_all_by_server_url_and_handle(server_url, handle)
      end

    assocs.reverse.each do |assoc|
      a = assoc.from_record    
      if a.expires_in == 0
        assoc.destroy
      else
        return a
      end
    end if assocs.any?
    
    return nil
  end
  
  def remove_association(server_url, handle)
    Association.delete_all(['server_url = ? AND handle = ?', server_url, handle]) > 0
  end
  
  def use_nonce(server_url, timestamp, salt)
    return false if Nonce.find_by_server_url_and_timestamp_and_salt(server_url, timestamp, salt)
    return false if (timestamp - Time.now.to_i).abs > OpenID::Nonce.skew
    Nonce.create!(:server_url => server_url, :timestamp => timestamp, :salt => salt)
    return true
  end
  
  def cleanup_nonces
    now = Time.now.to_i
    Nonce.delete_all(["timestamp > ? OR timestamp < ?", now + OpenID::Nonce.skew, now - OpenID::Nonce.skew])
  end

  def cleanup_associations
    now = Time.now.to_i
    Association.delete_all(['issued + lifetime < ?',now])
  end

end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
ruby-openid-2.9.2 examples/active_record_openid_store/lib/openid_ar_store.rb
ruby-openid-2.9.1 examples/active_record_openid_store/lib/openid_ar_store.rb
ruby-openid-2.8.0 examples/active_record_openid_store/lib/openid_ar_store.rb
ruby-openid-2.7.0 examples/active_record_openid_store/lib/openid_ar_store.rb
ruby-openid-2.6.0 examples/active_record_openid_store/lib/openid_ar_store.rb
ruby-openid-2.5.0 examples/active_record_openid_store/lib/openid_ar_store.rb
ruby-openid-2.4.0 examples/active_record_openid_store/lib/openid_ar_store.rb
ruby-openid-2.3.0 examples/active_record_openid_store/lib/openid_ar_store.rb
ruby-openid-2.2.3 examples/active_record_openid_store/lib/openid_ar_store.rb
ruby-openid-2.2.2 examples/active_record_openid_store/lib/openid_ar_store.rb
ruby-openid-2.2.1 examples/active_record_openid_store/lib/openid_ar_store.rb
ruby-openid-2.2.0 examples/active_record_openid_store/lib/openid_ar_store.rb