Skip to content

Commit

Permalink
Abstracted retrieval of Second factors
Browse files Browse the repository at this point in the history
The Registration and SecondFactor controllers retrieve their second
factor from the SecondFactorService who in turn wraps the SecondFactor
collections into the SecondFactorTypeCollection.
  • Loading branch information
MKodde committed Nov 7, 2017
1 parent ebbbe73 commit 887ceb4
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
namespace Surfnet\StepupSelfService\SelfServiceBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Surfnet\StepupSelfService\SamlStepupProviderBundle\Provider\ViewConfig;
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SecondFactorService;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Expand All @@ -41,40 +39,27 @@ public function displaySecondFactorTypesAction()
/** @var SecondFactorService $service */
$service = $this->get('surfnet_stepup_self_service_self_service.service.second_factor');

$availableSecondFactors = $this->getParameter('ss.enabled_second_factors');
if (!empty($institutionConfigurationOptions->allowedSecondFactors)) {
$availableSecondFactors = array_intersect(
$availableSecondFactors,
$institutionConfigurationOptions->allowedSecondFactors
);
}
$availableSecondFactors = array_combine($availableSecondFactors, $availableSecondFactors);
$availableGsspSecondFactors = [];

foreach ($availableSecondFactors as $index => $secondFactor) {
try {
/** @var ViewConfig $secondFactorConfig */
$secondFactorConfig = $this->get("gssp.view_config.{$secondFactor}");
$availableGsspSecondFactors[$index] = $secondFactorConfig;
// Remove the gssp second factors from the regular second factors.
unset($availableSecondFactors[$index]);
} catch (ServiceNotFoundException $e) {
continue;
}
}


// Get all available second factors from the config.
$allSecondFactors = $this->getParameter('ss.enabled_second_factors');
$unverified = $service->findUnverifiedByIdentity($identity->id);
$verified = $service->findVerifiedByIdentity($identity->id);
$vetted = $service->findVettedByIdentity($identity->id);
// Determine which Second Factors are still available for registration.
$available = $service->determineAvailable($allSecondFactors, $unverified, $verified, $vetted);

$secondFactors = $service->getSecondFactorsForIdentity(
$identity,
$allSecondFactors,
$institutionConfigurationOptions->allowedSecondFactors
);

$availableGsspSecondFactors = [];
foreach ($secondFactors->available as $index => $secondFactor) {
/** @var ViewConfig $secondFactorConfig */
$secondFactorConfig = $this->get("gssp.view_config.{$secondFactor}");
$availableGsspSecondFactors[$index] = $secondFactorConfig;
// Remove the gssp second factors from the regular second factors.
unset($secondFactors->available[$index]);
}

return [
'commonName' => $this->getIdentity()->commonName,
'availableSecondFactors' => array_combine($available, $available),
'availableSecondFactors' => $secondFactors->available,
'availableGsspSecondFactors' => $availableGsspSecondFactors,
'tiqrAppAndroidUrl' => $this->getParameter('tiqr_app_android_url'),
'tiqrAppIosUrl' => $this->getParameter('tiqr_app_ios_url'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public function listAction()

return [
'email' => $identity->email,
'unverifiedSecondFactors' => $secondFactors['unverified'],
'verifiedSecondFactors' => $verified,
'vettedSecondFactors' => $vetted,
'availableSecondFactors' => $available,
'unverifiedSecondFactors' => $secondFactors->unverified,
'verifiedSecondFactors' => $secondFactors->verified,
'vettedSecondFactors' => $secondFactors->vetted,
'availableSecondFactors' => $secondFactors->available,
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public function getRegistrationCode($secondFactorId, $identityId)
* @param VettedSecondFactorCollection $vettedCollection
* @return array
*/
public function determineAvailable(
private function determineAvailable(
array $allSecondFactors,
UnverifiedSecondFactorCollection $unverifiedCollection,
VerifiedSecondFactorCollection $verifiedCollection,
Expand Down Expand Up @@ -312,7 +312,7 @@ private function filterAvailableSecondFactors(array $allSecondFactors, Collectio
* @param $identity
* @param $allSecondFactors
* @param $allowedSecondFactors
* @return array
* @return SecondFactorTypeCollection
*/
public function getSecondFactorsForIdentity($identity, $allSecondFactors, $allowedSecondFactors)
{
Expand All @@ -329,11 +329,12 @@ public function getSecondFactorsForIdentity($identity, $allSecondFactors, $allow
);
}

return [
'unverified' => $unverified,
'verified' => $verified,
'vetted' => $vetted,
'available' => $available,
];
$collection = new SecondFactorTypeCollection();
$collection->unverified = $unverified;
$collection->verified = $verified;
$collection->vetted = $vetted;
$collection->available = array_combine($available, $available);

return $collection;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* Copyright 2014 SURFnet bv
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Surfnet\StepupSelfService\SelfServiceBundle\Service;


use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\UnverifiedSecondFactorCollection;
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\VerifiedSecondFactorCollection;
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\VettedSecondFactorCollection;

class SecondFactorTypeCollection
{
/**
* @var UnverifiedSecondFactorCollection
*/
public $unverified;

/**
* @var VerifiedSecondFactorCollection
*/
public $verified;

/**
* @var VettedSecondFactorCollection
*/
public $vetted;

/**
* @var array
*/
public $available;
}

0 comments on commit 887ceb4

Please sign in to comment.