-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from City-of-Helsinki/UHF-8377
UHF-8377: Use Vault to store credentials
- Loading branch information
Showing
8 changed files
with
155 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\helfi_navigation; | ||
|
||
use Drupal\Core\Config\ConfigFactoryInterface; | ||
use Drupal\helfi_api_base\Vault\VaultManager; | ||
|
||
/** | ||
* A BC layer to handle API authorization. | ||
*/ | ||
final class ApiAuthorization { | ||
|
||
public const VAULT_MANAGER_KEY = 'helfi_navigation'; | ||
|
||
/** | ||
* Constructs a new instance. | ||
* | ||
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory | ||
* The configuration factory service. | ||
* @param \Drupal\helfi_api_base\Vault\VaultManager $vaultManager | ||
* The vault manager service. | ||
*/ | ||
public function __construct( | ||
private readonly ConfigFactoryInterface $configFactory, | ||
private readonly VaultManager $vaultManager, | ||
) { | ||
} | ||
|
||
/** | ||
* Gets the authorization token. | ||
* | ||
* @return string|null | ||
* The authorization token. | ||
*/ | ||
public function getAuthorization() : ?string { | ||
if ($authorization = $this->vaultManager->get(self::VAULT_MANAGER_KEY)) { | ||
return $authorization->data(); | ||
} | ||
|
||
// Provide a BC layer to fetch API keys from previously used | ||
// configuration. | ||
// @todo remove this once all projects have migrated to Vault. | ||
return $this->configFactory->get('helfi_navigation.api') | ||
?->get('key'); | ||
} | ||
|
||
} |
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
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,60 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\Tests\helfi_navigation\Unit; | ||
|
||
use Drupal\helfi_api_base\Vault\AuthorizationToken; | ||
use Drupal\helfi_api_base\Vault\VaultManager; | ||
use Drupal\helfi_navigation\ApiAuthorization; | ||
use Drupal\Tests\UnitTestCase; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
|
||
/** | ||
* @coversDefaultClass \Drupal\helfi_navigation\ApiAuthorization | ||
* @group helfi_navigation | ||
*/ | ||
class ApiAuthorizationTest extends UnitTestCase { | ||
|
||
use ProphecyTrait; | ||
|
||
/** | ||
* @covers ::__construct | ||
* @covers ::getAuthorization | ||
*/ | ||
public function testVaultAuthorization() : void { | ||
$vaultManager = new VaultManager([ | ||
new AuthorizationToken(ApiAuthorization::VAULT_MANAGER_KEY, '123'), | ||
]); | ||
$sut = new ApiAuthorization( | ||
$this->getConfigFactoryStub([]), | ||
$vaultManager, | ||
); | ||
$this->assertEquals('123', $sut->getAuthorization()); | ||
} | ||
|
||
/** | ||
* @covers ::__construct | ||
* @covers ::getAuthorization | ||
*/ | ||
public function testEmptyAuthorization() : void { | ||
$sut = new ApiAuthorization( | ||
$this->getConfigFactoryStub([]), | ||
new VaultManager([]), | ||
); | ||
$this->assertNull($sut->getAuthorization()); | ||
} | ||
|
||
/** | ||
* @covers ::__construct | ||
* @covers ::getAuthorization | ||
*/ | ||
public function testFallbackConfigAuthorization() : void { | ||
$sut = new ApiAuthorization( | ||
$this->getConfigFactoryStub(['helfi_navigation.api' => ['key' => '123']]), | ||
new VaultManager([]), | ||
); | ||
$this->assertEquals('123', $sut->getAuthorization()); | ||
} | ||
|
||
} |
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