Library for building your own openid server/provider
version 1.09
use Net::OpenID::Server; my $nos = Net::OpenID::Server->new( args => $cgi, get_user => \&get_user, get_identity => \&get_identity, is_identity => \&is_identity, is_trusted => \&is_trusted, endpoint_url => "http://example.com/server.bml", setup_url => "http://example.com/pass-identity.bml", ); # From your OpenID server endpoint: my ($type, $data) = $nos->handle_page; if ($type eq "redirect") { WebApp::redirect_to($data); } elsif ($type eq "setup") { my %setup_opts = %$data; # ... show them setup page(s), with options from setup_map # it's then your job to redirect them at the end to "return_to" # (or whatever you've named it in setup_map) } else { WebApp::set_content_type($type); WebApp::print($data); }
This is the Perl \s-1API\s0 for (the server half of) OpenID, a distributed identity system based on proving you own a \s-1URL\s0, which is then your identity. More information is available at:
http://openid.net/
As of version 1.01 this module has support for both OpenID 1.1 and 2.0. Prior to this, only 1.1 was supported.
You can set anything in the constructor options that there are getters/setters methods for below. That includes: args, get_user, is_identity, is_trusted, setup_url, and setup_map. See below for docs.
Returns a $type and $data, where $type can be:
... in which case you redirect the user (via your web framework's redirect functionality) to the \s-1URL\s0 specified in $data. ... in which case you should show the user a page (or redirect them to one of your pages) where they can setup trust for the given \*(L"trust_root\*(R" in the hashref in $data, and then redirect them to \*(L"return_to\*(R" at the end. Note that the parameters in the $data hashref are as you named them with setup_map.
Otherwise, set the content type to $type and print the page out, the contents of which are in $data.
The optional %opts may contain: If set to a true value, signals that you don't want to handle the \*(C`setup\*(C' return type from handle_page, and you'd prefer it just be converted to a \*(C`redirect\*(C' type to your already-defined \*(C`setup_url\*(C', with the arguments from setup_map already appended.
Generates a positive identity assertion \s-1URL\s0 that you'd redirect a user to. Typically this would be after they've completed your setup_url. Once trust has been setup, the \*(C`handle_page\*(C' method will redirect you to this signed return automatically. The \s-1URL\s0 generated is the consumer site's return_to \s-1URL\s0, with a signed identity included in the \s-1GET\s0 arguments. The %opts are:
Required. The identity \s-1URL\s0 to sign. Optional. The claimed_id \s-1URL\s0 to sign. Required. The base of the \s-1URL\s0 being generated. The association handle to use for the signature. If blank, dumb consumer mode is used, and the library picks the handle. Optional. If present, the \*(C`return_to\*(C' \s-1URL\s0 will be checked to be within (\*(L"under\*(R") this trust_root. If not, the \s-1URL\s0 returned will be undef. Optional. Optional. If present, must be a hashref with keys starting with \*(L"\w+\.\*(R". All keys and values will be returned in the response, and signed. This is used for OpenID extensions.
Generates a cancel notice to the return_to \s-1URL\s0, if a user declines to share their identity. %opts are:
Required. The base of the \s-1URL\s0 being generated.
Can be used in 1 of 3 ways: 1. Setting the way which the Server instances obtains parameters: $nos->args( $reference ) Where $reference is either a \s-1HASH\s0 ref, \s-1CODE\s0 ref, Apache $r (for get_args only), Apache::Request $apreq, or \s-1CGI\s0.pm $cgi. If a \s-1CODE\s0 ref, the subref must return the value given one argument (the parameter to retrieve) 2. Get a paramater: my $foo = $nos->get_args(\*(L"foo\*(R"); When given an unblessed scalar, it retrieves the value. It croaks if you haven't defined a way to get at the parameters. 3. Get the getter: my $code = $nos->get_args; Without arguments, returns a subref that returns the value given a parameter name. Get/set the subref returning a defined value representing the logged in user, or undef if no user. The return value (let's call it $u) is not touched. It's simply given back to your other callbacks (is_identity and is_trusted). For OpenID 2.0. Get/set the subref returning a identity. This is called when claimed identity is 'identifier_select'. Get/set the subref which is responsible for returning true if the logged in user $u (which may be undef if user isn't logged in) owns the \s-1URL\s0 tree given by $identity_url. Note that if $u is undef, your function should always return 0. The framework doesn't do that for you so you can do unnecessary work on purpose if you care about exposing information via timing attacks. Get/set the subref which is responsible for returning true if the logged in user $u (which may be undef if user isn't logged in) trusts the \s-1URL\s0 given by $trust_root to know his/her identity. Note that if either $u is undef, or $is_identity is false (this is the result of your previous is_identity callback), you should return 0. But your callback is always run so you can avoid timing attacks, if you care. The server secret is used to generate and sign lots of per-consumer secrets, and is never handed out directly. In the simplest (and least secure) form, you configure a static secret value with a scalar. If you use this method and change the scalar value, all consumers that have cached their per-consumer secrets will start failing, since their secrets no longer work. The recommended usage, however, is to supply a subref that returns a secret based on the provided $time, a unix timestamp. And if one doesn't exist for that time, create, store and return it (with appropriate locking so you never return different secrets for the same time.) Your secret can just be random characters, but it's your responsibility to do the locking and storage. If you want help generating random characters, call \*(C`Net::OpenID::Server::rand_chars($len)\*(C'. Your secret may not exceed 255 characters. Get/set the user setup \s-1URL\s0. This is the \s-1URL\s0 the user is told to go to if they're either not logged in, not who they said they were, or trust hasn't been setup. You use the same \s-1URL\s0 in all three cases. Your setup \s-1URL\s0 may contain existing query parameters. For OpenID 2.0. Get/set the op_endpoint \s-1URL\s0. When this module gives a consumer site a user_setup_url from your provided setup_url, it also has to append a number of get parameters onto your setup_url, so your app based at that setup_url knows what it has to setup. Those keys are named, by default, \*(L"trust_root\*(R", \*(L"return_to\*(R", \*(L"identity\*(R", and \*(L"assoc_handle\*(R". If you don't like those parameter names, this $hashref setup_map lets you change one or more of them. The hashref's keys should be the default values, with values being the parameter names you want.
Utility function to return a string of $len random characters. May be called as package method, object method, or regular function. Returns the last error, in form \*(L"errcode: errtext\*(R"; Returns the last error code. Returns the last error text.
This module is Copyright (c) 2005 Brad Fitzpatrick. All rights reserved.
You may distribute under the terms of either the \s-1GNU\s0 General Public License or the Artistic License, as specified in the Perl \s-1README\s0 file. If you need more liberal licensing terms, please contact the maintainer.
This is free software. \s-1IT\s0 \s-1COMES\s0 \s-1WITHOUT\s0 \s-1WARRANTY\s0 \s-1OF\s0 \s-1ANY\s0 \s-1KIND\s0.
The Net::OpenID family of modules has a mailing list powered by Google Groups. For more information, see http://groups.google.com/group/openid-perl .
OpenID website: http://openid.net/
Brad Fitzpatrick <[email protected]>