# socialiteproviders/saml2 architecture

> Architecture diagram of the GitHub repository socialiteproviders/saml2, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/socialiteproviders/saml2
- Repository: https://github.com/socialiteproviders/saml2

## Overview

1. This repository is a small Laravel Socialite provider package that adds SAML2 as a Socialite driver. It is not an app; it is an adapter layer between Laravel Socialite and a SAML2 implementation.

2. The central runtime boundary is the `Provider` class. It is the driver implementation that Socialite calls for the SAML2 flow: redirect to the IdP, consume the callback, build the authenticated user object, support logout responses, and expose SP metadata. Most of the repo’s behavior likely lives here.

3. `Saml2ExtendSocialite.php` is the Laravel-Socialite integration hook for older Laravel setups. It handles the `SocialiteWasCalled` event and registers the `saml2` driver with the Socialite manager. In Laravel 11+, the README shows the same extension is done manually in an app service provider.

4. `User.php` is the domain object returned after successful SAML login. It carries the canonical Socialite user fields plus SAML-specific data:
   - `id` from SAML NameID
   - mapped profile fields such as name/email/first/last/upn
   - raw SAML attributes
   - the full assertion payload

5. `InvalidSignatureException.php` and `NotSupportedException.php` define package-specific failure boundaries. They separate SAML validation and protocol capability errors from normal auth flow, so consumers can distinguish bad assertions from unsupported/invalid configuration or message types.

6. `OasisAttributeNameUris.php` is a constants/namespace helper for well-known SAML attribute URIs. It exists to make attribute mapping configurable and readable, and it supports the package’s attribute normalization layer rather than business logic.

7. The main data flow is configuration-driven. The application defines a `saml2` section in `config/services.php` with IdP metadata or manual certificate settings, SP ACS/entity IDs, bindings, signing/encryption material, validation settings, and attribute maps. The provider consumes this config to shape SAML requests, responses, and generated metadata.

8. The authentication flow is:
   - application calls `Socialite::driver('saml2')->redirect()`
   - provider builds a SAML auth request and sends the browser to the IdP
   - IdP returns either a GET redirect or POST callback to the ACS route
   - provider validates timestamps/signatures/replay and extracts attributes
   - a `User` object is returned to the app

9. The package explicitly supports two SAML callback bindings: HTTP-Redirect via GET and HTTP-POST via POST. This affects the application boundary, because the callback route must be declared with the chosen HTTP method and POST callbacks may require CSRF exceptions or a route outside `web.php`.

10. Metadata handling is a second major subsystem. The provider can ingest IdP metadata from a URL or XML string/file, cache it, select a descriptor by `entityid`, and optionally expose SP metadata at a route. This lets the package act both as SAML service consumer and as metadata publisher for IdP configuration.

11. Validation and security are built into the provider layer rather than pushed to the app. The README describes signature checking, assertion timestamp validation, replay protection via repeated assertion IDs, optional SP signing/encryption certificates, and configurable clock skew and cache TTLs. These are important architecture concerns because they govern trust decisions at the auth boundary.

12. Overall, the repository is best drawn as a thin Laravel package with one integration entry point, one main SAML driver, a user model, constants, and custom exceptions. External dependencies and protocols that materially shape the architecture are Laravel Socialite/SocialiteProviders, SAML2/LightSAML concepts, and optional HTTP metadata retrieval/caching.

Main entry points: [`Provider.php`](https://github.com/socialiteproviders/saml2/blob/master/Provider.php) (Provider), [`Saml2ExtendSocialite.php`](https://github.com/socialiteproviders/saml2/blob/master/Saml2ExtendSocialite.php) (Socialite hook), [`User.php`](https://github.com/socialiteproviders/saml2/blob/master/User.php) (User), [`InvalidSignatureException.php`](https://github.com/socialiteproviders/saml2/blob/master/InvalidSignatureException.php) (Bad signature)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_pkg["Package"]
  node_saml2_extend_socialite["Socialite hook<br/>laravel integration"]
  node_provider["Provider<br/>socialite driver<br/>[Provider.php]"]
  node_user["User<br/>auth payload<br/>[User.php]"]
  node_invalid_signature_exception(("Bad signature<br/>auth exception"))
  node_not_supported_exception(("Unsupported flow<br/>protocol exception"))
  node_oasis_attribute_name_uris["Attribute URIs<br/>constants"]
end

subgraph group_ext["External"]
  node_socialite{{"Socialite<br/>external auth"}}
  node_saml_idp{{"IdP / ACS<br/>saml protocol"}}
  node_metadata_store["Metadata source<br/>external metadata"]
end

node_socialite -->|"event hook"| node_saml2_extend_socialite
node_socialite -->|"driver calls"| node_provider
node_saml2_extend_socialite -->|"registers"| node_provider
node_provider -->|"auth flow"| node_saml_idp
node_provider -->|"loads metadata"| node_metadata_store
node_provider -->|"returns"| node_user
node_provider -->|"throws"| node_invalid_signature_exception
node_provider -->|"throws"| node_not_supported_exception
node_provider -->|"maps claims"| node_oasis_attribute_name_uris

click node_saml2_extend_socialite "https://github.com/socialiteproviders/saml2/blob/master/Saml2ExtendSocialite.php"
click node_provider "https://github.com/socialiteproviders/saml2/blob/master/Provider.php"
click node_user "https://github.com/socialiteproviders/saml2/blob/master/User.php"
click node_invalid_signature_exception "https://github.com/socialiteproviders/saml2/blob/master/InvalidSignatureException.php"
click node_not_supported_exception "https://github.com/socialiteproviders/saml2/blob/master/NotSupportedException.php"
click node_oasis_attribute_name_uris "https://github.com/socialiteproviders/saml2/blob/master/OasisAttributeNameUris.php"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_saml2_extend_socialite,node_provider,node_user,node_invalid_signature_exception,node_not_supported_exception,node_oasis_attribute_name_uris toneBlue
class node_socialite,node_saml_idp,node_metadata_store toneAmber
```

## Components

### Package

Laravel Socialite SAML2 adapter components

- [Socialite hook](https://github.com/socialiteproviders/saml2/blob/master/Saml2ExtendSocialite.php): laravel integration · Registers the saml2 driver on older Laravel/Socialite setups. · `Saml2ExtendSocialite.php`
- [Provider](https://github.com/socialiteproviders/saml2/blob/master/Provider.php): socialite driver · Main SAML2 driver: redirects, handles callbacks, validates responses, builds users, exposes metadata, and supports logout. · `Provider.php`
- [User](https://github.com/socialiteproviders/saml2/blob/master/User.php): auth payload · Authenticated Socialite user enriched with SAML identifiers, mapped profile fields, raw attributes, and assertion data. · `User.php`
- [Bad signature](https://github.com/socialiteproviders/saml2/blob/master/InvalidSignatureException.php): auth exception · Raised when SAML assertions or signatures fail validation. · `InvalidSignatureException.php`
- [Unsupported flow](https://github.com/socialiteproviders/saml2/blob/master/NotSupportedException.php): protocol exception · Raised for unsupported SAML message types or invalid capability/configuration states. · `NotSupportedException.php`
- [Attribute URIs](https://github.com/socialiteproviders/saml2/blob/master/OasisAttributeNameUris.php): constants · Named SAML attribute URI constants used for readable, configurable claim mapping. · `OasisAttributeNameUris.php`

### External

Runtime and protocol boundaries outside this repository

- **Socialite**: external auth · Laravel Socialite manager and driver contract boundary.
- **IdP / ACS**: saml protocol · Identity provider, assertion consumer callbacks, and SAML login/logout exchanges.
- **Metadata source**: external metadata · IdP metadata fetched from URL, XML, or file and used to configure trust and endpoints.

## Connections

- Socialite → Socialite hook: event hook · Older Laravel setups register the driver through the SocialiteWasCalled event.
- Socialite → Provider: driver calls · Socialite invokes the provider for redirect, callback handling, and user retrieval.
- Socialite hook → Provider: registers · The extension hook binds the saml2 driver to the provider implementation.
- Provider → IdP / ACS: auth flow · Creates auth requests, consumes callbacks, and handles logout/metadata exchanges.
- Provider → Metadata source: loads metadata · Provider can read and cache IdP metadata from external sources.
- Provider → User: returns · Successful assertions are converted into a Socialite user object.
- Provider → Bad signature: throws · Signature or assertion validation failures are surfaced as package exceptions.
- Provider → Unsupported flow: throws · Unsupported protocol messages or invalid setup are surfaced separately.
- Provider → Attribute URIs: maps claims · Attribute URI constants support readable and configurable SAML claim mapping.
