Class: OpenIdAuthentication::DbStore

Inherits:
OpenID::Store::Interface
  • Object
show all
Defined in:
lib/open_id_authentication/db_store.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cleanup_associationsObject



13
14
15
16
# File 'lib/open_id_authentication/db_store.rb', line 13

def self.cleanup_associations
    now = Time.now.to_i
    Association.delete_all(:all, :conditions => ['issued + lifetime > ?', now])
end

.cleanup_noncesObject



6
7
8
9
10
11
# File 'lib/open_id_authentication/db_store.rb', line 6

def self.cleanup_nonces
    puts 'deleting nonces'
    now = Time.now.to_i
    Nonce.delete_all(["timestamp > ? OR timestamp < ?", now + OpenID::Nonce.skew, now - OpenID::Nonce.skew])
    puts 'done'
end

Instance Method Details

#get_association(server_url, handle = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/open_id_authentication/db_store.rb', line 28

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.each do |assoc|
        a = assoc.from_record
        if a.expires_in.to_i == 0
            assoc.destroy
        else
            return a
        end
    end if assocs.any?

    return nil
end

#remove_association(server_url, handle) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/open_id_authentication/db_store.rb', line 47

def remove_association(server_url, handle)

    as = Association.find(:all, :conditions => ['server_url = ? AND handle = ?', server_url, handle])
    as.each do |a|
        a.delete
    end
    return as.size > 0
end

#store_association(server_url, assoc) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/open_id_authentication/db_store.rb', line 18

def store_association(server_url, assoc)
    remove_association(server_url, assoc.handle)
    Association.create(:server_url => server_url,
                       :handle => assoc.handle,
                       :secret => Base64.encode64(assoc.secret),
                       :issued => Time.now.to_i, # assoc.issued,
                       :lifetime => assoc.lifetime,
                       :assoc_type => assoc.assoc_type)
end

#use_nonce(server_url, timestamp, salt) ⇒ Object



56
57
58
59
60
61
# File 'lib/open_id_authentication/db_store.rb', line 56

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