Kwiki-HatenaAuth

 view release on metacpan or  search on metacpan

lib/Kwiki/HatenaAuth.pm  view on Meta::CPAN

package Kwiki::HatenaAuth;
use strict;
use Hatena::API::Auth;
use URI::Escape qw(uri_escape_utf8);

use Kwiki::UserName '-Base';
use mixin 'Kwiki::Installer';

our $VERSION = '0.04';

const class_id => 'user_name';
const class_title => 'Kwiki with HatenaAuth authentication';
const css_file => 'user_name.css';
const cgi_class => 'Kwiki::HatenaAuth::CGI';

field -package => 'Kwiki::PageMeta', 'edit_by_icon';

sub register {
    my $registry = shift;
    $registry->add(preload => 'user_name');
    $registry->add(action  => "return_hatenaauth");
    $registry->add(action  => "logout_hatenaauth");
    $registry->add(hook    => "page_metadata:sort_order", post => 'sort_order_hook');
    $registry->add(hook    => "page_metadata:update", post => 'update_hook');
}

sub sort_order_hook {
    my $hook = pop;
    return $hook->returned, 'edit_by_icon';
}

sub update_hook {
    return unless ref($self->hub->users->current) eq 'Kwiki::HatenaAuth';
    my $meta = $self->hub->pages->current->metadata;
    $meta->edit_by_icon($self->hub->users->current->thumbnail_url);
}

sub return_hatenaauth {
    my %input = map { ($_ => scalar $self->cgi->$_) } qw(cert);
    my $user = $self->hatena_api_auth->login($input{cert});
    if ($user) {
        my %cookie = map { ($_ => scalar $user->$_) } qw(name image_url thumbnail_url);
        $self->hub->cookie->write(hatenaauth => \%cookie);
    }
    $self->redirect('?' . uri_escape_utf8($self->cgi->page_name));
}

sub logout_hatenaauth {
    $self->hub->cookie->write(hatenaauth => {}, { -expires => "-3d" });
    $self->render_screen(content_pane => 'logout_hatenaauth.html');
}

sub hatena_api_auth {
    Hatena::API::Auth->new({
        api_key => $self->hub->config->hatenaauth_key,
        secret  => $self->hub->config->hatenaauth_secret,
    });
}
sub uri_to_login {
    my $page_name = $self->hub->cgi->page_name;
    utf8::encode($page_name) if utf8::is_utf8($page_name);
    $self->hatena_api_auth->uri_to_login( page_name => $page_name )->as_string;
}

package Kwiki::HatenaAuth::CGI;
use Kwiki::CGI '-Base';

cgi 'cert';
cgi 'page_name';

package Kwiki::HatenaAuth;

1;

__DATA__

=head1 NAME

Kwiki::HatenaAuth - Kwiki HatenaAuth integration

=head1 SYNOPSIS

  > $EDITOR plugins
  # Kwiki::UserName <- If you use it, comment it out
  Kwiki::HatenaAuth
  Kwiki::Edit::HatenaAuthRequired <- Optional: If you don't allow anonymous writes
  > $EDITOR config.yaml
  users_class: Kwiki::Users::HatenaAuth
  hatenaauth_key: PUT YOUR KEY HERE
  hatenaauth_secret: PUT YOUR SECRET KEY HEAR
  > kwiki -update



( run in 1.085 second using v1.01-cache-2.11-cpan-7fcb06a456a )