Authenticates user with external OpenID provider, creates VJournal
session and sets approprieate cookie.
+=head1 WORKFLOW
+
+=over 4
+
+=item No paramteters,
+
+Show login form
+
+=item Parameters username and site
+
+Start openid auth
+
+=item OpenID parameters
+
+Perform authentication. If successul, create session.
+If E-mail is provided, redirect back to blog. Otherwise - show form
+to requset E-Mail. Create session anyway.
+
+=item email parameter + cookie
+
+Update session. Redirect to blog.
+
+=back
+
=cut
+
+use CGI;
+use Net::OpenID::Consumer;
+use VJournal::Session;
+
+sub verify {
+ my ($cgi,$consumer)=@_;
+ if (my $setup_url = $csr->user_setup_url) {
+ print $cgi->redirect(-location=>$setup_url);
+ return;
+ } elsif ($csr->user_cancel) {
+ show_error("OpenID server cancelled operation");
+ } elsif (my $vident= $csr->verified_identity) {
+ my $user = $vident->url();
+ # Getting OPENDID extensions from
+ my @extension =grep { defined } (
+ $vident->signed_extension_fields('http://openid.net/extensions/sreg/1.1'),
+ $vident->signed_extension_fields('http://openid.net/srv/ax/1.0'),
+
+ );
+
+ # getting foaf info;
+ my $foaf = $vident->declared_foaf();
+ my ($email,$avatar) = ($foaf?get_foaf($foaf):(undef,undef));
+ my $session = VJournal::Session->create(-cgi=>$cgi,
+ -name => $name, -idenity=>$user);
+ if (!$email) {
+ show_email_form);
+ }
+
+}
+
{
state: "notlogged",
providers: [
- {name: "Live journal",icon:"/avatars/lj.gif",format: "%s.livejournal.com"}
+ {name: "Live journal",icon:"/avatars/lj.gif",id: "lj"}
...
}
if (!defined $session) {
# User is not authenticated. Return list of providers;
$out->{state}="notlogged";
+ $sites=VJournal::ProviderList->new;
+ $out->{providers}=$sites->menu;
$session=$cgi;
} else {
if ($session->isowner()) {
--- /dev/null
+package VJournal::ProviderList;
+use VJournal::Session;
+use Carp;
+use constant PROVIDER_LIST_FILE="providers.lst";
+
+sub new {
+ my $class=shift;
+ if (!exists $VJournal::Session::config{-statedir})
+ croak("VJournal::Session config is not loaded");
+ my $f;
+ open
+ $f,"<",$VJournal::Session::config{-statedir}."/".PROVIDER_LIST_FILE or croak("cannot find provider list file");
+ my $self={};
+ while (<$f>) {
+ chomp;
+ my ($id,$name,$format,$translate_underscore,$format2,$urlasname}=split (":")
+ $self->{$id}={-id=>$id,-name=>$name,$format=>[$format,($format2?$format2:())],
+ -translate_underscore=>$translate_underscore,-url_as_name=>$urlasname};
+ }
+ close $f;
+ return bless $self,$class;
+}
+
+sub menu {
+ my $self=shift;
+ my @out;
+ for my $site (values $self) {
+ push @out,{id=>$site->{-id},name=>$site->{-name}};
+ }
+ push @out,{id=>"_",name=>"Other site");
+ return @out;
+}
+
+sub useruri {
+ my ($self,$user,$site_id) = @;
+ if (!exists $self->{$site_id}) {
+ # If no valid site id provided, assume that we have just openid
+ # url instead of user name
+ return $user;
+ }
+ my $site=$self->{$site_id};
+ if (!index($site->{-format}[0],"%s")>0) {
+ return $site->{-format}[0];
+ }
+ if ($site->{-translate_underscore}) {
+ if (defined $site->{-format}[1] && ($user=~/^_/ || $user=~/_$/))
+ {
+ return sprintf $site->{-format}[1],$user);
+ } else {
+ $user =~ tr/_/-/;
+ }
+ }
+ return sprintf $site->{-format}[0],$user);
+}
+
+sub meaningless_url {
+ my ($self,$id) = @_;
+ return !$self->{$id}{-uri_as_name};
+}
+
+1;