Skip to content

Instantly share code, notes, and snippets.

@oestrich
Created March 12, 2014 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oestrich/9508278 to your computer and use it in GitHub Desktop.
Save oestrich/9508278 to your computer and use it in GitHub Desktop.
smartchat test client
require 'faraday'
require 'json'
def hash_password_for_private_key(password, sha_klass = OpenSSL::Digest::SHA256)
sha256 = sha_klass.new
1000.times.inject(password) do |hash, _|
sha256.hexdigest(hash)
end
end
class Middleware < Faraday::Middleware
def call(env)
env[:request_headers]["Content-Type"] = "application/json"
env[:request_headers]["Accept"] = "application/json"
@app.call(env)
end
end
client = Faraday.new(:url => "http://localhost:5000/") do |f|
f.use Middleware
f.adapter Faraday.default_adapter
end
def sign_url(private_key, url)
digest = OpenSSL::Digest::SHA256.new
Base64.encode64(private_key.sign(digest, url))
end
response = client.get("/")
response_body = JSON.parse(response.body)
client.basic_auth("eric", "password")
response = client.post(response_body["_links"]["smartchat:user-sign-in"]["href"])
response_body = JSON.parse(response.body)
private_key = OpenSSL::PKey::RSA.new(response_body["private_key"], hash_password_for_private_key("password"))
client.basic_auth("eric", sign_url(private_key, "http://localhost:5000/"))
response = client.get("http://localhost:5000/")
response_body = JSON.parse(response.body)
media_url = response_body["_links"]["smartchat:media"]["href"]
client.basic_auth("eric", sign_url(private_key, media_url))
response = client.post(media_url, {
:media => {
:friend_ids => [1],
:file_name => "smartchat.png",
:file => Base64.encode64(File.read("/home/eric/Downloads/geordi.jpg"))
}
}.to_json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment