2019-11-08 18:31:50 +01:00
|
|
|
package Lemonldap::NG::Manager::Api::Providers;
|
|
|
|
our $VERSION = '2.0.7';
|
|
|
|
|
|
|
|
package Lemonldap::NG::Manager::Api;
|
|
|
|
|
2019-12-04 17:50:41 +01:00
|
|
|
sub getOidcRpByConfKey {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
|
|
|
|
my $confKey = $req->params('confKey')
|
|
|
|
or return $self->sendError( $req, 'confKey is missing', 400 );
|
|
|
|
|
|
|
|
$self->logger->debug("[API] OIDC RP $confKey configuration requested");
|
|
|
|
|
|
|
|
# Get latest configuration
|
|
|
|
my $conf = $self->_confAcc->getConf;
|
|
|
|
|
2019-12-05 15:06:33 +01:00
|
|
|
my $oidcRp = $self->_getOidcRp($conf, $confKey);
|
2019-12-04 17:50:41 +01:00
|
|
|
# To save configuration
|
|
|
|
#$self->_confAcc->saveConf( $conf ) ;
|
|
|
|
|
|
|
|
# Dump object
|
|
|
|
#use Data::Dumper; print STDERR Dumper($self);
|
|
|
|
|
2019-12-05 15:06:33 +01:00
|
|
|
# Return 404 if not found
|
|
|
|
unless (defined $oidcRp) {
|
2019-12-05 12:40:03 +01:00
|
|
|
return $self->sendError( $req, "OIDC relying party '$confKey' not found", 404 );
|
2019-12-04 17:50:41 +01:00
|
|
|
}
|
|
|
|
|
2019-12-05 15:06:33 +01:00
|
|
|
return $self->sendJSONresponse(
|
|
|
|
$req, $oidcRp
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub findOidcRpByConfKey {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
|
|
|
|
my $pattern = (
|
|
|
|
defined $req->params('uPattern') ?
|
|
|
|
$req->params('uPattern') :
|
|
|
|
( defined $req->params('pattern') ? $req->params('pattern') : undef )
|
|
|
|
);
|
|
|
|
|
|
|
|
unless (defined $pattern) {
|
|
|
|
return $self->sendError( $req, 'pattern is missing', 400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->logger->debug("[API] Find OIDC RPs by confKey regexp $pattern requested");
|
|
|
|
|
|
|
|
# Get latest configuration
|
|
|
|
my $conf = $self->_confAcc->getConf;
|
|
|
|
|
|
|
|
my @oidcRps;
|
|
|
|
|
|
|
|
foreach (
|
|
|
|
keys %{
|
|
|
|
$conf->{oidcRPMetaDataOptions}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if ($_ =~ $pattern) {
|
|
|
|
push @oidcRps, $self->_getOidcRp($conf, $_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $self->sendJSONresponse(
|
|
|
|
$req, [ @oidcRps ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-05 15:59:53 +01:00
|
|
|
sub findOidcRpByClientId {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
|
|
|
|
my $clientId = (
|
|
|
|
defined $req->params('uClientId') ?
|
|
|
|
$req->params('uClientId') :
|
|
|
|
( defined $req->params('clientId') ? $req->params('clientId') : undef )
|
|
|
|
);
|
|
|
|
|
|
|
|
unless (defined $clientId) {
|
|
|
|
return $self->sendError( $req, 'clientId is missing', 400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->logger->debug("[API] Find OIDC RPs by clientId $clientId requested");
|
|
|
|
|
|
|
|
# Get latest configuration
|
|
|
|
my $conf = $self->_confAcc->getConf;
|
|
|
|
|
|
|
|
foreach (
|
|
|
|
keys %{
|
|
|
|
$conf->{oidcRPMetaDataOptions}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if ($conf->{oidcRPMetaDataOptions}->{$_}->{oidcRPMetaDataOptionsClientID} eq $clientId) {
|
|
|
|
return $self->sendJSONresponse(
|
|
|
|
$req, $self->_getOidcRp($conf, $_)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $self->sendJSONresponse($req, {});
|
|
|
|
}
|
|
|
|
|
2019-12-05 15:06:33 +01:00
|
|
|
sub _getOidcRp {
|
|
|
|
my ( $self, $conf, $confKey ) = @_;
|
|
|
|
|
|
|
|
# Check if confKey is defined
|
|
|
|
unless ( defined $conf->{oidcRPMetaDataOptions}->{$confKey} ) {
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
2019-12-04 17:50:41 +01:00
|
|
|
# Get Client ID
|
|
|
|
my $clientId = $conf->{oidcRPMetaDataOptions}->{$confKey}
|
|
|
|
->{oidcRPMetaDataOptionsClientID};
|
|
|
|
|
|
|
|
# Get exported vars
|
|
|
|
my $exportedVars = $conf->{oidcRPMetaDataExportedVars}->{$confKey};
|
|
|
|
|
2019-12-05 10:29:35 +01:00
|
|
|
# Get extra claim
|
|
|
|
my $extraClaim = $conf->{oidcRPMetaDataOptionsExtraClaims}->{$confKey};
|
|
|
|
|
|
|
|
# Get options
|
|
|
|
my $options = $conf->{oidcRPMetaDataOptions}->{$confKey};
|
|
|
|
|
2019-12-05 15:06:33 +01:00
|
|
|
return {
|
|
|
|
confKey => $confKey,
|
|
|
|
clientId => $clientId,
|
|
|
|
exportedVars => $exportedVars,
|
|
|
|
extraClaim => $extraClaim,
|
|
|
|
options => $options
|
|
|
|
};
|
2019-12-04 17:50:41 +01:00
|
|
|
}
|
|
|
|
|
2019-12-05 12:40:03 +01:00
|
|
|
sub getSamlSpByConfKey {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
|
|
|
|
my $confKey = $req->params('confKey')
|
|
|
|
or return $self->sendError( $req, 'confKey is missing', 400 );
|
|
|
|
|
|
|
|
$self->logger->debug("[API] SAML SP $confKey configuration requested");
|
|
|
|
|
|
|
|
# Get latest configuration
|
|
|
|
my $conf = $self->_confAcc->getConf;
|
|
|
|
|
2019-12-05 15:06:33 +01:00
|
|
|
$samlSp = $self->_getSamlSp($conf, $confKey);
|
|
|
|
|
2019-12-05 12:40:03 +01:00
|
|
|
# Check if confKey is defined
|
2019-12-05 15:06:33 +01:00
|
|
|
unless (defined $samlSp) {
|
2019-12-05 12:40:03 +01:00
|
|
|
return $self->sendError( $req, "SAML service Provider '$confKey' not found", 404 );
|
|
|
|
}
|
|
|
|
|
2019-12-05 15:06:33 +01:00
|
|
|
return $self->sendJSONresponse(
|
|
|
|
$req, $samlSp
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub findSamlSpByConfKey {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
|
|
|
|
my $pattern = (
|
|
|
|
defined $req->params('uPattern') ?
|
|
|
|
$req->params('uPattern') :
|
|
|
|
( defined $req->params('pattern') ? $req->params('pattern') : undef )
|
|
|
|
);
|
|
|
|
|
|
|
|
unless (defined $pattern) {
|
|
|
|
return $self->sendError( $req, 'pattern is missing', 400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->logger->debug("[API] Find SAML SPs by confKey regexp $pattern requested");
|
|
|
|
|
|
|
|
# Get latest configuration
|
|
|
|
my $conf = $self->_confAcc->getConf;
|
|
|
|
|
|
|
|
my @samlSps;
|
|
|
|
|
|
|
|
foreach (
|
|
|
|
keys %{
|
|
|
|
$conf->{samlSPMetaDataXML}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if ($_ =~ $pattern) {
|
|
|
|
push @samlSps, $self->_getSamlSp($conf, $_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $self->sendJSONresponse(
|
|
|
|
$req, [ @samlSps ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-05 15:59:53 +01:00
|
|
|
sub findSamlSpByEntityId {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
|
|
|
|
my $entityId = (
|
|
|
|
defined $req->params('uEntityId') ?
|
|
|
|
$req->params('uEntityId') :
|
|
|
|
( defined $req->params('entityId') ? $req->params('entityId') : undef )
|
|
|
|
);
|
|
|
|
|
|
|
|
unless (defined $entityId) {
|
|
|
|
return $self->sendError( $req, 'entityId is missing', 400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->logger->debug("[API] Find SAML SPs by entityId $entityId requested");
|
|
|
|
|
|
|
|
# Get latest configuration
|
|
|
|
my $conf = $self->_confAcc->getConf;
|
|
|
|
|
|
|
|
foreach (
|
|
|
|
keys %{
|
|
|
|
$conf->{samlSPMetaDataXML}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if ($conf->{samlSPMetaDataXML}->{$_}->{samlSPMetaDataXML} =~ /entityID=['"](.+?)['"]/ && $1 eq $entityId) {
|
|
|
|
return $self->sendJSONresponse(
|
|
|
|
$req, $self->_getSamlSp($conf, $_)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $self->sendJSONresponse($req, {});
|
|
|
|
}
|
|
|
|
|
2019-12-05 15:06:33 +01:00
|
|
|
sub _getSamlSp {
|
|
|
|
my ( $self, $conf, $confKey ) = @_;
|
|
|
|
|
|
|
|
# Check if confKey is defined
|
|
|
|
if ( !defined $conf->{samlSPMetaDataXML}->{$confKey} ) {
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
2019-12-05 12:40:03 +01:00
|
|
|
# Get metadata
|
|
|
|
my $metadata = $conf->{samlSPMetaDataXML}->{$confKey}
|
|
|
|
->{samlSPMetaDataXML};
|
|
|
|
|
2019-12-05 16:58:21 +01:00
|
|
|
# Get options
|
|
|
|
my $options = $conf->{samlSPMetaDataOptions}->{$confKey};
|
|
|
|
|
|
|
|
my $samlSp = {
|
|
|
|
confKey => $confKey,
|
|
|
|
metadata => $metadata,
|
|
|
|
exportedAttributes => {},
|
|
|
|
options => $options
|
|
|
|
};
|
|
|
|
|
2019-12-05 12:40:03 +01:00
|
|
|
# Get exported attributes
|
|
|
|
foreach (
|
|
|
|
keys %{
|
|
|
|
$conf->{samlSPMetaDataExportedAttributes}
|
|
|
|
->{$confKey}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
{
|
|
|
|
# Extract fields from exportedAttr value
|
|
|
|
my ( $mandatory, $name, $format, $friendly_name ) =
|
|
|
|
split( /;/,
|
|
|
|
$conf->{samlSPMetaDataExportedAttributes}
|
|
|
|
->{$confKey}->{$_} );
|
|
|
|
|
|
|
|
$mandatory = !!$mandatory ? 'true' : 'false';
|
|
|
|
|
2019-12-05 16:58:21 +01:00
|
|
|
$samlSp->{exportedAttributes}->{$_} = {
|
2019-12-05 12:40:03 +01:00
|
|
|
name => $name,
|
|
|
|
mandatory => $mandatory
|
|
|
|
};
|
|
|
|
|
|
|
|
if (defined $friendly_name && $friendly_name ne '') {
|
2019-12-05 16:58:21 +01:00
|
|
|
$samlSp->{exportedAttributes}->{$_}->{friendlyName} = $friendly_name;
|
2019-12-05 12:40:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (defined $format && $format ne '') {
|
2019-12-05 16:58:21 +01:00
|
|
|
$samlSp->{exportedAttributes}->{$_}->{format} = $format;
|
2019-12-05 12:40:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 16:58:21 +01:00
|
|
|
return $samlSp;
|
2019-12-05 12:40:03 +01:00
|
|
|
}
|
|
|
|
|
2019-11-08 18:31:50 +01:00
|
|
|
1;
|