Merge remote-tracking branch 'origin/main' into 18368-notification-banner
[arvados.git] / src / services / api-client-authorization-service / api-client-authorization-service.ts
index 3bf4ae8a77f98091b322b5b7f6a4f16e1b317313..dbda0a42c79951de4431f796e1b73cc7a612a75c 100644 (file)
@@ -3,12 +3,44 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import { AxiosInstance } from "axios";
-import { ApiActions } from '~/services/api/api-actions';
-import { ApiClientAuthorization } from '~/models/api-client-authorization';
-import { CommonService } from '~/services/common-service/common-service';
+import { ApiActions } from 'services/api/api-actions';
+import { ApiClientAuthorization } from 'models/api-client-authorization';
+import { CommonService, ListResults } from 'services/common-service/common-service';
+import { extractUuidObjectType, ResourceObjectType } from "models/resource";
+import { FilterBuilder } from "services/api/filter-builder";
 
 export class ApiClientAuthorizationService extends CommonService<ApiClientAuthorization> {
     constructor(serverApi: AxiosInstance, actions: ApiActions) {
         super(serverApi, "api_client_authorizations", actions);
     }
-} 
\ No newline at end of file
+
+    createCollectionSharingToken(uuid: string, expDate: Date | undefined): Promise<ApiClientAuthorization> {
+        if (extractUuidObjectType(uuid) !== ResourceObjectType.COLLECTION) {
+            throw new Error(`UUID ${uuid} is not a collection`);
+        }
+        const data = {
+            scopes: [
+                `GET /arvados/v1/collections/${uuid}`,
+                `GET /arvados/v1/collections/${uuid}/`,
+                `GET /arvados/v1/keep_services/accessible`,
+            ]
+        }
+        return expDate !== undefined
+            ? this.create({...data, expiresAt: expDate.toUTCString()})
+            : this.create(data);
+    }
+
+    listCollectionSharingTokens(uuid: string): Promise<ListResults<ApiClientAuthorization>> {
+        if (extractUuidObjectType(uuid) !== ResourceObjectType.COLLECTION) {
+            throw new Error(`UUID ${uuid} is not a collection`);
+        }
+        return this.list({
+            filters: new FilterBuilder()
+                .addEqual("scopes", [
+                    `GET /arvados/v1/collections/${uuid}`,
+                    `GET /arvados/v1/collections/${uuid}/`,
+                    "GET /arvados/v1/keep_services/accessible"
+                ]).getFilters()
+        });
+    }
+}
\ No newline at end of file