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;
|
2010-09-17 17:32:43 +02:00
|
|
|
eval { require Lemonldap::NG::Common::OpenID::Server };
|
2010-02-21 21:17:13 +01:00
|
|
|
$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
|
|
|
|
|
|
|
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();
|
2010-09-03 15:34:35 +02:00
|
|
|
my $mode = $self->param('openid.mode');
|
|
|
|
|
|
|
|
unless ($mode) {
|
|
|
|
$self->lmLog( 'OpenID SP test', 'debug' );
|
|
|
|
return PE_OPENID_EMPTY;
|
|
|
|
}
|
2010-08-31 17:36:32 +02:00
|
|
|
|
2010-09-03 15:34:35 +02:00
|
|
|
unless ( $mode =~ /^checkid_(?:immediate|setup)/ ) {
|
|
|
|
$self->lmLog(
|
2010-09-03 15:37:49 +02:00
|
|
|
"OpenID error : $mode is not known at this step (issuerForAuthUser)",
|
|
|
|
'error'
|
2010-09-03 15:34:35 +02:00
|
|
|
);
|
|
|
|
return PE_ERROR;
|
|
|
|
}
|
|
|
|
return $self->_openIDResponse( $self->openIDServer->_mode_checkid() );
|
2010-08-31 17:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
## @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
|
|
|
}
|
|
|
|
|
2010-09-18 08:23:34 +02:00
|
|
|
sub sregHook {
|
|
|
|
my ( $self, %prm ) = splice @_;
|
|
|
|
my ( %r, @req, @opt );
|
|
|
|
while ( my ( $k, $v ) = each %prm ) {
|
|
|
|
if ( $k eq 'policy_url' ) {
|
2010-09-18 08:41:48 +02:00
|
|
|
if ( $v =~
|
|
|
|
m{^https?://(?:(?:(?:(?:(?:(?:[a-zA-Z0-9][-a-zA-Z0-9]*)?[a-zA-Z0-9])[.])*(?:[a-zA-Z][-a-zA-Z0-9]*[a-zA-Z0-9]|[a-zA-Z])[.]?)|(?:[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+)))(?::(?:(?:[0-9]*)))?(?:/(?:(?:(?:(?:(?:(?:[a-zA-Z0-9\-_.!~*'():@&=+\$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*)(?:;(?:(?:[a-zA-Z0-9\-_.!~*'():@&=+\$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*))*)(?:/(?:(?:(?:[a-zA-Z0-9\-_.!~*'():@&=+\$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*)(?:;(?:(?:[a-zA-Z0-9\-_.!~*'():@&=+\$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*))*))*))))?$}
|
|
|
|
)
|
|
|
|
{
|
2010-09-18 08:23:34 +02:00
|
|
|
$self->{openIdCustomerPolicy} = $v;
|
|
|
|
}
|
2010-09-18 08:41:48 +02:00
|
|
|
else {
|
|
|
|
$self->lmLog( "Bad policy url", 'error' );
|
|
|
|
}
|
2010-09-18 08:23:34 +02:00
|
|
|
}
|
|
|
|
elsif ( $k eq 'required' ) {
|
|
|
|
push @req, split( /,/, $v );
|
|
|
|
}
|
|
|
|
elsif ( $k eq 'optional' ) {
|
|
|
|
push @opt,
|
|
|
|
grep { defined $self->{"openIdSreg_$_"} } split( /,/, $v );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->lmLog( "Unknown OpenID SREG request $k", 'error' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# If a required data is not available, returns nothing
|
|
|
|
foreach my $k (@req) {
|
|
|
|
unless ( $self->{"openIdSreg_$k"} ) {
|
|
|
|
$self->lmLog(
|
|
|
|
"Parameter $k is required by customer but not defined in configuration",
|
|
|
|
'notice'
|
|
|
|
);
|
|
|
|
return ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach my $k ( @req, @opt ) {
|
|
|
|
unless ( $k =~
|
|
|
|
/^(?:(?:(?:full|nick)nam|languag|postcod|timezon)e|country|gender|email|dob)$/
|
|
|
|
)
|
|
|
|
{
|
|
|
|
$self->lmLog(
|
|
|
|
"Requested parameter $k is not a valid OpenID SREG parameter",
|
|
|
|
'error' );
|
|
|
|
return ();
|
|
|
|
}
|
|
|
|
$r{$k} = $self->{sessionInfo}->{ $self->{"openIdSreg_$k"} };
|
|
|
|
}
|
|
|
|
return %r;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2010-09-17 17:32:43 +02:00
|
|
|
$self->{_openidserver} = Lemonldap::NG::Common::OpenID::Server->new(
|
2010-08-27 17:34:03 +02:00
|
|
|
|
|
|
|
# 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-09-18 08:23:34 +02:00
|
|
|
},
|
|
|
|
extensions => {
|
|
|
|
sreg => sub {
|
|
|
|
return $self->sregHook(@_);
|
|
|
|
},
|
|
|
|
},
|
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-09-03 15:34:35 +02:00
|
|
|
|
|
|
|
# TODO: use autoRedirect instead
|
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
|