2010-02-21 21:17:13 +01:00
|
|
|
## @file
|
|
|
|
# OpenID Issuer file
|
|
|
|
|
|
|
|
## @class
|
|
|
|
# OpenID Issuer class
|
|
|
|
package Lemonldap::NG::Portal::IssuerDBOpenID;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Lemonldap::NG::Portal::Simple;
|
|
|
|
|
|
|
|
our $VERSION = '0.01';
|
|
|
|
|
|
|
|
## @method void issuerDBInit()
|
|
|
|
# Do nothing
|
|
|
|
# @return Lemonldap::NG::Portal error code
|
|
|
|
sub issuerDBInit {
|
|
|
|
my $self = shift;
|
|
|
|
eval { require Net::OpenID::Server };
|
|
|
|
$self->abort( 'Unable to load Net::OpenID::Server', $@ ) if ($@);
|
2010-09-01 11:16:02 +02:00
|
|
|
$self->{openIdSecret} ||= $self->{cipher}->encrypt(0);
|
2010-02-21 21:17:13 +01:00
|
|
|
return PE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
## @apmethod int issuerForUnAuthUser()
|
|
|
|
# Do nothing
|
|
|
|
# @return Lemonldap::NG::Portal error code
|
|
|
|
sub issuerForUnAuthUser {
|
|
|
|
my $self = shift;
|
|
|
|
|
2010-08-27 17:34:03 +02:00
|
|
|
# Restore datas
|
|
|
|
$self->restoreOpenIDprm();
|
|
|
|
my $mode = $self->param('openid.mode');
|
|
|
|
|
2010-08-31 17:36:32 +02:00
|
|
|
unless ($mode) {
|
2010-08-27 17:34:03 +02:00
|
|
|
$self->lmLog( 'OpenID SP test', 'debug' );
|
|
|
|
return PE_OPENID_EMPTY;
|
|
|
|
}
|
2010-08-31 17:36:32 +02:00
|
|
|
|
2010-08-27 17:34:03 +02:00
|
|
|
my ( $type, $data );
|
2010-08-31 17:36:32 +02:00
|
|
|
if ( $mode eq 'associate' ) {
|
2010-08-27 17:34:03 +02:00
|
|
|
return $self->_openIDResponse( $self->openIDServer->_mode_associate() );
|
|
|
|
}
|
2010-08-31 17:36:32 +02:00
|
|
|
elsif ( $mode eq 'check_authentication' ) {
|
|
|
|
return $self->_openIDResponse(
|
|
|
|
$self->openIDServer->_mode_check_authentication() );
|
2010-08-27 17:34:03 +02:00
|
|
|
}
|
|
|
|
else {
|
2010-08-31 17:36:32 +02:00
|
|
|
$self->storeOpenIDprm();
|
2010-08-27 17:34:03 +02:00
|
|
|
return PE_OK;
|
2010-02-21 21:17:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-31 17:36:32 +02:00
|
|
|
## @apmethod int issuerForAuthUser()
|
|
|
|
# Do nothing
|
|
|
|
# @return Lemonldap::NG::Portal error code
|
|
|
|
sub issuerForAuthUser {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Restore datas
|
|
|
|
$self->restoreOpenIDprm();
|
|
|
|
|
|
|
|
return $self->_openIDResponse( $self->openIDServer->handle_page() );
|
|
|
|
}
|
|
|
|
|
|
|
|
## @apmethod int issuerLogout()
|
|
|
|
# TODO
|
|
|
|
# @return Lemonldap::NG::Portal error code
|
|
|
|
sub issuerLogout {
|
|
|
|
PE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub storeOpenIDprm {
|
|
|
|
my $self = shift;
|
|
|
|
delete( $self->{_prm}->{lmhidden_openidprm} );
|
|
|
|
$self->setHiddenFormValue( 'openidprm',
|
|
|
|
Storable::nfreeze( $self->{_prm} ) );
|
|
|
|
}
|
|
|
|
|
2010-08-27 17:34:03 +02:00
|
|
|
sub restoreOpenIDprm {
|
2010-08-31 17:36:32 +02:00
|
|
|
my $self = shift;
|
|
|
|
return if ( $self->{openIDRestored} );
|
|
|
|
if ( my $tmp = $self->getHiddenFormValue('openidprm') ) {
|
|
|
|
$self->lmLog( 'Restore OpenID parameters', 'debug' );
|
|
|
|
eval {
|
|
|
|
$tmp = Storable::thaw($tmp);
|
|
|
|
$self->{_prm}->{$_} = $tmp->{$_} foreach ( keys %$tmp );
|
|
|
|
};
|
2010-08-27 17:34:03 +02:00
|
|
|
}
|
2010-08-31 17:36:32 +02:00
|
|
|
$self->{openIDRestored} = 1;
|
2010-08-27 17:34:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub openIDServer {
|
|
|
|
my $self = shift;
|
2010-08-31 17:36:32 +02:00
|
|
|
return $self->{_openidserver} if ( $self->{_openidserver} );
|
2010-08-27 17:34:03 +02:00
|
|
|
$self->{_openidPortal} = $self->{portal} . "/openidserver/";
|
|
|
|
$self->{_openidPortal} =~ s#(?<!:)//#/#g;
|
|
|
|
|
|
|
|
$self->{_openidserver} = Net::OpenID::Server->new(
|
|
|
|
|
|
|
|
# TODO
|
2010-08-31 17:36:32 +02:00
|
|
|
server_secret => sub { return $self->{openIdSecret} },
|
|
|
|
post_args => $self->{_prm},
|
|
|
|
get_args => $self->{_prm},
|
2010-08-27 17:34:03 +02:00
|
|
|
endpoint_url => $self->{_openidPortal},
|
|
|
|
setup_url => $self->{_openidPortal},
|
2010-02-21 21:17:13 +01:00
|
|
|
get_user => sub {
|
|
|
|
return $self->{sessionInfo}
|
|
|
|
->{ $self->{OpenIdAttr} || $self->{whatToTrace} };
|
|
|
|
},
|
|
|
|
get_identity => sub {
|
|
|
|
my ( $u, $identity ) = @_;
|
|
|
|
return $identity unless $u;
|
2010-08-31 17:36:32 +02:00
|
|
|
return $self->{_openidPortal} . $u;
|
2010-02-21 21:17:13 +01:00
|
|
|
},
|
|
|
|
is_identity => sub {
|
|
|
|
my ( $u, $identity ) = @_;
|
2010-08-31 17:36:32 +02:00
|
|
|
return 0 unless ( $u and $identity );
|
2010-09-01 14:52:56 +02:00
|
|
|
if ( $u eq ( split '/', $identity )[-1] ) {
|
2010-08-31 17:36:32 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->{_badOpenIdentity} = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2010-02-21 21:17:13 +01:00
|
|
|
},
|
|
|
|
is_trusted => sub {
|
|
|
|
my ( $u, $trust_root, $is_identity ) = @_;
|
2010-08-31 17:36:32 +02:00
|
|
|
return 0 unless ( $u and $is_identity );
|
|
|
|
if ( $self->{sessionInfo}->{"_openidTrust$trust_root"} ) {
|
|
|
|
$self->lmLog( 'OpenID request already trusted', 'debug' );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
elsif ( $self->param("confirm") ) {
|
|
|
|
$self->updateSession( { "_openidTrust$trust_root" => 1 } );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->lmLog( 'OpenID request not trusted', 'debug' );
|
|
|
|
$self->{_openIdTrustRequired} = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2010-02-21 21:17:13 +01:00
|
|
|
}
|
|
|
|
);
|
2010-08-27 17:34:03 +02:00
|
|
|
return $self->{_openidserver};
|
|
|
|
}
|
2010-02-21 21:17:13 +01:00
|
|
|
|
2010-08-27 17:34:03 +02:00
|
|
|
sub _openIDResponse {
|
2010-08-31 17:36:32 +02:00
|
|
|
my ( $self, $type, $data ) = splice @_;
|
2010-02-21 21:17:13 +01:00
|
|
|
if ( $type eq 'redirect' ) {
|
2010-08-27 17:34:03 +02:00
|
|
|
$self->lmLog( 'OpenID redirection', 'debug' );
|
2010-02-21 21:17:13 +01:00
|
|
|
print $self->redirect($data);
|
|
|
|
}
|
|
|
|
elsif ( $type eq 'setup' ) {
|
2010-08-31 17:36:32 +02:00
|
|
|
if ( $self->{_openIdTrustRequired} ) {
|
|
|
|
|
|
|
|
# TODO
|
2010-09-01 14:52:56 +02:00
|
|
|
$self->info(
|
|
|
|
'<h2>'
|
|
|
|
. &Lemonldap::NG::Portal::_i18n::msg( PM_OPENID_EXCHANGE,
|
|
|
|
$ENV{HTTP_ACCEPT_LANGUAGE} )
|
|
|
|
. "</h2>"
|
|
|
|
);
|
2010-08-31 17:36:32 +02:00
|
|
|
$self->lmLog( 'OpenID confirmation', 'debug' );
|
|
|
|
$self->storeOpenIDprm();
|
|
|
|
return PE_CONFIRM;
|
|
|
|
}
|
|
|
|
if ( $self->{_badOpenIdentity} ) {
|
|
|
|
$self->userNotice(
|
2010-09-01 14:52:56 +02:00
|
|
|
"The user $self->{sessionInfo}->{_user} tries to use the id \"$data->{identity}\" on $data->{trust_root}"
|
|
|
|
);
|
2010-08-31 17:36:32 +02:00
|
|
|
return PE_OPENID_BADID;
|
|
|
|
}
|
2010-02-21 21:17:13 +01:00
|
|
|
}
|
|
|
|
else {
|
2010-08-27 17:34:03 +02:00
|
|
|
$self->lmLog( 'OpenID generated page', 'debug' );
|
2010-02-21 21:17:13 +01:00
|
|
|
print $self->header($type);
|
|
|
|
print $data;
|
|
|
|
}
|
2010-08-27 17:34:03 +02:00
|
|
|
$self->quit();
|
|
|
|
PE_OK;
|
|
|
|
}
|
|
|
|
|
2010-02-21 21:17:13 +01:00
|
|
|
1;
|
|
|
|
|
|
|
|
__END__
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
=encoding utf8
|
|
|
|
|
|
|
|
Lemonldap::NG::Portal::IssuerDBOpenID - OpenID IssuerDB for Lemonldap::NG
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
OpenID Issuer implementation in LemonLDAP::NG
|
|
|
|
|
|
|
|
=head1 SEE ALSO
|
|
|
|
|
|
|
|
L<Lemonldap::NG::Portal>
|
|
|
|
|
|
|
|
=head1 AUTHOR
|
|
|
|
|
|
|
|
Xavier Guimard, E<lt>x.guimard@free.frE<gt>
|
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE
|
|
|
|
|
|
|
|
Copyright (C) 2010 by Xavier Guimard
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.10.0 or,
|
|
|
|
at your option, any later version of Perl 5 you may have available.
|
|
|
|
|
|
|
|
=cut
|