Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Nov 5, 2019
1 parent f0ad094 commit f554df8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 61 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"symfony/framework-bundle": "^4",
"symfony/http-client": "^4",
"symfony/psr-http-message-bridge": "^1.2",
"symfony/security-bundle": "^4"
"symfony/security-bundle": "^4",
"ext-json": "*"
},
"require-dev": {
"drupol/php-conventions": "^1"
Expand Down
94 changes: 38 additions & 56 deletions src/Security/Core/User/EuloginUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@

namespace drupol\EuloginBundle\Security\Core\User;

use SimpleXMLElement;
use drupol\CasBundle\Security\Core\User\CasUser;

/**
* Class EuloginUser.
*/
final class EuloginUser implements EuloginUserInterface
{
/**
* The user storage.
*
* @var array
* @var \drupol\CasBundle\Security\Core\User\CasUser
*/
private $storage;
private $user;

/**
* EuloginUser constructor.
Expand All @@ -25,7 +23,7 @@ final class EuloginUser implements EuloginUserInterface
*/
public function __construct(array $data)
{
$this->storage = $data['serviceResponse']['authenticationSuccess'];
$this->user = new CasUser($this->normalizeUserData($data));
}

/**
Expand All @@ -46,15 +44,15 @@ public function eraseCredentials(): void

public function getAssuranceLevel()
{
return $this->get('assuranceLevel');
return $this->user->getAttribute('assuranceLevel');
}

/**
* {@inheritdoc}
*/
public function getAttributes(): array
{
return $this->getStorage();
return $this->user->getAttributes();
}

public function getDepartmentNumber()
Expand All @@ -64,52 +62,52 @@ public function getDepartmentNumber()

public function getDomain()
{
return $this->get('domain');
return $this->user->getAttribute('domain');
}

public function getDomainUsername()
{
return $this->get('domainUsername');
return $this->user->getAttribute('domainUsername');
}

public function getEmail()
{
return $this->get('email');
return $this->user->getAttribute('email');
}

public function getEmployeeNumber()
{
return $this->get('employeeNumber');
return $this->user->getAttribute('employeeNumber');
}

public function getEmployeeType()
{
return $this->get('employeeType');
return $this->user->getAttribute('employeeType');
}

public function getFirstName()
{
return $this->get('firstName');
return $this->user->getAttribute('firstName');
}

public function getGroups()
{
return $this->get('groups');
return $this->user->getAttribute('groups');
}

public function getLastName()
{
return $this->get('lastName');
return $this->user->getAttribute('lastName');
}

public function getLocale()
{
return $this->get('locale');
return $this->user->getAttribute('locale');
}

public function getLoginDate()
{
return $this->get('loginDate');
return $this->user->getAttribute('loginDate');
}

public function getOrgId()
Expand All @@ -125,15 +123,15 @@ public function getPassword()

public function getPgt(): ?string
{
return $this->get('proxyGrantingTicket');
return $this->user->getAttribute('proxyGrantingTicket');
}

/**
* {@inheritdoc}
*/
public function getPgtIOU(): ?string
{
return $this->get('proxyGrantingTicket');
return $this->user->getAttribute('proxyGrantingTicket');
}

/**
Expand Down Expand Up @@ -161,17 +159,17 @@ public function getSalt()

public function getSso()
{
return $this->get('sso');
return $this->user->getAttribute('sso');
}

public function getStrengths()
{
return $this->get('strength');
return $this->user->getAttribute('strength');
}

public function getTelephoneNumber()
{
return $this->get('telephone');
return $this->user->getAttribute('telephone');
}

public function getTeleworkingPriority()
Expand All @@ -180,37 +178,25 @@ public function getTeleworkingPriority()

public function getTicketType()
{
return $this->get('ticketType');
return $this->user->getAttribute('ticketType');
}

public function getUid()
{
return $this->get('uid');
return $this->user->getAttribute('uid');
}

public function getUser()
{
return $this->get('user');
return $this->user->getAttribute('user');
}

/**
* {@inheritdoc}
*/
public function getUsername()
{
return $this->get('user');
}

/**
* {@inheritdoc}
*/
public function withPgt(string $pgt): EuloginUser
{
$clone = clone $this;

$clone->storage['proxyGrantingTicket'] = $pgt;

return $clone;
return $this->user->getAttribute('user');
}

/**
Expand All @@ -222,34 +208,30 @@ public function withPgt(string $pgt): EuloginUser
* @return mixed
* The value.
*/
private function get($key)
private function getAttribute($key)
{
return $this->getStorage()[$key] ?? null;
return $this->user->getAttribute($key);
}

/**
* Get the storage.
* Normalize user data from EU Login to standard CAS user data.
*
* @return array
*/
private function getStorage()
{
return $this->storage;
}

/**
* @param SimpleXMLElement $data
* @param array $data
* The data from EU Login
*
* @return array
* The normalized data.
*/
private function parseXml(SimpleXMLElement $data): array
private function normalizeUserData(array $data): array
{
$array = [];
$storage = [];
$rootAttributes = ['user', 'proxyGrantingTicket'];

foreach ((array) $data as $index => $node) {
$array[$index] = ($node instanceof SimpleXMLElement) ? $this->parseXml($node) : (string) $node;
foreach ($rootAttributes as $rootAttribute) {
$storage[$rootAttribute] = $data[$rootAttribute] ?? null;
}
$storage['attributes'] = array_diff_key($data, array_flip($rootAttributes));

return $array;
return array_filter($storage) + ['attributes' => []];
}
}
10 changes: 8 additions & 2 deletions src/Security/Core/User/EuloginUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace drupol\EuloginBundle\Security\Core\User;

use drupol\CasBundle\Security\Core\User\CasUserInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\UserInterface;

Expand All @@ -15,9 +17,13 @@ class EuloginUserProvider implements EuloginUserProviderInterface
/**
* {@inheritdoc}
*/
public function loadUserByArray(array $data): CasUserInterface
public function loadUserByResponse(ResponseInterface $response): CasUserInterface
{
return new EuloginUser($data);
if (false === $user = json_decode((string) $response->getBody(), true)) {
throw new AuthenticationException('Unable to load user from response.');
}

return new EuloginUser($user['serviceResponse']['authenticationSuccess']);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Security/Core/User/EuloginUserProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@

use drupol\CasBundle\Security\Core\User\CasUserInterface;
use drupol\CasBundle\Security\Core\User\CasUserProviderInterface;
use Psr\Http\Message\ResponseInterface;

/**
* Interface EuloginUserProviderInterface.
*/
interface EuloginUserProviderInterface extends CasUserProviderInterface
{
/**
* @param array $data
* @param \Psr\Http\Message\ResponseInterface $response
*
* @return \drupol\CasBundle\Security\Core\User\CasUserInterface|\drupol\EuloginBundle\Security\Core\User\EuloginUserInterface
*/
public function loadUserByArray(array $data): CasUserInterface;
public function loadUserByResponse(ResponseInterface $response): CasUserInterface;
}

0 comments on commit f554df8

Please sign in to comment.