| @@ 39-139 (lines=101) @@ | ||
| 36 | * ] |
|
| 37 | * ``` |
|
| 38 | */ |
|
| 39 | class HiamClient extends \yii\authclient\OAuth2 |
|
| 40 | { |
|
| 41 | /** |
|
| 42 | * Site for urls generation. |
|
| 43 | */ |
|
| 44 | public $site; |
|
| 45 | ||
| 46 | public function buildUrl($path, array $params = []) |
|
| 47 | { |
|
| 48 | $url = $this->site . '/' . $path; |
|
| 49 | ||
| 50 | return $params ? $this->composeUrl($url, $params) : $url; |
|
| 51 | } |
|
| 52 | ||
| 53 | /** |
|
| 54 | * {@inheritdoc} |
|
| 55 | */ |
|
| 56 | public function buildAuthUrl(array $params = []) |
|
| 57 | { |
|
| 58 | $params['language'] = \Yii::$app->language; |
|
| 59 | ||
| 60 | return parent::buildAuthUrl($params); |
|
| 61 | } |
|
| 62 | ||
| 63 | /** |
|
| 64 | * Inits Urls based on $site. |
|
| 65 | */ |
|
| 66 | public function init() |
|
| 67 | { |
|
| 68 | parent::init(); |
|
| 69 | if (!$this->site) { |
|
| 70 | $this->site = 'hiam.hipanel.com'; |
|
| 71 | } |
|
| 72 | if (strpos($this->site, '://') === false) { |
|
| 73 | $this->site = 'https://' . $this->site; |
|
| 74 | } |
|
| 75 | $defaults = [ |
|
| 76 | 'authUrl' => 'oauth/authorize', |
|
| 77 | 'tokenUrl' => 'oauth/token', |
|
| 78 | 'apiBaseUrl' => 'api', |
|
| 79 | ]; |
|
| 80 | foreach ($defaults as $k => $v) { |
|
| 81 | if (!$this->{$k}) { |
|
| 82 | $this->{$k} = $this->buildUrl($v); |
|
| 83 | } |
|
| 84 | } |
|
| 85 | } |
|
| 86 | ||
| 87 | /** {@inheritdoc} */ |
|
| 88 | protected function initUserAttributes() |
|
| 89 | { |
|
| 90 | return $this->getAccessToken()->getParam('user_attributes'); |
|
| 91 | } |
|
| 92 | ||
| 93 | /** {@inheritdoc} */ |
|
| 94 | protected function apiInternal($accessToken, $url, $method, array $params, array $headers) |
|
| 95 | { |
|
| 96 | if (!isset($params['format'])) { |
|
| 97 | $params['format'] = 'json'; |
|
| 98 | } |
|
| 99 | $params['oauth_token'] = $accessToken->getToken(); |
|
| 100 | ||
| 101 | return $this->sendRequest($method, $url, $params, $headers); |
|
| 102 | } |
|
| 103 | ||
| 104 | /** {@inheritdoc} */ |
|
| 105 | protected function defaultName() |
|
| 106 | { |
|
| 107 | return 'hiam'; |
|
| 108 | } |
|
| 109 | ||
| 110 | /** {@inheritdoc} */ |
|
| 111 | protected function defaultTitle() |
|
| 112 | { |
|
| 113 | return 'hiam'; |
|
| 114 | } |
|
| 115 | ||
| 116 | /** |
|
| 117 | * {@inheritdoc} |
|
| 118 | */ |
|
| 119 | public function setState($key, $value) |
|
| 120 | { |
|
| 121 | return parent::setState($key, $value); |
|
| 122 | } |
|
| 123 | ||
| 124 | /** |
|
| 125 | * {@inheritdoc} |
|
| 126 | */ |
|
| 127 | public function getState($key) |
|
| 128 | { |
|
| 129 | return parent::getState($key); |
|
| 130 | } |
|
| 131 | ||
| 132 | /** |
|
| 133 | * {@inheritdoc} |
|
| 134 | */ |
|
| 135 | public function removeState($key) |
|
| 136 | { |
|
| 137 | return parent::removeState($key); |
|
| 138 | } |
|
| 139 | } |
|
| 140 | ||
| @@ 39-139 (lines=101) @@ | ||
| 36 | * ] |
|
| 37 | * ``` |
|
| 38 | */ |
|
| 39 | class NopeClient extends \yii\authclient\OAuth2 |
|
| 40 | { |
|
| 41 | /** |
|
| 42 | * Site for urls generation. |
|
| 43 | */ |
|
| 44 | public $site; |
|
| 45 | ||
| 46 | public function buildUrl($path, array $params = []) |
|
| 47 | { |
|
| 48 | $url = $this->site . '/' . $path; |
|
| 49 | ||
| 50 | return $params ? $this->composeUrl($url, $params) : $url; |
|
| 51 | } |
|
| 52 | ||
| 53 | /** |
|
| 54 | * {@inheritdoc} |
|
| 55 | */ |
|
| 56 | public function buildAuthUrl(array $params = []) |
|
| 57 | { |
|
| 58 | $params['language'] = \Yii::$app->language; |
|
| 59 | ||
| 60 | return parent::buildAuthUrl($params); |
|
| 61 | } |
|
| 62 | ||
| 63 | /** |
|
| 64 | * Inits Urls based on $site. |
|
| 65 | */ |
|
| 66 | public function init() |
|
| 67 | { |
|
| 68 | parent::init(); |
|
| 69 | if (!$this->site) { |
|
| 70 | throw new \Exception('Nope site must not be empty!'); |
|
| 71 | } |
|
| 72 | if (strpos($this->site, '://') === false) { |
|
| 73 | $this->site = 'https://' . $this->site; |
|
| 74 | } |
|
| 75 | $defaults = [ |
|
| 76 | 'authUrl' => 'oauth/authorize', |
|
| 77 | 'tokenUrl' => 'oauth/token', |
|
| 78 | 'apiBaseUrl' => 'api', |
|
| 79 | ]; |
|
| 80 | foreach ($defaults as $k => $v) { |
|
| 81 | if (!$this->{$k}) { |
|
| 82 | $this->{$k} = $this->buildUrl($v); |
|
| 83 | } |
|
| 84 | } |
|
| 85 | } |
|
| 86 | ||
| 87 | /** {@inheritdoc} */ |
|
| 88 | protected function initUserAttributes() |
|
| 89 | { |
|
| 90 | return $this->getAccessToken()->getParam('user_attributes'); |
|
| 91 | } |
|
| 92 | ||
| 93 | /** {@inheritdoc} */ |
|
| 94 | protected function apiInternal($accessToken, $url, $method, array $params, array $headers) |
|
| 95 | { |
|
| 96 | if (!isset($params['format'])) { |
|
| 97 | $params['format'] = 'json'; |
|
| 98 | } |
|
| 99 | $params['oauth_token'] = $accessToken->getToken(); |
|
| 100 | ||
| 101 | return $this->sendRequest($method, $url, $params, $headers); |
|
| 102 | } |
|
| 103 | ||
| 104 | /** {@inheritdoc} */ |
|
| 105 | protected function defaultName() |
|
| 106 | { |
|
| 107 | return 'nope'; |
|
| 108 | } |
|
| 109 | ||
| 110 | /** {@inheritdoc} */ |
|
| 111 | protected function defaultTitle() |
|
| 112 | { |
|
| 113 | return 'nope'; |
|
| 114 | } |
|
| 115 | ||
| 116 | /** |
|
| 117 | * {@inheritdoc} |
|
| 118 | */ |
|
| 119 | public function setState($key, $value) |
|
| 120 | { |
|
| 121 | return parent::setState($key, $value); |
|
| 122 | } |
|
| 123 | ||
| 124 | /** |
|
| 125 | * {@inheritdoc} |
|
| 126 | */ |
|
| 127 | public function getState($key) |
|
| 128 | { |
|
| 129 | return parent::getState($key); |
|
| 130 | } |
|
| 131 | ||
| 132 | /** |
|
| 133 | * {@inheritdoc} |
|
| 134 | */ |
|
| 135 | public function removeState($key) |
|
| 136 | { |
|
| 137 | return parent::removeState($key); |
|
| 138 | } |
|
| 139 | } |
|
| 140 | ||