root/trunk/mixiComment/plugins/mixiComment/lib/mixiComment/Auth/mixi.pm @ 971

Revision 971, 3.0 kB (checked in by fumiakiy, 15 months ago)

Importing mixiComment plugin. This plugin enables Movable Type to levarage mixi OpenID Provider as another commenter authenticator.

Line 
1package mixiComment::Auth::mixi;
2
3use strict;
4use base qw( MT::Auth::OpenID );
5
6sub check_url_params {
7    my $class = shift;
8    my ( $app, $blog ) = @_;
9    my $q = $app->{query};
10
11    if ( $q->param('verify_blog_owner') ) {
12        my $path = MT->config->AdminCGIPath || MT->config->CGIPath;
13        $path = _adjust_path( $path, $blog );
14        $path .= MT->config->AdminScript;
15        my $return_to = $path . '?__mode=mixicomment_verify_blog_owner'
16            . '&blog_id='  . $blog->id;
17        return ( trust_root => $path, return_to => $return_to );
18    }
19    else {
20        return $class->SUPER::check_url_params(@_);
21    }
22}
23
24sub _adjust_path {
25    my ( $path, $blog ) = @_;
26    if ($path =~ m!^/!) {
27        # relative path, prepend blog domain
28        my ($blog_domain) = $blog->archive_url =~ m|(.+://[^/]+)|;
29        $path = $blog_domain . $path;
30    }
31    $path .= '/' unless $path =~ m|/$|;
32    $path;
33}
34
35# TODO: remove these once core code supports SREG - BugID:71011
36sub NS_OPENID_SREG { "http://openid.net/extensions/sreg/1.1" }
37
38sub login {
39    my $class = shift;
40    my ($app) = @_;
41    my $q = $app->{query};
42    return $app->errtrans("Invalid request.")
43        unless $q->param('blog_id');
44    my $blog = MT::Blog->load(scalar $q->param('blog_id'));
45    my %param = $app->param_hash;
46    my $csr = MT::Auth::OpenID::_get_csr(\%param, $blog) or return;
47    my $identity = $q->param('openid_url');
48    if (!$identity &&
49        (my $u = $q->param('openid_userid')) && $class->can('url_for_userid')) {
50        $identity = $class->url_for_userid($u);
51    }
52    my $claimed_identity = $csr->claimed_identity($identity);
53    if (!$claimed_identity) {
54        my ($err_code, $err_msg) = ($csr->errcode, $csr->errtext);
55        if ($err_code eq 'no_head_tag' || $err_code eq 'no_identity_server' || $err_code eq 'url_gone') {
56            $err_msg = $app->translate('The address entered does not appear to be an OpenID');
57        }
58        elsif ($err_code eq 'empty_url' || $err_code eq 'bogus_url') {
59            $err_msg = $app->translate('The text entered does not appear to be a web address');
60        }
61        elsif ($err_code eq 'url_fetch_error') {
62            $err_msg =~ s{ \A Error \s fetching \s URL: \s }{}xms;
63            $err_msg = $app->translate('Unable to connect to [_1]: [_2]', $identity, $err_msg);
64        }
65        return $app->error($app->translate("Could not verify the OpenID provided: [_1]", $err_msg));
66    }
67
68    my %params = $class->check_url_params( $app, $blog );
69
70    $claimed_identity->set_extension_args(NS_OPENID_SREG(), {
71        optional => join(",", qw/email nickname fullname/)
72    });
73
74    my $check_url = $claimed_identity->check_url(
75        ( %params, delayed_return => 1 )
76    );
77
78    return $app->redirect($check_url);
79}
80
81sub get_nickname {
82    my $class = shift;
83    my ($vident) = @_;
84
85    # Try SREG extension first
86    my $fields = $vident->extension_fields(NS_OPENID_SREG);
87    return $fields->{nickname} if exists $fields->{nickname};
88    return $fields->{fullname} if exists $fields->{fullname};
89    $class->SUPER::get_nickname(@_);
90}
91
921;
93__END__
Note: See TracBrowser for help on using the browser.