Class: Mechanize::HTTP::AuthStore

### Home Classes Methods

### In Files

- lib/mechanize/http/auth_store.rb

### Parent

Object

### Methods

- ::new
- #add_auth
- #add_default_auth
- #credentials?
- #credentials_for
- #remove_auth

### Files

- EXAMPLES.rdoc
- GUIDE.rdoc
- LICENSE.rdoc
- Manifest.txt
- README.rdoc

### Class/Module Index

Quicksearch

- Mechanize
- Mechanize::ChunkedTerminationError
- Mechanize::ContentTypeError
- Mechanize::Cookie
- Mechanize::CookieCMethods
- Mechanize::CookieDeprecated
- Mechanize::CookieIMethods
- Mechanize::CookieJar
- Mechanize::CookieJarIMethods
- Mechanize::DirectorySaver
- Mechanize::Download
- Mechanize::ElementMatcher
- Mechanize::ElementNotFoundError
- Mechanize::Error
- Mechanize::File
- Mechanize::FileConnection
- Mechanize::FileRequest
- Mechanize::FileResponse
- Mechanize::FileSaver
- Mechanize::Form
- Mechanize::Form::Button
- Mechanize::Form::CheckBox
- Mechanize::Form::Field
- Mechanize::Form::FileUpload
- Mechanize::Form::Hidden
- Mechanize::Form::ImageButton
- Mechanize::Form::Keygen
- Mechanize::Form::MultiSelectList
- Mechanize::Form::Option
- Mechanize::Form::RadioButton
- Mechanize::Form::Reset
- Mechanize::Form::SelectList
- Mechanize::Form::Submit
- Mechanize::Form::Text
- Mechanize::Form::Textarea
- Mechanize::HTTP
- Mechanize::HTTP::Agent
- Mechanize::HTTP::AuthChallenge
- Mechanize::HTTP::AuthRealm
- Mechanize::HTTP::AuthStore
- Mechanize::HTTP::ContentDispositionParser
- Mechanize::HTTP::WWWAuthenticateParser
- Mechanize::Headers
- Mechanize::History
- Mechanize::Image
- Mechanize::Page
- Mechanize::Page::Base
- Mechanize::Page::Frame
- Mechanize::Page::Image
- Mechanize::Page::Label
- Mechanize::Page::Link
- Mechanize::Page::MetaRefresh
- Mechanize::Parser
- Mechanize::PluggableParser
- Mechanize::Prependable
- Mechanize::RedirectLimitReachedError
- Mechanize::RedirectNotGetOrHeadError
- Mechanize::ResponseCodeError
- Mechanize::ResponseReadError
- Mechanize::RobotsDisallowedError
- Mechanize::TestCase
- Mechanize::UnauthorizedError
- Mechanize::UnsupportedSchemeError
- Mechanize::Util
- Mechanize::XmlFile
- HTTP
- HTTP::CookieJar

No matching classes.

# Mechanize::HTTP::AuthStore

A credential store for HTTP authentication.

    uri = URI 'http://example'

    store = Mechanize::HTTP::AuthStore.new
    store.add_auth uri, 'user1', 'pass'
    store.add_auth uri, 'user2', 'pass', 'realm'

    user, pass = store.credentials_for uri, 'realm' #=> 'user2', 'pass'
    user, pass = store.credentials_for uri, 'other' #=> 'user1', 'pass'

    store.remove_auth uri # removes all credentials

### Public Class Methods

new() click to toggle source

Creates a new AuthStore

    # File lib/mechanize/http/auth_store.rb, line 24
    def initialize
      @auth_accounts = Hash.new do |h, uri|
        h[uri] = {}
      end

      @default_auth = nil
    end

### Public Instance Methods

add_auth(uri, user, pass, realm = nil, domain = nil) click to toggle source

Adds credentials `user`, `pass` for the server at `uri`. If `realm` is set the credentials are used only for that realm. If `realm` is not set the credentials become the default for any realm on that URI.

`domain` and `realm` are exclusive as NTLM does not follow RFC

1. If `domain` is given it is only used for NTLM authentication.

    # File lib/mechanize/http/auth_store.rb, line 40
    def add_auth uri, user, pass, realm = nil, domain = nil
      uri = URI uri unless URI === uri

      raise ArgumentError,
            'NTLM domain given with realm which NTLM does not use' if
        realm and domain

      uri += '/'

      auth_accounts[uri][realm] = [user, pass, domain]

      self
    end

add_default_auth(user, pass, domain = nil) click to toggle source

USE OF add_default_auth IS NOT RECOMMENDED AS IT MAY EXPOSE PASSWORDS TO THIRD PARTIES

Adds credentials `user`, `pass` as the default authentication credentials. If no other credentials are available these will be returned from credentials_for.

If `domain` is given it is only used for NTLM authentication.

    # File lib/mechanize/http/auth_store.rb, line 64
    def add_default_auth user, pass, domain = nil
      warn You have supplied default authentication credentials that apply to ANY SERVER.Your username and password can be retrieved by ANY SERVER using Basicauthentication.THIS EXPOSES YOUR USERNAME AND PASSWORD TO DISCLOSURE WITHOUT YOUR KNOWLEDGE.Use add_auth to set authentication credentials that will only be deliveredonly to a particular server you specify.

      @default_auth = [user, pass, domain]
    end

credentials?(uri, challenges) click to toggle source

Returns true if credentials exist for the `challenges` from the server at `uri`.

    # File lib/mechanize/http/auth_store.rb, line 83
    def credentials? uri, challenges
      challenges.any? do |challenge|
        credentials_for uri, challenge.realm_name
      end
    end

credentials_for(uri, realm) click to toggle source

Retrieves credentials for `realm` on the server at `uri`.

    # File lib/mechanize/http/auth_store.rb, line 92
    def credentials_for uri, realm
      uri = URI uri unless URI === uri

      uri += '/'

      realms = @auth_accounts[uri]

      realms[realm] || realms[nil] || @default_auth
    end

remove_auth(uri, realm = nil) click to toggle source

Removes credentials for `realm` on the server at `uri`. If `realm` is not set all credentials for the server at `uri` are removed.

    # File lib/mechanize/http/auth_store.rb, line 106
    def remove_auth uri, realm = nil
      uri = URI uri unless URI === uri

      uri += '/'

      if realm then
        auth_accounts[uri].delete realm
      else
        auth_accounts.delete uri
      end

      self
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.