Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 96 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,98 @@
# OWC IDP UserData

This repository contains interfaces for userdata objects to be used by IDP plugins (e.g. DigiD, eHerkenning)
This package provides common `UserData` and `Session` classes for identity provider (IDP) plugins such as DigiD, eHerkenning, and eIDAS.

## Installation

Install via Composer:

```bash
composer require owc/idp-userdata
```

### Example: Providing User Data via IDP-Specific Filters

To provide user data to the session classes, use the IDP-specific WordPress filters in your plugin or theme:

```php
use OWC\IdpUserData\DigiDUserData;

add_filter('owc_digid_is_logged_in', function (bool $isLoggedIn): bool {
// Your logic to determine if the user is logged in with DigiD
return true;
});

add_filter('owc_digid_userdata', function (?DigiDUserData $userData): ?DigiDUserData {
return new DigiDUserData([
'bsn' => '123456789',
]);
});
```

### Example: Providing User Data via Generic Filter (with switch)

You can also use the generic filters and handle multiple IDPs with a switch statement:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

```php

use OWC\IdpUserData\DigiDUserData;
use OWC\IdpUserData\eHerkenningUserData;
use OWC\IdpUserData\eIDASUserData;
use OWC\IdpUserData\IdpSession;
use OWC\IdpUserData\Idp;
use OWC\IdpUserData\UserData;


add_filter('owc_idp_is_logged_in', function (bool $isLoggedIn, string $idp): bool {
switch ($idp) {
case Idp::DIGID:
// Your logic for DigiD
return true;
case Idp::EHERKENNING:
// Your logic for eHerkenning
return false;
case Idp::EIDAS:
// Your logic for eIDAS
return false;
default:
return $isLoggedIn;
}
}, 10, 2);

add_filter('owc_idp_userdata', function (?UserData $userData, string $idp): ?UserData {
switch ($idp) {
case Idp::DIGID:
return new DigiDUserData([
'bsn' => '123456789',
]);
case Idp::EHERKENNING:
return new eHerkenningUserData([
'kvk' => '987654321',
'rsin' => '123456789',
'vestigingsnummer' => '001',
'bsn' => null,
]);
case Idp::EIDAS:
return new eIDASUserData([
'bsn' => '555555555',
]);
default:
return $userData;
}
}, 10, 2);
```

### Example: Getting User Data

```php
use OWC\IdpUserData\DigiDSession;
use OWC\IdpUserData\DigiDUserData;

if (DigiDSession::isLoggedIn()) {
$userData = DigiDSession::getUserData();
if ($userData instanceof DigiDUserData) {
$bsn = $userData->getBsn();
// ...
}
}
```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "owc/idp-userdata",
"type": "library",
"description": "Common interfaces for IDP userdata (DigiD, eHerkenning)",
"description": "Common userdata classes and session helpers for IDP userdata (DigiD, eHerkenning)",
"authors": [
{
"name": "Yard | Digital Agency",
Expand Down
4 changes: 2 additions & 2 deletions src/DigiDSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class DigiDSession extends IdpSession
{
protected static function get_idp(): string
{
return 'digid';
return Idp::DIGID;
}

public static function getUserData(): ?DigiDUserDataInterface
public static function getUserData(): ?DigiDUserData
{
return parent::getUserData();
}
Expand Down
14 changes: 14 additions & 0 deletions src/DigiDUserData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace OWC\IdpUserData;

class DigiDUserData extends UserData
{
protected string $bsn;

public function getBsn(): string
{
return $this->bsn;
}

}
10 changes: 0 additions & 10 deletions src/DigiDUserDataInterface.php

This file was deleted.

10 changes: 10 additions & 0 deletions src/Idp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace OWC\IdpUserData;

// TODO: Convert to enums if PHP 8.1+ is supported
class Idp {
public const DIGID = 'digid';
public const EHERKENNING = 'eherkenning';
public const EIDAS = 'eidas';
}
2 changes: 1 addition & 1 deletion src/IdpSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static function isLoggedIn(): bool
return apply_filters('owc_' . static::get_idp() . '_is_logged_in', $isLoggedIn);
}

protected static function getUserData(): ?UserDataInterface
protected static function getUserData(): ?UserData
{
$userData = apply_filters('owc_idp_userdata', null, static::get_idp());

Expand Down
19 changes: 19 additions & 0 deletions src/UserData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace OWC\IdpUserData;

abstract class UserData
{
protected array $data;

public function __construct(array $data) {
$classVars = get_class_vars(static::class);

$data = wp_parse_args($data, $classVars);
$data = wp_array_slice_assoc($data, array_keys($classVars));

foreach ($data as $key => $value) {
$this->$key = $value;
}
}
}
9 changes: 0 additions & 9 deletions src/UserDataInterface.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/eHerkenningSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class eHerkenningSession extends IdpSession
{
protected static function get_idp(): string
{
return 'eherkenning';
return Idp::EHERKENNING;
}

public static function getUserData(): ?eHerkenningUserDataInterface
public static function getUserData(): ?eHerkenningUserData
{
return parent::getUserData();
}
Expand Down
32 changes: 32 additions & 0 deletions src/eHerkenningUserData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace OWC\IdpUserData;

class eHerkenningUserdata extends UserData
{
protected string $kvk;

protected ?string $rsin = null;

protected ?string $vestigingsnummer = null;

protected ?string $bsn = null;

public function getKvk(): string
{
return $this->kvk;
}

public function getBsn(): ?string
{
return $this->bsn;
}

public function getRsin(): ?string {
return $this->rsin;
}

public function getVestigingsnummer(): ?string {
return $this->vestigingsnummer;
}
}
10 changes: 0 additions & 10 deletions src/eHerkenningUserDataInterface.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/eIDASSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class eIDASSession extends IdpSession
{
protected static function get_idp(): string
{
return 'eidas';
return Idp::EIDAS;
}

public static function getUserData(): ?eIDASUserDataInterface
public static function getUserData(): ?eIDASUserData
{
return parent::getUserData();
}
Expand Down
14 changes: 14 additions & 0 deletions src/eIDASUserData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace OWC\IdpUserData;

class eIDASUserData extends UserData
{
protected string $bsn;

public function getBsn(): string {
return $this->bsn;
}
}
10 changes: 0 additions & 10 deletions src/eIDASUserDataInterface.php

This file was deleted.