Class: OpenID::OpenIDServiceEndpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/openid/consumer/discovery.rb

Overview

Object representing an OpenID service endpoint.

Constant Summary collapse

OPENID_TYPE_URIS =

OpenID service type URIs, listed in order of preference. The ordering of this list affects yadis and XRI service discovery.

[
  OPENID_IDP_2_0_TYPE,

  OPENID_2_0_TYPE,
  OPENID_1_1_TYPE,
  OPENID_1_0_TYPE,
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOpenIDServiceEndpoint



46
47
48
49
50
51
52
53
54
# File 'lib/openid/consumer/discovery.rb', line 46

def initialize
  @claimed_id = nil
  @server_url = nil
  @type_uris = []
  @local_id = nil
  @canonical_id = nil
  @used_yadis = false # whether this came from an XRDS
  @display_identifier = nil
end

Instance Attribute Details

#canonical_idObject

For XRI, the persistent identifier.



42
43
44
# File 'lib/openid/consumer/discovery.rb', line 42

def canonical_id
  @canonical_id
end

#claimed_idObject

the verified identifier.



39
40
41
# File 'lib/openid/consumer/discovery.rb', line 39

def claimed_id
  @claimed_id
end

#display_identifierObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/openid/consumer/discovery.rb', line 56

def display_identifier
  return @display_identifier if @display_identifier

  return @claimed_id if @claimed_id.nil?

  begin
    parsed_identifier = URI.parse(@claimed_id)
  rescue URI::InvalidURIError
    raise ProtocolError, "Claimed identifier #{claimed_id} is not a valid URI"
  end

  return @claimed_id unless parsed_identifier.fragment

  disp = parsed_identifier
  disp.fragment = nil

  disp.to_s
end

#local_idObject

Returns the value of attribute local_id.



44
45
46
# File 'lib/openid/consumer/discovery.rb', line 44

def local_id
  @local_id
end

#server_urlObject

Returns the value of attribute server_url.



44
45
46
# File 'lib/openid/consumer/discovery.rb', line 44

def server_url
  @server_url
end

#type_urisObject

Returns the value of attribute type_uris.



44
45
46
# File 'lib/openid/consumer/discovery.rb', line 44

def type_uris
  @type_uris
end

#used_yadisObject

Returns the value of attribute used_yadis.



44
45
46
# File 'lib/openid/consumer/discovery.rb', line 44

def used_yadis
  @used_yadis
end

Class Method Details

.from_basic_service_endpoint(endpoint) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/openid/consumer/discovery.rb', line 155

def self.from_basic_service_endpoint(endpoint)
  # Create a new instance of this class from the endpoint object
  # passed in.
  #
  # @return: nil or OpenIDServiceEndpoint for this endpoint object"""

  type_uris = endpoint.match_types(OPENID_TYPE_URIS)

  # If any Type URIs match and there is an endpoint URI specified,
  # then this is an OpenID endpoint
  if (!type_uris.nil? and !type_uris.empty?) and !endpoint.uri.nil?
    openid_endpoint = new
    openid_endpoint.parse_service(
      endpoint.yadis_url,
      endpoint.uri,
      endpoint.type_uris,
      endpoint.service_element,
    )
  else
    openid_endpoint = nil
  end

  openid_endpoint
end

.from_discovery_result(discovery_result) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/openid/consumer/discovery.rb', line 219

def self.from_discovery_result(discovery_result)
  # Create endpoints from a DiscoveryResult.
  #
  # @type discoveryResult: L{DiscoveryResult}
  #
  # @rtype: list of L{OpenIDServiceEndpoint}
  #
  # @raises L{XRDSError}: When the XRDS does not parse.
  meth = if discovery_result.is_xrds
    method(:from_xrds)
  else
    method(:from_html)
  end

  meth.call(
    discovery_result.normalized_uri,
    discovery_result.response_text,
  )
end

.from_html(uri, html) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/openid/consumer/discovery.rb', line 180

def self.from_html(uri, html)
  # Parse the given document as HTML looking for an OpenID <link
  # rel=...>
  #
  # @rtype: [OpenIDServiceEndpoint]

  discovery_types = [
    [OPENID_2_0_TYPE, "openid2.provider", "openid2.local_id"],
    [OPENID_1_1_TYPE, "openid.server", "openid.delegate"],
  ]

  link_attrs = OpenID.parse_link_attrs(html)
  services = []
  discovery_types.each do |type_uri, op_endpoint_rel, local_id_rel|
    op_endpoint_url = OpenID.find_first_href(link_attrs, op_endpoint_rel)

    next unless op_endpoint_url

    service = new
    service.claimed_id = uri
    service.local_id = OpenID.find_first_href(link_attrs, local_id_rel)
    service.server_url = op_endpoint_url
    service.type_uris = [type_uri]

    services << service
  end

  services
end

.from_op_endpoint_url(op_endpoint_url) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
# File 'lib/openid/consumer/discovery.rb', line 239

def self.from_op_endpoint_url(op_endpoint_url)
  # Construct an OP-Identifier OpenIDServiceEndpoint object for
  # a given OP Endpoint URL
  #
  # @param op_endpoint_url: The URL of the endpoint
  # @rtype: OpenIDServiceEndpoint
  service = new
  service.server_url = op_endpoint_url
  service.type_uris = [OPENID_IDP_2_0_TYPE]
  service
end

.from_session_value(value) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/openid/consumer/discovery.rb', line 145

def self.from_session_value(value)
  return value unless value.is_a?(Hash)

  new.tap do |endpoint|
    value.each do |name, val|
      endpoint.instance_variable_set(name, val)
    end
  end
end

.from_xrds(uri, xrds) ⇒ Object



210
211
212
213
214
215
216
217
# File 'lib/openid/consumer/discovery.rb', line 210

def self.from_xrds(uri, xrds)
  # Parse the given document as XRDS looking for OpenID services.
  #
  # @rtype: [OpenIDServiceEndpoint]
  #
  # @raises L{XRDSError}: When the XRDS does not parse.
  Yadis.apply_filter(uri, xrds, self)
end

Instance Method Details

#==(other) ⇒ Object



141
142
143
# File 'lib/openid/consumer/discovery.rb', line 141

def ==(other)
  to_session_value == other.to_session_value
end

#compatibility_modeObject



100
101
102
# File 'lib/openid/consumer/discovery.rb', line 100

def compatibility_mode
  preferred_namespace != OPENID_2_0_MESSAGE_NS
end

#get_local_idObject



127
128
129
130
131
132
133
134
135
# File 'lib/openid/consumer/discovery.rb', line 127

def get_local_id
  # Return the identifier that should be sent as the
  # openid.identity parameter to the server.
  if @local_id.nil? and @canonical_id.nil?
    @claimed_id
  else
    (@local_id or @canonical_id)
  end
end

#is_op_identifierObject



104
105
106
# File 'lib/openid/consumer/discovery.rb', line 104

def is_op_identifier
  @type_uris.member?(OPENID_IDP_2_0_TYPE)
end

#parse_service(yadis_url, uri, type_uris, service_element) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/openid/consumer/discovery.rb', line 108

def parse_service(yadis_url, uri, type_uris, service_element)
  # Set the state of this object based on the contents of the
  # service element.
  @type_uris = type_uris
  @server_url = uri
  @used_yadis = true

  return if is_op_identifier

  # XXX: This has crappy implications for Service elements that
  # contain both 'server' and 'signon' Types.  But that's a
  # pathological configuration anyway, so I don't think I care.
  @local_id = OpenID.find_op_local_identifier(
    service_element,
    @type_uris,
  )
  @claimed_id = yadis_url
end

#preferred_namespaceObject



81
82
83
84
85
86
87
88
# File 'lib/openid/consumer/discovery.rb', line 81

def preferred_namespace
  if @type_uris.member?(OPENID_IDP_2_0_TYPE) or
      @type_uris.member?(OPENID_2_0_TYPE)
    OPENID_2_0_MESSAGE_NS
  else
    OPENID_1_0_MESSAGE_NS
  end
end

#supports_type(type_uri) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/openid/consumer/discovery.rb', line 90

def supports_type(type_uri)
  # Does this endpoint support this type?
  #
  # I consider C{/server} endpoints to implicitly support C{/signon}.
  (
   @type_uris.member?(type_uri) or
   (type_uri == OPENID_2_0_TYPE and is_op_identifier)
 )
end

#to_sObject



251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/openid/consumer/discovery.rb', line 251

def to_s
  format(
    "<%s server_url=%s claimed_id=%s " +
                         "local_id=%s canonical_id=%s used_yadis=%s>",
    self.class,
    @server_url,
    @claimed_id,
    @local_id,
    @canonical_id,
    @used_yadis,
  )
end

#to_session_valueObject



137
138
139
# File 'lib/openid/consumer/discovery.rb', line 137

def to_session_value
  Hash[*instance_variables.flat_map { |name| [name, instance_variable_get(name)] }]
end

#uses_extension(extension_uri) ⇒ Object



77
78
79
# File 'lib/openid/consumer/discovery.rb', line 77

def uses_extension(extension_uri)
  @type_uris.member?(extension_uri)
end