Camping.goes :PgPirate
class String
def to_html
self.gsub("\n", "
")
end
def strip_html
self.gsub(/
/, "\n").gsub(/<\/?[^>]*>/, "")
end
end
module PgPirate::Controllers
class Index < R '/'
def get
render :test
end
end
class PassPhrase < R '/passphrase'
def get
passphrase :prompt
end
def post
GpgmeHelper.passphrase = input[:passphrase]
passphrase :success
end
end
class Verify < R '/verify'
def post
@response = {:valid => GpgmeHelper.verify(input[:message],
input[:signature]),
:message => input[:message].to_html,
:id => input[:id]}
render :response
end
end
class Decrypt < R '/decrypt'
def post
begin
message = GpgmeHelper.decrypt(input[:cipher])
rescue Exception => e
end
@response = {:id => input[:id],
:message => message || ""}
if message
render :decrypt_success
else
render :decrypt_fail
end
end
end
class Static < R '/static/(css|js|icons|images)/(.+)'
MIME_TYPES = {'.css' => 'text/css', '.js' => 'text/javascript', '.png' => 'image/png'}
def get(dir, path)
@headers['Content-Type'] = MIME_TYPES[path[/\.\w+$/, 0]] || "text/plain"
@headers['X-Sendfile'] = File.join(File.expand_path('~/.mouseHole/pgpirate/'), dir, path)
end
end
end
module PgPirate::Views
def passphrase(meth)
html do
head do
title "PgPirate"
link :rel => 'stylesheet', :href => 'static/css/pgpirate.css', :type => 'text/css'
end
body do
send(meth)
end
end
end
def prompt
p "Avast! Surrender your secret passphrase, ye scurvy ridden bilge rat!"
form :action => '/passphrase', :method => 'post' do
input :name => "passphrase", :type => "password"
br
input :type => "submit", :value => "Do what the pirate says"
end
end
def success
p "Ye got smarts, matey"
end
def response
div do
@response[:message]
end
script "$('pgpstatus#{@response[:id]}').innerHTML = " +
"'PGP Signature #{@response[:valid] ? "Valid" : "Invalid"}'"
end
def decrypt_success
div @response[:message]
script "$('pgpstatus#{@response[:id]}').innerHTML = " +
"'PGP Decryption Successful'"
end
def decrypt_fail
div do
span "Could not decrypt PGP message. Have you specified a "
a "pass phrase", :href => "http://localhost:3704/pgpirate/passphrase"
span "?"
end
script "$('pgpstatus#{@response[:id]}').innerHTML = " +
"'PGP Decryption Failed'"
end
end
class GpgmeHelper
class << self
attr_accessor :passphrase
def passphrase
@passphrase ||= ""
end
end
def self.verify(plain, sig)
ctx = GPGME::GpgmeCtx.new
ctx.armor = true
ctx.set_passphrase_cb(proc {|h,d,r|
if d;
""
end
})
gpgme_plain = GPGME::GpgmeData.new
gpgme_plain.write(plain)
gpgme_sig = GPGME::GpgmeData.new
gpgme_sig.write(sig)
ctx.verify(gpgme_sig, gpgme_plain) == 1
end
def self.decrypt(cipher)
ctx = GPGME::GpgmeCtx.new
ctx.armor = true
ctx.set_passphrase_cb(proc {|h,d,r|
if d;
self.passphrase
end
})
gpgme_cipher = GPGME::GpgmeData.new
gpgme_cipher.write(cipher)
begin
ret = ctx.decrypt(gpgme_cipher).read
rescue GPGME::GpgmeError
ret = nil
end
end
end