{"id":36993074,"url":"https://github.com/iwouldrathercode/php-custom-saml","last_synced_at":"2026-01-13T23:45:15.220Z","repository":{"id":56994965,"uuid":"271725429","full_name":"iwouldrathercode/php-custom-saml","owner":"iwouldrathercode","description":"Opinionated fork of OneLogin's SAML PHP Toolkit Compatible with PHP 5.X \u0026 7.X","archived":false,"fork":false,"pushed_at":"2022-10-14T03:48:00.000Z","size":66,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-22T01:45:19.498Z","etag":null,"topics":["laravel","laravel-5-package"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iwouldrathercode.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-06-12T06:27:48.000Z","updated_at":"2022-10-14T03:42:57.000Z","dependencies_parsed_at":"2022-08-21T13:50:33.478Z","dependency_job_id":null,"html_url":"https://github.com/iwouldrathercode/php-custom-saml","commit_stats":null,"previous_names":[],"tags_count":43,"template":false,"template_full_name":null,"purl":"pkg:github/iwouldrathercode/php-custom-saml","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iwouldrathercode%2Fphp-custom-saml","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iwouldrathercode%2Fphp-custom-saml/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iwouldrathercode%2Fphp-custom-saml/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iwouldrathercode%2Fphp-custom-saml/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iwouldrathercode","download_url":"https://codeload.github.com/iwouldrathercode/php-custom-saml/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iwouldrathercode%2Fphp-custom-saml/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28405292,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-13T21:51:37.118Z","status":"ssl_error","status_checked_at":"2026-01-13T21:45:14.585Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["laravel","laravel-5-package"],"created_at":"2026-01-13T23:45:15.152Z","updated_at":"2026-01-13T23:45:15.209Z","avatar_url":"https://github.com/iwouldrathercode.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"## iwouldrathercode/php-custom-saml\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/iwouldrathercode/cognito.svg?style=flat-square)](https://packagist.org/packages/iwouldrathercode/php-custom-saml)\n[![Total Downloads](https://img.shields.io/packagist/dt/iwouldrathercode/cognito.svg?style=flat-square)](https://packagist.org/packages/iwouldrathercode/php-custom-saml)\n\nOpinionated fork of OneLogin's SAML PHP Toolkit Compatible with PHP 5.X \u0026 7.X. Based on  [OneLogin](https://github.com/onelogin/php-saml) toolkit, which is much lighter and easier to install than simplesamlphp SP. It doesn't need separate routes or session storage to work!\n\n## Installation - Composer\n\nYou can install the package via composer:\n\n```\ncomposer require iwouldrathercode/php-custom-saml\n```\nOr manually add this to your composer.json:\n\n```json\n\"iwouldrathercode/php-custom-saml\": \"*\"\n```\n\nIf you are using Laravel 5.5 and up, the service provider will automatically get registered.\n\nFor older versions of Laravel (\u003c5.5), you have to add the service provider to config/app.php:\n\n```php\n'providers' =\u003e [\n        ...\n    \tIwouldrathercode\\Saml2\\Saml2ServiceProvider::class,\n]\n```\n\nThen publish the config files with `php artisan vendor:publish --provider=\"Iwouldrathercode\\Saml2\\Saml2ServiceProvider\"`. This will add the files `app/config/saml2_settings.php` \u0026 `app/config/saml2/test_idp_settings.php`, which you will need to customize.\n\nThe test_idp_settings.php config is handled almost directly by  [OneLogin](https://github.com/onelogin/php-saml) so you should refer to that for full details, but we'll cover here what's really necessary. There are some other config about routes you may want to check, they are pretty strightforward.\n\n### Configuration\n\n#### Define the IDPs\nDefine names of all the IDPs you want to configure in saml2_settings.php. Optionally keep 'test' as the first IDP if you want to use the simplesamlphp demo, and add real IDPs after that. The name of the IDP will show up in the URL used by the Saml2 routes this library makes, as well as internally in the filename for each IDP's config.\n\n```php\n    'idpNames' =\u003e ['test', 'myidp1', 'myidp2'],\n```\n\n#### Configure laravel-saml2 to know about each IDP\n\nYou will need to create a separate configuration file for each IDP under `app/config/saml2/` folder. e.g. `myidp1_idp_settings.php`. You can use `test_idp_settings.php` as the starting point; just copy it and rename it.\n\nConfiguration options are note explained in this project as they come from the [OneLogin project](https://github.com/onelogin/php-saml), please refer there for details.\n\nThe only real difference between this config and the one that OneLogin uses, is that the SP entityId, assertionConsumerService url and singleLogoutService URL are injected by the library. If you don't specify those URLs in the corresponding IDP config optional values, this library provides defaults values: the metadata, acs, and sls routes that this library creates for each IDP. If specify different values in the config, note that the acs and sls URLs should correspond to actual routes that you set up that are directed to the corresponding Saml2Controller function.\n\nIf you want to optionally define values in ENV vars instead of the \\*\\_idp_settings file, you'll see in there that there is a naming pattern you can follow for ENV values. For example, if in myipd1_idp_settings.php you set `$this_idp_env_id = 'MYIDP1';`, and in myidp2_idp_settings.php you set it to `'SECONDIDP'`, then you can set ENV vars starting with `SAML2_MYDP1_` and `SAML2_SECONDIDP_`, e.g.\n```env\nSAML2_MYIDP1_SP_x509=\"...\"\nSAML2_MYIDP1_SP_PRIVATEKEY=\"...\"\n// Other  SAML2_MYIDP1_* values\n\nSAML2_SECONDIDP_SP_x509=\"...\"\nSAML2_SECONDIDP_SP_PRIVATEKEY=\"...\"\n// Other SAML2_SECONDIDP_* values\n```\n\n#### URLs To Pass to The IDP configuration\nAs mentioned above, you don't need to implement the SP entityId, assertionConsumerService url and singleLogoutService routes, because Saml2Controller already does by default. But you need to know these routes, to provide them to the configuration of your actual IDP, i.e. the 3rd party you are asking to authenticate users.\n\nYou can check the actual routes in the metadata, by navigating to 'http(s)://laravel_url/myidp1/metadata', which incidentally will be the default entityId for this SP.\n\nIf you configure the optional `routesPrefix` setting in saml2_settings.php, then all idp routes will be prefixed by that value, so you'll need to adjust the metadata url accordingly. For example, if you configure routesPrefix to be `'single_sign_on'`, then your IDP metadata for myidp1 will be found at http://laravel_url/single_sign_on/myidp1/metadata.\n\n#### Example: simplesamlphp IDP configuration\nIf you use simplesamlphp as a test IDP, and your SP metadata url is `http://laravel_url/myidp1/metadata`, add the following to /metadata/sp-remote.php to inform the IDP of your laravel-saml2 SP identity:\n\n```php\n$metadata['http://laravel_url/myidp1/metadata'] = array(\n    'AssertionConsumerService' =\u003e 'http://laravel_url/myidp1/acs',\n    'SingleLogoutService' =\u003e 'http://laravel_url/myidp1/sls',\n    //the following two affect what the $Saml2user-\u003egetUserId() will return\n    'NameIDFormat' =\u003e 'urn:oasis:names:tc:SAML:2.0:nameid-format:persistent',\n    'simplesaml.nameidattribute' =\u003e 'uid' \n);\n```\n\n\n### Usage\n\nWhen you want your user to login, just redirect to the login route configured for the particular IDP, `route('saml2_login', 'myIdp1')`. You can also instantiate a `Saml2Auth` for the desired IDP using the `Saml2Auth::loadOneLoginAuthFromIpdConfig('myIdp1')` function to load the config and construct the OneLogin auth argment; just remember that it does not use any session storage, so if you ask it to login it will redirect to the IDP whether the user is already logged in or not. For example, you can change your authentication middleware.\n```php\npublic function handle($request, Closure $next)\n{\n    if ($this-\u003eauth-\u003eguest())\n    {\n        if ($request-\u003eajax())\n        {\n            return response('Unauthorized.', 401); // Or, return a response that causes client side js to redirect to '/routesPrefix/myIdp1/login'\n        }\n        else\n        {\n            $saml2Auth = new Saml2Auth(Saml2Auth::loadOneLoginAuthFromIpdConfig('myIdp1'));\n            return $saml2Auth-\u003elogin(URL::full());\n        }\n    }\n\n    return $next($request);\n}\n```\n\nSince Laravel 5.3, you can change your unauthenticated method in ```app/Exceptions/Handler.php```.\n```php\nprotected function unauthenticated($request, AuthenticationException $exception)\n{\n    if ($request-\u003eexpectsJson())\n    {\n        return response()-\u003ejson(['error' =\u003e 'Unauthenticated.'], 401); // Or, return a response that causes client side js to redirect to '/routesPrefix/myIdp1/login'\n    }\n\n    $saml2Auth = new Saml2Auth(Saml2Auth::loadOneLoginAuthFromIpdConfig('myIdp1'));\n    return $saml2Auth-\u003elogin('/my/redirect/path');\n}\n```\n\nFor login requests that come through redirects to the login route, 'routesPrefix/myidp1/login', the default login call does not pass a redirect URL to the Saml login request. That login argument is useful because the ACS handler can gets that value (passed back from the IDP as RelayPath) and by default will redirect there. To pass the redirect URL from the controller login, extend the Saml2Controller class and implement your own `login()` function. Set the saml2_settings value `saml2_controller` to be your extended class so that the routes will direct requests to your controller instead of the default.  \nE.g.  \n**saml_settings.php**\n```\n    'saml2_controller' =\u003e 'App\\Http\\Controllers\\MyNamespace\\MySaml2Controller'\n```\n**MySaml2Controller.php**\n```php\nuse Iwouldrathercode\\Saml2\\Http\\Controllers\\Saml2Controller;\n\nclass MySaml2Controller extends Saml2Controller\n{\n    public function login()\n    {\n        $loginRedirect = '...'; // Determine redirect URL\n        $this-\u003esaml2Auth-\u003elogin($loginRedirect);\n    }\n}\n```\n\nAfter login is called, the user will be redirected to the IDP login page. Then the IDP, which you have configured with an endpoint the library serves, will call back, e.g. `/myidp1/acs` or `/routesPrefix/myidp1/acs`. That will process the response and fire an event when ready. The next step for you is to handle that event. You just need to login the user or refuse.\n\n```php\n\n Event::listen('Iwouldrathercode\\Saml2\\Events\\Saml2LoginEvent', function (Saml2LoginEvent $event) {\n            $messageId = $event-\u003egetSaml2Auth()-\u003egetLastMessageId();\n            // Add your own code preventing reuse of a $messageId to stop replay attacks\n\n            $user = $event-\u003egetSaml2User();\n            $userData = [\n                'id' =\u003e $user-\u003egetUserId(),\n                'attributes' =\u003e $user-\u003egetAttributes(),\n                'assertion' =\u003e $user-\u003egetRawSamlAssertion()\n            ];\n             $laravelUser = //find user by ID or attribute\n             //if it does not exist create it and go on  or show an error message\n             Auth::login($laravelUser);\n        });\n\n```\n### Auth persistence\n\nBe careful about necessary Laravel middleware for Auth persistence in Session.\n\nFor exemple, it can be:\n\n```\n# in App\\Http\\Kernel\nprotected $middlewareGroups = [\n        'web' =\u003e [\n\t    ...\n\t],\n\t'api' =\u003e [\n            ...\n        ],\n        'saml' =\u003e [\n            \\App\\Http\\Middleware\\EncryptCookies::class,\n            \\Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse::class,\n            \\Illuminate\\Session\\Middleware\\StartSession::class,\n        ],\n\n```\n\nAnd in `config/saml2_settings.php` :\n```\n    /**\n     * which middleware group to use for the saml routes\n     * Laravel 5.2 will need a group which includes StartSession\n     */\n    'routesMiddleware' =\u003e ['saml'],\n```\n\n### Log out\nNow there are two ways the user can log out.\n + 1 - By logging out in your app: In this case you 'should' notify the IDP first so it closes global session.\n + 2 - By logging out of the global SSO Session. In this case the IDP will notify you on /myidp1/slo endpoint (already provided), if the IDP supports SLO\n\nFor case 1, initiate a logout by redirecting the user to the saml2_logout route (`route('saml2_logout', 'myidp1')`). Do not close the session immediately as you need to receive a response confirmation from the IDP (redirection). That response will be handled by the library at the `sls` route, and it will fire a `Saml2LogoutEvent` event that you can use to complete the logout in the same way as with case 2 below.\n\nFor case 2 you will only receive the event. Both cases 1 and 2 receive the same `Saml2LogoutEvent` event. \n\nNote that for case 2, 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)\n\n```php\n        Event::listen('Iwouldrathercode\\Saml2\\Events\\Saml2LogoutEvent', function ($event) {\n            Auth::logout();\n            Session::save();\n        });\n```\n\n\nThat's it. Feel free to ask any questions, make PR or suggestions, or open Issues.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiwouldrathercode%2Fphp-custom-saml","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiwouldrathercode%2Fphp-custom-saml","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiwouldrathercode%2Fphp-custom-saml/lists"}