Class: GitHandler::PublicKey

Inherits:
Object
  • Object
show all
Defined in:
lib/git_handler/public_key.rb

Constant Summary collapse

COMMAND_OPTIONS =
[
  'no-port-forwarding',
  'no-X11-forwarding',
  'no-agent-forwarding',
  'no-pty'
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content = nil) ⇒ PublicKey

Returns a new instance of PublicKey.



15
16
17
18
19
20
21
22
23
# File 'lib/git_handler/public_key.rb', line 15

def initialize(content=nil)
  @content = cleanup_content(content)
  if @content.empty?
    raise ArgumentError, 'Key content is empty!'
  end
  unless valid?
    raise ArgumentError, "Is not a valid public key!"
  end
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



13
14
15
# File 'lib/git_handler/public_key.rb', line 13

def content
  @content
end

Instance Method Details

#md5String

Get public key MD5 checksum

Returns:

  • (String)


33
34
35
# File 'lib/git_handler/public_key.rb', line 33

def md5
  Digest::MD5.hexdigest(@content)
end

#sha1String

Get public key SHA1 checksum

Returns:

  • (String)


39
40
41
# File 'lib/git_handler/public_key.rb', line 39

def sha1
  Digest::SHA1.hexdigest(@content)
end

#to_system_key(command) ⇒ String

Convert public key to system key with arbitrary command

Parameters:

  • command (String)

    arbitrary command

Returns:

  • (String)


46
47
48
# File 'lib/git_handler/public_key.rb', line 46

def to_system_key(command)
  "command=\"#{command}\",#{COMMAND_OPTIONS.join(",")} #{@content}"
end

#valid?Boolean

Check if public key contents is valid

Returns:

  • (Boolean)


27
28
29
# File 'lib/git_handler/public_key.rb', line 27

def valid?
  SSHKey.valid_ssh_public_key?(@content)
end