Merge branch '14479_ssh_keys'
[arvados-workbench2.git] / src / services / authorized-keys-service / authorized-keys-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 { SshKeyResource } from '~/models/ssh-key';
7 import { CommonResourceService, CommonResourceServiceError } from '~/services/common-service/common-resource-service';
8 import { ApiActions } from "~/services/api/api-actions";
9
10 export enum AuthorizedKeysServiceError {
11     UNIQUE_PUBLIC_KEY = 'UniquePublicKey',
12     INVALID_PUBLIC_KEY = 'InvalidPublicKey',
13 }
14
15 export class AuthorizedKeysService extends CommonResourceService<SshKeyResource> {
16     constructor(serverApi: AxiosInstance, actions: ApiActions) {
17         super(serverApi, "authorized_keys", actions);
18     }
19 }
20
21 export const getAuthorizedKeysServiceError = (errorResponse: any) => {
22     if ('errors' in errorResponse && 'errorToken' in errorResponse) {
23         const error = errorResponse.errors.join('');
24         switch (true) {
25             case /Public key does not appear to be a valid ssh-rsa or dsa public key/.test(error):
26                 return AuthorizedKeysServiceError.INVALID_PUBLIC_KEY;
27             case /Public key already exists in the database, use a different key./.test(error):
28                 return AuthorizedKeysServiceError.UNIQUE_PUBLIC_KEY;
29             default:
30                 return CommonResourceServiceError.UNKNOWN;
31         }
32     }
33     return CommonResourceServiceError.NONE;
34 };