Added an button to connect the oauth providers from WebUI

This commit is contained in:
Jan Böhmer 2023-07-16 03:18:33 +02:00
parent c203de082e
commit f7648e3311
6 changed files with 28 additions and 4 deletions

View file

@ -36,7 +36,7 @@ class DigikeyProvider implements InfoProviderInterface
private readonly HttpClientInterface $digikeyClient; private readonly HttpClientInterface $digikeyClient;
public function __construct(HttpClientInterface $httpClient, private readonly OAuthTokenManager $authTokenManager, string $currency, string $clientId) public function __construct(HttpClientInterface $httpClient, private readonly OAuthTokenManager $authTokenManager, string $currency, private readonly string $clientId)
{ {
//Create the HTTP client with some default options //Create the HTTP client with some default options
$this->digikeyClient = $httpClient->withOptions([ $this->digikeyClient = $httpClient->withOptions([
@ -57,6 +57,7 @@ class DigikeyProvider implements InfoProviderInterface
'name' => 'DigiKey', 'name' => 'DigiKey',
'description' => 'This provider uses the DigiKey API to search for parts.', 'description' => 'This provider uses the DigiKey API to search for parts.',
'url' => 'https://www.digikey.com/', 'url' => 'https://www.digikey.com/',
'oauth_app_name' => self::OAUTH_APP_NAME,
]; ];
} }
@ -78,7 +79,8 @@ class DigikeyProvider implements InfoProviderInterface
public function isActive(): bool public function isActive(): bool
{ {
return true; //The client ID has to be set and a token has to be available (user clicked connect)
return !empty($this->clientId) && $this->authTokenManager->hasToken(self::OAUTH_APP_NAME);
} }
public function searchByKeyword(string $keyword): array public function searchByKeyword(string $keyword): array

View file

@ -38,8 +38,9 @@ interface InfoProviderInterface
* - logo?: The logo of the provider (e.g. "digikey.png") * - logo?: The logo of the provider (e.g. "digikey.png")
* - url?: The url of the provider (e.g. "https://www.digikey.com") * - url?: The url of the provider (e.g. "https://www.digikey.com")
* - disabled_help?: A help text which is shown when the provider is disabled, explaining how to enable it * - disabled_help?: A help text which is shown when the provider is disabled, explaining how to enable it
* - oauth_app_name?: The name of the OAuth app which is used for authentication (e.g. "ip_digikey_oauth"). If this is set a connect button will be shown
* *
* @phpstan-return array{ name: string, description?: string, logo?: string, url?: string, disabled_help?: string } * @phpstan-return array{ name: string, description?: string, logo?: string, url?: string, disabled_help?: string, oauth_app_name?: string }
*/ */
public function getProviderInfo(): array; public function getProviderInfo(): array;

View file

@ -74,6 +74,16 @@ final class OAuthTokenManager
return $this->entityManager->getRepository(OAuthToken::class)->findOneBy(['name' => $app_name]); return $this->entityManager->getRepository(OAuthToken::class)->findOneBy(['name' => $app_name]);
} }
/**
* Checks if a token for the given app name is existing
* @param string $app_name
* @return bool
*/
public function hasToken(string $app_name): bool
{
return $this->getToken($app_name) !== null;
}
/** /**
* This function refreshes the token for the given app name. The new token is saved to the database * This function refreshes the token for the given app name. The new token is saved to the database
* The app_name must be registered in the knpu_oauth2_client.yaml * The app_name must be registered in the knpu_oauth2_client.yaml

View file

@ -30,6 +30,10 @@
{{ capability.translationKey|trans }} {{ capability.translationKey|trans }}
</span> </span>
{% endfor %} {% endfor %}
{% if provider.providerInfo.oauth_app_name is defined and provider.providerInfo.oauth_app_name is not empty %}
<br>
<a href="{{ path('oauth_client_connect', {'name': provider.providerInfo.oauth_app_name}) }}" target="_blank" class="btn btn-outline-secondary btn-sm mt-2">{% trans %}oauth_client.connect.btn{% endtrans %}</a>
{% endif %}
</div> </div>
</div> </div>
{% if provider.active == false %} {% if provider.active == false %}
@ -42,7 +46,6 @@
{% endif %} {% endif %}
</div> </div>
</div> </div>
{% endif %} {% endif %}
</td> </td>
</tr> </tr>

View file

@ -11,6 +11,8 @@
{% block card_content %} {% block card_content %}
<a href="{{ path('info_providers_list') }}">All info providers</a>
{{ form(form) }} {{ form(form) }}
{% if results is not null %} {% if results is not null %}

View file

@ -11495,5 +11495,11 @@ Please note, that you can not impersonate a disabled user. If you try you will g
<target>Created by Information provider</target> <target>Created by Information provider</target>
</segment> </segment>
</unit> </unit>
<unit id="Qhaj.j4" name="oauth_client.connect.btn">
<segment>
<source>oauth_client.connect.btn</source>
<target>Connect OAuth</target>
</segment>
</unit>
</file> </file>
</xliff> </xliff>