mirror of
https://github.com/1sept/oauth2-1sept.git
synced 2024-05-30 17:38:52 +03:00
Init
First public commit
This commit is contained in:
66
src/Provider/SeptemberFirstProvider.php
Executable file
66
src/Provider/SeptemberFirstProvider.php
Executable file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Sept\OAuth2\Client\Provider;
|
||||
|
||||
use League\OAuth2\Client\Provider\AbstractProvider;
|
||||
use League\OAuth2\Client\Token\AccessToken;
|
||||
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
|
||||
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
class SeptemberFirstProvider extends AbstractProvider
|
||||
{
|
||||
use BearerAuthorizationTrait;
|
||||
|
||||
/**
|
||||
* Личный кабинет Первое сентября
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const AUTH_BASE = 'https://my.1sept.ru';
|
||||
|
||||
/**
|
||||
* API Первое сентября
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const API_BASE = 'https://api.1sept.ru';
|
||||
const API_VERSION = '2.0';
|
||||
|
||||
public function getBaseAuthorizationUrl(): string
|
||||
{
|
||||
return static::AUTH_BASE.'/oauth/authorize';
|
||||
}
|
||||
|
||||
public function getBaseAccessTokenUrl(array $params): string
|
||||
{
|
||||
return static::API_BASE.'/oauth/access_token';
|
||||
}
|
||||
|
||||
public function getResourceOwnerDetailsUrl(AccessToken $token): string
|
||||
{
|
||||
return static::API_BASE.'/'.static::API_VERSION.'/userinfo';
|
||||
}
|
||||
|
||||
public function getDefaultScopes(): array
|
||||
{
|
||||
return ['basic'];
|
||||
}
|
||||
|
||||
protected function getScopeSeparator(): string
|
||||
{
|
||||
return ' ';
|
||||
}
|
||||
|
||||
protected function checkResponse(ResponseInterface $response, $data)
|
||||
{
|
||||
if (!empty($data['error'])) {
|
||||
throw new IdentityProviderException($data['error'].': '.$data['message'], null, $response);
|
||||
}
|
||||
}
|
||||
|
||||
protected function createResourceOwner(array $response, AccessToken $token): SeptemberFirstUser
|
||||
{
|
||||
return new SeptemberFirstUser($response);
|
||||
}
|
||||
}
|
||||
194
src/Provider/SeptemberFirstUser.php
Executable file
194
src/Provider/SeptemberFirstUser.php
Executable file
@@ -0,0 +1,194 @@
|
||||
<?php
|
||||
|
||||
namespace Sept\OAuth2\Client\Provider;
|
||||
|
||||
use League\OAuth2\Client\Provider\ResourceOwnerInterface;
|
||||
|
||||
class SeptemberFirstUser implements ResourceOwnerInterface
|
||||
{
|
||||
/**
|
||||
* Массив с данными о пользователе
|
||||
*/
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* Конструктор
|
||||
*/
|
||||
public function __construct(array $response)
|
||||
{
|
||||
$this->data = $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Значение массива (многомерного)
|
||||
*
|
||||
* @param string $key Ключ поля (например: `email` или `name.first` — вложенность оформляется точкой)
|
||||
* @return mixed|null
|
||||
*/
|
||||
public static function getFieldFromArray(string $key, ?array $array)
|
||||
{
|
||||
if (strpos($key, '.')) { // key.subKey.subSubKey
|
||||
list ($key, $subKey) = explode('.', $key, 2);
|
||||
return isset($array[$key]) ? static::getFieldFromArray($subKey, $array[$key]) : null;
|
||||
}
|
||||
|
||||
return isset($array[$key]) ? $array[$key] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Элемент массива данных о пользователе
|
||||
*
|
||||
* @param string $key Ключ поля (например: email или name.first — вложенность оформляется точкой)
|
||||
* @return mixed|null
|
||||
*/
|
||||
protected function getField(string $key)
|
||||
{
|
||||
return static::getFieldFromArray($key, $this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Данные о пользователе в виде массива
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* ID пользователя
|
||||
*/
|
||||
public function getId(): ?string
|
||||
{
|
||||
return $this->getField('id');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getLastName(): ?string
|
||||
{
|
||||
return $this->getField('personal_name.surname');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFirstName(): ?string
|
||||
{
|
||||
return $this->getField('personal_name.name');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getMiddleName(): ?string
|
||||
{
|
||||
return $this->getField('personal_name.patronymic');
|
||||
}
|
||||
|
||||
/**
|
||||
* Девичья фамилия пользователя (только для женского пола)
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getMaidenName(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDisplayName(): ?string
|
||||
{
|
||||
return $this->getField('display_name');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSex(): ?string
|
||||
{
|
||||
return $this->getField('sex');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getEmail(): ?string
|
||||
{
|
||||
return $this->getField('email');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getBirthday(): ?\DateTime
|
||||
{
|
||||
return !empty($this->data['birthday']) ? new \DateTime($this->data['birthday']) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAvatarUrl(bool $rejectEmptyAvatar = false): ?string
|
||||
{
|
||||
if (empty($this->data['avatar']) or ((!isset($this->data['has_avatar']) or !$this->data['has_avatar']) and $rejectEmptyAvatar)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->getField('avatar');
|
||||
}
|
||||
|
||||
/**
|
||||
* Адрес аватарки 50x50
|
||||
*
|
||||
* @param bool $rejectEmptyAvatar Если true, то пустые аватарки (заглушки) не отдаются
|
||||
* @return string|null
|
||||
*/
|
||||
public function getAvatar50Url(bool $rejectEmptyAvatar = false): ?string
|
||||
{
|
||||
return $this->getAvatarUrl($rejectEmptyAvatar);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getProfileUrl(): ?string
|
||||
{
|
||||
return $this->getField('profile_url');
|
||||
}
|
||||
|
||||
/**
|
||||
* Номер телефона
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getPhone(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Локаль
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getLocale(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Имя временной зоны (от -24 до 24)
|
||||
* @example Europe/Moscow
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getTimezone(): ?string
|
||||
{
|
||||
return $this->getField('timezone');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user