Skip to content
This repository was archived by the owner on Nov 14, 2025. It is now read-only.

Commit ab34261

Browse files
committed
Avoid DB for configuration
1 parent 85dadba commit ab34261

30 files changed

+276
-1433
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
9+
## [UNRELEASED]
10+
11+
### Changed
12+
- IdPs configuration is now stored in the main configuration file
13+
- IdPs are identified by a key instead of UUID
14+
- Name ID format configurable
15+
16+
### Removed
17+
- Tenant model and repository
18+
- CLI commands
19+
20+
821
## [2.4.0] - 2024-04-13
922

1023
### Added

README.md

Lines changed: 64 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[![Code Coverage][ico-code-coverage]][link-code-coverage]
88
[![Total Downloads][ico-downloads]][link-downloads]
99

10-
An integration to add SSO to your service via SAML2 protocol based on [OneLogin](https://github.com/onelogin/php-saml) toolkit.
10+
An integration to add SSO to your service via SAML2 protocol based on [SAML PHP Toolkit] toolkit.
1111

1212
This package turns your application into Service Provider with the support of multiple Identity Providers.
1313

@@ -48,52 +48,30 @@ For older versions, you have to add the service provider and alias to your `conf
4848
php artisan vendor:publish --provider="Slides\Saml2\ServiceProvider"
4949
```
5050

51-
##### Step 3. Run migrations
52-
53-
```
54-
php artisan migrate
55-
```
5651

5752
### Configuring
5853

59-
Once you publish `saml2.php` to `app/config`, you need to configure your SP. Most of options are inherited from [OneLogin Toolkit](https://github.com/onelogin/php-saml), so you can check documentation there.
60-
61-
#### Identity Providers (IdPs)
62-
63-
To distinguish between identity providers there is an entity called Tenant that represent each IdP.
54+
Once you publish `saml2.php` to `app/config`, you need to configure your service provider (SP).
55+
Most of the options are inherited from [SAML PHP Toolkit], so you can check documentation there.
56+
This relates to identity providers (IdPs) as well.
6457

65-
When request comes to an application, the middleware parses UUID and resolves the Tenant.
6658

67-
You can easily manage tenants using the following console commands:
6859

69-
- `artisan saml2:create-tenant`
70-
- `artisan saml2:update-tenant`
71-
- `artisan saml2:delete-tenant`
72-
- `artisan saml2:restore-tenant`
73-
- `artisan saml2:list-tenants`
74-
- `artisan saml2:tenant-credentials`
60+
#### Identity Providers
7561

76-
> To learn their options, run a command with `-h` parameter.
62+
Identity providers (IdPs) are configured in the same `saml2.php` configuration file under `idps` key.
63+
**N.B.** That it is plural (`idp**S**`), unlike in [SAML PHP Toolkit], because we support multiple IdPs.
7764

78-
Each Tenant has the following attributes:
79-
80-
- **UUID** — a unique identifier that allows to resolve a tenannt and configure SP correspondingly
81-
- **Key** — a custom key to use for application needs
82-
- **Entity ID**[Identity Provider Entity ID](https://spaces.at.internet2.edu/display/InCFederation/Entity+IDs)
83-
- **Login URL** — Identity Provider Single Sign On URL
84-
- **Logout URL** — Identity Provider Logout URL
85-
- **x509 certificate** — The certificate provided by Identity Provider in **base64** format
86-
- **Metadata** — Custom parameters for your application needs
8765

8866
#### Default routes
8967

9068
The following routes are registered by default:
9169

92-
- `GET saml2/{uuid}/login`
93-
- `GET saml2/{uuid}/logout`
94-
- `GET saml2/{uuid}/metadata`
95-
- `POST saml2/{uuid}/acs`
96-
- `POST saml2/{uuid}/sls`
70+
- `GET saml2/{key}/login`
71+
- `GET saml2/{key}/logout`
72+
- `GET saml2/{key}/metadata`
73+
- `POST saml2/{key}/acs`
74+
- `POST saml2/{key}/sls`
9775

9876
You may disable them by setting `saml2.useRoutes` to `false`.
9977

@@ -163,15 +141,15 @@ protected $middlewareGroups = [
163141

164142
There are two ways the user can logout:
165143
- By logging out in your app. In this case you SHOULD notify the IdP first so it'll close the global session.
166-
- By logging out of the global SSO Session. In this case the IdP will notify you on `/saml2/{uuid}/slo` endpoint (already provided).
144+
- By logging out of the global SSO Session. In this case the IdP will notify you on `/saml2/{key}/sls` endpoint (already provided).
167145

168146
For the first case, call `Saml2Auth::logout();` or redirect the user to the route `saml.logout` which does just that.
169147
Do not close the session immediately as you need to receive a response confirmation from the IdP (redirection).
170148
That response will be handled by the library at `/saml2/sls` and will fire an event for you to complete the operation.
171149

172150
For the second case you will only receive the event. Both cases receive the same event.
173151

174-
Note that for the second case, you may have to manually save your session to make the logout stick (as the session is saved by middleware, but the OneLogin library will redirect back to your IdP before that happens):
152+
Note that for the second case, you may have to manually save your session to make the logout stick (as the session is saved by middleware, but the [SAML PHP Toolkit] library will redirect back to your IdP before that happens):
175153

176154
```php
177155
Event::listen('Slides\Saml2\Events\SignedOut', function (SignedOut $event) {
@@ -184,18 +162,18 @@ Event::listen('Slides\Saml2\Events\SignedOut', function (SignedOut $event) {
184162

185163
Sometimes, you need to create links to your application with support of SSO lifecycle. It means you expect a user to be signed in once you click on that link.
186164

187-
The most popular example is generating links from emails, where you need to make sure when user goes to your application from email, he will be logged in.
188-
To solve this issue, you can use helpers that allow you create SSO-friendly routes and URLs — `saml_url()` and `saml_route()`.
189-
190-
To generate a link, you need to call one of functions and pass UUID of the tenant as a second parameter, unless your session knows that user was resolved by SSO.
165+
The most popular example is generating links from emails, where you need to make sure when user goes to your application from email, they will be logged in.
166+
To solve this issue, you can use helpers that allow you to create SSO-friendly routes and URLs — `saml_url()` and `saml_route()`.
191167

192-
> To retrieve UUID based on user, you should implement logic that links your internal user to a tenant.
168+
To generate a link, you need to call one of functions and pass the IdP key as a second parameter, unless your session knows that user was resolved by SSO.
193169

194170
Then, it generates a link like this:
195171
```
196-
https://yourdomain/saml/63fffdd1-f416-4bed-b3db-967b6a56896b/login?returnTo=https://yourdomain.com/your/actual/link
172+
https://yourdomain/saml2/default/login?returnTo=https://yourdomain.com/your/actual/link
197173
```
198174

175+
where `default` is the IdP key from the `saml2.php` configuration file.
176+
199177
Basically, when user clicks on a link, it initiates SSO login process and redirects it back to your needed URL.
200178

201179
## Examples
@@ -215,40 +193,55 @@ You need to retrieve the following parameters:
215193
- Logout URL
216194
- Certificate (Base64)
217195

218-
##### Step 2. Create a Tenant
219-
220-
Based on information you received below, create a Tenant, like this:
221-
222-
```
223-
php artisan saml2:create-tenant \
224-
--key=azure_testing \
225-
--entityId=https://sts.windows.net/fb536a7a-7251-4895-a09a-abd8e614c70b/ \
226-
--loginUrl=https://login.microsoftonline.com/fb536a7a-7251-4895-a09a-abd8e614c70b/saml2 \
227-
--logoutUrl=https://login.microsoftonline.com/common/wsfederation?wa=wsignout1.0 \
228-
--x509cert="MIIC0jCCAbqgAw...CapVR4ncDVjvbq+/S" \
229-
--metadata="customer:11235,anotherfield:value" // you might add some customer parameters here to simplify logging in your customer afterwards
196+
##### Step 2. Configure Identity Provider
197+
198+
Based on information you received in step one, configure your IdP like this:
199+
200+
```shell
201+
cat config/saml2.php
202+
...
203+
204+
'idps' => [
205+
// The key will be used as an IdP identifier as well as in routes.
206+
'azure_testing' => [
207+
'relay_state_url' => env('SAML2_RELAY_STATE_URL', ''),
208+
// Place any other IdP related configuration from the 'idp' section
209+
// in the https://github.com/SAML-Toolkits/php-saml#settings below.
210+
// Identifier of the IdP entity (must be a URI).
211+
'entityId' => 'https://sts.windows.net/fb536a7a-7251-4895-a09a-abd8e614c70b/',
212+
// SSO endpoint info of the IdP. (Authentication Request protocol)
213+
'singleSignOnService' => [
214+
// URL Target of the IdP where the Authentication Request Message will be sent.
215+
'url' => 'https://login.microsoftonline.com/fb536a7a-7251-4895-a09a-abd8e614c70b/saml2',
216+
],
217+
// SLO endpoint info of the IdP.
218+
'singleLogoutService' => [
219+
// URL Location of the IdP where SLO Request will be sent.
220+
'url' => 'https://login.microsoftonline.com/common/wsfederation?wa=wsignout1.0',
221+
// URL location of the IdP where SLO Response will be sent (ResponseLocation)
222+
// if not set, url for the SLO Request will be used.
223+
'responseUrl' => '',
224+
],
225+
'x509cert' => env('SAML2_IDP_X509', ''),
226+
],
227+
],
228+
];
229+
printenv SAML2_IDP_X509
230+
MIIC0jCCAbqgAw...
230231
```
231232
232-
Once you successfully created the tenant, you will receive the following output:
233233
234-
```
235-
The tenant #1 (63fffdd1-f416-4bed-b3db-967b6a56896b) was successfully created.
234+
##### Step 3. Register your service provider in Identity Provider
236235
237-
Credentials for the tenant
238-
--------------------------
236+
Assign parameters to your IdP on the application Single-Sign-On settings page.
239237
240-
Identifier (Entity ID): https://yourdomain.com/saml/63fffdd1-f416-4bed-b3db-967b6a56896b/metadata
241-
Reply URL (Assertion Consumer Service URL): https://yourdomain.com/saml/63fffdd1-f416-4bed-b3db-967b6a56896b/acs
242-
Sign on URL: https://yourdomain.com/saml/63fffdd1-f416-4bed-b3db-967b6a56896b/login
243-
Logout URL: https://yourdomain.com/saml/63fffdd1-f416-4bed-b3db-967b6a56896b/logout
244-
Relay State: / (optional)
245-
```
246-
247-
##### Step 3. Configure Identity Provider
238+
![Azure AD](https://i.imgur.com/3hkjFLZ.png)
248239
249-
Using the output below, assign parameters to your IdP on application Single-Sign-On settings page.
240+
- Identifier (Entity ID) - `https://yourdomain.com/saml2/azure_testing/metadata` or ID you assigned to your SP in the `saml2.php`
241+
- Reply URL (Assertion Consumer Service URL) - `https://yourdomain.com/saml2/azure_testing/acs`
242+
- Sign on URL - `https://yourdomain.com/saml2/azure_testing/login`
243+
- Logout URL - `https://yourdomain.com/saml2/azure_testing/logout`
250244
251-
![Azure AD](https://i.imgur.com/3hkjFLZ.png)
252245
253246
##### Step 4. Make sure your application accessible by Azure AD
254247
@@ -306,3 +299,5 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio
306299
[link-original-author]: https://github.com/aacotroneo
307300
[link-author]: https://github.com/brezzhnev
308301
[link-contributors]: ../../contributors
302+
303+
[SAML PHP Toolkit]: https://github.com/SAML-Toolkits/php-saml

composer.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@
1414
"require": {
1515
"php": ">=7.1",
1616
"ext-openssl": "*",
17-
"illuminate/console": "~5.5|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
18-
"illuminate/database": "~5.5|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
1917
"illuminate/support": "~5.4|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
20-
"onelogin/php-saml": "^3.0|^4.0",
21-
"ramsey/uuid": "^3.8|^4.0"
18+
"onelogin/php-saml": "^3.0|^4.0"
2219
},
2320
"require-dev": {
2421
"mockery/mockery": "^0.9.9",

config/saml2.php

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,6 @@
22

33
return [
44

5-
/*
6-
|--------------------------------------------------------------------------
7-
| Tenant Model
8-
|--------------------------------------------------------------------------
9-
|
10-
| This will allow you to override the tenant model with your own.
11-
|
12-
*/
13-
14-
'tenantModel' => \Slides\Saml2\Models\Tenant::class,
15-
165
/*
176
|--------------------------------------------------------------------------
187
| Use built-in routes
@@ -22,11 +11,11 @@
2211
|
2312
| Method | URI | Name
2413
| -------|---------------------------------|------------------
25-
| POST | {routesPrefix}/{uuid}/acs | saml.acs
26-
| GET | {routesPrefix}/{uuid}/login | saml.login
27-
| GET | {routesPrefix}/{uuid}/logout | saml.logout
28-
| GET | {routesPrefix}/{uuid}/metadata | saml.metadata
29-
| GET | {routesPrefix}/{uuid}/sls | saml.sls
14+
| POST | {routesPrefix}/{key}/acs | saml.acs
15+
| GET | {routesPrefix}/{key}/login | saml.login
16+
| GET | {routesPrefix}/{key}/logout | saml.logout
17+
| GET | {routesPrefix}/{key}/metadata | saml.metadata
18+
| GET | {routesPrefix}/{key}/sls | saml.sls
3019
|
3120
*/
3221

@@ -159,6 +148,8 @@
159148
*/
160149

161150
'NameIDFormat' => 'urn:oasis:names:tc:SAML:2.0:nameid-format:persistent',
151+
// Set to true to use the format specified in the IdP.
152+
'NameIDFormatFromIdp' => false,
162153

163154
/*
164155
|--------------------------------------------------------------------------
@@ -387,11 +378,35 @@
387378

388379
/*
389380
|--------------------------------------------------------------------------
390-
| Load default migrations
381+
| Identity providers (IdPs) configuration.
391382
|--------------------------------------------------------------------------
392383
|
393-
| This will allow you to disable or enable the default migrations of the package.
394-
|
384+
| N.B. Unlike underlying SAML PHP Toolkit library which supports only
385+
| one IdP at a time, we can handle multiple IdPs and thus the key below
386+
| is plural.
395387
*/
396-
'load_migrations' => true,
388+
'idps' => [
389+
// The key will be used as an IdP identifier as well as in routes.
390+
'default' => [
391+
'relay_state_url' => env('SAML2_RELAY_STATE_URL', ''),
392+
// Place any other IdP related configuration from the 'idp' section
393+
// in the https://github.com/SAML-Toolkits/php-saml#settings below.
394+
// Identifier of the IdP entity (must be a URI).
395+
'entityId' => '',
396+
// SSO endpoint info of the IdP. (Authentication Request protocol).
397+
'singleSignOnService' => [
398+
// URL Target of the IdP where the Authentication Request Message
399+
// will be sent.
400+
'url' => '',
401+
],
402+
// SLO endpoint info of the IdP.
403+
'singleLogoutService' => [
404+
// URL Location of the IdP where SLO Request will be sent.
405+
'url' => '',
406+
// URL location of the IdP where SLO Response will be sent (ResponseLocation)
407+
// if not set, url for the SLO Request will be used.
408+
'responseUrl' => '',
409+
],
410+
],
411+
],
397412
];

database/migrations/2019_06_24_140207_create_saml2_tenants_table.php

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

database/migrations/2020_10_22_140856_add_relay_state_url_column_to_saml2_tenants_table.php

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

0 commit comments

Comments
 (0)