Skip to content

Commit 1e81980

Browse files
committed
wip
1 parent 0672f15 commit 1e81980

13 files changed

+54
-288
lines changed

README.md

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,6 @@ $token = $request->send()->dto();
7979
// Returns AuthenticationToken with sessionId
8080
```
8181

82-
### Logout
83-
84-
```php
85-
use CodebarAg\MFiles\Requests\Authentication\LogOutFromVaultRequest;
86-
87-
$logout = (new LogOutFromVaultRequest(config: $config))->send()->dto();
88-
// Returns true on successful logout, clears cached token
89-
```
90-
9182
## Requests
9283

9384
### Authentication Requests
@@ -133,25 +124,6 @@ $result = $request->send()->dto();
133124
// Returns true on successful logout
134125
```
135126

136-
### Vault Requests
137-
138-
#### GetVaultsRequest
139-
140-
Gets the list of available vaults for the authenticated user.
141-
142-
**Request:**
143-
```php
144-
use CodebarAg\MFiles\Requests\GetVaultsRequest;
145-
146-
$request = new GetVaultsRequest();
147-
```
148-
149-
**Response:**
150-
```php
151-
$vaults = $connector->send($request)->json();
152-
// Returns array of available vaults
153-
```
154-
155127
### Object Requests
156128

157129
#### GetObjectInformationRequest
@@ -273,28 +245,11 @@ $request = new DownloadFileRequest(
273245
objectId: 123,
274246
fileId: 456,
275247
objectTypeId: 0,
276-
includeDeleted: false
277248
);
278249
```
279250

280251
## DTOs
281252

282-
### Authentication DTOs
283-
284-
#### AuthenticationToken
285-
286-
Represents an M-Files authentication token.
287-
288-
**Properties:**
289-
- `sessionId` (string) - The session ID for the authenticated session
290-
291-
**Usage:**
292-
```php
293-
use CodebarAg\MFiles\DTO\AuthenticationToken;
294-
295-
$token = new AuthenticationToken(sessionId: 'abc123');
296-
```
297-
298253
### Configuration DTOs
299254

300255
#### ConfigWithCredentials

src/Connectors/MFilesConnector.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use CodebarAg\MFiles\DTO\AuthenticationToken;
88
use CodebarAg\MFiles\DTO\ConfigWithCredentials;
9-
use Saloon\Http\Auth\HeaderAuthenticator;
109
use Saloon\Http\Connector;
1110
use Saloon\Traits\Plugins\AcceptsJson;
1211

@@ -28,13 +27,14 @@ public function defaultHeaders(): array
2827
return [
2928
'Accept' => 'application/json',
3029
'Content-Type' => 'application/json',
30+
'X-Requested-With' => $this->getToken(),
3131
];
3232
}
3333

34-
protected function defaultAuth(): HeaderAuthenticator
34+
public function getToken(): ?string
3535
{
36-
$token = AuthenticationToken::getOrCreate($this->configuration);
36+
$authToken = AuthenticationToken::getOrCreate($this->configuration);
3737

38-
return new HeaderAuthenticator($token->value, 'X-Authentication');
38+
return $authToken->sessionId;
3939
}
4040
}

src/DTO/AuthenticationToken.php

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,33 @@
55
namespace CodebarAg\MFiles\DTO;
66

77
use CodebarAg\MFiles\Helpers\CacheKeyManager;
8-
use CodebarAg\MFiles\Requests\LogInToVaultRequest;
9-
use Illuminate\Support\Str;
108

119
class AuthenticationToken
1210
{
1311
public function __construct(
14-
public string $value,
15-
public ?string $sessionId = null,
12+
public readonly string $sessionId
1613
) {}
1714

18-
public function toArray(): array
15+
public static function getOrCreate(ConfigWithCredentials $config): self
1916
{
20-
return [
21-
'Value' => $this->value,
22-
'SessionID' => $this->sessionId,
23-
];
24-
}
25-
26-
/**
27-
* Get or create an authentication token with caching
28-
*/
29-
public static function getOrCreate(
30-
ConfigWithCredentials $config
31-
): AuthenticationToken {
32-
3317
$cacheManager = new CacheKeyManager($config);
3418

3519
return $cacheManager->rememberAuthToken(3600, function () use ($config) {
36-
return new LogInToVaultRequest(
20+
$request = new \CodebarAg\MFiles\Requests\LogInToVaultRequest(
3721
url: $config->url,
3822
vaultGuid: $config->vaultGuid,
3923
username: $config->username,
4024
password: $config->password,
41-
sessionId: Str::uuid()->toString()
42-
)->send()->dto();
25+
);
26+
27+
$sessionId = $request->send()->dto();
28+
29+
return new self($sessionId);
4330
});
4431
}
32+
33+
public function __toString(): string
34+
{
35+
return $this->sessionId;
36+
}
4537
}

src/Requests/GetVaultsRequest.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/Requests/LogInToVaultRequest.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace CodebarAg\MFiles\Requests;
66

7-
use CodebarAg\MFiles\DTO\AuthenticationToken;
87
use Illuminate\Support\Arr;
98
use Saloon\Contracts\Body\HasBody;
109
use Saloon\Enums\Method;
@@ -25,7 +24,6 @@ public function __construct(
2524
public string $vaultGuid,
2625
public string $username,
2726
public string $password,
28-
public ?string $sessionId = null,
2927
) {}
3028

3129
public function resolveEndpoint(): string
@@ -44,28 +42,16 @@ protected function defaultHeaders(): array
4442
protected function defaultBody(): array
4543
{
4644
$body = [
47-
'VaultGuid' => $this->vaultGuid,
4845
'Username' => $this->username,
4946
'Password' => $this->password,
50-
'Expiration' => now()->addDay()->toIso8601String(),
51-
'SessionID' => $this->sessionId,
47+
'VaultGuid' => $this->vaultGuid,
5248
];
5349

5450
return $body;
5551
}
5652

57-
public function createDtoFromResponse(Response $response): AuthenticationToken
53+
public function createDtoFromResponse(Response $response): ?string
5854
{
59-
$responseData = $response->json();
60-
61-
$value = Arr::get($responseData, 'Value');
62-
if (empty($value)) {
63-
throw new \InvalidArgumentException('Authentication token value not found in response');
64-
}
65-
66-
return new AuthenticationToken(
67-
value: $value,
68-
sessionId: $this->sessionId,
69-
);
55+
return Arr::get($response->json(), 'Value');
7056
}
7157
}

src/Requests/LogOutFromVaultRequest.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/Responses/CreateSingleFileDocumentResponse.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ final class CreateSingleFileDocumentResponse
1111
{
1212
public static function createDtoFromResponse(Response $response): Document
1313
{
14-
$data = $response->json();
15-
16-
return Document::fromArray($data);
14+
return Document::fromArray($response->json());
1715
}
1816
}

src/Responses/GetObjectInformationResponse.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ final class GetObjectInformationResponse
1111
{
1212
public static function createDtoFromResponse(Response $response): ObjectProperties
1313
{
14-
$data = $response->json();
15-
16-
return ObjectProperties::fromArray($data);
14+
return ObjectProperties::fromArray($response->json());
1715
}
1816
}

tests/Feature/Connectors/MFilesConnectorTest.php

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

33
declare(strict_types=1);
44

5-
use CodebarAg\MFiles\Connectors\MFilesConnector;
6-
use CodebarAg\MFiles\DTO\AuthenticationToken;
75
use CodebarAg\MFiles\DTO\ConfigWithCredentials;
8-
use CodebarAg\MFiles\Helpers\CacheKeyManager;
96
use CodebarAg\MFiles\Requests\LogInToVaultRequest;
10-
use CodebarAg\MFiles\Requests\LogOutFromVaultRequest;
11-
use Illuminate\Support\Facades\Cache;
127
use Saloon\Http\Faking\MockResponse;
138
use Saloon\Laravel\Facades\Saloon;
149

@@ -19,11 +14,10 @@
1914
]);
2015

2116
$config = new ConfigWithCredentials(
22-
url: 'https://test.m-files.com',
23-
vaultGuid: 'test-vault-guid',
24-
username: 'test-user',
25-
password: 'test-password',
26-
cacheDriver: 'array'
17+
url: config('m-files.auth.url'),
18+
vaultGuid: config('m-files.vault_guid'),
19+
username: config('m-files.auth.username'),
20+
password: config('m-files.auth.password')
2721
);
2822

2923
expect($config)->toBeInstanceOf(ConfigWithCredentials::class);
@@ -35,50 +29,19 @@
3529
]);
3630

3731
$config = new ConfigWithCredentials(
38-
url: 'https://test.m-files.com',
39-
vaultGuid: 'test-vault-guid',
40-
username: 'test-user',
41-
password: 'test-password',
42-
cacheDriver: 'array'
32+
url: config('m-files.auth.url'),
33+
vaultGuid: config('m-files.vault_guid'),
34+
username: config('m-files.auth.username'),
35+
password: config('m-files.auth.password')
4336
);
4437

4538
$config2 = new ConfigWithCredentials(
46-
url: 'https://test.m-files.com',
47-
vaultGuid: 'test-vault-guid',
48-
username: 'test-user',
49-
password: 'test-password',
50-
cacheDriver: 'array'
39+
url: config('m-files.auth.url'),
40+
vaultGuid: config('m-files.vault_guid'),
41+
username: config('m-files.auth.username'),
42+
password: config('m-files.auth.password')
5143
);
5244

5345
expect($config)->toBeInstanceOf(ConfigWithCredentials::class);
5446
expect($config->toArray())->toBe($config2->toArray());
5547
});
56-
57-
test('can logout session', function () {
58-
$config = new ConfigWithCredentials(
59-
url: 'https://test.m-files.com',
60-
vaultGuid: 'test-vault-guid',
61-
username: 'test-user',
62-
password: 'test-password',
63-
cacheDriver: 'array'
64-
);
65-
66-
Saloon::fake([
67-
LogInToVaultRequest::class => MockResponse::fixture('login-to-vault'),
68-
LogOutFromVaultRequest::class => MockResponse::fixture('logout-from-vault'),
69-
]);
70-
71-
$cacheManager = new CacheKeyManager($config);
72-
$cacheKey = $cacheManager->getAuthKey();
73-
74-
// First, create an authentication token
75-
AuthenticationToken::getOrCreate($config);
76-
77-
expect(Cache::store($config->cacheDriver)->has($cacheKey))->toBeTrue();
78-
79-
$connector = new MFilesConnector($config);
80-
$logout = $connector->send(new LogOutFromVaultRequest($config))->dto();
81-
82-
expect($logout)->toBeTrue();
83-
expect(Cache::store($config->cacheDriver)->has($cacheKey))->toBeFalse();
84-
});

tests/Feature/Requests/GetVaultsRequestTest.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)