Class: OpenID::DiffieHelmanAssociator

### Home Classes Methods

### In Files

- lib/openid/association.rb

### Parent

Object

### Methods

- ::new
- #associate
- #get_mod_gen

### Included Modules

- OpenID

### Class/Module Index

Quicksearch

- OpenID
- OpenID::Association
- OpenID::AssociationManager
- OpenID::AuthenticationError
- OpenID::BaseAssociationManager
- OpenID::Consumer
- OpenID::ConsumerAssociation
- OpenID::DiffieHelmanAssociator
- OpenID::DumbAssociationManager
- OpenID::NoArgumentsError
- OpenID::NoOpenIDArgs
- OpenID::ProtocolError
- OpenID::Request
- OpenID::Response
- OpenID::ServerAssociation
- OpenID::SimpleHTTPClient
- OpenID::UserCancelled
- OpenID::UserSetupNeeded
- OpenID::ValueMismatchError
- String
- String::OpenID
- Bignum
- Crypto_math
- DateTime
- Fixnum

No matching classes.

# OpenID::DiffieHelmanAssociator

A class for establishing associations with OpenID servers.

### Public Class Methods

new(http_client) click to toggle source

    # File lib/openid/association.rb, line 146
    def initialize(http_client)
            @http_client = http_client
    end

### Public Instance Methods

associate(server_url) click to toggle source

Establishes an association with an OpenID server, indicated by server_url. Returns a ConsumerAssociation.

    # File lib/openid/association.rb, line 158
    def associate(server_url)
            p, g = get_mod_gen()
            private_key = (1 + rand(p-2))
            dh_public = g.mod_exp(private_key, p)
            args = {
                    'openid.mode' => 'associate',
                    'openid.assoc_type' => 'HMAC-SHA1',
                    'openid.session_type' => 'DH-SHA1',
                    'openid.dh_modulus' => Base64.encode64(p.to_btwoc).delete("\n"),
                    'openid.dh_gen' => Base64.encode64(g.to_btwoc).delete("\n"),
                    'openid.dh_consumer_public' => Base64.encode64(dh_public.to_btwoc).delete("\n")
            }
            body = url_encode(args)
            url, data = @http_client.post(server_url, body)
            now = Time.now.utc
            # results are temporarily stored in an instance variable.
            # I'd like to restructure to avoid this.
            @results = parse_kv(data)

            assoc_type = get_result('assoc_type')
            if assoc_type != 'HMAC-SHA1'
                    raise RuntimeError, "Unknown association type: #{assoc_type}", caller
            end

            assoc_handle = get_result('assoc_handle')
            issued = DateTime.strptime(get_result('issued')).to_time
            expiry = DateTime.strptime(get_result('expiry')).to_time

            delta = now - issued
            expiry = expiry + delta

            replace_after_s = @results['replace_after']
            if replace_after_s
                    replace_after = DateTime.strptime(replace_after_s).to_time + delta
            else
                    replace_after = nil
            end

            session_type = @results['session_type']
            if session_type
                    if session_type != 'DH-SHA1'
                            raise RuntimeError, "Unknown Session Type: #{session_type}", caller
                    end
                    dh_server_pub = from_btwoc(Base64.decode64(get_result('dh_server_public')))
                    enc_mac_key = get_result('enc_mac_key')
                    dh_shared = dh_server_pub.mod_exp(private_key, p)

                    secret = Base64.decode64(enc_mac_key) ^ Digest::SHA1.digest(dh_shared.to_btwoc)
            else
                    secret = get_result('mac_key')
            end
            @results = nil
            return ConsumerAssociation.new(server_url, assoc_handle, secret, expiry, replace_after)
    end

get_mod_gen() click to toggle source

Returns modulus and generator for Diffie-Helman. Override this for non-default values.

    # File lib/openid/association.rb, line 152
    def get_mod_gen()
            return DEFAULT_DH_MODULUS, DEFAULT_DH_GEN
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.