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.



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

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

Instance Method Details

#associationObject



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

def association
  OpenidCouchdbStore::Association
end

#cleanup_associationsObject



135
136
137
138
# File 'lib/openid_couchdb_store.rb', line 135

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

#cleanup_noncesObject



131
132
133
# File 'lib/openid_couchdb_store.rb', line 131

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

#get_association(server_url, handle = nil) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/openid_couchdb_store.rb', line 93

def get_association(server_url, handle=nil)
  assoc_records = if (handle.nil? or handle.empty?)
    Association.by_server_url(:key => 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



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

def nonce
  OpenidCouchdbStore::Nonce
end

#remove_association(server_url, handle) ⇒ Object



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

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/openid_couchdb_store.rb', line 75

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

  issued = if association.issued.to_s =~ /\A\d+\Z/
    association.issued
  else
    Time.parse(association.issued.to_s).to_i
  end

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

#use_nonce(server_url, timestamp, salt) ⇒ Object



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

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