Class: OpenID::Server::DiffieHellmanSHA1ServerSession

Inherits:
BaseServerSession show all
Defined in:
lib/openid/server.rb

Overview

An object that knows how to handle association requests with the Diffie-Hellman session type.

See OpenID Specs, Section 8: Establishing Associations <openid.net/specs/openid-authentication-2_0-12.html#associations>

Direct Known Subclasses

DiffieHellmanSHA256ServerSession

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseServerSession

#allowed_assoc_type?

Constructor Details

#initialize(dh, consumer_pubkey) ⇒ DiffieHellmanSHA1ServerSession

Returns a new instance of DiffieHellmanSHA1ServerSession.



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

def initialize(dh, consumer_pubkey)
  super('DH-SHA1', ['HMAC-SHA1'])

  @hash_func = CryptUtil.method('sha1')
  @dh = dh
  @consumer_pubkey = consumer_pubkey
end

Instance Attribute Details

#consumer_pubkeyObject

The public key sent by the consumer in the associate request



205
206
207
# File 'lib/openid/server.rb', line 205

def consumer_pubkey
  @consumer_pubkey
end

#dhObject

The Diffie-Hellman algorithm values for this request



202
203
204
# File 'lib/openid/server.rb', line 202

def dh
  @dh
end

#session_typeObject (readonly)

The session_type for this association session.



208
209
210
# File 'lib/openid/server.rb', line 208

def session_type
  @session_type
end

Class Method Details

.from_message(message) ⇒ Object

Construct me from OpenID Message

Raises ProtocolError when parameters required to establish the session are missing.



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/openid/server.rb', line 222

def self.from_message(message)
  dh_modulus = message.get_arg(OPENID_NS, 'dh_modulus')
  dh_gen = message.get_arg(OPENID_NS, 'dh_gen')
  if ((!dh_modulus and dh_gen) or
      (!dh_gen and dh_modulus))

    if !dh_modulus
      missing = 'modulus'
    else
      missing = 'generator'
    end

    raise ProtocolError.new(message,
            sprintf('If non-default modulus or generator is ' +
                    'supplied, both must be supplied. Missing %s',
                    missing))
  end

  if dh_modulus or dh_gen
    dh_modulus = CryptUtil.base64_to_num(dh_modulus)
    dh_gen = CryptUtil.base64_to_num(dh_gen)
    dh = DiffieHellman.new(dh_modulus, dh_gen)
  else
    dh = DiffieHellman.from_defaults()
  end

  consumer_pubkey = message.get_arg(OPENID_NS, 'dh_consumer_public')
  if !consumer_pubkey
    raise ProtocolError.new(message,
            sprintf("Public key for DH-SHA1 session " +
                    "not found in message %s", message))
  end

  consumer_pubkey = CryptUtil.base64_to_num(consumer_pubkey)

  return self.new(dh, consumer_pubkey)
end

Instance Method Details

#answer(secret) ⇒ Object



260
261
262
263
264
265
266
267
268
# File 'lib/openid/server.rb', line 260

def answer(secret)
  mac_key = @dh.xor_secret(@hash_func,
                           @consumer_pubkey,
                           secret)
  return {
      'dh_server_public' => CryptUtil.num_to_base64(@dh.public),
      'enc_mac_key' => Util.to_base64(mac_key),
      }
end