Class: CloudKit::OpenIDStore

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

Overview

An OpenIDStore provides the interface expected by the ruby-openid gem, mapping it to a CloudKit::Store instance.

Constant Summary collapse

@@store =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOpenIDStore

Initialize an OpenIDStore.



10
11
12
13
14
15
# File 'lib/cloudkit/openid_store.rb', line 10

def initialize
  unless @@store
    @@store = Store.new(
      :collections => [:cloudkit_openid_associations, :cloudkit_openid_nonces])
  end
end

Class Method Details

.cleanupObject

:nodoc:



77
78
79
# File 'lib/cloudkit/openid_store.rb', line 77

def self.cleanup #:nodoc:
  # TODO
end

.cleanup_associationsObject

:nodoc:



81
82
83
# File 'lib/cloudkit/openid_store.rb', line 81

def self.cleanup_associations #:nodoc:
  # TODO
end

.cleanup_noncesObject

:nodoc:



85
86
87
# File 'lib/cloudkit/openid_store.rb', line 85

def self.cleanup_nonces #:nodoc:
  # TODO
end

Instance Method Details

#get_association(server_url, handle = nil) ⇒ Object

:nodoc:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cloudkit/openid_store.rb', line 17

def get_association(server_url, handle=nil) #:nodoc:
  options = {:server_url => server_url}
  options.merge!(:handle => Base64.encode64(handle)) if (handle && handle != '')
  result = @@store.get(CloudKit::URI.new('/cloudkit_openid_associations'), options)
  return nil unless result.status == 200
  return nil if result.parsed_content['total'] == 0

  ignore, associations = resolve_associations(result.parsed_content)
  return nil if associations.empty?

  associations.sort_by{|a| a['issued']}
  a = associations[-1]
  OpenID::Association.new(
    Base64.decode64(a['handle']),
    Base64.decode64(a['secret']),
    Time.at(a['issued']),
    a['lifetime'],
    a['assoc_type'])
end

#remove_association(server_url, handle) ⇒ Object

:nodoc:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cloudkit/openid_store.rb', line 37

def remove_association(server_url, handle) #:nodoc:
  result = @@store.get(
    CloudKit::URI.new('/cloudkit_openid_associations'),
    :server_url => server_url,
    :handle     => Base64.encode64(handle))
  return nil unless result.status == 200

  responses, associations = resolve_associations(result.parsed_content)
  return nil if associations.empty?

  uris = result.parsed_content['uris']
  responses.each_with_index do |r, index|
    @@store.delete(CloudKit::URI.new(uris[index]), :etag => r.etag)
  end
end

#store_association(server_url, association) ⇒ Object

:nodoc:



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cloudkit/openid_store.rb', line 53

def store_association(server_url, association) #:nodoc:
  remove_association(server_url, association.handle)
  json = JSON.generate(
    :server_url => server_url,
    :handle     => Base64.encode64(association.handle),
    :secret     => Base64.encode64(association.secret),
    :issued     => association.issued.to_i,
    :lifetime   => association.lifetime,
    :assoc_type => association.assoc_type)
  result = @@store.post(CloudKit::URI.new('/cloudkit_openid_associations'), :json => json)
  return (result.status == 201)
end

#use_nonce(server_url, timestamp, salt) ⇒ Object

:nodoc:



66
67
68
69
70
71
72
73
74
75
# File 'lib/cloudkit/openid_store.rb', line 66

def use_nonce(server_url, timestamp, salt) #:nodoc:
  return false if (timestamp - Time.now.to_i).abs > OpenID::Nonce.skew

  fragment = ::URI.escape(
    [server_url, timestamp, salt].join('-'), 
    Regexp.union(::URI::REGEXP::UNSAFE, '/', ':'))
  uri    = "/cloudkit_openid_nonces/#{fragment}"
  result = @@store.put(CloudKit::URI.new(uri), :json => '{}')
  return (result.status == 201)
end

#versionObject

Return the version number for this store.



90
# File 'lib/cloudkit/openid_store.rb', line 90

def version; 1; end