21128: cleanup, renamed some types Arvados-DCO-1.1-Signed-off-by: Lisa Knox <lisa...
[arvados.git] / src / services / api-client-authorization-service / api-client-authorization-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { AxiosInstance } from "axios";
6 import { ApiActions } from 'services/api/api-actions';
7 import { ApiClientAuthorization } from 'models/api-client-authorization';
8 import { CommonService, ListResults } from 'services/common-service/common-service';
9 import { extractUuidObjectType, ResourceObjectType } from "models/resource";
10 import { FilterBuilder } from "services/api/filter-builder";
11
12 export class ApiClientAuthorizationService extends CommonService<ApiClientAuthorization> {
13     constructor(serverApi: AxiosInstance, actions: ApiActions) {
14         super(serverApi, "api_client_authorizations", actions);
15     }
16
17     createCollectionSharingToken(uuid: string, expDate: Date | undefined): Promise<ApiClientAuthorization> {
18         if (extractUuidObjectType(uuid) !== ResourceObjectType.COLLECTION) {
19             throw new Error(`UUID ${uuid} is not a collection`);
20         }
21         const data = {
22             scopes: [
23                 `GET /arvados/v1/collections/${uuid}`,
24                 `GET /arvados/v1/collections/${uuid}/`,
25                 `GET /arvados/v1/keep_services/accessible`,
26             ]
27         }
28         return expDate !== undefined
29             ? this.create({...data, expiresAt: expDate.toUTCString()})
30             : this.create(data);
31     }
32
33     listCollectionSharingTokens(uuid: string): Promise<ListResults<ApiClientAuthorization>> {
34         if (extractUuidObjectType(uuid) !== ResourceObjectType.COLLECTION) {
35             throw new Error(`UUID ${uuid} is not a collection`);
36         }
37         return this.list({
38             filters: new FilterBuilder()
39                 .addEqual("scopes", [
40                     `GET /arvados/v1/collections/${uuid}`,
41                     `GET /arvados/v1/collections/${uuid}/`,
42                     "GET /arvados/v1/keep_services/accessible"
43                 ]).getFilters()
44         });
45     }
46 }