Class: Mechanize::Form::Keygen

Inherits:
Field
  • Object
show all
Defined in:
lib/mechanize/form/keygen.rb

Overview

This class represents a keygen (public / private key generator) found in a Form. The field will automatically generate a key pair and compute its own value to match the challenge. Call key to access the public/private key pair.

Instance Attribute Summary collapse

Attributes inherited from Field

#index, #name, #node, #raw_value, #type, #value

Instance Method Summary collapse

Methods inherited from Field

#<=>, #dom_class, #dom_id, #inspect, #query_value

Constructor Details

#initialize(node, value = nil) ⇒ Keygen

Returns a new instance of Keygen.



15
16
17
18
19
20
21
22
23
24
# File 'lib/mechanize/form/keygen.rb', line 15

def initialize(node, value = nil)
  super
  @challenge = node['challenge']

  @spki = OpenSSL::Netscape::SPKI.new
  @spki.challenge = @challenge

  @key = nil
  generate_key if value.nil? || value.empty?
end

Instance Attribute Details

#challengeObject (readonly)

The challenge for this <keygen>.



10
11
12
# File 'lib/mechanize/form/keygen.rb', line 10

def challenge
  @challenge
end

#keyObject (readonly)

The key associated with this <keygen> tag.



13
14
15
# File 'lib/mechanize/form/keygen.rb', line 13

def key
  @key
end

Instance Method Details

#generate_key(key_size = 2048) ⇒ Object

Generates a key pair and sets the field’s value.



27
28
29
30
31
32
33
# File 'lib/mechanize/form/keygen.rb', line 27

def generate_key(key_size = 2048)
  # Spec at http://dev.w3.org/html5/spec/Overview.html#the-keygen-element
  @key = OpenSSL::PKey::RSA.new key_size
  @spki.public_key = @key.public_key
  @spki.sign @key, OpenSSL::Digest::MD5.new
  self.value = @spki.to_pem
end