UNPKG

3.35 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const utils_fs_1 = require("@ionic/utils-fs");
4const os = require("os");
5const path = require("path");
6const guards_1 = require("../guards");
7const http_1 = require("./http");
8exports.ERROR_SSH_MISSING_PRIVKEY = 'SSH_MISSING_PRIVKEY';
9exports.ERROR_SSH_INVALID_PUBKEY = 'SSH_INVALID_PUBKEY';
10exports.ERROR_SSH_INVALID_PRIVKEY = 'SSH_INVALID_PRIVKEY';
11async function getGeneratedPrivateKeyPath(userId = 0) {
12 return path.resolve(os.homedir(), '.ssh', 'ionic', String(userId));
13}
14exports.getGeneratedPrivateKeyPath = getGeneratedPrivateKeyPath;
15async function parsePublicKeyFile(pubkeyPath) {
16 return parsePublicKey((await utils_fs_1.readFile(pubkeyPath, { encoding: 'utf8' })).trim());
17}
18exports.parsePublicKeyFile = parsePublicKeyFile;
19/**
20 * @return [full pubkey, algorithm, public numbers, annotation]
21 */
22function parsePublicKey(pubkey) {
23 const r = /^(ssh-[A-z0-9]+)\s([A-z0-9+\/=]+)\s?(.+)?$/.exec(pubkey);
24 if (!r) {
25 throw exports.ERROR_SSH_INVALID_PUBKEY;
26 }
27 if (!r[3]) {
28 r[3] = '';
29 }
30 r[1] = r[1].trim();
31 r[2] = r[2].trim();
32 r[3] = r[3].trim();
33 return [pubkey, r[1], r[2], r[3]];
34}
35exports.parsePublicKey = parsePublicKey;
36async function validatePrivateKey(keyPath) {
37 try {
38 await utils_fs_1.stat(keyPath);
39 }
40 catch (e) {
41 if (e.code === 'ENOENT') {
42 throw exports.ERROR_SSH_MISSING_PRIVKEY;
43 }
44 throw e;
45 }
46 const f = await utils_fs_1.readFile(keyPath, { encoding: 'utf8' });
47 const lines = f.split('\n');
48 if (!lines[0].match(/^\-{5}BEGIN [A-Z]+ PRIVATE KEY\-{5}$/)) {
49 throw exports.ERROR_SSH_INVALID_PRIVKEY;
50 }
51}
52exports.validatePrivateKey = validatePrivateKey;
53class SSHKeyClient extends http_1.ResourceClient {
54 constructor({ client, token, user }) {
55 super();
56 this.client = client;
57 this.token = token;
58 this.user = user;
59 }
60 async create({ pubkey }) {
61 const { req } = await this.client.make('POST', `/users/${this.user.id}/sshkeys`);
62 this.applyAuthentication(req, this.token);
63 req.send({ pubkey });
64 const res = await this.client.do(req);
65 if (!guards_1.isSSHKeyResponse(res)) {
66 throw http_1.createFatalAPIFormat(req, res);
67 }
68 return res.data;
69 }
70 async load(id) {
71 const { req } = await this.client.make('GET', `/users/${this.user.id}/sshkeys/${id}`);
72 this.applyAuthentication(req, this.token);
73 const res = await this.client.do(req);
74 if (!guards_1.isSSHKeyResponse(res)) {
75 throw http_1.createFatalAPIFormat(req, res);
76 }
77 return res.data;
78 }
79 async delete(id) {
80 const { req } = await this.client.make('DELETE', `/users/${this.user.id}/sshkeys/${id}`);
81 this.applyAuthentication(req, this.token);
82 await this.client.do(req);
83 }
84 paginate(args = {}) {
85 return this.client.paginate({
86 reqgen: async () => {
87 const { req } = await this.client.make('GET', `/users/${this.user.id}/sshkeys`);
88 this.applyAuthentication(req, this.token);
89 return { req };
90 },
91 guard: guards_1.isSSHKeyListResponse,
92 });
93 }
94}
95exports.SSHKeyClient = SSHKeyClient;