|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Kanboard\Plugin\OAuth2\Auth; |
| 4 | + |
| 5 | +use Kanboard\Core\Base; |
| 6 | +use Kanboard\Core\Security\OAuthAuthenticationProviderInterface; |
| 7 | +use Kanboard\Plugin\OAuth2\User\GenericOAuth2UserProvider; |
| 8 | + |
| 9 | +/** |
| 10 | + * GenericOAuth2Provider |
| 11 | + * |
| 12 | + * @package Kanboard\Auth |
| 13 | + * @author Frederic Guillot |
| 14 | + */ |
| 15 | +class GenericOAuth2Provider extends Base implements OAuthAuthenticationProviderInterface |
| 16 | +{ |
| 17 | + /** |
| 18 | + * User properties |
| 19 | + * |
| 20 | + * @access private |
| 21 | + * @var GenericOAuth2UserProvider |
| 22 | + */ |
| 23 | + private $userInfo = null; |
| 24 | + |
| 25 | + /** |
| 26 | + * OAuth2 instance |
| 27 | + * |
| 28 | + * @access protected |
| 29 | + * @var \Kanboard\Core\Http\OAuth2 |
| 30 | + */ |
| 31 | + protected $service; |
| 32 | + |
| 33 | + /** |
| 34 | + * OAuth2 code |
| 35 | + * |
| 36 | + * @access protected |
| 37 | + * @var string |
| 38 | + */ |
| 39 | + protected $code = ''; |
| 40 | + |
| 41 | + /** |
| 42 | + * Get authentication provider name |
| 43 | + * |
| 44 | + * @access public |
| 45 | + * @return string |
| 46 | + */ |
| 47 | + public function getName() |
| 48 | + { |
| 49 | + return 'OAuth2'; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Authenticate the user |
| 54 | + * |
| 55 | + * @access public |
| 56 | + * @return boolean |
| 57 | + */ |
| 58 | + public function authenticate() |
| 59 | + { |
| 60 | + $profile = $this->getProfile(); |
| 61 | + |
| 62 | + if (! empty($profile)) { |
| 63 | + $this->userInfo = new GenericOAuth2UserProvider($this->container, $profile); |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Set Code |
| 72 | + * |
| 73 | + * @access public |
| 74 | + * @param string $code |
| 75 | + * @return $this |
| 76 | + */ |
| 77 | + public function setCode($code) |
| 78 | + { |
| 79 | + $this->code = $code; |
| 80 | + return $this; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Get user object |
| 85 | + * |
| 86 | + * @access public |
| 87 | + * @return GenericOAuth2UserProvider |
| 88 | + */ |
| 89 | + public function getUser() |
| 90 | + { |
| 91 | + return $this->userInfo; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Get configured OAuth2 service |
| 96 | + * |
| 97 | + * @access public |
| 98 | + * @return \Kanboard\Core\Http\OAuth2 |
| 99 | + */ |
| 100 | + public function getService() |
| 101 | + { |
| 102 | + if (empty($this->service)) { |
| 103 | + $this->service = $this->oauth->createService( |
| 104 | + $this->getClientId(), |
| 105 | + $this->getClientSecret(), |
| 106 | + $this->helper->url->to('OAuthController', 'handler', array('plugin' => 'OAuth2'), '', true), |
| 107 | + $this->getOAuthAuthorizeUrl(), |
| 108 | + $this->getOAuthTokenUrl(), |
| 109 | + array() |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + return $this->service; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Get user profile |
| 118 | + * |
| 119 | + * @access public |
| 120 | + * @return array |
| 121 | + */ |
| 122 | + public function getProfile() |
| 123 | + { |
| 124 | + $token = $this->getService()->getAccessToken($this->code); |
| 125 | + |
| 126 | + if (DEBUG) { |
| 127 | + $this->logger->debug(__METHOD__.': Got access token: '.(empty($token) ? 'No' : 'Yes')); |
| 128 | + $this->logger->debug(__METHOD__.': Fetch user profile from '.$this->getUserAPiUrl()); |
| 129 | + } |
| 130 | + |
| 131 | + return $this->httpClient->getJson( |
| 132 | + $this->getUserAPiUrl(), |
| 133 | + array($this->getService()->getAuthorizationHeader()) |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Unlink user |
| 139 | + * |
| 140 | + * @access public |
| 141 | + * @param integer $userId |
| 142 | + * @return bool |
| 143 | + */ |
| 144 | + public function unlink($userId) |
| 145 | + { |
| 146 | + return $this->userModel->update(array( |
| 147 | + 'id' => $userId, |
| 148 | + 'oauth2_user_id' => '', |
| 149 | + )); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Get client id |
| 154 | + * |
| 155 | + * @access public |
| 156 | + * @return string |
| 157 | + */ |
| 158 | + public function getClientId() |
| 159 | + { |
| 160 | + return $this->configModel->get('oauth2_client_id'); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Get client secret |
| 165 | + * |
| 166 | + * @access public |
| 167 | + * @return string |
| 168 | + */ |
| 169 | + public function getClientSecret() |
| 170 | + { |
| 171 | + return $this->configModel->get('oauth2_client_secret'); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Get authorize url |
| 176 | + * |
| 177 | + * @access public |
| 178 | + * @return string |
| 179 | + */ |
| 180 | + public function getOAuthAuthorizeUrl() |
| 181 | + { |
| 182 | + return $this->configModel->get('oauth2_authorize_url'); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Get token url |
| 187 | + * |
| 188 | + * @access public |
| 189 | + * @return string |
| 190 | + */ |
| 191 | + public function getOAuthTokenUrl() |
| 192 | + { |
| 193 | + return $this->configModel->get('oauth2_token_url'); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Get User API url |
| 198 | + * |
| 199 | + * @access public |
| 200 | + * @return string |
| 201 | + */ |
| 202 | + public function getUserAPiUrl() |
| 203 | + { |
| 204 | + return $this->configModel->get('oauth2_user_api_url'); |
| 205 | + } |
| 206 | +} |
0 commit comments