55
55
use OCP \PreConditionNotMetException ;
56
56
use OCP \Security \ISecureRandom ;
57
57
use OCP \Server ;
58
+ use phpDocumentor \Reflection \Types \Self_ ;
58
59
use Psr \Log \LoggerInterface ;
60
+ use OCA \AppAPI \Service \ExAppService ;
59
61
60
62
define ('CACHE_TTL ' , 3600 );
61
63
@@ -130,6 +132,7 @@ class OpenProjectAPIService {
130
132
private ISecureRandom $ random ;
131
133
private IEventDispatcher $ eventDispatcher ;
132
134
private AuditLogger $ auditLogger ;
135
+ private ExAppService $ exAppService ;
133
136
134
137
public function __construct (
135
138
string $ appName ,
@@ -150,6 +153,7 @@ public function __construct(
150
153
ISubAdmin $ subAdminManager ,
151
154
IDBConnection $ db ,
152
155
ILogFactory $ logFactory ,
156
+ ExAppService $ exAppService ,
153
157
) {
154
158
$ this ->appName = $ appName ;
155
159
$ this ->avatarManager = $ avatarManager ;
@@ -169,6 +173,7 @@ public function __construct(
169
173
$ this ->eventDispatcher = $ eventDispatcher ;
170
174
$ this ->db = $ db ;
171
175
$ this ->logFactory = $ logFactory ;
176
+ $ this ->exAppService = $ exAppService ;
172
177
}
173
178
174
179
/**
@@ -316,9 +321,13 @@ public function getOpenProjectAvatar(
316
321
$ this ->config ->getAppValue (Application::APP_ID , 'openproject_client_id ' );
317
322
$ this ->config ->getAppValue (Application::APP_ID , 'openproject_client_secret ' );
318
323
$ openprojectUrl = $ this ->config ->getAppValue (Application::APP_ID , 'openproject_instance_url ' );
324
+ $ options = [];
325
+ if ($ this ->isOpenProjectRunningAsExApp ($ openprojectUrl )) {
326
+ $ options = $ this ->setHeadersForProxy ($ nextcloudUserId , $ options );
327
+ }
319
328
try {
320
329
$ response = $ this ->rawRequest (
321
- $ accessToken , $ openprojectUrl , 'users/ ' .$ openprojectUserId .'/avatar '
330
+ $ accessToken , $ openprojectUrl , 'users/ ' .$ openprojectUserId .'/avatar ' , [], ' GET ' , $ options
322
331
);
323
332
$ imageMimeType = $ response ->getHeader ('Content-Type ' );
324
333
$ imageData = $ response ->getBody ();
@@ -362,6 +371,21 @@ public function getOpenProjectAvatar(
362
371
}
363
372
}
364
373
374
+ public function isOpenProjectRunningAsExApp (string $ openprojectUrl ) : bool {
375
+ return str_ends_with ($ openprojectUrl , '/proxy/openproject-nextcloud-app ' );
376
+ }
377
+
378
+ public function setHeadersForProxy (string $ nextcloudUser , array $ options ): array {
379
+ $ options = [];
380
+ $ exAppconfigInformation = $ this ->exAppService ->getExApp (Application::APP_ID_PROXY_OPENPROJECT );
381
+ $ authorizationAppAPI = base64_encode ($ nextcloudUser . ": " . $ exAppconfigInformation ->getSecret ());
382
+ $ options ['headers ' ]['host ' ] = $ exAppconfigInformation ->getHost () . ": " . $ exAppconfigInformation ->getPort ();
383
+ $ options ['headers ' ]['ex-app-id ' ] = $ exAppconfigInformation ->getAppid ();
384
+ $ options ['headers ' ]['authorization-app-api ' ] = $ authorizationAppAPI ;
385
+ $ options ['headers ' ]['ex-app-version ' ] = $ exAppconfigInformation ->getVersion ();
386
+ return $ options ;
387
+ }
388
+
365
389
/**
366
390
* @param string $accessToken
367
391
* @param string $openprojectUrl
@@ -443,8 +467,12 @@ public function request(string $userId,
443
467
if (!$ openprojectUrl || !OpenProjectAPIService::validateURL ($ openprojectUrl )) {
444
468
return ['error ' => 'OpenProject URL is invalid ' , 'statusCode ' => 500 ];
445
469
}
470
+ $ options = [];
471
+ if ($ this ->isOpenProjectRunningAsExApp ($ openprojectUrl )) {
472
+ $ options = $ this ->setHeadersForProxy ($ userId , $ options );
473
+ }
446
474
try {
447
- $ response = $ this ->rawRequest ($ accessToken , $ openprojectUrl , $ endPoint , $ params , $ method );
475
+ $ response = $ this ->rawRequest ($ accessToken , $ openprojectUrl , $ endPoint , $ params , $ method, $ options );
448
476
if (($ method === 'DELETE ' || $ method === 'POST ' ) &&
449
477
$ response ->getStatusCode () === Http::STATUS_NO_CONTENT
450
478
) {
@@ -464,7 +492,7 @@ public function request(string $userId,
464
492
'client_secret ' => $ clientSecret ,
465
493
'grant_type ' => 'refresh_token ' ,
466
494
'refresh_token ' => $ refreshToken ,
467
- ], 'POST ' );
495
+ ], 'POST ' , $ options );
468
496
if (isset ($ result ['refresh_token ' ])) {
469
497
$ refreshToken = $ result ['refresh_token ' ];
470
498
$ this ->config ->setUserValue (
@@ -517,17 +545,13 @@ public function request(string $userId,
517
545
* @param string $url
518
546
* @param array<mixed> $params passed to `http_build_query` for GET requests, else send as body
519
547
* @param string $method
548
+ * @param array<mixed> $options
520
549
* @return array<mixed>
521
550
*/
522
- public function requestOAuthAccessToken (string $ url , array $ params = [], string $ method = 'GET ' ): array {
551
+ public function requestOAuthAccessToken (string $ url , array $ params = [], string $ method = 'GET ' , array $ options = [] ): array {
523
552
try {
524
553
$ url = $ url . '/oauth/token ' ;
525
- $ options = [
526
- 'headers ' => [
527
- 'User-Agent ' => 'Nextcloud OpenProject integration ' ,
528
- ]
529
- ];
530
-
554
+ $ options ['headers ' ]['User-Agent ' ] = 'Nextcloud OpenProject integration ' ;
531
555
if (count ($ params ) > 0 ) {
532
556
if ($ method === 'GET ' ) {
533
557
$ paramsContent = http_build_query ($ params );
0 commit comments