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

#name, #node, #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.



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

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>.



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

def challenge
  @challenge
end

#keyObject (readonly)

The key associated with this <keygen> tag.



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

def key
  @key
end

Instance Method Details

#generate_key(key_size = 2048) ⇒ Object

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



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

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