Skip to content
Snippets Groups Projects
Commit 314cca48 authored by Guillaume ROUSSE's avatar Guillaume ROUSSE
Browse files

factorisation

parent 1737cbac
No related branches found
No related tags found
No related merge requests found
...@@ -66,21 +66,39 @@ sub init_db { ...@@ -66,21 +66,39 @@ sub init_db {
return $db; return $db;
} }
sub check_authentication { sub init_user {
my $self = shift; my $self = shift;
my $headers = $self->req()->headers();
my $idp = my $idp =
$ENV{'Shib_Identity_Provider'} || # local SP $ENV{'Shib_Identity_Provider'} || # local SP
$self->req()->headers()->header('Shib-Identity-Provider'); # remote SP $headers->header('Shib-Identity-Provider'); # remote SP
my $name =
$ENV{displayName} || # local SP
$headers->header('displayName'); # remote SP
my $user = {
idp => $idp,
name => $name
};
$self->stash(user => $user);
return $user;
}
sub check_authentication {
my $self = shift;
return $self->abort( return $self->abort(
status => 401, status => 401,
log_message => sprintf("unauthenticated user for action %s", $self->current_route()), log_message => sprintf("unauthenticated user for action %s", $self->current_route()),
user_message => Registry::Error::AuthenticationRequired->new() user_message => Registry::Error::AuthenticationRequired->new()
) if !$idp; ) if !$self->stash('user');
return 1; return 1;
} }
sub check_token { sub check_token {
......
...@@ -15,12 +15,14 @@ sub run { ...@@ -15,12 +15,14 @@ sub run {
$self->init_db(); $self->init_db();
$self->init_l10n(); $self->init_l10n();
$self->init_user();
if ($config->{app}->{login_url}) { if ($config->{app}->{login_url}) {
return if !$self->check_authentication(); return if !$self->check_authentication();
} }
my $db = $self->stash('db'); my $db = $self->stash('db');
my $user = $self->stash('user');
my $sps = AccountManager::Entity->get_entities( my $sps = AccountManager::Entity->get_entities(
db => $db, db => $db,
...@@ -31,12 +33,12 @@ sub run { ...@@ -31,12 +33,12 @@ sub run {
); );
my $idp; my $idp;
if ($ENV{HTTP_SHIB_IDENTITY_PROVIDER}) { if ($user) {
my $idps = AccountManager::Entity->get_entities( my $idps = AccountManager::Entity->get_entities(
db => $db, db => $db,
query => [ query => [
type => 'idp', type => 'idp',
entityid => $ENV{HTTP_SHIB_IDENTITY_PROVIDER}, entityid => $user->{idp}
] ]
); );
$idp = $idps->[0]; $idp = $idps->[0];
......
...@@ -13,6 +13,7 @@ sub run { ...@@ -13,6 +13,7 @@ sub run {
$self->init_db(); $self->init_db();
$self->init_l10n(); $self->init_l10n();
$self->init_user();
if ($config->{app}->{login_url}) { if ($config->{app}->{login_url}) {
return if !$self->check_authentication(); return if !$self->check_authentication();
......
...@@ -20,6 +20,7 @@ sub run { ...@@ -20,6 +20,7 @@ sub run {
$self->init_db(); $self->init_db();
$self->init_l10n(); $self->init_l10n();
$self->init_user();
if ($config->{app}->{login_url}) { if ($config->{app}->{login_url}) {
return if !$self->check_authentication(); return if !$self->check_authentication();
...@@ -29,6 +30,7 @@ sub run { ...@@ -29,6 +30,7 @@ sub run {
my $email = $self->param('email'); my $email = $self->param('email');
my $db = $self->stash('db'); my $db = $self->stash('db');
my $l10n = $self->stash('l10n'); my $l10n = $self->stash('l10n');
my $user = $self->stash('user');
my $sp = $self->get_sp(entityid => $entityid); my $sp = $self->get_sp(entityid => $entityid);
return if !$sp; return if !$sp;
...@@ -103,14 +105,7 @@ sub run { ...@@ -103,14 +105,7 @@ sub run {
$base_templates_dir->child('mail'), $base_templates_dir->child('mail'),
] ]
}); });
my $user =
$ENV{'HTTP_DISPLAYNAME'} ? $ENV{'HTTP_DISPLAYNAME'} :
$ENV{'displayName'} ? $ENV{'displayName'} :
undef;
my $idp =
$ENV{'HTTP_SHIB_IDENTITY_PROVIDER'} ? $ENV{'HTTP_SHIB_IDENTITY_PROVIDER'} :
$ENV{'Shib-Identity-Provider'} ? $ENV{'Shib-Identity-Provider'} :
undef;
my $data = { my $data = {
app => { app => {
url => $config->{app}->{url}, url => $config->{app}->{url},
...@@ -118,9 +113,9 @@ sub run { ...@@ -118,9 +113,9 @@ sub run {
version => $config->{app}->{version}, version => $config->{app}->{version},
name => $config->{app}->{name}, name => $config->{app}->{name},
}, },
user => $user, user => $user->{name},
source_ip => $self->client_ip(), source_ip => $self->client_ip(),
idp => { entityid => $idp, }, idp => { entityid => $user->{idp}, },
sp => { entityid => $entityid, }, sp => { entityid => $entityid, },
to => $email, to => $email,
token => $token->secret(), token => $token->secret(),
......
...@@ -21,6 +21,7 @@ sub run { ...@@ -21,6 +21,7 @@ sub run {
$self->init_db(); $self->init_db();
$self->init_l10n(); $self->init_l10n();
$self->init_user();
if ($config->{app}->{login_url}) { if ($config->{app}->{login_url}) {
return if !$self->check_authentication(); return if !$self->check_authentication();
......
...@@ -21,6 +21,7 @@ sub run { ...@@ -21,6 +21,7 @@ sub run {
$self->init_db(); $self->init_db();
$self->init_l10n(); $self->init_l10n();
$self->init_user();
if ($config->{app}->{login_url}) { if ($config->{app}->{login_url}) {
return if !$self->check_authentication(); return if !$self->check_authentication();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment