Module: OpenID::Util

### Home Classes Methods

### In Files

- lib/openid/kvform.rb
- lib/openid/util.rb

### Methods

- ::append_args
- ::assert
- ::auto_submit_html
- ::dict_to_kv
- ::from_base64
- ::html_encode
- ::kv_to_dict
- ::kv_to_seq
- ::log
- ::logger
- ::logger=
- ::parse_query
- ::seq_to_kv
- ::to_base64
- ::urlencode

### 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::Util

### Constants

BASE64_CHARS
BASE64_RE
ESCAPE_TABLE

### Public Class Methods

append_args(url, args) click to toggle source

    # File lib/openid/util.rb, line 67
    def Util.append_args(url, args)
      url = url.dup
      return url if args.length == 0

      if args.respond_to?('each_pair')
        args = args.sort
      end

      url << (url.include?("?") ? "&" : "?")
      url << Util.urlencode(args)
    end

assert(value, message=nil) click to toggle source

    # File lib/openid/util.rb, line 27
    def Util.assert(value, message=nil)
      if not value
        raise AssertionError, message or value
      end
    end

auto_submit_html(form, title='OpenID transaction in progress') click to toggle source

    # File lib/openid/util.rb, line 95
        def Util.auto_submit_html(form, title='OpenID transaction in progress')
          return "
    <html>
    <head>
      <title>#{title}</title>
    </head>
    <body onload='document.forms[0].submit();'>
    #{form}
    <script>
    var elements = document.forms[0].elements;
    for (var i = 0; i < elements.length; i++) {
      elements[i].style.display = \"none\";
    }
    </script>
    </body>
    </html>
    "
        end

dict_to_kv(d) click to toggle source

    # File lib/openid/kvform.rb, line 127
    def Util.dict_to_kv(d)
      return seq_to_kv(d.entries.sort)
    end

from_base64(s) click to toggle source

    # File lib/openid/util.rb, line 37
    def Util.from_base64(s)
      without_newlines = s.gsub(/[\r\n]+/, '')
      if !BASE64_RE.match(without_newlines)
        raise ArgumentError, "Malformed input: #{s.inspect}"
      end
      without_newlines.unpack('m').first
    end

html_encode(str) click to toggle source

Modified from ERb's html_encode

    # File lib/openid/util.rb, line 116
    def Util.html_encode(str)
      str.to_s.gsub(/[&<>"']/) {|s| ESCAPE_TABLE[s] }
    end

kv_to_dict(s) click to toggle source

    # File lib/openid/kvform.rb, line 131
    def Util.kv_to_dict(s)
      seq = kv_to_seq(s)
      return Hash[*seq.flatten]
    end

kv_to_seq(data, strict=false) click to toggle source

    # File lib/openid/kvform.rb, line 63
    def Util.kv_to_seq(data, strict=false)
      # After one parse, seq_to_kv and kv_to_seq are inverses, with no
      # warnings:
      #
      # seq = kv_to_seq(s)
      # seq_to_kv(kv_to_seq(seq)) == seq
      err = lambda { |msg|
        msg = "kv_to_seq warning: #{msg}: #{data.inspect}"
        if strict
          raise KVFormError, msg
        else
          Util.log(msg)
        end
      }

      lines = data.split("\n")
      if data.length == 0
        return []
      end

      if data[-1].chr != "\n"
        err.call("Does not end in a newline")
        # We don't expect the last element of lines to be an empty
        # string because split() doesn't behave that way.
      end

      pairs = []
      line_num = 0
      lines.each { |line|
        line_num += 1

        # Ignore blank lines
        if line.strip() == ""
          next
        end

        pair = line.split(':', 2)
        if pair.length == 2
          k, v = pair
          k_s = k.strip()
          if k_s != k
            msg = "In line #{line_num}, ignoring leading or trailing whitespace in key #{k.inspect}"
            err.call(msg)
          end

          if k_s.length == 0
            err.call("In line #{line_num}, got empty key")
          end

          v_s = v.strip()
          if v_s != v
            msg = "In line #{line_num}, ignoring leading or trailing whitespace in value #{v.inspect}"
            err.call(msg)
          end

          pairs << [k_s, v_s]
        else
          err.call("Line #{line_num} does not contain a colon")
        end
      }

      return pairs
    end

log(message) click to toggle source

change the message below to do whatever you like for logging

    # File lib/openid/util.rb, line 91
    def Util.log(message)
      logger.info(message)
    end

logger() click to toggle source

    # File lib/openid/util.rb, line 86
    def Util.logger
      @@logger
    end

logger=(logger) click to toggle source

    # File lib/openid/util.rb, line 82
    def Util.logger=(logger)
      @@logger = logger
    end

parse_query(qs) click to toggle source

    # File lib/openid/util.rb, line 61
    def Util.parse_query(qs)
      query = {}
      CGI::parse(qs).each {|k,v| query[k] = v[0]}
      return query
    end

seq_to_kv(seq, strict=false) click to toggle source

    # File lib/openid/kvform.rb, line 9
    def Util.seq_to_kv(seq, strict=false)
      # Represent a sequence of pairs of strings as newline-terminated
      # key:value pairs. The pairs are generated in the order given.
      #
      # @param seq: The pairs
      #
      # returns a string representation of the sequence
      err = lambda { |msg|
        msg = "seq_to_kv warning: #{msg}: #{seq.inspect}"
        if strict
          raise KVFormError, msg
        else
          Util.log(msg)
        end
      }

      lines = []
      seq.each { |k, v|
        if !k.is_a?(String)
          err.call("Converting key to string: #{k.inspect}")
          k = k.to_s
        end

        if !k.index("\n").nil?
          raise KVFormError, "Invalid input for seq_to_kv: key contains newline: #{k.inspect}"
        end

        if !k.index(":").nil?
          raise KVFormError, "Invalid input for seq_to_kv: key contains colon: #{k.inspect}"
        end

        if k.strip() != k
          err.call("Key has whitespace at beginning or end: #{k.inspect}")
        end

        if !v.is_a?(String)
          err.call("Converting value to string: #{v.inspect}")
          v = v.to_s
        end

        if !v.index("\n").nil?
          raise KVFormError, "Invalid input for seq_to_kv: value contains newline: #{v.inspect}"
        end

        if v.strip() != v
          err.call("Value has whitespace at beginning or end: #{v.inspect}")
        end

        lines << k + ":" + v + "\n"
      }

      return lines.join("")
    end

to_base64(s) click to toggle source

    # File lib/openid/util.rb, line 33
    def Util.to_base64(s)
      [s].pack('m').gsub("\n", "")
    end

urlencode(args) click to toggle source

    # File lib/openid/util.rb, line 45
    def Util.urlencode(args)
      a = []
      args.each do |key, val|
        if val.nil?
          val = ''
        elsif !!val == val
          #it's boolean let's convert it to string representation
          # or else CGI::escape won't like it
          val = val.to_s
        end

        a << (CGI::escape(key) + "=" + CGI::escape(val))
      end
      a.join("&")
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.