Skip to content

Commit

Permalink
Merge pull request #165 from codebar-ag/feature-updates
Browse files Browse the repository at this point in the history
Updates
  • Loading branch information
StanBarrows authored Sep 12, 2024
2 parents bbacbf9 + 80e9781 commit 71cad3d
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 31 deletions.
15 changes: 9 additions & 6 deletions .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,41 @@ labels: ["bug"]
body:
- type: markdown
attributes:
value: |
We're sorry to hear you have a problem. Can you help us solve it by providing the following details.
value: "|
We're sorry to hear you have a problem. Can you help us solve it by providing the following details."
- type: textarea
id: what-happened
attributes:
label: What happened?
description: What did you expect to happen?
placeholder: I cannot currently do X thing because when I do, it breaks X thing.
placeholder: "I cannot currently do X thing because when I do, it breaks X thing."
validations:
required: true
- type: input
id: package-version
attributes:
label: Package Version
description: What version of our Package are you running? Please be as specific as possible
placeholder: 1.0.0
placeholder: "11.0"
value: "11.0"
validations:
required: true
- type: input
id: php-version
attributes:
label: PHP Version
description: What version of PHP are you running? Please be as specific as possible
placeholder: 8.3.0
placeholder: "8.3.0"
value: "8.3.0"
validations:
required: true
- type: input
id: laravel-version
attributes:
label: Laravel Version
description: What version of Laravel are you running? Please be as specific as possible
placeholder: 11.0.0
placeholder: "11.0.0"
value: "11.0.0"
validations:
required: true
- type: dropdown
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
fail-fast: true
max-parallel: 1
matrix:
os: [ ubuntu-latest, windows-latest ]
os: [ ubuntu-latest ]
php: [ 8.2, 8.3 ]
laravel: [ 11.* ]
stability: [ prefer-lowest, prefer-stable ]
Expand Down
2 changes: 1 addition & 1 deletion .phpunit.cache/test-results

Large diffs are not rendered by default.

75 changes: 53 additions & 22 deletions src/Connectors/DocuWareConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use CodebarAg\DocuWare\Requests\Authentication\OAuth\GetResponsibleIdentityService;
use CodebarAg\DocuWare\Requests\Authentication\OAuth\RequestTokenWithCredentials;
use CodebarAg\DocuWare\Requests\Authentication\OAuth\RequestTokenWithCredentialsTrustedUser;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Crypt;
use Psr\SimpleCache\InvalidArgumentException;
Expand Down Expand Up @@ -52,39 +53,39 @@ protected function defaultAuth(): TokenAuthenticator

/**
* @throws InvalidArgumentException
* @throws \Exception
*/
protected function getOrCreateNewOAuthToken()
protected function getOrCreateNewOAuthToken(): string
{
$cache = Cache::store($this->configuration->cacheDriver);

// get instance name of $this->configuration as string
$instanceName = get_class($this->configuration);

$cacheKey = 'docuware.oauth.'.$instanceName.'.'.$this->configuration->url.'.'.($this->configuration->username ?? '');

$token = null;

if ($cache->has(key: $cacheKey)) {
$token = Crypt::decrypt($cache->get(key: $cacheKey));
$cacheKey = 'docuware.oauth.'.$this->configuration->identifier;

// Check if the token exists in cache and return it if found
if ($cache->has($cacheKey)) {
$token = Crypt::decrypt($cache->get($cacheKey));
DocuWareOAuthLog::dispatch($this->configuration->url, $this->configuration->username, 'Token retrieved from cache');
} else {
if ($this->configuration instanceof ConfigWithCredentials) {
$token = $this->getNewOAuthTokenWithCredentials();

DocuWareOAuthLog::dispatch($this->configuration->url, $this->configuration->username, 'Token retrieved from API');
}

if ($this->configuration instanceof ConfigWithCredentialsTrustedUser) {
$token = $this->getNewOAuthTokenWithCredentialsTrustedUser();
return $token->accessToken;
}

DocuWareOAuthLog::dispatch($this->configuration->url, $this->configuration->username, 'Token retrieved from API');
}
// Handle token retrieval for ConfigWithCredentials
if ($this->configuration instanceof ConfigWithCredentials) {
$token = $this->getNewOAuthTokenWithCredentials();
DocuWareOAuthLog::dispatch($this->configuration->url, $this->configuration->username, 'Token retrieved from API');
$cache->put($cacheKey, Crypt::encrypt($token), $token->expiresIn - 60);

$cache->put(key: $cacheKey, value: Crypt::encrypt($token), ttl: $token->expiresIn - 60);
return $token->accessToken;
}

return $token->accessToken;
// Handle token retrieval for ConfigWithCredentialsTrustedUser
if ($this->configuration instanceof ConfigWithCredentialsTrustedUser) {
$token = $this->getNewOAuthTokenWithCredentialsTrustedUser();
DocuWareOAuthLog::dispatch($this->configuration->url, $this->configuration->username, 'Token retrieved from API');
$cache->put($cacheKey, Crypt::encrypt($token), $token->expiresIn - 60);

return $token->accessToken;
}
}

protected function getAuthenticationTokenEndpoint(): IdentityServiceConfiguration
Expand All @@ -98,6 +99,10 @@ protected function getAuthenticationTokenEndpoint(): IdentityServiceConfiguratio
return $identityServiceConfigurationResponse->dto();
}

/**
* @throws \Throwable
* @throws \JsonException
*/
protected function getNewOAuthTokenWithCredentials(): RequestTokenDto
{
$requestTokenResponse = (new RequestTokenWithCredentials(
Expand All @@ -108,9 +113,24 @@ protected function getNewOAuthTokenWithCredentials(): RequestTokenDto
password: $this->configuration->password,
))->send();

throw_if(
$requestTokenResponse->failed(),
trim(preg_replace('/\s\s+/', ' ', Arr::get(
array: $requestTokenResponse->json(),
key: 'error_description',
default: $requestTokenResponse->body()
)))
);

throw_if($requestTokenResponse->dto() == null, 'Token response is null');

return $requestTokenResponse->dto();
}

/**
* @throws \Throwable
* @throws \JsonException
*/
protected function getNewOAuthTokenWithCredentialsTrustedUser(): RequestTokenDto
{
$requestTokenResponse = (new RequestTokenWithCredentialsTrustedUser(
Expand All @@ -122,6 +142,17 @@ protected function getNewOAuthTokenWithCredentialsTrustedUser(): RequestTokenDto
impersonateName: $this->configuration->impersonatedUsername,
))->send();

throw_if(
$requestTokenResponse->failed(),
trim(preg_replace('/\s\s+/', ' ', Arr::get(
array: $requestTokenResponse->json(),
key: 'error_description',
default: $requestTokenResponse->body()
)))
);

throw_if($requestTokenResponse->dto() == null, 'Token response is null');

return $requestTokenResponse->dto();
}
}
7 changes: 7 additions & 0 deletions src/DTO/Config/ConfigWithCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

namespace CodebarAg\DocuWare\DTO\Config;

use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Hash;

final class ConfigWithCredentials
{
public string $identifier;

public string $username;

public string $password;
Expand Down Expand Up @@ -49,5 +54,7 @@ public function __construct(
$this->clientId = filled($clientId) ? $clientId : config('laravel-docuware.configurations.client_id');

$this->scope = filled($scope) ? $scope : config('laravel-docuware.configurations.scope');

$this->identifier = Hash::make($this->url.$this->username.Crypt::encrypt($this->password));
}
}
7 changes: 7 additions & 0 deletions src/DTO/Config/ConfigWithCredentialsTrustedUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

namespace CodebarAg\DocuWare\DTO\Config;

use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Hash;

final class ConfigWithCredentialsTrustedUser
{
public string $identifier;

public string $username;

public string $password;
Expand Down Expand Up @@ -53,5 +58,7 @@ public function __construct(
$this->clientId = filled($clientId) ? $clientId : config('laravel-docuware.configurations.client_id');

$this->scope = filled($scope) ? $scope : config('laravel-docuware.configurations.scope');

$this->identifier = Hash::make($this->url.$this->username.Crypt::encrypt($this->password));
}
}
2 changes: 1 addition & 1 deletion src/DocuWareUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function make(): string
}

// Source: https://support.docuware.com/en-US/forums/help-with-technical-problems/ea9618df-c491-e911-80e7-0003ff59a7c6
$key = utf8_encode($this->passphrase);
$key = mb_convert_encoding($this->passphrase, 'UTF-8', 'ISO-8859-1');
$passphrase = hash('sha512', $key, true);
$encryption_key = substr($passphrase, 0, 32);
$iv = substr($passphrase, 32, 16);
Expand Down
16 changes: 16 additions & 0 deletions tests/Feature/DocuWareAuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,19 @@
Event::assertDispatched(DocuWareOAuthLog::class);

})->group('authentication');

it('throws an error if credentials are wrong', function () {
Event::fake();

$connector = new DocuWareConnector(new ConfigWithCredentials(
username: 'wrong-username',
password: 'wrong-password',
));

$connector->send(new GetOrganization);

Event::assertDispatched(DocuWareOAuthLog::class);

})
->group('authentication')
->throws('Invalid user credentials. Please check your username and password.');

0 comments on commit 71cad3d

Please sign in to comment.