class Giticious::Service::Pubkey

Public Class Methods

new() click to toggle source
# File lib/giticious/service/pubkey.rb, line 5
def initialize
    @filename = "#{Dir.home}/.ssh/authorized_keys"

    if false == File.exists?(@filename)
      FileUtils.touch(@filename)
    end

    if false == File.owned?(@filename)
      raise RuntimeError, "File #{@filename} is not owned by the current user"
    end
end

Public Instance Methods

add(user, pubkey) click to toggle source
# File lib/giticious/service/pubkey.rb, line 27
def add(user, pubkey)
  if exists?(pubkey)
    raise RuntimeError, "This public key does already exist"
  end

  append_line('command="/usr/local/bin/giticious gitserve ' + user + '",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ' + pubkey)
end
all() click to toggle source
# File lib/giticious/service/pubkey.rb, line 39
def all
  matches = {}

  File.open(@filename) do |file|
    file.each_line do |line|
      res = line.match(/gitserve ([A-Za-z0-9_]+)\".*no\-pty (.*)/)

      if res.nil? == false
        username = res[1]
        key = res[2]

        if matches.has_key?(username) == false
          matches[username] = []
        end

        matches[username] << key
      end
    end
  end

  matches
end
delete(pubkey) click to toggle source
# File lib/giticious/service/pubkey.rb, line 35
def delete(pubkey)
  delete_matches(pubkey)
end
exists?(pubkey) click to toggle source
# File lib/giticious/service/pubkey.rb, line 17
def exists?(pubkey)
  File.open(@filename) do |file|
    file.each_line do |line|
      return true if line.include?(pubkey)
    end
  end

  false
end
user_pubkeys(user) click to toggle source
# File lib/giticious/service/pubkey.rb, line 62
def user_pubkeys(user)
  pubkeys = all()

  if pubkeys.has_key?(user)
    return pubkeys[user]
  end

  return []
end

Protected Instance Methods

append_line(line) click to toggle source
# File lib/giticious/service/pubkey.rb, line 77
def append_line(line)
  IO.write(@filename, "#{line}\n", mode: "a")
end
delete_matches(search) click to toggle source
# File lib/giticious/service/pubkey.rb, line 81
def delete_matches(search)
  if search == ""
    return nil
  end

  new_file = ""

  File.open(@filename) do |file|
    file.each_line do |line|
      new_file += "#{line}" unless line.include?(search)
    end
  end

  IO.write(@filename, new_file, mode: "w+")
end
file_contents() click to toggle source
# File lib/giticious/service/pubkey.rb, line 73
def file_contents
  File.read(@filename)
end