-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Remove authentication responsibility from authorizers * Updated Response interface naming * Add isStatusCode assert to Response
- Loading branch information
Pe Ell
authored
May 21, 2017
1 parent
6fb6a16
commit 8ea9ced
Showing
16 changed files
with
310 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of YouTrack REST PHP. | ||
* | ||
* (c) Anton Komarev <a.komarev@cybercog.su> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
// Boot third party libraries | ||
require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
||
// Application configuration (replace with your YouTrack server values) | ||
$apiBaseUri = 'https://write-youtrack-domain.here'; | ||
$apiUsername = 'YOUR_USERNAME'; | ||
$apiPassword = 'YOUR_PASSWORD'; | ||
|
||
// Instantiate HTTP Client | ||
$http = new \GuzzleHttp\Client([ | ||
'base_uri' => $apiBaseUri, | ||
]); | ||
|
||
// Instantiate YouTrack API Cookie Authenticator | ||
$authenticator = new \Cog\YouTrack\Rest\Authenticator\CookieAuthenticator($apiUsername, $apiPassword); | ||
|
||
// Instantiate YouTrack API Cookie Authorizer | ||
$authorizer = new \Cog\YouTrack\Rest\Authorizer\CookieAuthorizer($authenticator); | ||
|
||
// Instantiate YouTrack API Client | ||
$client = new \Cog\YouTrack\Rest\Client\YouTrackClient($http, $authorizer); | ||
|
||
// Do request to the API | ||
$response = $client->get('/admin/project'); | ||
|
||
// Convert response to array | ||
$projects = $response->toArray(); | ||
|
||
// Render projects one by one | ||
echo 'Project list:'; | ||
foreach ($projects as $project) { | ||
echo ' #' . $project['id']; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of YouTrack REST PHP. | ||
* | ||
* (c) Anton Komarev <a.komarev@cybercog.su> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Cog\YouTrack\Rest\Authenticator\Contracts; | ||
|
||
use Cog\YouTrack\Rest\Client\Contracts\Client as ClientContract; | ||
|
||
/** | ||
* Interface Authorizer. | ||
* | ||
* @package Cog\YouTrack\Rest\Authenticator\Contracts | ||
*/ | ||
interface Authenticator | ||
{ | ||
/** | ||
* Authenticate API Client. | ||
* | ||
* @param \Cog\YouTrack\Rest\Client\Contracts\Client $client | ||
* @return void | ||
*/ | ||
public function authenticate(ClientContract $client): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of YouTrack REST PHP. | ||
* | ||
* (c) Anton Komarev <a.komarev@cybercog.su> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Cog\YouTrack\Rest\Authenticator; | ||
|
||
use Cog\YouTrack\Rest\Authenticator\Contracts\Authenticator as AuthenticatorContract; | ||
use Cog\YouTrack\Rest\Client\Contracts\Client as ClientContract; | ||
|
||
/** | ||
* Class CookieAuthenticator. | ||
* | ||
* @package Cog\YouTrack\Rest\Authenticator | ||
*/ | ||
class CookieAuthenticator implements AuthenticatorContract | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $username = ''; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $password = ''; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $cookie = ''; | ||
|
||
/** | ||
* Determine is trying to authenticate. | ||
* | ||
* @var bool | ||
*/ | ||
private $isAuthenticating = false; | ||
|
||
/** | ||
* CookieAuthenticator constructor. | ||
* | ||
* @param string $username | ||
* @param string $password | ||
*/ | ||
public function __construct(string $username, string $password) | ||
{ | ||
$this->username = $username; | ||
$this->password = $password; | ||
} | ||
|
||
/** | ||
* Authenticate client and returns cookie on success login. | ||
* | ||
* @param \Cog\YouTrack\Rest\Client\Contracts\Client $client | ||
* @return void | ||
* | ||
* @throws \Cog\YouTrack\Rest\Authenticator\Exceptions\AuthenticationException | ||
*/ | ||
public function authenticate(ClientContract $client): void | ||
{ | ||
if ($this->cookie === '' && !$this->isAuthenticating) { | ||
$this->isAuthenticating = true; | ||
$response = $client->request('POST', '/user/login', [ | ||
'login' => $this->username, | ||
'password' => $this->password, | ||
]); | ||
$this->isAuthenticating = false; | ||
|
||
if ($response->isStatusCode(200)) { | ||
$this->cookie = $response->cookie(); | ||
} | ||
} | ||
|
||
$client->putHeader('Cookie', $this->cookie); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.