Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

Commit

Permalink
[project @ initial checkin]
Browse files Browse the repository at this point in the history
  • Loading branch information
tailor committed Dec 16, 2005
0 parents commit bb24b85
Show file tree
Hide file tree
Showing 225 changed files with 35,643 additions and 0 deletions.
20 changes: 20 additions & 0 deletions COPYING
@@ -0,0 +1,20 @@
Ruby-OpenID-Consumer Library

Copyright (C) 2005 Janrain, Inc

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

Contact:
eng+ruby@janrain.com
24 changes: 24 additions & 0 deletions INSTALL
@@ -0,0 +1,24 @@
Ruby-OpenID-Consumer Installation

Requirements:

These packages are required by the OpenID library.

htmltokenizer - http://rubyforge.org/projects/htmltokenizer/
- Debian users may: apt-get install libhtml-htmltokenizer-ruby

ruby-hmac - http://deisui.org/~ueno/ruby/hmac.html

Installation:

unpack the archive and run setup.rb (you may need to be root)

ruby setup.rb

Make sure everything installed ok:
$> irb
irb(main):001:0> require "openid/consumer"
=> true



24 changes: 24 additions & 0 deletions README
@@ -0,0 +1,24 @@
Ruby OpenID Consumer

Author: Janrain, Inc - Brian Ellin - brian@janrain.com
Copyright: 2005, Janrain, In
License: LGPL

This OpenID consumer library is a port of the consumer functionality
of the Python-OpenID library - http://www.openidenabled.com/openid/libraries/python/

Please read the INSTALL file, and then have a look at the examples
directory. A fully functional example using Ruby's WEBrick HTTP
server library is available. I encorage you to run the example, and
have a look at the code.

The library is well documented. To get started, have a look at the
example source code(examples/consumer.rb) and lib/openid/consumer.rb.

To run the test suite, make sure the library is installed, use the
runtests script in the test directory.

The library and dependancies have been tested using Ruby 1.8 only,
though it may work with earlier versions.


5 changes: 5 additions & 0 deletions TODO
@@ -0,0 +1,5 @@
12/15/2005

* Add more store types (SQL, etc)
* Server support?

4 changes: 4 additions & 0 deletions docs/README
@@ -0,0 +1,4 @@
Ruby library documentation is being written. In the meantime, please
see the Python API documentation. The two libraries use the exact same API.

http://www.openidenabled.com/openid/libraries/python/
8 changes: 8 additions & 0 deletions examples/README
@@ -0,0 +1,8 @@
This directory contains examples that demonstrate use of the OpenID
conusmer library.

consumer.rb runs a simple web server (webrick) and will verify OpenID identities. To test this out, you'll need a valid OpenID URL, and to install the library (see ../INSTALL), and run ruby consumer.rb

Point your web browser at http://localhost:2000/ to test.


238 changes: 238 additions & 0 deletions examples/consumer.rb
@@ -0,0 +1,238 @@
#!/usr/bin/env ruby
require "cgi"
require "uri"
require "webrick"
include WEBrick

require "openid/consumer"
require "openid/filestore"

################ endconfig ##########################
# use your desired store implementation here
store = OpenID::FilesystemOpenIDStore.new("/tmp/simpleopenid/")

$host = "localhost"
$port = 2000
################ end config ##########################

$consumer = OpenID::OpenIDConsumer.new(store)

if $port.nil?
$base_url = "http://#{$host}/"
else
$base_url = "http://#{$host}:#{$port}/"
end


server = HTTPServer.new(:Port=>$port)

class SimpleServlet < HTTPServlet::AbstractServlet

def do_GET(req, res)
@req = req
@res = res
begin
case req.path
when "", "/", "/start"
self.render
when "/begin"
self.doBegin
when "/complete"
self.doComplete
else
self.redirect(self.buildURL("/"))
end
ensure
@req = nil
@res = nil
end
end

def doBegin
# First make sure the user entered something
openid_url = @req.query.fetch("openid_url", "")
if openid_url.empty?
self.render("Enter an identity URL to verify",
css_class="error", form_contents=openid_url)
return HTTPStatus::Success
end

# Then ask the openid library to begin the authorization
status, info = $consumer.beginAuth(openid_url)

# If the URL was unusable (either because of network conditions,
# a server error, or that the response returned was not an OpenID
# identity page), the library will return HTTP_FAILURE or PARSE_ERROR.
# Let the user know that the URL is unusable.
case status
when OpenID::HTTP_FAILURE
self.render("Failed to retrieve <q>#{openid_url}</q>",
css_class="error", form_contents=openid_url)
return HTTPStatus::Success

when OpenID::PARSE_ERROR
self.render("Failed to retrieve <q>#{openid_url}</q>",
css_class="error", form_contents=openid_url)
return HTTPStatus::Success

when OpenID::SUCCESS
# The URL was a valid identity URL. Now we construct a URL
# that will get us to process the server response. We will
# need the token from the auth request when processing the
# response, so we have to save it somewhere. The obvious
# options are including it in the URL, storing it in a
# cookie, and storing it in a session object if one is
# available. For this example, we have no session and we
# do not want to deal with cookies, so just add it as a
# query parameter to the URL.
return_to = self.buildURL("/complete", {"token"=>info.token})

# Now ask the library for the URL to redirect the user to
# his OpenID server. The auth request is what the library
# returned before. We just constructed the return_to. The
# return_to URL must be under the specified trust_root. We
# just use the base_url for this server as a trust root.
redirect_url = $consumer.constructRedirect(info,
return_to,
trust_root=$base_url)

# Send the redirect response
self.redirect(redirect_url)
else
# Should never get here
raise "Not Reached"
end
end

# handle the redirect from the OpenID server
def doComplete
# get the token from the environment (in this case, the URL)
token = @req.query.fetch("token", "")

# Ask the library to check the response that the server sent
# us. Status is a code indicating the response type. info is
# either nil or a string containing more information about
# the return type.
status, info = $consumer.completeAuth(token, @req.query)

css_class = "error"
openid_url = nil

if status == OpenID::FAILURE and info
# In the case of failure, if info is non-nil, it is the
# URL that we were verifying. We include it in the error
# message to help the user figure out what happened.
openid_url = info
message = "Verification of #{openid_url} failed"

elsif status == OpenID::SUCCESS
# Success means that the transaction completed without
# error. If info is nil, it means that the user cancelled
# the verification.
css_class = "alert"
if info
openid_url = info
message = "You have successfully verified #{openid_url} as your identity."
else
# cancelled
message = "Verification cancelled."
end
else
# Either we don't understand the code or there is no
# openid_url included with the error. Give a generic
# failure message. The library should supply debug
# information in a log.
message = "Verification failed."
end
self.render(message, css_class, openid_url)
end

# build a URL relative to the server base URL, with the given query
# parameters added.
def buildURL(action, query=nil)
url = @req.request_uri.merge(action).to_s
url = OpenID::Util.appendArgs(url, query) unless query.nil?
url
end

def redirect(url)
@res.set_redirect(HTTPStatus::MovedPermanently, url)
end

def render(message=nil, css_class="alert", form_contents="")
@res.body = self.pageHeader
unless message.nil?
@res.body << "<div class=\"#{css_class}\">#{message}</div>"
end
@res.body << self.pageFooter(form_contents)
end

def pageHeader(title="Ruby OpenID WEBrick example")
header = <<END_OF_STRING
<html>
<head><title>#{title}</title></head>
<style type="text/css">
* {
font-family: verdana,sans-serif;
}
body {
width: 50em;
margin: 1em;
}
div {
padding: .5em;
}
table {
margin: none;
padding: none;
}
.alert {
border: 1px solid #e7dc2b;
background: #fff888;
}
.error {
border: 1px solid #ff0000;
background: #ffaaaa;
}
#verify-form {
border: 1px solid #777777;
background: #dddddd;
margin-top: 1em;
padding-bottom: 0em;
}
</style>
<body>
<h1>#{title}</h1>
<p>
This example consumer uses the Ruby-OpenID-Consumer library
on a WEBrick platform. The example just verifies that the URL that
you enter is your identity URL.
</p>
END_OF_STRING
end


def pageFooter(form_contents="")
form_contents = "" if form_contents == "/"
footer = <<END_OF_STRING
<div id="verify-form">
<form method="get" action=#{self.buildURL("/begin")}>
Identity&nbsp;URL:
<input type="text" name="openid_url" value="#{form_contents}" />
<input type="submit" value="Verify" />
</form>
</div>
</body>
</html>
END_OF_STRING
end


end

# Bootstrap the example
server.mount("/", SimpleServlet)
trap("INT") {server.shutdown}
print "\nVisit http://#{$host}:#{$port}/ in your browser.\n\n"
server.start

0 comments on commit bb24b85

Please sign in to comment.