Authentication Handler
This code sample is a little bit different. We will not be utilizing the Plugin.pm
file as we have done in previous examples. An OpenID endpoint requires us to create a file dedicated to this one purpose. Let’s create it:
Create a directory called:
/path/to/mt/plugins/Example/lib/Example
In that directory create a file called
MyOpenIDAuth.pm
and open it in a text editorStart by adding the following text to your
MyOpenIDAuth.pm
file:package Example::OpenIDAuth; use strict; use base qw( MT::Auth::OpenID );
This is not a working code sample yet. What you should notice is that this plugin inherits from the MT::Auth::OpenID
class reducing the amount of code you need to write substantially. All we have to do is customize a few of the functions.
The class
property within the registry identifies the authentication handler for the corresponding OpenID endpoint. We need to make sure that the value of class
points to the file we created above.
Complete Code Sample
Let’s look at the complete MyOpenIDAuth.pm
file now:
package Example::OpenIDAuth;
use strict;
use base qw( MT::Auth::OpenID );
sub url_for_userid {
my $class = shift;
my ($uid) = @_;
return "http://www.livejournal.com/users/$uid";
};
sub _get_nickname {
my ($vident, $blog_id) = @_;
## LJ username
my $url = $vident->url;
if( $url =~ m(^https?://www\.livejournal\.com\/users/([^/]+)/$) ||
$url =~ m(^https?://www\.livejournal\.com\/~([^/]+)/$) ||
$url =~ m(^https?://([^\.]+)\.livejournal\.com\/$)
) {
return $1;
}
*MT::Auth::OpenID::_get_nickname->(@_);
}
sub commenter_auth_params {
my ( $key, $blog_id, $entry_id, $static ) = @_;
my $params = {
blog_id => $blog_id,
static => $static,
};
$params->{entry_id} = $entry_id if defined $entry_id;
return $params;
}
sub openid_commenter_condition {
eval "require Digest::SHA1;";
return $@ ? 0 : 1;
}
1;
How it all works
The example above shows the authentication handler for a LiveJournal OpenID endpoint. This handler is responsible for translating the input from the user submitted from the login form into a valid OpenID URL. For example, the template we registered (remember all that HTML above?) prompts the user for their LiveJournal username. The authentication handler converts that username into the corresponding OpenID URL:
http://username.livejournal.com
The user ID submitted by the user is what is responsible for translating a user’s input into a valid OpenID URL using the url_for_userid
subroutine in the above code sample.
Once a user has successfully logged in, Movable Type needs to extract a nickname or display name from the OpenID URL returned by the OpenID service provider. This is used for example to indicate the name or nickame of the person leaving a comment. This nickname extraction is performed by the _get_nickname
subroutine. In the event the nickame cannot be detected or extracted from the URL returned by the provider, the subroutine below instructs the default Movable Type OpenID handler to return the default nickname.