From 979db126cae04c8b62ccd162b294bb33c886e797 Mon Sep 17 00:00:00 2001
From: Greg Bowler
You are logged in as $email.
"; } else { - echo <<You are not logged in! - - HTML; + echo ''; } ``` +Load the autoloader before starting the session, and construct `Authenticator` before sending output. Its constructor processes returning authentication responses and can redirect immediately. Run the same setup on every route that can receive a login response. + +`getAuthUser()` provides the read-only `id` and `email` properties, `getData($name)` for an optional custom field, and `getKvp()` for all supplied custom fields. Use the user ID to associate local records with an account, and keep permission checks in the application. The provider's login response supplies ID and email; custom fields may be absent. + +The `onAuthenticated` callback runs when a new authentication completes, before the client removes the response parameter with a redirect. It does not run when a user is restored from the session. The callback is marked before invocation and is not retried automatically if it throws. + +Login destinations and self-hosting +---------------------------------- + +Use `setReturnUri()` before login to choose an application path, query string and fragment: + +```php +$auth->setReturnUri("/members/orders?status=open#latest"); +$auth->login(); +``` + +Without an override, login requests the current URI. The provider accepts root-relative paths and absolute URLs matching the deployment's configured client scheme, host and effective port. Invalid or external destinations fall back to the configured client return URI. + +The `authwaveHost` constructor argument selects the provider and defaults to `login.authwave.com`. For a self-hosted provider, supply its scheme, host and optional port, such as `https://login.example.com`. + Logging out ----------- -Call `logout()` before sending output to clear the application's authentication state and send the browser to the provider, which clears its own session. An explicit destination takes priority over a destination configured with `setReturnUri()`: +After the application's normal POST and CSRF checks, call `logout()` before sending output: ```php $auth->logout("/signed-out"); ``` -With no explicit destination, `logout()` uses a configured return URI. If no return URI has been configured, the provider uses the deployment's default return location. The provider resolves relative paths against the deployment's client URI. Keep destinations under application control. +The client clears its stored and in-memory authentication state, preserving unrelated session data, then redirects to the provider to clear its session. An explicit destination takes priority over `setReturnUri()`. Without either setting, the provider uses the deployment's default return location. Relative logout paths are resolved against the deployment's client URI. Keep destinations under application control. + +The default redirect handler ends the request. Custom handlers must transfer control appropriately; the authenticator is already logged out when they run. The default development fake user is logged out locally without visiting the provider. -The default redirect handler ends the request. Custom handlers must transfer control appropriately; the authenticator is already logged out when they run. Other application session data is preserved. The default development fake user is logged out locally without visiting the provider. +Documentation +------------- -Running unit tests ------------------- +Follow the [quick start guide](https://github.com/Authwave/php-client/wiki/Quick-start-guide) for a complete setup, or browse the [wiki](https://github.com/Authwave/php-client/wiki) for session integration, callbacks, destination rules and the API reference. -From the root directory, unit tests can be ran with the following command: +Running checks +-------------- + +From the repository root, install development dependencies and run the configured checks: ```bash -vendor/bin/phpunit test/phpunit --coverage-clover test/phpunit/_coverage --whitelist src +composer install +composer test ``` + +The test script runs PHPUnit, PHPStan, PHP_CodeSniffer and PHPMD. To run the unit tests alone, use `composer phpunit`.