Class: OpenID::Consumer

### Home Classes Methods

### In Files

- lib/openid/consumer.rb
- lib/openid/consumer/associationmanager.rb
- lib/openid/consumer/checkid_request.rb
- lib/openid/consumer/discovery_manager.rb
- lib/openid/consumer/idres.rb
- lib/openid/consumer/responses.rb
- lib/openid/consumer/session.rb

### Parent

Object

### Namespace

- MODULE OpenID::Consumer::Response
- CLASS OpenID::Consumer::AssociationManager
- CLASS OpenID::Consumer::CancelResponse
- CLASS OpenID::Consumer::CheckIDRequest
- CLASS OpenID::Consumer::DiffieHellmanSHA1Session
- CLASS OpenID::Consumer::DiffieHellmanSHA256Session
- CLASS OpenID::Consumer::DiffieHellmanSession
- CLASS OpenID::Consumer::DiscoveredServices
- CLASS OpenID::Consumer::DiscoveryManager
- CLASS OpenID::Consumer::FailureResponse
- CLASS OpenID::Consumer::IdResHandler
- CLASS OpenID::Consumer::NoEncryptionSession
- CLASS OpenID::Consumer::Session
- CLASS OpenID::Consumer::SetupNeededResponse
- CLASS OpenID::Consumer::SuccessResponse

### Methods

- ::new
- ::openid1_return_to_claimed_id_name
- ::openid1_return_to_claimed_id_name=
- ::openid1_return_to_nonce_name
- ::openid1_return_to_nonce_name=
- #association_manager
- #begin
- #begin_without_discovery
- #cleanup_last_requested_endpoint
- #cleanup_session
- #complete
- #complete_cancel
- #complete_error
- #complete_id_res
- #complete_invalid
- #complete_setup_needed
- #discover
- #discovery_manager
- #handle_idres
- #last_requested_endpoint
- #last_requested_endpoint=
- #negotiator
- #session_get
- #session_key
- #session_set

### Files

- LICENSE

### Class/Module Index

Quicksearch

- OpenID
- OpenID::AX
- OpenID::AX::AXMessage
- OpenID::AX::AttrInfo
- OpenID::AX::Error
- OpenID::AX::FetchRequest
- OpenID::AX::FetchResponse
- OpenID::AX::KeyValueMessage
- OpenID::AX::StoreRequest
- OpenID::AX::StoreResponse
- OpenID::AssertionError
- OpenID::Association
- OpenID::AssociationNegotiator
- OpenID::Consumer
- OpenID::Consumer::AssociationManager
- OpenID::Consumer::CancelResponse
- OpenID::Consumer::CheckIDRequest
- OpenID::Consumer::DiffieHellmanSHA1Session
- OpenID::Consumer::DiffieHellmanSHA256Session
- OpenID::Consumer::DiffieHellmanSession
- OpenID::Consumer::DiscoveredServices
- OpenID::Consumer::DiscoveryManager
- OpenID::Consumer::FailureResponse
- OpenID::Consumer::IdResHandler
- OpenID::Consumer::NoEncryptionSession
- OpenID::Consumer::Response
- OpenID::Consumer::Session
- OpenID::Consumer::SetupNeededResponse
- OpenID::Consumer::SuccessResponse
- OpenID::CryptUtil
- OpenID::DiffieHellman
- OpenID::DiscoveryFailure
- OpenID::Extension
- OpenID::FetchingError
- OpenID::HTTPRedirectLimitReached
- OpenID::HTTPResponse
- OpenID::HTTPStatusError
- OpenID::InvalidOpenIDNamespace
- OpenID::KVFormError
- OpenID::KVPostNetworkError
- OpenID::Message
- OpenID::Message::KeyNotFound
- OpenID::NamespaceAliasRegistrationError
- OpenID::NamespaceMap
- OpenID::Nonce
- OpenID::OAuth
- OpenID::OAuth::Request
- OpenID::OAuth::Response
- OpenID::OpenIDError
- OpenID::OpenIDServiceEndpoint
- OpenID::PAPE
- OpenID::PAPE::Request
- OpenID::PAPE::Response
- OpenID::ProtocolError
- OpenID::RealmVerificationRedirected
- OpenID::SReg
- OpenID::SReg::NamespaceError
- OpenID::SReg::Request
- OpenID::SReg::Response
- OpenID::SSLFetchingError
- OpenID::Server
- OpenID::Server::AlreadySigned
- OpenID::Server::AssociateRequest
- OpenID::Server::BaseServerSession
- OpenID::Server::CheckAuthRequest
- OpenID::Server::CheckIDRequest
- OpenID::Server::Decoder
- OpenID::Server::DiffieHellmanSHA1ServerSession
- OpenID::Server::DiffieHellmanSHA256ServerSession
- OpenID::Server::Encoder
- OpenID::Server::EncodingError
- OpenID::Server::MalformedReturnURL
- OpenID::Server::MalformedTrustRoot
- OpenID::Server::NoReturnToError
- OpenID::Server::OpenIDRequest
- OpenID::Server::OpenIDResponse
- OpenID::Server::PlainTextServerSession
- OpenID::Server::ProtocolError
- OpenID::Server::Server
- OpenID::Server::Signatory
- OpenID::Server::SigningEncoder
- OpenID::Server::UntrustedReturnURL
- OpenID::Server::VersionError
- OpenID::Server::WebResponse
- OpenID::ServerError
- OpenID::StandardFetcher
- OpenID::Store
- OpenID::Store::Filesystem
- OpenID::Store::Interface
- OpenID::Store::Memcache
- OpenID::Store::Memory
- OpenID::TrustRoot
- OpenID::TrustRoot::TrustRoot
- OpenID::TypeURIMismatch
- OpenID::UI
- OpenID::UI::Request
- OpenID::URINorm
- OpenID::UndefinedOpenIDNamespace
- OpenID::Util
- OpenID::Yadis
- OpenID::Yadis::BasicServiceEndpoint
- OpenID::Yadis::CompoundFilter
- OpenID::Yadis::DiscoveryResult
- OpenID::Yadis::TransformFilterMaker
- OpenID::Yadis::XRDSError
- OpenID::Yadis::XRDSFraud
- OpenID::Yadis::XRI
- OpenID::Yadis::XRI::ProxyResolver
- OpenID::Yadis::XRI::XRIHTTPError
- HMAC
- HMAC::Base
- HMAC::SHA1
- HMAC::SHA256
- HMAC::SHA384
- HMAC::SHA512
- Net
- Net::HTTP
- HTMLComment
- HTMLTag
- HTMLText
- HTMLToken
- HTMLTokenizer
- HTMLTokenizerError
- Object

No matching classes.

# OpenID::Consumer

OpenID support for Relying Parties (aka Consumers).

This module documents the main interface with the OpenID consumer library. The only part of the library which has to be used and isn't documented in full here is the store required to create an Consumer instance.

# OVERVIEW

The OpenID identity verification process most commonly uses the following steps, as visible to the user of this library:

1. The user enters their OpenID into a field on the consumer's site, and hits a login button.

2. The consumer site discovers the user's OpenID provider using the Yadis protocol.

3. The consumer site sends the browser a redirect to the OpenID provider. This is the authentication request as described in the OpenID specification.

4. The OpenID provider's site sends the browser a redirect back to the consumer site. This redirect contains the provider's response to the authentication request.

The most important part of the flow to note is the consumer's site must handle two separate HTTP requests in order to perform the full identity check.

# LIBRARY DESIGN

This consumer library is designed with that flow in mind. The goal is to make it as easy as possible to perform the above steps securely.

At a high level, there are two important parts in the consumer library. The first important part is this module, which contains the interface to actually use this library. The second is openid/store/interface.rb, which describes the interface to use if you need to create a custom method for storing the state this library needs to maintain between requests.

In general, the second part is less important for users of the library to know about, as several implementations are provided which cover a wide variety of situations in which consumers may use the library.

The Consumer class has methods corresponding to the actions necessary in each of steps 2, 3, and 4 described in the overview. Use of this library should be as easy as creating an Consumer instance and calling the methods appropriate for the action the site wants to take.

This library automatically detects which version of the OpenID protocol should be used for a transaction and constructs the proper requests and responses. Users of this library do not need to worry about supporting multiple protocol versions; the library supports them implicitly. Depending on the version of the protocol in use, the OpenID transaction may be more secure. See the OpenID specifications for more information.

# SESSIONS, STORES, AND STATELESS MODE

The Consumer object keeps track of two types of state:

1. State of the user's current authentication attempt. Things like the identity URL, the list of endpoints discovered for that URL, and in case where some endpoints are unreachable, the list of endpoints already tried. This state needs to be held from Consumer.begin() to Consumer.complete(), but it is only applicable to a single session with a single user agent, and at the end of the authentication process (i.e. when an OP replies with either `id_res`. or `cancel` it may be discarded.

2. State of relationships with servers, i.e. shared secrets (associations) with servers and nonces seen on signed messages. This information should persist from one session to the next and should not be bound to a particular user-agent.

These two types of storage are reflected in the first two arguments of Consumer's constructor, `session` and `store`. `session` is a dict-like object and we hope your web framework provides you with one of these bound to the user agent. `store` is an instance of Store.

Since the store does hold secrets shared between your application and the OpenID provider, you should be careful about how you use it in a shared hosting environment. If the filesystem or database permissions of your web host allow strangers to read from them, do not store your data there! If you have no safe place to store your data, construct your consumer with nil for the store, and it will operate only in stateless mode. Stateless mode may be slower, put more load on the OpenID provider, and trusts the provider to keep you safe from replay attacks.

Several store implementation are provided, and the interface is fully documented so that custom stores can be used as well. See the documentation for the Consumer class for more information on the interface for stores. The implementations that are provided allow the consumer site to store the necessary data in several different ways, including several SQL databases and normal files on disk.

# IMMEDIATE MODE

In the flow described above, the user may need to confirm to the OpenID provider that it's ok to disclose his or her identity. The provider may draw pages asking for information from the user before it redirects the browser back to the consumer's site. This is generally transparent to the consumer site, so it is typically ignored as an implementation detail.

There can be times, however, where the consumer site wants to get a response immediately. When this is the case, the consumer can put the library in immediate mode. In immediate mode, there is an extra response possible from the server, which is essentially the server reporting that it doesn't have enough information to answer the question yet.

# USING THIS LIBRARY

Integrating this library into an application is usually a relatively straightforward process. The process should basically follow this plan:

Add an OpenID login field somewhere on your site. When an OpenID is entered in that field and the form is submitted, it should make a request to the your site which includes that OpenID URL.

First, the application should instantiate a Consumer with a session for per-user state and store for shared state using the store of choice.

Next, the application should call the `begin` method of Consumer instance. This method takes the OpenID URL as entered by the user. The `begin` method returns a CheckIDRequest object.

Next, the application should call the redirect_url method on the CheckIDRequest object. The parameter `return_to` is the URL that the OpenID server will send the user back to after attempting to verify his or her identity. The `realm` parameter is the URL (or URL pattern) that identifies your web site to the user when he or she is authorizing it. Send a redirect to the resulting URL to the user's browser.

That's the first half of the authentication process. The second half of the process is done after the user's OpenID Provider sends the user's browser a redirect back to your site to complete their login.

When that happens, the user will contact your site at the URL given as the `return_to` URL to the redirect_url call made above. The request will have several query parameters added to the URL by the OpenID provider as the information necessary to finish the request.

Get a Consumer instance with the same session and store as before and call its complete() method, passing in all the received query arguments and URL currently being handled.

There are multiple possible return types possible from that method. These indicate the whether or not the login was successful, and include any additional information appropriate for their type.

### Constants

CANCEL

  Code returned by OpenIDConsumer.complete_auth when the user cancels the operation from the server.

FAILURE

  Code OpenID::OpenIDConsumer.complete_auth returns when the value it received indicated an invalid login.

SETUP_NEEDED

  Code returned by OpenID::OpenIDConsumer.complete_auth when the OpenIDConsumer instance is in immediate mode and ther server sends back a URL for the user to login with.

SUCCESS

  Code returned when either the of the OpenID::OpenIDConsumer.begin_auth or OpenID::OpenIDConsumer.complete_auth methods return successfully.

### Attributes

session_key_prefix[RW]

### Public Class Methods

new(session, store) click to toggle source

Initialize a Consumer instance.

You should create a new instance of the Consumer object with every HTTP request that handles OpenID transactions.

session: the session object to use to store request information. The session should behave like a hash.

store: an object that implements the interface in Store.

    # File lib/openid/consumer.rb, line 192
    def initialize(session, store)
      @origin_session = session
      @session = Session.new(session, OpenID::OpenIDServiceEndpoint)
      @store = store
      @session_key_prefix = 'OpenID::Consumer::'
    end

openid1_return_to_claimed_id_name() click to toggle source

See openid1_return_to_claimed_id_name=

    # File lib/openid/consumer/idres.rb, line 42
    def self.openid1_return_to_claimed_id_name
      @openid1_return_to_claimed_id_name
    end

openid1_return_to_claimed_id_name=(query_arg_name) click to toggle source

Set the name of the query parameter that this library will use to thread the requested URL through an OpenID 1 transaction (for use when verifying discovered information). It will be appended to the return_to URL.

    # File lib/openid/consumer/idres.rb, line 37
    def self.openid1_return_to_claimed_id_name=(query_arg_name)
      @openid1_return_to_claimed_id_name = query_arg_name
    end

openid1_return_to_nonce_name() click to toggle source

See openid1_return_to_nonce_name= documentation

    # File lib/openid/consumer/idres.rb, line 29
    def self.openid1_return_to_nonce_name
      @openid1_return_to_nonce_name
    end

openid1_return_to_nonce_name=(query_arg_name) click to toggle source

Set the name of the query parameter that this library will use to thread a nonce through an OpenID 1 transaction. It will be appended to the return_to URL.

    # File lib/openid/consumer/idres.rb, line 24
    def self.openid1_return_to_nonce_name=(query_arg_name)
      @openid1_return_to_nonce_name = query_arg_name
    end

### Public Instance Methods

begin(openid_identifier, anonymous=false) click to toggle source

Start the OpenID authentication process. See steps 1-2 in the overview for the Consumer class.

user_url: Identity URL given by the user. This method performs a textual transformation of the URL to try and make sure it is normalized. For example, a user_url of example.com will be normalized to example.com/ normalizing and resolving any redirects the server might issue.

anonymous: A boolean value. Whether to make an anonymous request of the OpenID provider. Such a request does not ask for an authorization assertion for an OpenID identifier, but may be used with extensions to pass other data. e.g. "I don't care who you are, but I'd like to know your time zone."

Returns a CheckIDRequest object containing the discovered information, with a method for building a redirect URL to the server, as described in step 3 of the overview. This object may also be used to add extension arguments to the request, using its add_extension_arg method.

Raises DiscoveryFailure when no OpenID server can be found for this URL.

    # File lib/openid/consumer.rb, line 222
    def begin(openid_identifier, anonymous=false)
      manager = discovery_manager(openid_identifier)
      service = manager.get_next_service(&method(:discover))

      if service.nil?
        raise DiscoveryFailure.new("No usable OpenID services were found "                                    "for #{openid_identifier.inspect}", nil)
      else
        begin_without_discovery(service, anonymous)
      end
    end

begin_without_discovery(service, anonymous) click to toggle source

Start OpenID verification without doing OpenID server discovery. This method is used internally by Consumer.begin() after discovery is performed, and exists to provide an interface for library users needing to perform their own discovery.

service: an OpenID service endpoint descriptor. This object and factories for it are found in the openid/consumer/discovery.rb module.

Returns an OpenID authentication request object.

    # File lib/openid/consumer.rb, line 244
    def begin_without_discovery(service, anonymous)
      assoc = association_manager(service).get_association
      checkid_request = CheckIDRequest.new(assoc, service)
      checkid_request.anonymous = anonymous

      if service.compatibility_mode
        rt_args = checkid_request.return_to_args
        rt_args[Consumer.openid1_return_to_nonce_name] = Nonce.mk_nonce
        rt_args[Consumer.openid1_return_to_claimed_id_name] =
          service.claimed_id
      end

      self.last_requested_endpoint = service
      return checkid_request
    end

complete(query, current_url) click to toggle source

Returns a subclass of Response. The type of response is indicated by the status attribute, which will be one of SUCCESS, CANCEL, FAILURE, or SETUP_NEEDED.

    # File lib/openid/consumer.rb, line 283
    def complete(query, current_url)
      message = Message.from_post_args(query)
      mode = message.get_arg(OPENID_NS, 'mode', 'invalid')
      begin
        meth = method('complete_' + mode)
      rescue NameError
        meth = method(:complete_invalid)
      end
      response = meth.call(message, current_url)
      cleanup_last_requested_endpoint
      if [SUCCESS, CANCEL].member?(response.status)
        cleanup_session
      end
      return response
    end

### Protected Instance Methods

association_manager(service) click to toggle source

    # File lib/openid/consumer.rb, line 342
    def association_manager(service)
      AssociationManager.new(@store, service.server_url,
                             service.compatibility_mode, negotiator)
    end

cleanup_last_requested_endpoint() click to toggle source

    # File lib/openid/consumer.rb, line 321
    def cleanup_last_requested_endpoint
      @session[session_key('last_requested_endpoint')] = nil
    end

cleanup_session() click to toggle source

    # File lib/openid/consumer.rb, line 329
    def cleanup_session
      discovery_manager(nil).cleanup(true)
    end

complete_cancel(unused_message, unused_return_to) click to toggle source

    # File lib/openid/consumer.rb, line 357
    def complete_cancel(unused_message, unused_return_to)
      return CancelResponse.new(last_requested_endpoint)
    end

complete_error(message, unused_return_to) click to toggle source

    # File lib/openid/consumer.rb, line 361
    def complete_error(message, unused_return_to)
      error = message.get_arg(OPENID_NS, 'error')
      contact = message.get_arg(OPENID_NS, 'contact')
      reference = message.get_arg(OPENID_NS, 'reference')

      return FailureResponse.new(last_requested_endpoint,
                                 error, contact, reference)
    end

complete_id_res(message, current_url) click to toggle source

    # File lib/openid/consumer.rb, line 379
    def complete_id_res(message, current_url)
      if message.is_openid1
        setup_url = message.get_arg(OPENID_NS, 'user_setup_url')
        if !setup_url.nil?
          return SetupNeededResponse.new(last_requested_endpoint, setup_url)
        end
      end

      begin
        idres = handle_idres(message, current_url)
      rescue OpenIDError => why
        return FailureResponse.new(last_requested_endpoint, why.message)
      else
        return SuccessResponse.new(idres.endpoint, message,
                                     idres.signed_fields)
      end
    end

complete_invalid(message, unused_return_to) click to toggle source

    # File lib/openid/consumer.rb, line 351
    def complete_invalid(message, unused_return_to)
      mode = message.get_arg(OPENID_NS, 'mode', '<No mode set>')
      return FailureResponse.new(last_requested_endpoint,
                                 "Invalid openid.mode: #{mode}")
    end

complete_setup_needed(message, unused_return_to) click to toggle source

    # File lib/openid/consumer.rb, line 370
    def complete_setup_needed(message, unused_return_to)
      if message.is_openid1
        return complete_invalid(message, nil)
      else
        setup_url = message.get_arg(OPENID2_NS, 'user_setup_url')
        return SetupNeededResponse.new(last_requested_endpoint, setup_url)
      end
    end

discover(identifier) click to toggle source

    # File lib/openid/consumer.rb, line 334
    def discover(identifier)
      OpenID.discover(identifier)
    end

discovery_manager(openid_identifier) click to toggle source

    # File lib/openid/consumer.rb, line 325
    def discovery_manager(openid_identifier)
      DiscoveryManager.new(@origin_session, openid_identifier, @session_key_prefix)
    end

handle_idres(message, current_url) click to toggle source

    # File lib/openid/consumer.rb, line 347
    def handle_idres(message, current_url)
      IdResHandler.new(message, current_url, @store, last_requested_endpoint)
    end

last_requested_endpoint() click to toggle source

    # File lib/openid/consumer.rb, line 313
    def last_requested_endpoint
      session_get('last_requested_endpoint')
    end

last_requested_endpoint=(endpoint) click to toggle source

    # File lib/openid/consumer.rb, line 317
    def last_requested_endpoint=(endpoint)
      session_set('last_requested_endpoint', endpoint)
    end

negotiator() click to toggle source

    # File lib/openid/consumer.rb, line 338
    def negotiator
      DefaultNegotiator
    end

session_get(name) click to toggle source

    # File lib/openid/consumer.rb, line 301
    def session_get(name)
      @session[session_key(name)]
    end

session_key(suffix) click to toggle source

    # File lib/openid/consumer.rb, line 309
    def session_key(suffix)
      @session_key_prefix + suffix
    end

session_set(name, val) click to toggle source

    # File lib/openid/consumer.rb, line 305
    def session_set(name, val)
      @session[session_key(name)] = val
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.