Abstract Class Yiisoft\Yii\AuthClient\OAuth
BaseOAuth is a base class for the OAuth clients.
Protected Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $accessToken | array|Yiisoft\ |
Access token instance or its array configuration. | Yiisoft\ |
| $authUrl | string | Authorize URL. | Yiisoft\ |
| $autoRefreshAccessToken | boolean | Whether to automatically perform 'refresh access token' request on expired access token. | Yiisoft\ |
| $endpoint | string | API base URL. | Yiisoft\ |
| $factory | \ |
Yiisoft\ |
|
| $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\ |
|
| $returnUrl | string | URL, which user will be redirected after authentication at the OAuth provider web site. | Yiisoft\ |
| $scope | string | String auth request scope. | Yiisoft\ |
| $title | string | Custom title, overrides the class default if set. | Yiisoft\ |
| $viewOptions | array | View options in format: optionName => optionValue | Yiisoft\ |
Public Methods
Protected Methods
Property Details
Access token instance or its array configuration.
Whether to automatically perform 'refresh access token' request on expired access token.
API base URL.
This field will be used as {@see \
URL, which user will be redirected after authentication at the OAuth provider web site. Note: this should be absolute URL (with http:// or https:// leading). By default current URL will be used.
Method Details
BaseOAuth constructor.
| public mixed __construct ( \ | ||
| $httpClient | \ |
|
| $requestFactory | \ |
|
| $stateStorage | Yiisoft\ |
|
| $factory | \ |
|
public function __construct(
ClientInterface $httpClient,
RequestFactoryInterface $requestFactory,
StateStorageInterface $stateStorage,
protected YiisoftFactory $factory,
) {
parent::__construct($httpClient, $requestFactory, $stateStorage);
}
Performs request to the OAuth API returning response data.
You may use {@see \
See also createApiRequest().
| public array api ( string $apiSubUrl, string $method = 'GET', array|string $data = [], array $headers = [] ) | ||
| $apiSubUrl | string |
API sub URL, which will be append to {@see \ |
| $method | string |
Request method. |
| $data | array|string |
Request data or content. |
| $headers | array |
Additional request headers. |
| return | array |
API response data. |
|---|---|---|
| throws | Exception | |
public function api($apiSubUrl, $method = 'GET', $data = [], $headers = []): array
{
$request = $this->createApiRequest($method, $apiSubUrl);
$request = RequestUtil::addHeaders($request, $headers);
if (!empty($data)) {
if (is_array($data)) {
$request = RequestUtil::addParams($request, $data);
} else {
$request->getBody()->write($data);
}
}
$request = $this->beforeApiRequestSend($request);
$response = $this->sendRequest($request);
if ($response->getStatusCode() !== 200) {
throw new InvalidResponseException(
$response,
'Request failed with code: ' . $response->getStatusCode() . ', message: ' . $response->getBody(),
);
}
return (array) Json::decode($response->getBody()->getContents());
}
Applies access token to the HTTP request instance.
| public abstract \ | ||
| $request | \ |
HTTP request instance. |
| $accessToken | Yiisoft\ |
Access token instance. |
abstract public function applyAccessTokenToRequest(
RequestInterface $request,
OAuthToken $accessToken,
): RequestInterface;
| public \ | ||
| $request | \ |
|
public function beforeApiRequestSend(RequestInterface $request): RequestInterface
{
$accessToken = $this->getAccessToken();
if (!is_object($accessToken) || !$accessToken->getIsValid()) {
throw new Exception('Invalid access token.');
}
return $this->applyAccessTokenToRequest($request, $accessToken);
}
| public abstract string buildAuthUrl ( \ | ||
| $incomingRequest | \ |
|
| $params | array | |
abstract public function buildAuthUrl(ServerRequestInterface $incomingRequest, array $params): string;
Creates an HTTP request for the API call.
The created request will be automatically processed adding access token parameters and signature
before sending. You may use {@see \
See also createRequest().
| public \ | ||
| $method | string | |
| $uri | string | |
| return | \ |
HTTP request instance. |
|---|---|---|
public function createApiRequest(string $method, string $uri): RequestInterface
{
return $this->createRequest($method, $this->endpoint . $uri);
}
| public \ | ||
| $method | string | |
| $uri | string | |
public function createRequest(string $method, string $uri): RequestInterface
{
return $this->requestFactory->createRequest($method, $uri);
}
Creates token from its configuration.
See also \
| protected Yiisoft\ | ||
| $tokenConfig | array |
Token configuration. |
| throws | \ |
|
|---|---|---|
protected function createToken(array $tokenConfig): OAuthToken
{
if (!array_key_exists('class', $tokenConfig)) {
$tokenConfig['class'] = OAuthToken::class;
}
/** @var OAuthToken $token */
$token = $this->factory->create($tokenConfig['class']);
if (isset($tokenConfig['params']) && is_array($tokenConfig['params'])) {
$token->setParams($tokenConfig['params']);
}
if (isset($tokenConfig['tokenParamKey']) && is_string($tokenConfig['tokenParamKey'])) {
$token->setTokenParamKey($tokenConfig['tokenParamKey']);
}
return $token;
}
Defined in:
Yiisoft\
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 [];
}
Composes default {@see returnUrl} value.
| protected string defaultReturnUrl ( \ | ||
| $request | \ |
|
| return | string |
Return URL. |
|---|---|---|
protected function defaultReturnUrl(ServerRequestInterface $request): string
{
return (string) $request->getUri();
}
Defined in:
Yiisoft\
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,
];
}
| public Yiisoft\ | ||
| return | Yiisoft\ |
Auth token instance. |
|---|---|---|
public function getAccessToken(): ?OAuthToken
{
if (!is_object($this->accessToken)) {
$this->accessToken = $this->restoreAccessToken();
}
return $this->accessToken;
}
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;
}
| public string getReturnUrl ( \ | ||
| $request | \ |
|
| return | string |
Return URL. |
|---|---|---|
public function getReturnUrl(ServerRequestInterface $request): string
{
if ($this->returnUrl === '') {
$this->returnUrl = $this->defaultReturnUrl($request);
}
return $this->returnUrl;
}
| public string getScope ( ) |
public function getScope(): string
{
if ($this->scope === null) {
return $this->getDefaultScope();
}
return $this->scope;
}
Defined in:
Yiisoft\
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);
}
Defined in:
Yiisoft\
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;
Defined in:
Yiisoft\
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;
}
| public \ |
public function getYiisoftFactory(): YiisoftFactory
{
return $this->factory;
}
Defined in:
Yiisoft\
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 [];
}
Gets new auth token to replace expired one.
| public abstract Yiisoft\ | ||
| $token | Yiisoft\ |
Expired auth token. |
| return | Yiisoft\ |
New auth token. |
|---|---|---|
abstract public function refreshAccessToken(OAuthToken $token): OAuthToken;
Defined in:
Yiisoft\
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);
}
Restores access token.
| protected Yiisoft\ |
protected function restoreAccessToken(): ?OAuthToken
{
if (($token = $this->getState('token')) instanceof OAuthToken) {
if ($token->getIsExpired() && $this->autoRefreshAccessToken) {
return $this->refreshAccessToken($token);
}
return $token;
}
return null;
}
Saves token as persistent state.
| protected $this saveAccessToken ( Yiisoft\ | ||
| $token | Yiisoft\ |
Auth token to be saved. |
| return | $this |
The object itself. |
|---|---|---|
protected function saveAccessToken(?OAuthToken $token = null): self
{
return $this->setState('token', $token);
}
Defined in:
Yiisoft\
| protected \ | ||
| $request | \ |
|
protected function sendRequest(RequestInterface $request): ResponseInterface
{
return $this->httpClient->sendRequest($request);
}
Sets access token to be used.
| public void setAccessToken ( array|Yiisoft\ | ||
| $token | array|Yiisoft\ |
Access token or its configuration. |
public function setAccessToken(array|OAuthToken $token): void
{
if (is_array($token) && !empty($token)) {
$newToken = $this->createToken($token);
$this->accessToken = $newToken;
$this->saveAccessToken($newToken);
}
if ($token instanceof OAuthToken) {
$this->accessToken = $token;
$this->saveAccessToken($token);
}
}
| public void setAuthUrl ( string $authUrl ) | ||
| $authUrl | string | |
public function setAuthUrl(string $authUrl): void
{
$this->authUrl = $authUrl;
}
Defined in:
Yiisoft\
| 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;
}
| public void setReturnUrl ( string $returnUrl ) | ||
| $returnUrl | string |
Return URL |
public function setReturnUrl(string $returnUrl): void
{
$this->returnUrl = $returnUrl;
}
| public void setScope ( string $scope ) | ||
| $scope | string |
Auth request scope, overriding {@see \ |
public function setScope(string $scope): void
{
$this->scope = $scope;
}
Defined in:
Yiisoft\
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;
}
Defined in:
Yiisoft\
| public void setTitle ( string $title ) | ||
| $title | string | |
public function setTitle(string $title): void
{
$this->title = $title;
}
| public void setYiisoftFactory ( \ | ||
| $factory | \ |
|
public function setYiisoftFactory(YiisoftFactory $factory): void
{
$this->factory = $factory;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.