Skip to content

Commit 71cad3d

Browse files
authored
Merge pull request #165 from codebar-ag/feature-updates
Updates
2 parents bbacbf9 + 80e9781 commit 71cad3d

File tree

8 files changed

+95
-31
lines changed

8 files changed

+95
-31
lines changed

.github/ISSUE_TEMPLATE/bug_report.yml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,41 @@ labels: ["bug"]
55
body:
66
- type: markdown
77
attributes:
8-
value: |
9-
We're sorry to hear you have a problem. Can you help us solve it by providing the following details.
8+
value: "|
9+
We're sorry to hear you have a problem. Can you help us solve it by providing the following details."
1010
- type: textarea
1111
id: what-happened
1212
attributes:
1313
label: What happened?
1414
description: What did you expect to happen?
15-
placeholder: I cannot currently do X thing because when I do, it breaks X thing.
15+
placeholder: "I cannot currently do X thing because when I do, it breaks X thing."
1616
validations:
1717
required: true
1818
- type: input
1919
id: package-version
2020
attributes:
2121
label: Package Version
2222
description: What version of our Package are you running? Please be as specific as possible
23-
placeholder: 1.0.0
23+
placeholder: "11.0"
24+
value: "11.0"
2425
validations:
2526
required: true
2627
- type: input
2728
id: php-version
2829
attributes:
2930
label: PHP Version
3031
description: What version of PHP are you running? Please be as specific as possible
31-
placeholder: 8.3.0
32+
placeholder: "8.3.0"
33+
value: "8.3.0"
3234
validations:
3335
required: true
3436
- type: input
3537
id: laravel-version
3638
attributes:
3739
label: Laravel Version
3840
description: What version of Laravel are you running? Please be as specific as possible
39-
placeholder: 11.0.0
41+
placeholder: "11.0.0"
42+
value: "11.0.0"
4043
validations:
4144
required: true
4245
- type: dropdown

.github/workflows/run-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
fail-fast: true
1414
max-parallel: 1
1515
matrix:
16-
os: [ ubuntu-latest, windows-latest ]
16+
os: [ ubuntu-latest ]
1717
php: [ 8.2, 8.3 ]
1818
laravel: [ 11.* ]
1919
stability: [ prefer-lowest, prefer-stable ]

.phpunit.cache/test-results

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/Connectors/DocuWareConnector.php

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use CodebarAg\DocuWare\Requests\Authentication\OAuth\GetResponsibleIdentityService;
1212
use CodebarAg\DocuWare\Requests\Authentication\OAuth\RequestTokenWithCredentials;
1313
use CodebarAg\DocuWare\Requests\Authentication\OAuth\RequestTokenWithCredentialsTrustedUser;
14+
use Illuminate\Support\Arr;
1415
use Illuminate\Support\Facades\Cache;
1516
use Illuminate\Support\Facades\Crypt;
1617
use Psr\SimpleCache\InvalidArgumentException;
@@ -52,39 +53,39 @@ protected function defaultAuth(): TokenAuthenticator
5253

5354
/**
5455
* @throws InvalidArgumentException
56+
* @throws \Exception
5557
*/
56-
protected function getOrCreateNewOAuthToken()
58+
protected function getOrCreateNewOAuthToken(): string
5759
{
5860
$cache = Cache::store($this->configuration->cacheDriver);
5961

60-
// get instance name of $this->configuration as string
61-
$instanceName = get_class($this->configuration);
62-
63-
$cacheKey = 'docuware.oauth.'.$instanceName.'.'.$this->configuration->url.'.'.($this->configuration->username ?? '');
64-
65-
$token = null;
66-
67-
if ($cache->has(key: $cacheKey)) {
68-
$token = Crypt::decrypt($cache->get(key: $cacheKey));
62+
$cacheKey = 'docuware.oauth.'.$this->configuration->identifier;
6963

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

75-
DocuWareOAuthLog::dispatch($this->configuration->url, $this->configuration->username, 'Token retrieved from API');
76-
}
77-
78-
if ($this->configuration instanceof ConfigWithCredentialsTrustedUser) {
79-
$token = $this->getNewOAuthTokenWithCredentialsTrustedUser();
69+
return $token->accessToken;
70+
}
8071

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

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

87-
return $token->accessToken;
81+
// Handle token retrieval for ConfigWithCredentialsTrustedUser
82+
if ($this->configuration instanceof ConfigWithCredentialsTrustedUser) {
83+
$token = $this->getNewOAuthTokenWithCredentialsTrustedUser();
84+
DocuWareOAuthLog::dispatch($this->configuration->url, $this->configuration->username, 'Token retrieved from API');
85+
$cache->put($cacheKey, Crypt::encrypt($token), $token->expiresIn - 60);
86+
87+
return $token->accessToken;
88+
}
8889
}
8990

9091
protected function getAuthenticationTokenEndpoint(): IdentityServiceConfiguration
@@ -98,6 +99,10 @@ protected function getAuthenticationTokenEndpoint(): IdentityServiceConfiguratio
9899
return $identityServiceConfigurationResponse->dto();
99100
}
100101

102+
/**
103+
* @throws \Throwable
104+
* @throws \JsonException
105+
*/
101106
protected function getNewOAuthTokenWithCredentials(): RequestTokenDto
102107
{
103108
$requestTokenResponse = (new RequestTokenWithCredentials(
@@ -108,9 +113,24 @@ protected function getNewOAuthTokenWithCredentials(): RequestTokenDto
108113
password: $this->configuration->password,
109114
))->send();
110115

116+
throw_if(
117+
$requestTokenResponse->failed(),
118+
trim(preg_replace('/\s\s+/', ' ', Arr::get(
119+
array: $requestTokenResponse->json(),
120+
key: 'error_description',
121+
default: $requestTokenResponse->body()
122+
)))
123+
);
124+
125+
throw_if($requestTokenResponse->dto() == null, 'Token response is null');
126+
111127
return $requestTokenResponse->dto();
112128
}
113129

130+
/**
131+
* @throws \Throwable
132+
* @throws \JsonException
133+
*/
114134
protected function getNewOAuthTokenWithCredentialsTrustedUser(): RequestTokenDto
115135
{
116136
$requestTokenResponse = (new RequestTokenWithCredentialsTrustedUser(
@@ -122,6 +142,17 @@ protected function getNewOAuthTokenWithCredentialsTrustedUser(): RequestTokenDto
122142
impersonateName: $this->configuration->impersonatedUsername,
123143
))->send();
124144

145+
throw_if(
146+
$requestTokenResponse->failed(),
147+
trim(preg_replace('/\s\s+/', ' ', Arr::get(
148+
array: $requestTokenResponse->json(),
149+
key: 'error_description',
150+
default: $requestTokenResponse->body()
151+
)))
152+
);
153+
154+
throw_if($requestTokenResponse->dto() == null, 'Token response is null');
155+
125156
return $requestTokenResponse->dto();
126157
}
127158
}

src/DTO/Config/ConfigWithCredentials.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
namespace CodebarAg\DocuWare\DTO\Config;
44

5+
use Illuminate\Support\Facades\Crypt;
6+
use Illuminate\Support\Facades\Hash;
7+
58
final class ConfigWithCredentials
69
{
10+
public string $identifier;
11+
712
public string $username;
813

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

5156
$this->scope = filled($scope) ? $scope : config('laravel-docuware.configurations.scope');
57+
58+
$this->identifier = Hash::make($this->url.$this->username.Crypt::encrypt($this->password));
5259
}
5360
}

src/DTO/Config/ConfigWithCredentialsTrustedUser.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
namespace CodebarAg\DocuWare\DTO\Config;
44

5+
use Illuminate\Support\Facades\Crypt;
6+
use Illuminate\Support\Facades\Hash;
7+
58
final class ConfigWithCredentialsTrustedUser
69
{
10+
public string $identifier;
11+
712
public string $username;
813

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

5560
$this->scope = filled($scope) ? $scope : config('laravel-docuware.configurations.scope');
61+
62+
$this->identifier = Hash::make($this->url.$this->username.Crypt::encrypt($this->password));
5663
}
5764
}

src/DocuWareUrl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function make(): string
8484
}
8585

8686
// Source: https://support.docuware.com/en-US/forums/help-with-technical-problems/ea9618df-c491-e911-80e7-0003ff59a7c6
87-
$key = utf8_encode($this->passphrase);
87+
$key = mb_convert_encoding($this->passphrase, 'UTF-8', 'ISO-8859-1');
8888
$passphrase = hash('sha512', $key, true);
8989
$encryption_key = substr($passphrase, 0, 32);
9090
$iv = substr($passphrase, 32, 16);

tests/Feature/DocuWareAuthenticationTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,19 @@
3838
Event::assertDispatched(DocuWareOAuthLog::class);
3939

4040
})->group('authentication');
41+
42+
it('throws an error if credentials are wrong', function () {
43+
Event::fake();
44+
45+
$connector = new DocuWareConnector(new ConfigWithCredentials(
46+
username: 'wrong-username',
47+
password: 'wrong-password',
48+
));
49+
50+
$connector->send(new GetOrganization);
51+
52+
Event::assertDispatched(DocuWareOAuthLog::class);
53+
54+
})
55+
->group('authentication')
56+
->throws('Invalid user credentials. Please check your username and password.');

0 commit comments

Comments
 (0)