]> wagner.pp.ru Git - oss/vjournal.git/commitdiff
Added providerlist handling
authorVictor Wagner <wagner@atlas-card.ru>
Fri, 11 Oct 2013 14:14:53 +0000 (18:14 +0400)
committerVictor Wagner <wagner@atlas-card.ru>
Fri, 11 Oct 2013 14:14:53 +0000 (18:14 +0400)
bin/login
bin/userinfo
lib/VJournal/ProviderList.pm [new file with mode: 0644]

index fffadaa34f3fa82026e2976c0c77e014eb914f96..d14d86d19c5c9d7b600ea98895dbc80803f4c445 100644 (file)
--- a/bin/login
+++ b/bin/login
@@ -13,4 +13,60 @@ vjlogin - authenticate openid users
 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);
+       }
+
+}
+
index a0dbbdecb0e3c90376fd6f64c50dea48f3ebc50b..92a45e5fd45b1f0d76e267e622a0d9b80ecc45a7 100644 (file)
@@ -27,7 +27,7 @@ If user is not logged in, returns following structure:
     {
                state: "notlogged",
                providers: [
-                       {name: "Live journal",icon:"/avatars/lj.gif",format: "%s.livejournal.com"}
+                       {name: "Live journal",icon:"/avatars/lj.gif",id: "lj"}
            ...
        }
 
@@ -43,6 +43,8 @@ my $out={};
 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())  {
diff --git a/lib/VJournal/ProviderList.pm b/lib/VJournal/ProviderList.pm
new file mode 100644 (file)
index 0000000..6ce8e81
--- /dev/null
@@ -0,0 +1,61 @@
+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;