Improve Documentation of Attribute Mapping

Created on 26 February 2024, 10 months ago
Updated 27 February 2024, 10 months ago

According to the current documentation, it is possible to map User object field data to a Simplesaml attribute. For example:

array('field_name' => 'field_first_name', 'attribute_name' => 'givenName'),

However it's not clear how to use custom fields or more complex fields or if this is possible at all. For instance, I am using the name module, which is a more complex field with subfields like given and family for first and last name. How would I go about mapping a first and last name using the name field to an Simplesaml attribute?

✨ Feature request
Status

Active

Version

2.0

Component

Documentation

Created by

πŸ‡ΊπŸ‡ΈUnited States brooke_heaton

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @brooke_heaton
  • πŸ‡ΊπŸ‡ΈUnited States brooke_heaton

    For the benefit of others, here's how I was able to get custom attributes/mapping working with drupalauth4ssp.

    First, you will need to patch the drupalauth/simplesamlphp-module-drupalauth module required by drupalauth4ssp using this PR. As of Feb 27, 2024 the PR has not yet been integrated. What that PR does is it creates an Event in the drupalauth SSP module to allow for a Drupal EventSubscriber.

    Next you will need to create your custom EventSubscriber to subscribe to the SimpleSAML\Module\drupalauth\Event\SetAttributesEvent created with the patch from the PR above.

    Below is an example of how I am mapping to First and Last names in Drupal which are created via the 'name' module, which is not accounted for with the out-of-the-box attribute mapping of drupaluth.

     declare(strict_types=1);
    
    namespace Drupal\custom_sso\EventSubscriber;
    
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    use SimpleSAML\Module\drupalauth\Event\SetAttributesEvent;
    
    /**
     * @todo Add description for this subscriber.
     */
    final class CustomSsoSubscriber implements EventSubscriberInterface
    {
    
      /**
       * {@inheritdoc}
       */
        public static function getSubscribedEvents(): array
        {
            $message = "getSubcribedEvents fired";
            return [
            SetAttributesEvent::EVENT_NAME => ['setAttributes'],
            ];
        }
    
      /**
       * React to attributes
       *
       * @param \SimpleSAML\Module\drupalauth\Event\SetAttributesEvent $event
       *   Drupalauth setAttributes event.
       */
        public function setAttributes(SetAttributesEvent $event)
        {
            $user = $event->getUser();
            if (!$user->get('field_name')->isEmpty()) {
                $first = isset($user->get('field_name')->given) ? $user->get('field_name')->given : NULL;
                $last = isset($user->get('field_name')->family) ? $user->get('field_name')->family : NULL;
                $attributes = $event->getAttributes();
                $attributes['givenName'] = [$first];
                $attributes['sn'] = [$last];
                $event->setAttributes($attributes);
            }
        }
    }
    
    
Production build 0.71.5 2024