Skip to content

Instantly share code, notes, and snippets.

@amscotti
Created April 17, 2013 02:20
Show Gist options
  • Select an option

  • Save amscotti/5401306 to your computer and use it in GitHub Desktop.

Select an option

Save amscotti/5401306 to your computer and use it in GitHub Desktop.
jQuery(function($) {
function gotAssertion(assertion) {
// got an assertion, now send it up to the server for verification
if (assertion !== null) {
$.ajax({
type: 'POST',
url: '/auth/login',
data: { assertion: assertion },
success: function(res, status, xhr) {
window.location.reload();
},
error: function(res, status, xhr) {
alert("login failure" + res);
}
});
}
};
$('#browserid').click(function() {
navigator.id.get(gotAssertion);
});
});
require "rubygems"
require "haml"
require "sinatra"
require 'nestful'
require 'digest/md5'
enable :sessions
helpers do
def login?
return !session[:email].nil?
end
def getGravatarURL
return "http://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(session[:email].strip.downcase)}"
end
end
get "/" do
haml :index
end
post "/auth/login" do
if params[:assertion]
data = Nestful.post "https://browserid.org/verify", :format => :json, :params => { :assertion => "#{params[:assertion]}", :audience => "http://#{request.host}:#{request.port}" }
if data["status"] == "okay"
session[:email] = data["email"]
return data.to_json
end
end
return {:status => "error"}.to_json
end
get "/auth/logout" do
session[:email] = nil
redirect "/"
end
-if login?
%p="Welcome #{session[:email]}!"
#avatar
%img{:src => getGravatarURL, :alt => "Gravatar"}
%a{:href => "/auth/logout", :title => "Sign-out"} Sign-out
-else
%a#browserid{:href => "#", :title=>"Sign-in with BrowserID"}
%img{:src=> "/images/sign_in_blue.png", :alt=>"Sign-in with BrowserID"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment