From: Victor Wagner Date: Fri, 11 Oct 2013 14:14:53 +0000 (+0400) Subject: Added providerlist handling X-Git-Url: http://wagner.pp.ru/gitweb/?a=commitdiff_plain;h=c18ba47bd2daa9239244e087a9967f24ceffc200;p=oss%2Fvjournal.git Added providerlist handling --- diff --git a/bin/login b/bin/login index fffadaa..d14d86d 100644 --- 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); + } + +} + diff --git a/bin/userinfo b/bin/userinfo index a0dbbde..92a45e5 100644 --- a/bin/userinfo +++ b/bin/userinfo @@ -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 index 0000000..6ce8e81 --- /dev/null +++ b/lib/VJournal/ProviderList.pm @@ -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;