Class: OpenidCouchdbStore::Store

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

Instance Method Summary collapse

Constructor Details

#initialize(couch_database) ⇒ Store

Returns a new instance of Store.



61
62
63
64
# File 'lib/openid_couchdb_store.rb', line 61

def initialize(couch_database)
  association.use_database(couch_database)
  nonce.use_database(couch_database)
end

Instance Method Details

#associationObject



66
67
68
# File 'lib/openid_couchdb_store.rb', line 66

def association
  OpenidCouchdbStore::Association
end

#cleanup_associationsObject



128
129
130
131
# File 'lib/openid_couchdb_store.rb', line 128

def cleanup_associations
  now = Time.now.to_i
  Association.expired.each {|a| a.destroy }
end

#cleanup_noncesObject



124
125
126
# File 'lib/openid_couchdb_store.rb', line 124

def cleanup_nonces
  nonce.expired.each {|n| n.destroy }
end

#get_association(server_url, handle = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/openid_couchdb_store.rb', line 86

def get_association(server_url, handle=nil)
  assoc_records = if (handle.nil? or handle.empty?)
    Association.by_server_url(server_url)
  else
    Association.by_server_url_and_handle(server_url,handle)
  end

  # TODO: Removed .reverse here, make sure that was reasonable.
  assoc_records.each do |a|
    openid_association = OpenID::Association.new(a['handle'],
                                                 OpenID::Util.from_base64(a['secret']),
                                                 a['issued'],
                                                 a['lifetime'],
                                                 a['assoc_type'])
    if openid_association.expires_in == 0
      a.destroy
    else
      return openid_association
    end
  end if assoc_records.any? # <- may not be needed

  # Fail if there isn't an acceptable association
  return nil
end

#nonceObject



70
71
72
# File 'lib/openid_couchdb_store.rb', line 70

def nonce
  OpenidCouchdbStore::Nonce
end

#remove_association(server_url, handle) ⇒ Object



111
112
113
114
115
# File 'lib/openid_couchdb_store.rb', line 111

def remove_association(server_url, handle)
  Association.by_server_url_and_handle(server_url,handle).each do |association|
    association.destroy
  end
end

#store_association(server_url, association) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/openid_couchdb_store.rb', line 74

def store_association(server_url, association)
  remove_association(server_url, association.handle)

  assoc = OpenidCouchdbStore::Association.create('server_url' => server_url,
                     'handle'     => association.handle,
                     'secret'     => OpenID::Util.to_base64(association.secret),
                     'issued'     => association.issued,
                     'lifetime'   => association.lifetime,
                     'assoc_type' => association.assoc_type)
  assoc
end

#use_nonce(server_url, timestamp, salt) ⇒ Object



117
118
119
120
121
122
# File 'lib/openid_couchdb_store.rb', line 117

def use_nonce(server_url, timestamp, salt)
  return false if Nonce.first_nonce(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