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

#initialize(uri = nil) ⇒ OpenIDStore

Initialize an OpenIDStore and its required views.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cloudkit/openid_store.rb', line 10

def initialize(uri=nil)
  unless @@store
    association_view = ExtractionView.new(
      :cloudkit_openid_server_handles,
      :observe => :cloudkit_openid_associations,
      :extract => [:server_url, :handle])
    @@store = Store.new(
      :collections => [:cloudkit_openid_associations, :cloudkit_openid_nonces],
      :views       => [association_view],
      :adapter     => SQLAdapter.new(uri))
  end
end

Class Method Details

.cleanupObject

:nodoc:



83
84
85
# File 'lib/cloudkit/openid_store.rb', line 83

def self.cleanup #:nodoc:
  # TODO
end

.cleanup_associationsObject

:nodoc:



87
88
89
# File 'lib/cloudkit/openid_store.rb', line 87

def self.cleanup_associations #:nodoc:
  # TODO
end

.cleanup_noncesObject

:nodoc:



91
92
93
# File 'lib/cloudkit/openid_store.rb', line 91

def self.cleanup_nonces #:nodoc:
  # TODO
end

Instance Method Details

#get_association(server_url, handle = nil) ⇒ Object

:nodoc:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cloudkit/openid_store.rb', line 23

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_openid_server_handles', 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:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cloudkit/openid_store.rb', line 43

def remove_association(server_url, handle) #:nodoc:
  result = @@store.get(
    '/cloudkit_openid_server_handles',
    :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(uris[index], :etag => r.etag)
  end
end

#store_association(server_url, association) ⇒ Object

:nodoc:



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cloudkit/openid_store.rb', line 59

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_openid_associations', :json => json)
  return (result.status == 201)
end

#use_nonce(server_url, timestamp, salt) ⇒ Object

:nodoc:



72
73
74
75
76
77
78
79
80
81
# File 'lib/cloudkit/openid_store.rb', line 72

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(uri, :json => '{}')
  return (result.status == 201)
end