diff options
author | Frederic Guillot <fred@kanboard.net> | 2015-07-16 07:28:46 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2015-07-16 07:28:46 -0400 |
commit | 12036aa21f4308aca4d816864b357f9627a0f437 (patch) | |
tree | b333a38750e06430c667c49d259155d492fbf087 /app/Core/OAuth2.php | |
parent | 0bbc6da50ae8b23a8cc6c1217dcd345c0ddb5b7a (diff) |
Refactoring of Google Authentication (new callback url)
Diffstat (limited to 'app/Core/OAuth2.php')
-rw-r--r-- | app/Core/OAuth2.php | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/app/Core/OAuth2.php b/app/Core/OAuth2.php new file mode 100644 index 00000000..a0b33e31 --- /dev/null +++ b/app/Core/OAuth2.php @@ -0,0 +1,75 @@ +<?php + +namespace Core; + +/** + * OAuth2 client + * + * @package core + * @author Frederic Guillot + */ +class OAuth2 extends Base +{ + private $clientId; + private $secret; + private $callbackUrl; + private $authUrl; + private $tokenUrl; + private $scopes; + private $tokenType; + private $accessToken; + + public function createService($clientId, $secret, $callbackUrl, $authUrl, $tokenUrl, array $scopes) + { + $this->clientId = $clientId; + $this->secret = $secret; + $this->callbackUrl = $callbackUrl; + $this->authUrl = $authUrl; + $this->tokenUrl = $tokenUrl; + $this->scopes = $scopes; + + return $this; + } + + public function getAuthorizationUrl() + { + $params = array( + 'response_type' => 'code', + 'client_id' => $this->clientId, + 'redirect_uri' => $this->callbackUrl, + 'scope' => implode(' ', $this->scopes), + ); + + return $this->authUrl.'?'.http_build_query($params); + } + + public function getAuthorizationHeader() + { + if ($this->tokenType === 'Bearer') { + return 'Authorization: Bearer '.$this->accessToken; + } + + return ''; + } + + public function getAccessToken($code) + { + if (empty($this->accessToken) && ! empty($code)) { + + $params = array( + 'code' => $code, + 'client_id' => $this->clientId, + 'client_secret' => $this->secret, + 'redirect_uri' => $this->callbackUrl, + 'grant_type' => 'authorization_code', + ); + + $response = json_decode($this->httpClient->postForm($this->tokenUrl, $params), true); + + $this->tokenType = isset($response['token_type']) ? $response['token_type'] : ''; + $this->accessToken = isset($response['access_token']) ? $response['access_token'] : ''; + } + + return $this->accessToken; + } +} |