Sha256: b3a6025bb28c26de812c589bd96f34f74dd5e0a0222d9f941f02844fff3097b6

Contents?: true

Size: 1.7 KB

Versions: 5

Compression:

Stored size: 1.7 KB

Contents

module Potato
  module DAmn
    # Interface to grab dAmn authtokens.
    module Token
      extend self
      
      # Converts a hash to a URL query string.
      # @param [Hash<String, String>] hash
      # @return [String]
      def safe_qstring hash
        hash.map do |a,b|
          a + "=" + b.to_s.gsub(/([^A-Za-z0-9\-])/) do
            '%' + $1.unpack("U*")[0].to_s(16)
          end
        end.join("&")
      end
      
      # Gets an authtoken.
      # @param [String] username
      # @param [String] password
      # @return [String, nil]
      def get username, password
        uri = URI.parse("https://www.deviantart.com/users/login")
        http = Net::HTTP.new(uri.host, uri.port)
        http.use_ssl = true
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
        cookies = nil
        begin
          http.start do
            data = Token.safe_qstring({"username" => username, "password" => password, "remember_me" => 1})
            http.request_post(uri.path,data) do |res|
              uri = URI.parse("http://chat.deviantart.com/chat/Botdom")
              Net::HTTP.new(uri.host, uri.port).start do |ht|
                request = Net::HTTP::Get.new(uri.path)
                request["cookie"] = res["Set-Cookie"].scan(/[a-z_]+=[^ ]{20,}/).join(";")
                ht.request(request) do |req|
                  if req.body.empty?
                    return nil
                  else
                    return req.body.scan(/[0-9a-f]{32}/)[0]
                  end
                end
              end
            end
          end
        rescue Errno::ECONNRESET
          warn "Connection reset by peer when attempting to retrieve authtoken."
          nil
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
potato-0.0.12 lib/potato/damn/token.rb
potato-0.0.11 lib/potato/damn/token.rb
potato-0.0.10 lib/potato/damn/token.rb
potato-0.0.7 lib/potato/damn/token.rb
potato-0.0.6 lib/potato/damn/token.rb