Skip to content

Instantly share code, notes, and snippets.

@chuckwagoncomputing
Created May 1, 2013 23:12
Show Gist options
  • Save chuckwagoncomputing/5499111 to your computer and use it in GitHub Desktop.
Save chuckwagoncomputing/5499111 to your computer and use it in GitHub Desktop.
Camping OpenID Fail
Camping.goes :Test
module Test::Controllers
class Home < R '/'
def get
render :home
end
end
class Login < R '/openid'
def get
require 'openid'
require 'openid/extensions/sreg'
OpenID::Util.logger.sev_threshold = Logger::WARN
# return to login if canceled
return redirect('/') if input['openid.mode'] == 'cancel'
this_url = URL(Login).to_s
unless input.finish.to_s == '1'
# start doing the auth here
begin
@state.openid_request = Hash.new
oid_request = OpenID::Consumer.new(@state.openid_request, nil).begin(input.identity)
# start sreg
sreg = OpenID::SReg::Request.new
sreg.request_fields(['nickname', 'fullname'], false)
oid_request.add_extension(sreg)
oid_request.return_to_args['finish'] = '1'
redirect(oid_request.redirect_url(URL('/').to_s, this_url))
rescue OpenID::DiscoveryFailure
'Couldn\'t find an OpenID at that address, are you sure it is one?'
end
else
# finish the auth here
response = OpenID::Consumer.new(@state.openid_request || {}, nil).complete(input, this_url)
@state.delete('openid_request')
case response.status
when OpenID::Consumer::SUCCESS
@state.identity = response.identity_url.to_s
# start sreg
sreg = OpenID::SReg::Response.from_success_response(response)
unless sreg.empty?
@state.default_username = sreg['fullname'] || sreg['nickname'] || nil
end
# end sreg
grab_avatar
when_done = @cookies.once_logged_in
@cookies.delete('once_logged_in')
redirect(when_done || Home)
when OpenID::Consumer::FAILURE
'The OpenID thing doesn\'t think you really are that person, they said: ' + response.message
else
'Crazy response is crazy: ' + response.inspect
end
end
end
def post; get; end
end
end
module Test::Views
def home
text 'You made it home'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment