Sha256: 5f763e76680fbd9653d830692c33c70d460a32d3955d42e9a2d9252f80c352d2

Contents?: true

Size: 1.74 KB

Versions: 10

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

10 entries across 10 versions & 4 rubygems

Version Path
pelle-ruby-openid-2.1.8 examples/active_record_openid_store/lib/openid_ar_store.rb
entp-ruby-openid-2.2 examples/active_record_openid_store/lib/openid_ar_store.rb
nov-ruby-openid-2.1.9 examples/active_record_openid_store/lib/openid_ar_store.rb
ruby-openid-2.1.8 examples/active_record_openid_store/lib/openid_ar_store.rb
pelle-ruby-openid-2.1.9 examples/active_record_openid_store/lib/openid_ar_store.rb
ruby-openid-2.1.7 examples/active_record_openid_store/lib/openid_ar_store.rb
ruby-openid-2.1.6 examples/active_record_openid_store/lib/openid_ar_store.rb
ruby-openid-2.1.4 examples/active_record_openid_store/lib/openid_ar_store.rb
ruby-openid-2.1.5 examples/active_record_openid_store/lib/openid_ar_store.rb
ruby-openid-2.1.2 examples/active_record_openid_store/lib/openid_ar_store.rb