Class: OpenidMongodbStore::Store

Inherits:
OpenID::Store::Interface
  • Object
show all
Includes:
OpenidMongodbStore
Defined in:
lib/openid_mongodb_store/store.rb

Instance Method Summary collapse

Methods included from OpenidMongodbStore

database, database=

Constructor Details

#initialize(db = nil) ⇒ Store

Returns a new instance of Store.



7
8
9
# File 'lib/openid_mongodb_store/store.rb', line 7

def initialize(db = nil)
  OpenidMongodbStore.database = db
end

Instance Method Details

#associationsObject



11
12
13
# File 'lib/openid_mongodb_store/store.rb', line 11

def associations
  @@associations ||= OpenidMongodbStore.database.collection('openid_mongo_store_associations')
end

#cleanup_associationsObject



84
85
86
87
# File 'lib/openid_mongodb_store/store.rb', line 84

def cleanup_associations
  now = Time.now.to_i
  associations.remove('expire_at' => {'$lt' => now})
end

#cleanup_noncesObject



78
79
80
81
82
# File 'lib/openid_mongodb_store/store.rb', line 78

def cleanup_nonces
  now = Time.now.to_i
  nonces.remove({'timestamp' => {'$gt'=> (now + OpenID::Nonce.skew)}})
  nonces.remove({'timestamp' => {'$lt'=> (now - OpenID::Nonce.skew)}})
end

#get_association(server_url, handle = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/openid_mongodb_store/store.rb', line 38

def get_association(server_url, handle=nil)
  assocs = if (handle.nil? or handle.empty?)
    associations.find({'server_url' => server_url})
  else
    associations.find({'server_url' => server_url, 'handle' => handle})
  end

  assoc_records = assocs.collect {|a| a }

  # TODO: Removed .reverse here, make sure that was reasonable.
  assoc_records.each do |a|
    openid_association = OpenID::Association.new(a['handle'],
                                                 a['secret'].to_s,
                                                 a['issued'],
                                                 a['lifetime'],
                                                 a['assoc_type'])
    if openid_association.expires_in == 0
      associations.remove({'_id' => a['_id']})
    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

#noncesObject



15
16
17
# File 'lib/openid_mongodb_store/store.rb', line 15

def nonces
  @@nonces ||= OpenidMongodbStore.database.collection('openid_mongo_store_nonces')
end

#remove_association(server_url, handle) ⇒ Object



65
66
67
# File 'lib/openid_mongodb_store/store.rb', line 65

def remove_association(server_url, handle)
  associations.remove({'server_url'=> server_url, 'handle' => handle})
end

#store_association(server_url, association) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/openid_mongodb_store/store.rb', line 19

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

  secret = BSON::Binary.new(association.secret)
  associations.insert('server_url' => server_url,
                      'handle'     => association.handle,
                      'secret'     => secret,
                      'issued'     => issued,
                      'lifetime'   => association.lifetime,
                      'assoc_type' => association.assoc_type,
                      'expire_at'  => (issued + association.lifetime))
end

#use_nonce(server_url, timestamp, salt) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/openid_mongodb_store/store.rb', line 69

def use_nonce(server_url, timestamp, salt)
  return false if nonces.find_one({'server_url'=> server_url,
                                   'timestamp' => timestamp,
                                   'salt'      => salt})
  return false if (timestamp - Time.now.to_i).abs > OpenID::Nonce.skew
  nonces.insert({'server_url' => server_url, 'timestamp' => timestamp, 'salt' => salt})
  return true
end