Skip to content

Commit eef1a6d

Browse files
author
Sebastian Fix
committed
wip
1 parent c78091a commit eef1a6d

60 files changed

Lines changed: 738 additions & 1449 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/run-tests.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ jobs:
5252
M_FILES_PASSWORD: ${{ secrets.M_FILES_PASSWORD }}
5353
M_FILES_VAULT_GUID: ${{ secrets.M_FILES_VAULT_GUID }}
5454
M_FILES_CACHE_DRIVER: ${{ secrets.M_FILES_CACHE_DRIVER }}
55-
M_FILES_CACHE_LIFETIME_IN_SECONDS: ${{ secrets.M_FILES_CACHE_LIFETIME_IN_SECONDS }}
56-
SALOON_FIXTURE_REDACTION: true
5755

5856
- name: Store Log Artifacts
5957
if: failure()

README.md

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,13 @@ M_FILES_CACHE_DRIVER=file
4040

4141
#### Authentication
4242
- `GetAuthenticationToken` - Get authentication token using username/password
43-
- `LogoutSession` - Logout a session with session ID
43+
- `LogOutFromVaultRequest` - Logout from vault with session ID
4444

45-
#### User Operations
46-
- `GetCurrentUserRequest` - Get information about the current authenticated user
45+
#### Vault Operations
46+
- `GetVaultsRequest` - Get list of available vaults for the authenticated user
4747

48-
#### Document Operations
49-
- `GetDocumentsRequest` - Get documents with optional filtering
50-
- `GetDocumentPropertiesRequest` - Get document properties
48+
#### Object Operations
49+
- `GetObjectPropertiesRequest` - Get object properties (works for documents, folders, and other object types)
5150

5251
#### File Operations
5352
- `UploadFileRequest` - Upload a file to M-Files
@@ -62,6 +61,7 @@ M_FILES_CACHE_DRIVER=file
6261
- `PropertyValue` - Represents a property value for creating documents
6362
- `Document` - Represents a document in M-Files
6463
- `Documents` - Represents a collection of documents
64+
- `ObjectProperties` - Represents object properties in M-Files
6565
- `File` - Represents a file in M-Files
6666
- `DownloadedFile` - Represents a downloaded file with content and metadata
6767
- `User` - Represents a user in M-Files
@@ -112,62 +112,60 @@ $token = $request->send()->dto();
112112
// Returns AuthenticationToken with sessionId
113113
```
114114

115-
**Logout Session**
115+
**Logout from Vault**
116116
```php
117-
use CodebarAg\MFiles\Requests\Authentication\LogoutSession;
117+
use CodebarAg\MFiles\Requests\Authentication\LogOutFromVaultRequest;
118118

119-
$logout = (new LogoutSession(config: $config))->send()->dto();
119+
$logout = (new LogOutFromVaultRequest(config: $config))->send()->dto();
120120
// Returns true on successful logout, clears cached token
121121
```
122122

123-
#### User Operations
123+
#### Vault Operations
124124

125-
**Get Current User**
125+
**Get Available Vaults**
126126
```php
127-
use CodebarAg\MFiles\Requests\GetCurrentUserRequest;
128-
use CodebarAg\MFiles\DTO\User;
127+
use CodebarAg\MFiles\Requests\GetVaultsRequest;
129128

130-
$user = $connector->send(new GetCurrentUserRequest())->dto();
131-
// Returns User DTO with id, name, email, etc.
129+
$vaults = $connector->send(new GetVaultsRequest())->json();
130+
// Returns array of available vaults for the authenticated user
132131
```
133132

134-
#### Document Operations
133+
#### Object Operations
135134

136-
**Get Documents**
135+
**Get Object Properties**
137136
```php
138-
use CodebarAg\MFiles\Requests\GetDocumentsRequest;
139-
use CodebarAg\MFiles\DTO\Documents;
137+
use CodebarAg\MFiles\Requests\GetObjectPropertiesRequest;
138+
use CodebarAg\MFiles\DTO\ObjectProperties;
140139

141-
$documents = $connector->send(new GetDocumentsRequest())->dto();
142-
// Returns Documents collection with pagination info
140+
$properties = $connector->send(new GetObjectPropertiesRequest(
141+
objectType: 0, // 0 for documents, 1 for folders, etc.
142+
objectId: 123
143+
))->dto();
144+
// Returns ObjectProperties with property details
143145

144-
// With filtering parameters
145-
$documents = $connector->send(new GetDocumentsRequest(
146-
page: 1,
147-
pageSize: 5,
148-
searchString: 'Sample',
149-
objectTypeId: 0,
150-
includeDeleted: false,
151-
includeSubfolders: true,
152-
sortBy: 'Title',
153-
sortDirection: 'asc'
146+
// With optional parameters
147+
$properties = $connector->send(new GetObjectPropertiesRequest(
148+
objectType: 0,
149+
objectId: 123,
150+
includeDeleted: false
154151
))->dto();
155152
```
156153

157-
**Get Document Properties**
154+
**Get Object Properties**
158155
```php
159-
use CodebarAg\MFiles\Requests\GetDocumentPropertiesRequest;
160-
use CodebarAg\MFiles\DTO\DocumentProperties;
156+
use CodebarAg\MFiles\Requests\GetObjectPropertiesRequest;
157+
use CodebarAg\MFiles\DTO\ObjectProperties;
161158

162-
$properties = $connector->send(new GetDocumentPropertiesRequest(
159+
$properties = $connector->send(new GetObjectPropertiesRequest(
160+
objectType: 0, // 0 for documents, 1 for folders, etc.
163161
objectId: 123
164162
))->dto();
165-
// Returns DocumentProperties with property details
163+
// Returns ObjectProperties with property details
166164

167165
// With optional parameters
168-
$properties = $connector->send(new GetDocumentPropertiesRequest(
166+
$properties = $connector->send(new GetObjectPropertiesRequest(
167+
objectType: 0,
169168
objectId: 123,
170-
objectTypeId: 0,
171169
includeDeleted: false
172170
))->dto();
173171
```
@@ -246,8 +244,8 @@ $downloadedFile = $connector->send(new DownloadFileRequest(
246244
```php
247245
use CodebarAg\MFiles\Connectors\MFilesConnector;
248246
use CodebarAg\MFiles\DTO\Config\ConfigWithCredentials;
249-
use CodebarAg\MFiles\Requests\GetCurrentUserRequest;
250-
use CodebarAg\MFiles\Requests\GetDocumentsRequest;
247+
use CodebarAg\MFiles\Requests\GetVaultsRequest;
248+
use CodebarAg\MFiles\Requests\GetObjectPropertiesRequest;
251249
use CodebarAg\MFiles\Requests\CreateSingleFileDocumentRequest;
252250
use CodebarAg\MFiles\Requests\UploadFileRequest;
253251
use CodebarAg\MFiles\Requests\DownloadFileRequest;
@@ -266,11 +264,11 @@ $config = new ConfigWithCredentials(
266264

267265
$connector = new MFilesConnector(config: $config);
268266

269-
// Get current user
270-
$user = $connector->send(new GetCurrentUserRequest())->dto();
267+
// Get available vaults
268+
$vaults = $connector->send(new GetVaultsRequest())->json();
271269

272-
// Get documents
273-
$documents = $connector->send(new GetDocumentsRequest())->dto();
270+
// Get document properties (example with document ID 123)
271+
$properties = $connector->send(new GetObjectPropertiesRequest(objectType: 0, objectId: 123))->dto();
274272

275273
// Upload a file
276274
$fileContent = file_get_contents('document.pdf');
@@ -290,10 +288,9 @@ $document = $connector->send(new CreateSingleFileDocumentRequest(
290288
))->dto();
291289

292290
// Download a file from a document
293-
$file = $document->files->first();
294291
$downloadedFile = $connector->send(new DownloadFileRequest(
295292
objectId: $document->id,
296-
fileId: $file->id
293+
fileId: 101
297294
))->dto();
298295

299296

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"saloonphp/cache-plugin": "^3.0",
2828
"saloonphp/laravel-plugin": "^3.0",
2929
"saloonphp/saloon": "^3.10.1",
30-
"spatie/laravel-package-tools": "^1.19"
30+
"spatie/laravel-package-tools": "^1.19",
31+
"spatie/laravel-ray": "^1.40"
3132
},
3233
"require-dev": {
3334
"laravel/pint": "^1.21",

config/m-files.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
'url' => env('M_FILES_URL'),
66
'username' => env('M_FILES_USERNAME'),
77
'password' => env('M_FILES_PASSWORD'),
8-
'expiration' => env('M_FILES_EXPIRATION_SECONDS', '3600'), // Default to 1 hour
8+
'expiration' => env('M_FILES_EXPIRATION_SECONDS', 1),
99
'session_id' => env('M_FILES_SESSION_ID'),
1010
],
1111

phpunit.xml.dist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
<env name="M_FILES_PASSWORD" value=""/>
2727
<env name="M_FILES_VAULT_GUID" value=""/>
2828
<env name="M_FILES_CACHE_DRIVER" value="file"/>
29-
<env name="M_FILES_CACHE_LIFETIME_IN_SECONDS" value="0"/>
30-
<env name="SALOON_FIXTURE_REDACTION" value="true"/>
3129
</php>
3230
<source>
3331
<include>

src/Connectors/MFilesConnector.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace CodebarAg\MFiles\Connectors;
66

7+
use CodebarAg\MFiles\DTO\Authentication\AuthenticationToken;
78
use CodebarAg\MFiles\DTO\Config\ConfigWithCredentials;
9+
use Saloon\Http\Auth\HeaderAuthenticator;
810
use Saloon\Http\Connector;
911
use Saloon\Traits\Plugins\AcceptsJson;
1012

@@ -26,7 +28,13 @@ public function defaultHeaders(): array
2628
return [
2729
'Accept' => 'application/json',
2830
'Content-Type' => 'application/json',
29-
'X-Authentication' => $this->configuration->authenticationToken->value,
3031
];
3132
}
33+
34+
protected function defaultAuth(): HeaderAuthenticator
35+
{
36+
$token = AuthenticationToken::getOrCreate($this->configuration);
37+
38+
return new HeaderAuthenticator($token->value, 'X-Authentication');
39+
}
3240
}

src/DTO/Authentication/AuthenticationToken.php

Lines changed: 17 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,22 @@
44

55
namespace CodebarAg\MFiles\DTO\Authentication;
66

7-
use Carbon\CarbonImmutable;
8-
use CodebarAg\MFiles\Requests\Authentication\GetAuthenticationToken;
9-
use Illuminate\Support\Arr;
10-
use Illuminate\Support\Facades\Cache;
7+
use CodebarAg\MFiles\DTO\Config\ConfigWithCredentials;
8+
use CodebarAg\MFiles\Helpers\CacheKeyManager;
9+
use CodebarAg\MFiles\Requests\LogInToVaultRequest;
10+
use Illuminate\Support\Str;
1111

1212
class AuthenticationToken
1313
{
1414
public function __construct(
1515
public string $value,
16-
public ?CarbonImmutable $expiration = null,
17-
public ?string $sessionId = null
16+
public ?string $sessionId = null,
1817
) {}
1918

20-
public static function fromArray(array $data): self
21-
{
22-
$expiration = Arr::get($data, 'Expiration');
23-
24-
return new self(
25-
value: Arr::get($data, 'Value', ''),
26-
expiration: $expiration ? CarbonImmutable::parse($expiration) : null,
27-
sessionId: Arr::get($data, 'SessionID')
28-
);
29-
}
30-
3119
public function toArray(): array
3220
{
3321
return [
3422
'Value' => $this->value,
35-
'Expiration' => $this->expiration?->toISOString(),
3623
'SessionID' => $this->sessionId,
3724
];
3825
}
@@ -41,77 +28,19 @@ public function toArray(): array
4128
* Get or create an authentication token with caching
4229
*/
4330
public static function getOrCreate(
44-
string $url,
45-
string $username,
46-
string $password,
47-
?string $vaultGuid = null,
48-
?string $cacheDriver = null,
31+
ConfigWithCredentials $config
4932
): AuthenticationToken {
50-
$cacheKey = self::generateCacheKey($url, $username, $password, $vaultGuid);
51-
$cacheDriver = $cacheDriver ?? config('m-files.cache_driver');
52-
53-
$cachedToken = Cache::store($cacheDriver)->get($cacheKey);
54-
55-
if ($cachedToken && $cachedToken->isValid()) {
56-
return $cachedToken;
57-
}
58-
59-
// Create new token
60-
$expirationInSeconds = (int) config('m-files.auth.expiration', 3600);
61-
$expiration = CarbonImmutable::now()->addSeconds($expirationInSeconds)->toISOString();
62-
63-
$token = new GetAuthenticationToken(
64-
url: $url,
65-
username: $username,
66-
password: $password,
67-
vaultGuid: $vaultGuid,
68-
expiration: $expiration
69-
)->send()->dto();
70-
71-
// Store in cache
72-
self::storeInCache($cacheKey, $token, $cacheDriver);
73-
74-
return $token;
75-
}
76-
77-
/**
78-
* Check if the token is still valid
79-
*/
80-
public function isValid(): bool
81-
{
82-
// If no expiration is set, consider the token valid
83-
// (it will be managed by the cache TTL instead)
84-
if (! $this->expiration) {
85-
return true;
86-
}
87-
88-
$currentTime = CarbonImmutable::now();
89-
90-
// Consider token valid if it expires in more than 5 minutes
91-
return $this->expiration->gt($currentTime->addMinutes(5));
92-
}
93-
94-
/**
95-
* Generate a unique cache key based on URL, username, password, and vault GUID
96-
*/
97-
public static function generateCacheKey(string $url, string $username, string $password, ?string $vaultGuid = null): string
98-
{
99-
$uniqueString = $url.'|'.$username.'|'.$password.'|'.($vaultGuid ?? 'default');
100-
101-
return 'm-files-auth-token:'.hash('sha256', $uniqueString);
102-
}
103-
104-
/**
105-
* Store the authentication token in cache
106-
*/
107-
private static function storeInCache(string $cacheKey, self $token, string $cacheDriver): void
108-
{
109-
// Use the same expiration time from config for cache TTL
110-
$ttl = config('m-files.auth.expiration', 3600);
111-
112-
// Ensure TTL is positive and reasonable
113-
$ttl = max(300, min($ttl, 3600)); // Between 5 minutes and 1 hour
11433

115-
Cache::store($cacheDriver)->put($cacheKey, $token, $ttl);
34+
$cacheManager = new CacheKeyManager($config);
35+
36+
return $cacheManager->rememberAuthToken(3600, function () use ($config) {
37+
return new LogInToVaultRequest(
38+
url: $config->url,
39+
vaultGuid: $config->vaultGuid,
40+
username: $config->username,
41+
password: $config->password,
42+
sessionId: Str::uuid()->toString()
43+
)->send()->dto();
44+
});
11645
}
11746
}

0 commit comments

Comments
 (0)