File: passwordmanager.rb

package info (click to toggle)
libnora-ruby 1%3A0.0.20041021-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 408 kB
  • ctags: 722
  • sloc: ruby: 5,186; ansic: 669; makefile: 266; sql: 10
file content (54 lines) | stat: -rwxr-xr-x 1,597 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
module Web
  class Agent
    class PasswordManager
      def initialize
        @db=Hash.new
      end

      def get(agent)
        return unless agent.rsp
        agent.req.header.delete 'authorization'
        text=agent.rsp.header['www-authenticate']
        if text
          if text=~/basic\s+realm\s*=\s*["']?([^"']+)["']?/i    #'
            realm = $1
            pass = @db[[agent.uri.scheme,agent.uri.host,agent.uri.port,realm]]
            if pass
              agent.req.header.add 'authorization', "Basic " + ["#{pass.username}:#{pass.password}"].pack('m').chomp
            end
          else
            raise "authenticate error"
          end
        end
      end

      def set(agent, username, password)
        text=agent.rsp.header['www-authenticate']
        if text
          if text=~/basic\s+realm\s*=\s*["']?([^"']+)["']?/i    #'
            realm = $1
            @db[[agent.uri.scheme,agent.uri.host,agent.uri.port,realm]] = Password.new(agent.uri.scheme,agent.uri.host,agent.uri.port,realm,username,password)
          else
            raise "authenticate error"
          end
        end
      end

      def clear
        @db.clear
      end

      class Password
        def initialize(scheme, host, port, realm, username, password)
          @scheme   = scheme
          @host     = host
          @port     = port
          @realm    = realm
          @username = username
          @password = password
        end
        attr_accessor :scheme, :host, :port, :realm, :username, :password
      end # Password
    end # PasswordManager
  end # Agent
end # Web