Merge remote-tracking branch 'origin/main' into 18169-cancel-button-not-working
[arvados-workbench2.git] / src / services / authorized-keys-service / authorized-keys-service.ts
index b51704a0223ddd84e7f23ae2176973d0ca304ed6..ef296e95852b50789f120aaec01b1817d539d021 100644 (file)
@@ -3,12 +3,32 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import { AxiosInstance } from "axios";
-import { SshKeyResource } from '~/models/ssh-key';
-import { CommonResourceService } from "~/services/common-service/common-resource-service";
-import { ApiActions } from "~/services/api/api-actions";
+import { SshKeyResource } from 'models/ssh-key';
+import { CommonResourceService, CommonResourceServiceError } from 'services/common-service/common-resource-service';
+import { ApiActions } from "services/api/api-actions";
+
+export enum AuthorizedKeysServiceError {
+    UNIQUE_PUBLIC_KEY = 'UniquePublicKey',
+    INVALID_PUBLIC_KEY = 'InvalidPublicKey',
+}
 
 export class AuthorizedKeysService extends CommonResourceService<SshKeyResource> {
     constructor(serverApi: AxiosInstance, actions: ApiActions) {
         super(serverApi, "authorized_keys", actions);
     }
-}
\ No newline at end of file
+}
+
+export const getAuthorizedKeysServiceError = (errorResponse: any) => {
+    if ('errors' in errorResponse && 'errorToken' in errorResponse) {
+        const error = errorResponse.errors.join('');
+        switch (true) {
+            case /Public key does not appear to be a valid ssh-rsa or dsa public key/.test(error):
+                return AuthorizedKeysServiceError.INVALID_PUBLIC_KEY;
+            case /Public key already exists in the database, use a different key./.test(error):
+                return AuthorizedKeysServiceError.UNIQUE_PUBLIC_KEY;
+            default:
+                return CommonResourceServiceError.UNKNOWN;
+        }
+    }
+    return CommonResourceServiceError.NONE;
+};
\ No newline at end of file