Abstract Class Yiisoft\Yii\AuthClient\AuthClient
AuthClient is a base Auth Client class.
Protected Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $httpClient | \ |
Yiisoft\ |
|
| $name | string | Custom name, set from the config array key. | Yiisoft\ |
| $normalizeUserAttributeMap | array | Map used to normalize user attributes fetched from external auth service in format: normalizedAttributeName => sourceSpecification 'sourceSpecification' can be: - string, raw attribute name - array, pass to raw attribute value - callable, PHP callback, which should accept array of raw attributes and return normalized value. | Yiisoft\ |
| $requestFactory | \ |
Yiisoft\ |
|
| $title | string | Custom title, overrides the class default if set. | Yiisoft\ |
| $viewOptions | array | View options in format: optionName => optionValue | Yiisoft\ |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
|
| buildAuthUrl() | Yiisoft\ |
|
| createRequest() | Yiisoft\ |
|
| getClientId() | The Client id is publically visible in button urls The Client secret must not be made available publically => exclude from interface | Yiisoft\ |
| getName() | Yiisoft\ |
|
| getNormalizeUserAttributeMap() | Yiisoft\ |
|
| getRequestFactory() | Yiisoft\ |
|
| getTitle() | Yiisoft\ |
|
| getUserAttributes() | Returns the authenticated user's attributes, as fetched by {@see initUserAttributes()} and normalized according to {@see normalizeUserAttributeMap}. | Yiisoft\ |
| getViewOptions() | Yiisoft\ |
|
| setName() | Yiisoft\ |
|
| setRequestFactory() | Yiisoft\ |
|
| setTitle() | Yiisoft\ |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| defaultNormalizeUserAttributeMap() | Returns the default {@see normalizeUserAttributeMap} value. | Yiisoft\ |
| defaultViewOptions() | Returns the default {@see viewOptions} value. | Yiisoft\ |
| getState() | Returns persistent state value. | Yiisoft\ |
| getStateKeyPrefix() | Returns session key prefix, which is used to store internal states. | Yiisoft\ |
| initUserAttributes() | Fetches the authenticated user's raw attribute data from the external auth provider. | Yiisoft\ |
| removeState() | Removes persistent state value. | Yiisoft\ |
| sendRequest() | Yiisoft\ |
|
| setState() | Sets persistent state. | Yiisoft\ |
Property Details
Custom name, set from the config array key. Empty means use the class default.
Map used to normalize user attributes fetched from external auth service in format: normalizedAttributeName => sourceSpecification 'sourceSpecification' can be:
- string, raw attribute name
- array, pass to raw attribute value
- callable, PHP callback, which should accept array of raw attributes and return normalized value.
For example:
'normalizeUserAttributeMap' => [
'about' => 'bio',
'language' => ['languages', 0, 'name'],
'fullName' => function ($attributes) {
return $attributes['firstName'] . ' ' . $attributes['lastName'];
},
],
Custom title, overrides the class default if set.
View options in format: optionName => optionValue
Method Details
| public mixed __construct ( \ | ||
| $httpClient | \ |
|
| $requestFactory | \ |
|
| $stateStorage | Yiisoft\ |
|
public function __construct(
protected PsrClientInterface $httpClient,
protected RequestFactoryInterface $requestFactory,
/**
* @var StateStorageInterface state storage to be used.
*/
private readonly StateStorageInterface $stateStorage,
) {}
| public abstract string buildAuthUrl ( \ | ||
| $incomingRequest | \ |
|
| $params | array | |
abstract public function buildAuthUrl(ServerRequestInterface $incomingRequest, array $params): string;
| public \ | ||
| $method | string | |
| $uri | string | |
public function createRequest(string $method, string $uri): RequestInterface
{
return $this->requestFactory->createRequest($method, $uri);
}
Returns the default {@see normalizeUserAttributeMap} value.
Particular client may override this method in order to provide specific default map.
| protected array defaultNormalizeUserAttributeMap ( ) | ||
| return | array |
Normalize attribute map. |
|---|---|---|
protected function defaultNormalizeUserAttributeMap(): array
{
return [];
}
Returns the default {@see viewOptions} value.
Particular client may override this method in order to provide specific default view options.
| protected array defaultViewOptions ( ) | ||
| return | array |
List of default {@see \ |
|---|---|---|
protected function defaultViewOptions(): array
{
return [
'popupWidth' => 860,
'popupHeight' => 480,
];
}
Defined in:
Yiisoft\
The Client id is publically visible in button urls The Client secret must not be made available publically => exclude from interface
| public abstract string getClientId ( ) |
public function getClientId(): string;
| public array getNormalizeUserAttributeMap ( ) | ||
| return | array |
Normalize user attribute map. |
|---|---|---|
public function getNormalizeUserAttributeMap(): array
{
if (empty($this->normalizeUserAttributeMap)) {
$this->normalizeUserAttributeMap = $this->defaultNormalizeUserAttributeMap();
}
return $this->normalizeUserAttributeMap;
}
| public \ |
public function getRequestFactory(): RequestFactoryInterface
{
return $this->requestFactory;
}
Returns persistent state value.
| protected mixed getState ( string $key ) | ||
| $key | string |
State key. |
| return | mixed |
State value. |
|---|---|---|
protected function getState(string $key): mixed
{
return $this->stateStorage->get($this->getStateKeyPrefix() . $key);
}
Returns session key prefix, which is used to store internal states.
| protected string getStateKeyPrefix ( ) | ||
| return | string |
Session key prefix. |
|---|---|---|
protected function getStateKeyPrefix(): string
{
return static::class . '_' . $this->getName() . '_';
}
| public abstract string getTitle ( ) | ||
| return | string |
Service title. |
|---|---|---|
public function getTitle(): string;
Returns the authenticated user's attributes, as fetched by {@see initUserAttributes()} and normalized according to {@see normalizeUserAttributeMap}.
| public array getUserAttributes ( ) | ||
| return | array |
User attributes. |
|---|---|---|
public function getUserAttributes(): array
{
$attributes = $this->initUserAttributes();
$normalizeMap = $this->getNormalizeUserAttributeMap();
return array_merge($attributes, $this->normalizeUserAttributes($attributes, $normalizeMap));
}
| public array getViewOptions ( ) | ||
| return | array |
View options in format: optionName => optionValue |
|---|---|---|
public function getViewOptions(): array
{
if (empty($this->viewOptions)) {
$this->viewOptions = $this->defaultViewOptions();
}
return $this->viewOptions;
}
Fetches the authenticated user's raw attribute data from the external auth provider.
Particular client should override this method in order to provide actual attribute fetching.
| protected array initUserAttributes ( ) | ||
| return | array |
Raw user attributes. |
|---|---|---|
protected function initUserAttributes(): array
{
return [];
}
Removes persistent state value.
| protected void removeState ( string $key ) | ||
| $key | string |
State key. |
protected function removeState(string $key): void
{
$this->stateStorage->remove($this->getStateKeyPrefix() . $key);
}
| protected \ | ||
| $request | \ |
|
protected function sendRequest(RequestInterface $request): ResponseInterface
{
return $this->httpClient->sendRequest($request);
}
| public void setName ( string $name ) | ||
| $name | string | |
public function setName(string $name): void
{
$this->name = $name;
}
| public void setRequestFactory ( \ | ||
| $requestFactory | \ |
|
public function setRequestFactory(RequestFactoryInterface $requestFactory): void
{
$this->requestFactory = $requestFactory;
}
Sets persistent state.
| protected $this setState ( string $key, mixed $value ) | ||
| $key | string |
State key. |
| $value | mixed |
State value |
| return | $this |
The object itself |
|---|---|---|
protected function setState(string $key, $value): self
{
$this->stateStorage->set($this->getStateKeyPrefix() . $key, $value);
return $this;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.