Skip to content

Instantly share code, notes, and snippets.

@vanstee
Created February 23, 2013 08:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vanstee/5018967 to your computer and use it in GitHub Desktop.
Save vanstee/5018967 to your computer and use it in GitHub Desktop.
Ruby script for AuthorizedKeysCommand that grabs public keys off github with the login
#!/usr/bin/env ruby
require 'rubygems'
require 'net/http'
require 'uri'
require 'json'
require 'pry'
class User
attr_accessor :login
def initialize(login)
self.login = login
end
def keys
GitHubClient.get("users/#{self.login}/keys")
end
end
class GitHubClient
class << self
def host
'api.github.com'
end
def uri(path = '')
URI.parse("https://#{host}/#{path}")
end
def client
client = Net::HTTP.new(uri.host, uri.port)
client.use_ssl = true
client.verify_mode = OpenSSL::SSL::VERIFY_NONE
client
end
def request(path)
puts uri(path)
Net::HTTP::Get.new(uri(path).request_uri)
end
def response(request)
parse(client.request(request).body)
end
def get(path)
response(request(path))
end
def parse(body)
JSON.parse(body)
end
end
end
class AuthorizedKeys
attr_accessor :keys
def self.for_login(login)
keys = User.new(login).keys
new(keys)
end
def initialize(keys)
self.keys = keys
end
def export
keys.map{ |key| export_key(key) }.join("\n")
end
private
def export_key(key)
key['key']
end
end
puts AuthorizedKeys.for_login(ARGV.first).export
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment