Download the PHP package germania-kg/authapi-client without Composer
On this page you can find all versions of the php package germania-kg/authapi-client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download germania-kg/authapi-client
More information about germania-kg/authapi-client
Files in germania-kg/authapi-client
Package authapi-client
Short Description HTTP-Client for Germania's Authentication API
License
Informations about the package authapi-client
AuthApi Client
Service-side PHP client for retrieving an Auth token from Germania's Authentication API (AuthApi).
Installation
Interfaces and abstracts
AuthApiInterface
The AuthApiInterface provides three public methods:
- getToken which requires username and password and returns an AuthToken instance, optionally with long TTL (“refresh”)
- login which requires username and password and returns an AuthToken instance, usually with short TTL.
- refresh which accepts an AuthToken instance and returns a new AuthToken instance
AuthApiAbstract
Abstract class AuthApiAbstract implements AuthApiInterface, so you will have to bring the abstract methods to life. They use Psr\Log\LoggerAwareTrait
and the LoglevelTrait
that comes with this package. So with all instances extending from AuthApiAbstract, you can:
Implementations
Using Guzzle
The GuzzleAuthApi client extends AuthApiAbstract and implements AuthApiInterface. It uses the GuzzleHttp Client to authenticate at Germania's AuthApi. The Guzzle client needs to be configured with the AuthAPI's base_uri
.
Set Guzzle Client
Using PSR-18 HTTP client
Class HttpClientAuthApi extends abstract AuthApiDecorator and thus also implements AuthApiInterface.
The constructor requires the AuthApi base URL and a PSR-18 client instance. An optional PSR-3 Logger may be passed as third parameter.
Set base URL
Set PSR-18 HTTP client
Set PSR-17 Request and Stream factory
PSR-6 Cache support
Class CacheAuthApiDecorator is a PSR-6 Cache decorator for any AuthApiInterface instance. It extends abstract AuthApiDecorator and thus also implements AuthApiInterface.
The constructor requires AuthApiInterface instance and a PSR-6 CacheItemPool. An optional PSR-3 Logger may be passed as third parameter.
Set PSR-6 CacheItemPool
Usage Example
This usage example combines the above AuthApi clients.
The AuthToken's life time is usually quite short; to obtain one with longer TTL (i.e. refresh after login), pass a third boolean parameter to the getToken method. The new TTL is derived from the token lifetime.
Exception handling
Any exception thrown by the AuthApi client implements AuthApiExceptionInterface. So catching \Germania\AuthApiClient\Exceptions\AuthApiExceptionInterface
usually will be plenty.
Any request-related exception thrown by the Guzzle client will be wrapped up in a AuthApiRequestException. This exception class extends \RuntimeException and implements AuthApiExceptionInterface.
When there's something wrong with the retrieved AuthApi response, an AuthApiResponseException will be thrown. This exception class extends \UnexpectedValueException and implements AuthApiExceptionInterface.
The AuthToken
The AuthToken and AuthTokenInterface rely on Germania's germania-kg/token package: https://packagist.org/packages/germania-kg/token
Interface AuthTokenInterface
The Germania\AuthApiClient\AuthTokenInterface
extends Germania\Token\TokenInterface
and thus provides these methods inherited from AuthTokenInterface:
Class AuthToken
The Germania\AuthApiClient\AuthToken
implements the AuthTokenInterface
. It is an extension from Germania\Token\Token
.
Create authorized requests: PSR-17 Requestfactory
Once you have your AuthToken, you'll want to authorize at an API. In order to create an authorized PSR-7 Request, you can use class AuthTokenRequestFactory which wraps another “inner” PSR-17 RequestFactory.
The constructor requires an AuthToken instance or equivalent string; it optionally accepts any PSR-17 instance; per default Nyholm's PSR-17 RequestFactory will be used.
Testing
Copy phpunit.xml.dist
to phpunit.xml
and fill in your credentials in the <php>
section.
All versions of authapi-client with dependencies
guzzlehttp/guzzle Version ^6.3|^7.0
psr/log Version ^1.1
psr/cache Version ^1.0|^2.0|^3.0
symfony/polyfill-php73 Version ^1.11
germania-kg/jsondecoder Version ^1.0
germania-kg/token Version ^1.0
nyholm/psr7 Version ^1.3