Skip to content

Commit cdb0b13

Browse files
committed
(feat) rewrite
1 parent a95986c commit cdb0b13

15 files changed

Lines changed: 193 additions & 48 deletions

README.md

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,98 @@
11
# OWC IDP UserData
22

3-
This repository contains interfaces for userdata objects to be used by IDP plugins (e.g. DigiD, eHerkenning)
3+
This package provides common `UserData` and `Session` classes for identity provider (IDP) plugins such as DigiD, eHerkenning, and eIDAS.
4+
5+
## Installation
6+
7+
Install via Composer:
8+
9+
```bash
10+
composer require owc/idp-userdata
11+
```
12+
13+
### Example: Providing User Data via IDP-Specific Filters
14+
15+
To provide user data to the session classes, use the IDP-specific WordPress filters in your plugin or theme:
16+
17+
```php
18+
use OWC\IdpUserData\DigiDUserData;
19+
20+
add_filter('owc_digid_is_logged_in', function (bool $isLoggedIn): bool {
21+
// Your logic to determine if the user is logged in with DigiD
22+
return true;
23+
});
24+
25+
add_filter('owc_digid_userdata', function (?DigiDUserData $userData): ?DigiDUserData {
26+
return new DigiDUserData([
27+
'bsn' => '123456789',
28+
]);
29+
});
30+
```
31+
32+
### Example: Providing User Data via Generic Filter (with switch)
33+
34+
You can also use the generic filters and handle multiple IDPs with a switch statement:
35+
36+
```php
37+
38+
use OWC\IdpUserData\DigiDUserData;
39+
use OWC\IdpUserData\eHerkenningUserData;
40+
use OWC\IdpUserData\eIDASUserData;
41+
use OWC\IdpUserData\IdpSession;
42+
use OWC\IdpUserData\Idp;
43+
use OWC\IdpUserData\UserData;
44+
45+
46+
add_filter('owc_idp_is_logged_in', function (bool $isLoggedIn, string $idp): bool {
47+
switch ($idp) {
48+
case Idp::DIGID:
49+
// Your logic for DigiD
50+
return true;
51+
case Idp::EHERKENNING:
52+
// Your logic for eHerkenning
53+
return false;
54+
case Idp::EIDAS:
55+
// Your logic for eIDAS
56+
return false;
57+
default:
58+
return $isLoggedIn;
59+
}
60+
}, 10, 2);
61+
62+
add_filter('owc_idp_userdata', function (?UserData $userData, string $idp): ?UserData {
63+
switch ($idp) {
64+
case Idp::DIGID:
65+
return new DigiDUserData([
66+
'bsn' => '123456789',
67+
]);
68+
case Idp::EHERKENNING:
69+
return new eHerkenningUserData([
70+
'kvk' => '987654321',
71+
'rsin' => '123456789',
72+
'vestigingsnummer' => '001',
73+
'bsn' => null,
74+
]);
75+
case Idp::EIDAS:
76+
return new eIDASUserData([
77+
'bsn' => '555555555',
78+
]);
79+
default:
80+
return $userData;
81+
}
82+
}, 10, 2);
83+
```
84+
85+
### Example: Getting User Data
86+
87+
```php
88+
use OWC\IdpUserData\DigiDSession;
89+
use OWC\IdpUserData\DigiDUserData;
90+
91+
if (DigiDSession::isLoggedIn()) {
92+
$userData = DigiDSession::getUserData();
93+
if ($userData instanceof DigiDUserData) {
94+
$bsn = $userData->getBsn();
95+
// ...
96+
}
97+
}
98+
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "owc/idp-userdata",
33
"type": "library",
4-
"description": "Common interfaces for IDP userdata (DigiD, eHerkenning)",
4+
"description": "Common userdata classes and session helpers for IDP userdata (DigiD, eHerkenning)",
55
"authors": [
66
{
77
"name": "Yard | Digital Agency",

src/DigiDSession.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ class DigiDSession extends IdpSession
88
{
99
protected static function get_idp(): string
1010
{
11-
return 'digid';
11+
return Idp::DIGID;
1212
}
1313

14-
public static function getUserData(): ?DigiDUserDataInterface
14+
public static function getUserData(): ?DigiDUserData
1515
{
1616
return parent::getUserData();
1717
}

src/DigiDUserData.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace OWC\IdpUserData;
4+
5+
class DigiDUserData extends UserData
6+
{
7+
protected string $bsn;
8+
9+
public function getBsn(): string
10+
{
11+
return $this->bsn;
12+
}
13+
14+
}

src/DigiDUserDataInterface.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Idp.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace OWC\IdpUserData;
4+
5+
// Convert to enums if PHP 8.1+ is supported
6+
class Idp {
7+
public const DIGID = 'digid';
8+
public const EHERKENNING = 'eherkenning';
9+
public const EIDAS = 'eidas';
10+
}

src/IdpSession.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static function isLoggedIn(): bool
1515
return apply_filters('owc_' . static::get_idp() . '_is_logged_in', $isLoggedIn);
1616
}
1717

18-
protected static function getUserData(): ?UserDataInterface
18+
protected static function getUserData(): ?UserData
1919
{
2020
$userData = apply_filters('owc_idp_userdata', null, static::get_idp());
2121

src/UserData.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace OWC\IdpUserData;
4+
5+
abstract class UserData
6+
{
7+
protected array $data;
8+
9+
public function __construct(array $data) {
10+
$classVars = get_class_vars(static::class);
11+
12+
$data = wp_parse_args($data, $classVars);
13+
$data = wp_array_slice_assoc($data, array_keys($classVars));
14+
15+
foreach ($data as $key => $value) {
16+
$this->$key = $value;
17+
}
18+
}
19+
}

src/UserDataInterface.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/eHerkenningSession.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ class eHerkenningSession extends IdpSession
88
{
99
protected static function get_idp(): string
1010
{
11-
return 'eherkenning';
11+
return Idp::EHERKENNING;
1212
}
1313

14-
public static function getUserData(): ?eHerkenningUserDataInterface
14+
public static function getUserData(): ?eHerkenningUserData
1515
{
1616
return parent::getUserData();
1717
}

0 commit comments

Comments
 (0)