Class: OpenID::Store::Sequel

Inherits:
Object
  • Object
show all
Defined in:
lib/openid/store/sequel.rb,
lib/openid/store/sequel/nonce.rb,
lib/openid/store/sequel/version.rb,
lib/openid/store/sequel/association.rb

Defined Under Namespace

Classes: Association, Nonce

Constant Summary collapse

VERSION =
"0.0.2"

Instance Method Summary collapse

Instance Method Details

#cleanup_associationsObject



65
66
67
68
69
70
71
72
73
74
# File 'lib/openid/store/sequel.rb', line 65

def cleanup_associations
  count = 0
  OpenID::Store::Sequel::Association.where('issued > 0').each do |association|
    if association.lifetime + association.issued > Time.now.to_i
      association.destroy
      count += 1
    end
  end
  count
end

#cleanup_noncesObject



55
56
57
58
59
60
61
62
63
# File 'lib/openid/store/sequel.rb', line 55

def cleanup_nonces
  now = Time.now.to_i
  count = 0
  OpenID::Store::Sequel::Nonce.where("timestamp > '#{Time.at(now + OpenID::Nonce.skew)}' OR timestamp < '#{Time.at(now - OpenID::Nonce.skew)}'").each do |nonce|
    nonce.destroy
    count += 1
  end
  count
end

#get_association(server_url, handle = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/openid/store/sequel.rb', line 26

def get_association(server_url, handle=nil)
  assocs = if handle.nil? || handle.empty?
      OpenID::Store::Sequel::Association.where(server_url: server_url)
    else
      OpenID::Store::Sequel::Association.where(server_url: server_url, handle: handle)
    end

  assocs.to_a.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

#remove_association(server_url, handle) ⇒ Object



45
46
47
# File 'lib/openid/store/sequel.rb', line 45

def remove_association(server_url, handle)
  OpenID::Store::Sequel::Association.dataset.filter(server_url: server_url, handle: handle).delete > 0 ? true : false
end

#store_association(server_url, assoc) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/openid/store/sequel.rb', line 14

def store_association(server_url, assoc)
  remove_association(server_url, assoc.handle)
  OpenID::Store::Sequel::Association.create(
                     server_url: server_url,
                     handle: assoc.handle,
                     secret: Base64.encode64(assoc.secret),
                     issued: assoc.issued.to_i,
                     lifetime: assoc.lifetime,
                     assoc_type: assoc.assoc_type
  )
end

#use_nonce(server_url, timestamp, salt) ⇒ Object



49
50
51
52
53
# File 'lib/openid/store/sequel.rb', line 49

def use_nonce(server_url, timestamp, salt)
  return false if OpenID::Store::Sequel::Nonce.first(server_url: server_url, timestamp: Time.at(timestamp), salt: salt) || (timestamp - Time.now.to_i).abs > OpenID::Nonce.skew
  OpenID::Store::Sequel::Nonce.create(server_url: server_url, timestamp: Time.at(timestamp), salt: salt)
  return true
end