Commit d114827e authored by Clément OUDOT's avatar Clément OUDOT
Browse files

Store metadata in raw format (#104)

parent edaec866
......@@ -14,8 +14,7 @@ use XML::Simple;
use Safe;
use Encode;
our $VERSION = '0.1';
our $DEBUG = 0;
our $VERSION = '0.2';
## @cmethod Lemonldap::NG::Common::Conf::SAML::Metadata new(hashRef args)
# Class constructor.
......@@ -33,19 +32,6 @@ sub new {
return $self;
}
## @method void setDebug(boolean debug)
# Set debug flag
# @param boolean debug value
# @return nothing
sub setDebug {
my $self = shift;
my $debug = shift || 1;
$DEBUG = $debug;
return;
}
## @method public boolean initiliazeFromConf(string s)
# Initialize this object from configuration string.
# @param $s Configuration string.
......
......@@ -3,50 +3,98 @@ package Lemonldap::NG::Common::Conf::Serializer;
use Data::Dumper;
BEGIN {
*Lemonldap::NG::Common::Conf::normalize = \&normalize;
*Lemonldap::NG::Common::Conf::unnormalize = \&unnormalize;
*Lemonldap::NG::Common::Conf::serialize = \&serialize;
*Lemonldap::NG::Common::Conf::unserialize = \&unserialize;
}
## @method string normalize(string value)
# Change quotes, spaces and line breaks
# @param value Input value
# @return normalized string
sub normalize {
my ( $self, $value ) = splice @_;
# trim white spaces
$value =~ s/^\s*(.*?)\s*$/$1/;
# Convert carriage returns (\r) and line feeds (\n)
$value =~ s/\r/%0D/g;
$value =~ s/\n/%0A/g;
# Convert simple quotes
$value =~ s/'/'/g;
# Surround with simple quotes
$value = "'$value'" unless ( $self->{noQuotes} );
return $value;
}
## @method string unnormalize(string value)
# Revert quotes, spaces and line breaks
# @param value Input value
# @return unnormalized string
sub unnormalize {
my ( $self, $value ) = splice @_;
# Convert simple quotes
$value =~ s/&#?39;/'/g;
# Convert carriage returns (\r) and line feeds (\n)
$value =~ s/%0D/\r/g;
$value =~ s/%0A/\n/g;
return $value;
}
## @method hashref serialize(hashref conf)
# Parse configuration and convert it into fields
# @param conf Configuration
# @return fields
sub serialize {
my ( $self, $conf ) = @_;
my ( $self, $conf ) = splice @_;
my $fields;
# Data::Dumper options
local $Data::Dumper::Indent = 0;
local $Data::Dumper::Varname = "data";
# Parse configuration
while ( my ( $k, $v ) = each(%$conf) ) {
# Ignore reVhost and cipher
next if ( $k =~ /^(?:reVHosts|cipher)$/ );
# 1.Hash ref
if ( ref($v) ) {
$fields->{$k} = Dumper($v);
$fields->{$k} =~ s/'/'/g;
$fields->{$k} = "'$fields->{$k}'" unless ( $self->{noQuotes} );
$fields->{$k} = $self->normalize( Dumper($v) );
}
# 2. Numeric values
elsif ( $v =~ /^\d+$/ ) {
$fields->{$k} = "$v";
}
else {
# trim white spaces
$v =~ s/^\s*(.*?)\s*$/$1/;
# Convert carriage returns (\r) and line feeds (\n)
$v =~ s/\r/%0D/g;
$v =~ s/\n/%0A/g;
# Convert simple quotes
$v =~ s/'/'/g;
# Surround with simple quotes
$v = "'$v'" unless ( $self->{noQuotes} );
# Store value in field
$fields->{$k} = $v;
# 3. Standard values
else {
$fields->{$k} = $self->normalize($v);
}
}
return $fields;
}
## @method hashref unserialize(hashref fields)
# Convert fields into configuration
# @param fields Fields
# @return configuration
sub unserialize {
my ( $self, $fields ) = @_;
my ( $self, $fields ) = splice @_;
my $conf;
# Parse fields
while ( my ( $k, $v ) = each(%$fields) ) {
# Remove surrounding quotes
......@@ -59,34 +107,45 @@ sub unserialize {
and not ref($v) )
{
$conf->{$k} = {};
# Value should be a Data::Dumper, else this is an old format
if ( defined($v) and $v !~ /^\$/ ) {
print STDERR
"Lemonldap::NG : Warning: configuration is in old format, you've to migrate !\n";
$msg .=
" Warning: configuration is in old format, you've to migrate!";
eval { require Storable; require MIME::Base64; };
if ($@) {
$msg = "Error : $@";
$msg .= " Error: $@";
return 0;
}
$conf->{$k} = Storable::thaw( MIME::Base64::decode_base64($v) );
}
# Convert Data::Dumper
else {
my $data;
$v =~ s/^\$([_a-zA-Z][_a-zA-Z0-9]*) *=/\$data =/;
$v =~ s/&#?39;/'/g;
$v = $self->unnormalize($v);
# Evaluate expression
eval $v;
print STDERR
"Lemonldap::NG : Error while reading configuration with $k key: $@\n"
if ($@);
if ($@) {
$msg .= " Error: cannot read configuration key $k: $@";
}
# Store value in configuration object
$conf->{$k} = $data;
}
}
# Other fields type
else {
$v =~ s/&#?39;/'/g;
$v =~ s/%0D/\r/g;
$v =~ s/%0A/\n/g;
$conf->{$k} = $v;
$conf->{$k} = $self->unnormalize($v);
}
}
return $conf;
}
......
......@@ -189,15 +189,25 @@ sub confNode {
my $h = $self->keyToH( $target, $self->conf );
$h = $h->{samlIDPMetaDataXML} if ( $h->{samlIDPMetaDataXML} );
$h = $h->{samlSPMetaDataXML} if ( $h->{samlSPMetaDataXML} );
my $data;
# Manage old metadata format
if ( ref($h) eq "HASH" ) {
$self->lmLog( "Convert metadata from old format", 'debug' );
my $metadata = Lemonldap::NG::Common::Conf::SAML::Metadata->new();
$metadata->initializeFromConfHash($h);
$data = $metadata->toXML();
}
else {
$data = $h;
}
my $text = $target;
$text =~ s/^\/([^\/]+)\/.*$/$1/;
$res .= $self->li("$target/")
. $self->span(
id => "$target/",
text => $text,
data => $metadata->toXML(),
data => $data,
js => $js,
help => $help,
target => "samlmetadata",
......
......@@ -506,18 +506,9 @@ sub findAllConfKeys {
# @return A formated value.
sub formatValue {
my ( $self, $key, $value ) = @_;
my $newvalue = $value;
if ( $key =~ /^(samlIDPMetaDataXML|samlSPMetaDataXML)/ ) {
my $metadata = Lemonldap::NG::Common::Conf::SAML::Metadata->new();
if ( ref($value) ) {
$metadata->initializeFromConfHash($value);
}
else {
$metadata->initializeFromXML($value);
}
$newvalue = $metadata->toHash();
}
return $newvalue;
# Not used now
return $value;
}
## @method protected void setKeyToH(hashref h,string key,string k2,string value)
......
......@@ -165,22 +165,18 @@ sub loadIDPs {
$self->lmLog( "Get Metadata for IDP $_", 'debug' );
# Get metadata from configuration
my $idp_metadata = Lemonldap::NG::Common::Conf::SAML::Metadata->new();
unless (
$idp_metadata->initializeFromConfHash(
$self->{samlIDPMetaDataXML}->{$_}->{samlIDPMetaDataXML}
)
)
{
$self->lmLog( "Fail to read IDP $_ Metadata from configuration",
'error' );
return 0;
my $idp_metadata =
$self->{samlIDPMetaDataXML}->{$_}->{samlIDPMetaDataXML};
# Check metadata format
if ( ref $idp_metadata eq "HASH" ) {
$self->abort(
"Metadata for IDP $_ is in old format. Please reload them from Manager"
);
}
# Add this IDP to Lasso::Server
my $result =
$self->addIDP( $self->{_lassoServer}, $idp_metadata->toXML() );
my $result = $self->addIDP( $self->{_lassoServer}, $idp_metadata );
unless ($result) {
$self->lmLog( "Fail to use IDP $_ Metadata", 'error' );
......@@ -188,7 +184,7 @@ sub loadIDPs {
}
# Store IDP entityID and Organization Name
my $entityID = $idp_metadata->{entityID};
my ($entityID) = ( $idp_metadata =~ /entityID="(.+?)"/i );
my $name =
$self->getOrganizationName( $self->{_lassoServer}, $entityID )
|| ucfirst($_);
......@@ -248,22 +244,17 @@ sub loadSPs {
$self->lmLog( "Get Metadata for SP $_", 'debug' );
# Get metadata from configuration
my $sp_metadata = Lemonldap::NG::Common::Conf::SAML::Metadata->new();
unless (
$sp_metadata->initializeFromConfHash(
$self->{samlSPMetaDataXML}->{$_}->{samlSPMetaDataXML}
)
)
{
$self->lmLog( "Fail to read SP $_ Metadata from configuration",
'error' );
return 0;
my $sp_metadata = $self->{samlSPMetaDataXML}->{$_}->{samlSPMetaDataXML};
# Check metadata format
if ( ref $sp_metadata eq "HASH" ) {
$self->abort(
"Metadata for SP $_ is in old format. Please reload them from Manager"
);
}
# Add this SP to Lasso::Server
my $result =
$self->addSP( $self->{_lassoServer}, $sp_metadata->toXML() );
my $result = $self->addSP( $self->{_lassoServer}, $sp_metadata );
unless ($result) {
$self->lmLog( "Fail to use SP $_ Metadata", 'error' );
......@@ -271,7 +262,7 @@ sub loadSPs {
}
# Store SP entityID and Organization Name
my $entityID = $sp_metadata->{entityID};
my ($entityID) = ( $sp_metadata =~ /entityID="(.+?)"/i );
my $name =
$self->getOrganizationName( $self->{_lassoServer}, $entityID )
|| ucfirst($_);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment